Wednesday, March 31, 2010

GLTexture implementation

While using the template of OpenGL ES that comes bundle with xCode to develop OpenGL applications, I've started experimenting around with GLTexture class implementation to load images to the OpenGL ES system. Thanks to the help of 71squared Game Development Tutorials I was able to create an image texture to be display by my program.

#import "GLTexture.h"

First we imported the GLTexture header file for our EAGLView header file to find. Later on we created our texture image:


// Create texture image.

GLTexture *diamond;


This code is included in @private of @interface EAGLView : UIView. When we are done making the proper modifications in the header file we now go into the implementation file in our function -(id)initWithCoder:(NSCoder*)coder. In this case we are setting very important properties here; first, before we start drawing our GLTexture which we recently setup in our header file of EAGLView we must setup our OpenGL ES system as follows:


// Get the bounds of the main screen

CGRect rect = [[UIScreen mainScreen] bounds];

// Set up OpenGL projection matrix

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);

glMatrixMode(GL_MODELVIEW);

// Initialize OpenGL states

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D);

glDisable(GL_DEPTH_TEST);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND_SRC);

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);


The above source code should be familiar by now for those of you who know how to work around in the OpenGL System. We set these properties here to make our drawView function more organized, readable and understandable. Now on to our drawView function.

- (void)drawView {

[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

glViewport(0, 0, backingWidth, backingHeight);

// Init image

diamond = [[GLTexture alloc] initWithImage:[UIImage imageNamed:@"Sprite.png"]];

[diamond drawAtPoint:CGPointMake(160.0f, 240.0f)];

// Draw game scene

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

[context presentRenderbuffer:GL_RENDERBUFFER_OES];

}


As we can see our define diamond instance that we made of GLTexture is currently allocating memory to be loaded by using CG functions such as UIImage and imageNamed: @"Sprite.png" the name of our texture we are using right at this moment. Later on we simply use the function CGPointMake() to place the image where we want it located. When we compile and run the example application we get the exact image that we have in our resources folder.

Quite interesting how to load an image into the OpenGL ES system with this implementation GLTexture. I just wonder if we can rotate, translate, scale, etc... The current image we have available? 71squared decided to create and implement further functions that takes this image attach it to a OpenGL object to be display and manipulated in such ways. FYI 71squared is a YouTube user that is devoted in iPhone Game Development and has uploaded videos that explain these functionalities. Thanks 71squared!

Julio Cruz

No comments:

Post a Comment