아이폰어플개발정보2010. 6. 24. 14:13
OpenGL ES Progg Guide for iPhone

Ch1. OpenGL ES on the iPhone

OpenGLES isaclientofCoreAnimation.

YourapplicationcreatesaUIViewclasswithaspecialCoreAnimationlayer,aCAEAGLLayer.

ACAEAGLLayerobjectisawareofOpenGLESandcanbeusedtocreaterenderingtargetsthatactaspartofCoreAnimation.

Whenyourapplicationfinishesrenderingaframe,youpresentthecontentsoftheCAEAGLLayerobject,wheretheywillbecompositedwiththedatafromotherviews.


* categories of functions

- ReadingthecurrentstateofanOpenGLEScontext.

- ChangingstatevariablesinanOpenGLEScontext.

- Creating,modifyingordestroyingOpenGLESobjects.

- Submittinggeometrytoberendered. (rasterized to a framebuffer)


* objects

- texture: image

- buffer: set of memory (vertex data)

- shader

- renderbuffer: normally as a part of a framebuffer

- framebuffer: ultimate destination of the graphics pipeline


*common behavior of objects

- generate an OID: it simply allocates a reference to an object

- bound to OpenGL ES context:The first time you bind toan object identifier,OpenGL ES allocates memory and initializes that object.

- modify the state

- used for rendering

- deleted


OntheiPhone,OpenGLESobjectsaremanagedbyasharegroupobject.


Twoormorerenderingcontextscanbeconfiguredtousethesamesharegroup.


Appledoesnotprovideaplatforminterfaceforcreatingframebufferobjects.Instead,allframebufferobjectsarecreatedusingtheOES_framebuffer_objectextension.


* framebuffer creating procedure

1.Generateandbindaframebufferobject.

2.Generate,bindandconfigureanimage.

3.Attachtheimagetotheframebuffer.

4.Repeatsteps2and3forotherimages.

5.Testtheframebufferforcompleteness.


AllimplementationsofOpenGLESrequiresomeplatformspecificcodetocreatearenderingcontextandtouseittodrawtothescreen.

EAGL: Embedded Apple OpenGL Extension for MacOS X


* EAGLContextclass

- defines rendering context

- target of OpenGL ES commands

- presents images to Core Animation for display


EveryEAGLContextobjectcontainsareferencetoanEAGLSharegroupobject (texture, buffer, framebuffer, shader, ...).


EAGLDrawableprotocol:objectcanbeusedtoallocatestoreforarenderbufferthatcanlater bepresentedbytheuser. ImplementedonlybytheCAEAGLLayerclass.


** OpenGL ES on the iPhone

= OpenGL ES 1.1 (fixed function graphics pipeline)

+ OpenGL ES 2.0 (shader pipeline)


**IfyourapplicationfailstotestthecapabilitiesofOpenGLESatruntime,itmaycrashorfailtorun.


- v1.1: good baseline behavior for 3D graphics pipeline (on all iTouches)

- v2.0: more flexible. custom vertex and fragment operations are implemented trivially. this is not super set of 1.1.


EAGLContext* myContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES<1|2>];

If a particular implementation of OpenGL ES is not available,initWithAPI: will return nil. Application must test it before using it.


Ch2. Determining OpenGL ES Capabilities

Sincethecapabilitiesofthecontextwillnotchangeonceithasbeencreated,yourapplicationcantestthisonceanddeterminewhichpathitwilluse.


* common: GL_MAX_TEXTURE_SIZE, GL_DEPTH_BITS, GL_STENCIL_BITS

* 1.1: GL_MAX_TEXTURE_UNITS, GL_MAX_CLIP_PLANES

* 2.0: GL_MAX_VERTEX_ATTRIBS, GL_MAX_VERTEX_UNIFORM_VECTORS, GL_MAX_FRAGMENT_UNIFORM_VECTORS, GL_MAX_VARYING_VECTORS, GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, GL_MAX_TEXTURE_IMAGE_UNITS


BOOL CheckForExtension(NSString *searchName)

{

    NSString *extensionsString = [NSString stringWithCString:glGetString(GL_EXTENSIONS) encoding: NSASCIIStringEncoding];

    NSArray *extensionsNames = [extensionsString componentsSeparatedByString:@" "];

    return [extensionsNames containsObject: searchName];

}


ThedebugversionofyourapplicationshouldcallglGetErroraftereveryOpenGLEScommand.


Ch3. Working with EAGL

// creating EAGL context

EAGLContext* myContext = [[EAGLContext alloc]

initWithAPI:kEAGLRenderingAPIOpenGLES1];

[EAGLContext setCurrentContext: myContext];


// create framebuffer object

1. create framebuffer object

2. create target (renderbuffer or texture), allocate storage for target, attatch it to the framebuffer object

3. test framebuffer for completeness


1. Create the framebuffer and bind it so that future OpenGL ES framebuffer commands are directed to it. 

GLuint framebuffer;

glGenFramebuffersOES(1, &framebuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);

2. Create a color renderbuffer, allocate storage for it, and attach it to the framebuffer. 

GLuint colorRenderbuffer;

glGenRenderbuffersOES(1, &colorRenderbuffer);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);

glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, width, height);

glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

3. Perform similar steps to create and attach a depth renderbuffer. 

GLuint depthRenderbuffer;

glGenRenderbuffersOES(1, &depthRenderbuffer);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);

glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, width,height);

glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

4. Test the framebuffer for completeness. 

GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) ;

if(status != GL_FRAMEBUFFER_COMPLETE_OES) {

    NSLog(@"failed to make complete framebuffer object %x", status);

}


If your CAEAGLLayer object must be blended with other layers, you will see a significant performance penalty.You can reduce this penalty by playingy our CAEAGLLayer behind other UIKit layers.


* Sharegroup *

EAGLContext* firstContext = [[EAGLContext alloc]

initWithAPI:kEAGLRenderingAPIOpenGLES1];

EAGLContext* secondContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup: [firstContext sharegroup]];


Ch4. Working with Vertex Data

AvoidtheuseoftheOpenGLESGL_FIXEDdatatype.

-glDrawArraystodrawyourgeometry

- useglDrawElementstospecifyindicesforthetrianglesinyourgeometry


If the data did not change, use vertex buffer object (VBO).


Ch5. Working with Texture Data

PowerVR Texture Compression (PRVTC) format by implementing theGL_IMG_texture_compression_pvrtc extension


Future Apple hardware may not support the PVRTC texture format. You must test for the existence of compressed texture.


Createandloadyourtexturedataduringinitialization.


BindingtoatexturechangesOpenGLESstate. Avoid unnecessary change.


Onewaytoavoidchangingthetextureistocombinemultiplesmallertexturesintoasinglelargetexture, knownasatextureatlas.


Yourapplicationshouldprovidemipmapsforalltexturesexceptthosebeingusedtodraw2Dunscaledimages.

GL_LINEAR_MIPMAP_LINEARfiltermodeprovidesthebestquality.GL_LINEAR_MIPMAP_NEARESTfiltermode better performance.


Ch6. Performance

* Redraw Scenes Only when Necessary

: Aslower,butfixedframerate (e.g. 30 fps)oftenappearssmoothertotheuserthanafastbutvariableframerate.


* Use Floating Point Arithmetic

: use ARM instruction set but not Thumb


* Disable Unused OpenGL ES features


* Minimize the Number of Draw Calls

:consolidate geometry that is in close spacialproximity


* Memory

-After loading an image into your OpenGL ES texture using glTexImage, you can free the original image.

-Only allocate a depth buffer when your application requires it.

-If your application does not need allof its resources at once,only load a subset of the totalresources.


* Avoid Querying OpenGL ES State

: Callsto glGet*() including glGetError() may require OpenGL ES to execute allprevious commands before retrieving any state variables.This synchronization forces the graphics hardware to run lockstep withthe CPU,reducing opportunities for parallelism. UseglGetError() only in debug build.


* Avoid Changing OpenGL ES State Unnecessarily


* Drawing order

: Do not waste CPU time sorting objects front to back.

: Sort objects by their opacity (opaque > alpha testing > alpha blended)


App.A. Using Texturetool

ex) Encode Image.png into PVRTC using linear weights and 4 bpp, and saving the output as ImageL4.pvrtc and a PNG preview as ImageL4.png


user$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-4 -o ImageL4.pvrtc -p ImageL4.png Image.png


ex) uploading image

void texImage2DPVRTC(GLint level, GLsizei bpp, GLboolean hasAlpha, GLsizei width, GLsizei height, void *pvrtcData)

{

    GLenum format;

    GLsizei size = width * height * bpp / 8;

    if(hasAlpha) {

        format = (bpp == 4) ? GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG :

GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;

    } else {

        format = (bpp == 4) ? GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG :

GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;

    }

    if(size < 32) {

        size = 32;

    }

    glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height, 0, size,

data);

}




출처 : http://blog.naver.com/PostView.nhn?blogId=gonagi&logNo=150067227174
Posted by 오늘마감

댓글을 달아 주세요