存档

2010年4月 的存档

天气不好。雨加雪~

2010年4月22日 没有评论

                                                             

                                                              雨加雪天气。

 

天气不好。雨加雪~

 

                天气,也是3D引擎中一个必备的节目。有它,可以活跃游戏的环境因素。

               对于云,一般的做法是用BillBoard图来做。也就是一个图片而已。我随机的生成了几种不同的云,并用Shader给他加了点色彩。效果还不错。

              但需要注意的时,在渲染前关闭写Z缓冲,因为图有ALPHA效果嘛。不关闭写Z缓冲,会出现透空背景的BUG。

               对于雨,我是用了一堆线条粒子。

              雪,也是一堆图片粒子,不过在下落过程中加入了自身的随机转动。

 

              怎么样,还可以看吧。虽然地表很呔~

分类: 未分类 标签:

基于四叉树的LOD地形场景的怪异优化探索

2010年4月21日 没有评论

           基于四叉树的LOD地形场景是开发3D大规模场景地形一个避不开的话题。小弟对此一直耿耿于怀。虽然说号称自已学了几年游戏编程。但实际上真的还不会做四叉树的LOD地形。但眼下要么就干掉他,要么就被他干掉。

          

           所以,解决这个问题,实属无奈。。。。。

 

           大概的看了一些资料,西里糊涂的。

 

           不过明确两点:

          1。通过视野远近调整近地表显示精度。降低渲染顶点数量。

          2。通过将地表平分为四(左上,右上,左下,右下),然后循环调用判定函数对所处范围再进行四平分(左上,右上,左下,右下)然后进行拆分判定。通过判定标准的继续调用函数。最后跟据判定结果来进行逐个正方形绘制。

 

          OK,放下书,往开了想。

 

          先不管怎么LOD还是四叉树。其本质目的是优化场景,也就是减少相对不必要的渲染。提高渲染效率。

 

          其中心思想是强调,近处提高精度,远处降低精度。高度相差不大的就减少顶点数。

 

         ====================================================

 

         那么?我要做的。就是把处于一个面上的顶点尽可能进行删简,然后跟据摄像机所在位置周边范围N米内设为高精显示即可。

        1。 在初始化时先设格子数量BOOL数组 NoArray[mTerrainWidth*mTerrainHeight];

         全清为0,即代表地表上没有不用拆分的格子。然后开始一项计算。

         将地表平分为四(左上,右上,左下,右下),在中点,从左上到右下,计算每个面的法向量,看是否相同。如果相同,将中点所在位置的NoArray元素值设为TRUE。即此范围铁定不用拆分。

         如果发现有不同,返回,对所处范围进行四平分(左上,右上,左下,右下)继续循环调用此函数。看四个平分面是否符合。

                      再设格子数组BOOL数组YesArray[mTerrainWidth*mTerrainHeight];

 

        2。在渲染前,设YesArray都为TRUE,代表进行拆分。

            将地表平分为四(左上,右上,左下,右下),看中点的NoArray元素值是否为TRUE,如果TRUE,YesArray对应元素改为FALSE并返回即可,如果FALSE,对所处范围进行视野判定。看是否符合距离标准,并设定对应的YesArray元素值。四平分(左上,右上,左下,右下)继续循环调用此函数,

 

        3。渲染时 将地表平分为四(左上,右上,左下,右下),看中点的YesArray来进行判定是否渲染此正方形。并四平分(左上,右上,左下,右下)继续循环调用此函数

 

 

                 

 

 基于四叉树的LOD地形场景的怪异优化探索

分类: 未分类 标签:

3ds max 导出插件 中的多重材质问题。

2010年4月21日 没有评论

        现在负责开发公司的3D引擎了。3ds max导出插件自然是第一步的工作。

       

        之前对3ds max导出插件有过一些研究。也顺利的编写了基础模型和骨骼动画的导出。

 

        但在提交给美术后,很快发现了一个问题,即多重材质的问题。

 

        在3ds max中,对于一个模型的子实体,可以赋予不同的材质贴图,也可以赋予一个多重材质贴图,即这个材质贴图集合了多个材质贴图。而这在导出后,如果不做处理,将导致不正确的贴图显示。

 

        怎么办?

 

       首先要明白单一材质贴图和多重材质贴图的区别

 

       对于单一材质贴图,每个三角面的材质索引就为对应的材质贴图索引。

       对于多重材质贴图,每个三角面的材质索引就为多重材质的第几个子材质。

 

      OK,那下面要做的。就是分解出使用多重材质的子模型中使用不同子材质的三角面。并将其材质索引转换为真实的单一材质贴图索引。分别进行导出。

 

      。。。。。。。。。。

 

      很好。3ds max 导出插件这一步搞定了。

 

      

分类: 未分类 标签:

场景中的巨量几何体替代方案

2010年4月21日 没有评论

 人群:

 

     在3D游戏中,有时需要显示大量的人物或NPC动画。如果这些动画都使用模型,相信你的机器难以承受巨量的人物动画所需的计算量。

今天介绍一种方法。

 

     这是在RenderWare引擎的例子中看到的。如图,一个体育馆中的大量观众场景。这些观众都是实时动画。但FPS数量很高。做法就是使用片来代替模型。

场景中的巨量几何体替代方案

 

 

这是人物的图例。

 

场景中的巨量几何体替代方案

 

只用一张纹理,设好UV动画,提供给大量的片粒子,就OK了。

 

 

 

 

这一招我们也可以许多地方也看到。比如对OGRE 进行海量几何体优化的PagedGeometry代码包中对于场景中的树的LOD分级,近时是棵树的模型,远时过渡为片。其实效果也不错。毕竟远嘛。省了不少内存和计算量。FPS很高。

 

             

分类: 基础知识 标签:

Naming convention in Objective C /C , start with “_”?

2010年4月18日 没有评论

Something I see ppl define the variable like this:

b2World *_world;
b2Body *_body;
CCSprite *_ball;

instead of

b2World *world;
b2Body *body;
CCSprite *ball;

I familiar with the second one, but not the first one. So, I checked the Wikipedia about naming convention:

Names beginning with double underscore
or an underscore and a capital letter
are reserved for implementation
(compiler, standard library) and
should not be used (e.g. __reserved or
_Reserved).

So, is that any special meaning which is start with “_”?

The code I saw which using “_” to begin is here:

http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls

The wiki page.

Usually they’re used for variables that shouldn’t be accessed outside the current file/module/namespace/whatever, in languages that don’t support restricting access with something like a private keyword

There’s a long-standing convention among some Objective-C developers to prefix instance variables with an underscore. It can be helpful in several ways: one, it makes it easier to spot instance variables in a .m file; two, it relieves developers of having to come up with creative names for method parameters to avoid colliding with instance variable names; and three, as others have noted, it indicates that the instance variables are private, and therefore shouldn’t be accessed willy nilly throughout the code.

In fact, I’d argue for avoiding accessing instance variables directly in methods other than accessors (getters and setters), -dealloc, and -init.... Not that you should never, ever use them anywhere else, but you should at least give it some thought before using an instance variable directly in other methods.

Apple reserves names beginning with underscore for its own private ivars and methods. In Objective-C on any Apple platform, it is recommended that you do not prefix your identifiers with an underscore.

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html

It’s really really helpful, but most people don’t know why, and that’s a shame.
Apple uses underscores to separate the way other objects access a particular object’s variables, and the way a particular object access its own variables.
Now this may sound a little bit strange, but imagine the following: You probably all recognize the following compiler warning

.h
@property (nonatomic, retain, readonly) UITableView *tableView;

.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self loadSomethingElseForTableView:tableView];
}

This will result in a compiler warning, because it does not know wether you reference to the local variable “tableView”, or the instance variable.
Therefore, Apple recommends you to add the following to the top of your @implementation.

@synthesize tableView = _tableView;

Now, when you reference to _tableView, the compiler knows that you mean the instance variable, and not the local one.

Also, this makes it a lot easier to understand the Garbage Collection in Obj-C, and to prevent making common mistakes.

For example, when doing the following:

@property (nonatomic, retain, readonly) NSString *title;

- (id)initWithTitle:(NSString *)title {
    if ((self = [super init])) {
        self.title = title; // Is not possible, since it's read only.
        title = title; // Is not possible, since it's the same (local) variable.
        // Changing the method to initWithTitle:(NSString *)aTitle;
        title = aTitle;
    }
    return self;
}

Now, since you do not use the default setter (actually, you can’t, because it’s read-only) you need to retain the variable yourself.
This is a lot easier to remember when you give every instance variable a prefix (so you know you need to retain it yourself).

So, basically, it’s important to understand the difference between self.variable and (_)variable. (that is: self.variable maps to [self setVariable:...] and variable maps directly to your pointer.

Furthermore, when you add it as a private variable, like this:

@interface TSSomeObject : NSObject {
@private
    NSString *_privateTitle;
}
@end

The underscore prefix isn’t really necessary, unless you may encounter local variables that have the same name. Besides that, again, it’s also an easy way to remind you that it’s a local pointer and that you need to retain (and release) the variable when you assign it to your object.

What is wrong is to create a property with a underscore prefix, like this:

@property (nonatomic, retain) NSString *_title;

That’s really wrong, and I’m not even gonna explain why ;)


So yes! You should really use underscore prefixes, it makes your code a lot easier to read, and to interpret by the compiler! In Xcode 4, Apple even added these @synthesizes to the default templates.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html#//apple_ref/doc/uid/20001284-1001757

Both by convention and recommendation in the above document, you should prefix ivars with an underscore.

Admittedly, it is in reference to explicitly set ivars for properties.

But the usage is the same, to indicate the usage of an ivar wherever it is seen.

I am however open to the possibility, that in that context, the use of an underscore prefixed ivar could signal to the user that they are doing something wrong. Meanwhile a postfixed underscore could be used for pure ivars that are meant to be accessed directly.

This blog has some good thoughts from an experienced practitioner and it recommends using prefixed underscores.

http://blog.bignerdranch.com/463-a-motivation-for-ivar-decorations/

Wether you choose to use prefixed underscores to decorate your own ivars, there is at least some evidence that some kind of decoration will help you avoid bugs. And prefix’d underscores are the most common decoration.

分类: stackoverflow精选, unity3d 标签:

Cocos2d for this?

2010年4月8日 没有评论

I just finished my concept for an iphone app. I have a main program and in that program I want to start a game.

MAIN PROGRAM (BUTTON 1 / BUTTON 2 / START GAME)
|
|
Cocos2d Game

Is this possible? To use cocos2D in a normal “iphone app”???

Thanks so much!!!

It is definitely possible to mix UIKit with Cocos2d. Usually people accomplish this by putting their UIKit views inside a Cocos2d layer. It sounds like you want to embed the Cocos2d game inside a UIKit view instead. This should be possible, but I am unsure of the specifics involved. Try looking at -(BOOL)attachInView:(UIView *) view in CCDirector.

Note that Cocos2d uses OpenGL and there are possible performance issues when mixing OpenGL and UIKit. Pausing Cocos2d when using the other part of your application will help.

I found a vast array of great tutorials on Cocos2D for iPhone at Ray Wenderlich’s site. One tutorial you might especially want to check out is
How to integrate Cocos2D and UIKit

Note: the original “Cocos2D” framework was written in Python and not designed for iOS; here’s the actual homepage for the iOS port.

It is possible, but you should know that the main core of your app must be controlled by Cocos2D, that means CCDirector needs to be instantiated in AppDelegate.

Start with:

a). install Cocos2D:

Go to Terminal and write (if you save cocos2D file in desktop, if not use the path where you save it)

    cd  Desktop/cocos2d-iphone-1.1-beta
    sudo ./install-templates.sh

and if you couldn’t install try again with

    ./install-templates.sh -f -u

b). Import cocos2d.h inside AppDelegate

c). Declare CCDirector in applicationDidFinishLaunching method inside
AppDelegate (you should check the code for this in Cocos2D template is pretty straight
forward)

I did this in my applicationDidFinishLaunching method:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];

CCDirector *director = [CCDirector sharedDirector];

// Create an EAGLView with a RGB8 color buffer, and a depth buffer of 24-bits
EAGLView *glView = [EAGLView viewWithFrame:[window frame]
                               pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8
                               depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                    ];

[glView setMultipleTouchEnabled:YES]; 

// attach the openglView to the director
[director setOpenGLView:glView];

// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
    CCLOG(@"Retina Display Not supported");

// make the OpenGLView a child of the main window
[window addSubview:glView];

// make main window visible
[window makeKeyAndVisible]; 

GameScene *gs = [GameScene node];

[[CCDirector sharedDirector] runWithScene:gs];
//General is part of UIKit where I load the tabbar
General *principal;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    principal = [[General alloc] initWithNibName:@"General-iPad" bundle:nil];
} else {
    principal = [[General alloc] initWithNibName:@"General" bundle:nil];
}
self.general = principal;
[principal release];
[self showUIViewController:general];

}

To push a scene from tabbar in UIKit

    [[CCDirector sharedDirector] pushScene: [CCTransitionMoveInB transitionWithDuration:0.0f scene:[MyScene scene]]];

Next I recommend you to follow Ray Wenderlich’s tutorial How to integrate Cocos2D and UIKit and read this post aswell how we can show UIViewController and UIView by using cocos2d? Maybe you should search for some code in the Cocos2D forums too, always help to see that.

EDIT:

1) You can download a easy example from here: UIKit Cocos2D

You could check out this framework too: Kobol2D “is an extended and improved version of the popular Cocos2D for iPhone game engine. Everything you know about Cocos2D can still be applied, and Kobold2D is easier to get started with, more convenient to use, more powerful and flexible than Cocos2D with all the documentation available online and offline. Use Kobold2D to develop iPhone, iPod touch, iPad and Mac OS X games for both Apple App Stores. And it has some examples using UIKIT and Cocos2D”.

You can probably do this easily……

You have to only make the (UIView *) view in CCDirector.

after attaching the view…..

You can certainly create a game with your requirements in Cocos2D. You might want to ask some follow-up questions over at the Cocos2D forums and read through their documentation.

You can download an example code from here: Uikit cocos2D

Yes.It is very easy and handy to implement Cocos2D with normal Iphone FrameWork.Refer Ray Wenderlich UIKit and Cocos2D Integration Tutorial here.It will helpful to you.

Yes! Is very possible, only install the Cocos2d and his templates.
Inclusive, I used Cocos2d to create simple apps with a lot of effects :)

分类: stackoverflow精选, unity3d 标签: