GLKit & adding tints to textures - iphone

I am having issue with tinting a PNG image with GLKit.
I have a white PNG image that I load into the application and then use it to create a texture:
UIImage *image = [ UIImage imageNamed:#"brushImage" ];
NSError *error = nil;
texture_m = [[ GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error] retain ];
if (error) {
NSLog(#"Error loading texture from image: %#",error);
}
the texture is created with no errors.
however when I want to render the texture with blend activated the colour seems to be ignored and I get a white image. I had no issues when I was doing this in OpenGL1 were the image would pick up the colour that was defined in glColor4f(). Here is my render code:
-(void)render{
if (texture_m != nil) {
effect_m.texture2d0.enabled = GL_TRUE;
effect_m.texture2d0.envMode = GLKTextureEnvModeReplace;
effect_m.texture2d0.target = GLKTextureTarget2D;
effect_m.texture2d0.name = texture_m.name;
}
[effect_m prepareToDraw];
glClear(GL_COLOR_BUFFER_BIT);
GLfloat squareVertices[] = {
50, 50,
150, 50,
50, 150,
150, 150
};
GLfloat squareTexture[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
glColor4f( 1, 0, 0, 1 );
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glEnable(GL_BLEND);
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
}
Could anybody help to solve this issue
Thanks
Reza

I have managed to solve my problem and here is the solution
-(void)render
{
if (texture_m != nil) {
effect_m.texture2d0.enabled = GL_TRUE;
// here you need to env mode to GLKTextureEnvModeModulate rather than GLKTextureEnvModeReplace
effect_m.texture2d0.envMode = GLKTextureEnvModeModulate;
effect_m.texture2d0.target = GLKTextureTarget2D;
effect_m.texture2d0.name = texture_m.name;
}
// then here I have added the tint colour to the GLKBaseEffect class as constant colour which I imagine replaces the calls to glColor4f for OpenGL1.1
effect_m.useConstantColor = YES;
float alphaValue = 0.7;
GLKVector4 colour = GLKVector4Make( 0* alphaValue, 1* alphaValue, 1* alphaValue, alphaValue );
effect_m.constantColor = colour;
// remember multiplying the alpha value to each colour component
[effect_m prepareToDraw];
glClear(GL_COLOR_BUFFER_BIT);
GLfloat squareVertices[] = {
50, 50,
150, 50,
50, 150,
150, 150
};
GLfloat squareTexture[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
// glColor4f not necessary
// glColor4f( 1, 0, 0, 1 );
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glEnable(GL_BLEND);
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
}

Related

OpenGl ES 2.0 and GLKit : From GLKBaseEffect shaders to OpenGl

I'm coding a 2D game with OpenGL ES 2.0 and GLKit. My architecture is based on the Games by Ian Terrel tutorials.
I discover recently that the GLKBaseEffect (provide simple shaders mgmt) leaks and makes sometimes my app crash. I'm now using my own shaders files (based on the Ray Wenderlich tutorial) but, at this time, I've just succeed to display openGl background color. My shapes colors and textures aren't displayed anymore.
IMPORTANT : I was setting the current context badly, now openGL says invalid Drawable.
Here is a part of my shape.m code :
#implementation P3Shape
const GLushort indices[] = {
0,1,2,3
};
typedef struct {
float Position[2];
float Color[4];
float TexCoord[2];
} Vertex;
//testing
const Vertex Vertices[] = {
// Front
{{10, -15}, {1, 0, 0, 1}, {1, 0}},
{{10, 15}, {0, 1, 0, 1}, {1, 0}},
{{-10, 10}, {0, 0, 1, 1}, {0, 0}},
{{-10, -1}, {0, 0, 0, 1}, {0, 1}},
};
- (id)initWithLayer:(CAEAGLLayer *)layer {
self = [super init];
if (self) {
[...]
_currentContext = [P3SessionState currentContext];
_eaglLayer = layer;
_eaglLayer.opaque = YES;
[self setupRenderBuffer];
[self setupFrameBuffer];
[self addToProgram];
[self createBuffers];
}
return self;
}
- (int)numVertices {
return 0;
}
- (void) addToProgram {
// setted before in the gamemanager
GLuint programHandle = [P3SessionState programHandle];
_positionSlot = glGetAttribLocation(programHandle, "Position");
_colorSlot = glGetAttribLocation(programHandle, "SourceColor");
_projectionUniform = glGetUniformLocation(programHandle, "Projection");
_modelViewUniform = glGetUniformLocation(programHandle, "Modelview");
_texCoordSlot = glGetAttribLocation(programHandle, "TexCoordIn");
_textureUniform = glGetUniformLocation(programHandle, "Texture");
}
- (void)setupRenderBuffer {
glGenRenderbuffers(1, &_colorRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer);
[_currentContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];
}
- (void)setupFrameBuffer {
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
}
- (void)createBuffers {
// Static index data
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
// initialize the vertex data
- (GLKVector2 *)vertices {
if (vertexData == nil) {
vertexData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.numVertices];
}
return [vertexData mutableBytes];
}
// set a textureImage, allocate a glname and enable the GL_TEXTURE_2D and all
- (void)setTextureImage:(UIImage *)image {
CGImageRef spriteImage = image.CGImage;
if (!spriteImage) {
NSLog(#"Failed to load image %#", image);
exit(1);
}
size_t width = CGImageGetWidth(spriteImage);
size_t height = CGImageGetHeight(spriteImage);
GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));
CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
CGContextRelease(spriteContext);
GLuint texName;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
free(spriteData);
_imageTexture = texName;
}
- (GLKVector2 *)textureCoordinates {
if (textureCoordinateData == nil)
textureCoordinateData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.numVertices];
return [textureCoordinateData mutableBytes];
}
- (void)render {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelview.m);
glUniformMatrix4fv(_projectionUniform, 1, 0, projectionMatrix.m);
glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glVertexAttribPointer(_positionSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
if(texture != nil) {
glEnableVertexAttribArray(_texCoordSlot);
glActiveTexture(GL_TEXTURE0); // unneccc in practice
glBindTexture(GL_TEXTURE_2D, _imageTexture);
glUniform1i(_textureUniform, 0); // unnecc in practice
glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));
}
glDrawElements(GL_TRIANGLE_FAN, sizeof(indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, (void*)0);
}
//is cleanup code that tells the shader that we’re done using vertex position data.
glDisableVertexAttribArray(GLKVertexAttribPosition);
if (texture != nil)
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
[_currentContext presentRenderbuffer:GL_RENDERBUFFER];
glDisable(GL_BLEND);
}
// draw the shape in the selected scene
- (void)renderInScene:(P3Scene *)theScene {
projectionMatrix = [theScene projectionMatrix];
modelview = GLKMatrix4Multiply(GLKMatrix4MakeTranslation(0, 0, 0), GLKMatrix4MakeRotation(0, 0, 0, 1));
modelview = GLKMatrix4Multiply(modelview, GLKMatrix4MakeScale(scale.x, scale.y, 1));
[self render];
}
-(void)update:(NSTimeInterval)dt {
if(self.toDraw)
[spriteAnimation update:dt];
}
#end
As you can see, I'm testing with a Vertices array and a basic color but it makes no differences. OpenGl seems to be set correctly because my view is green thanks to the glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0); in the render function.
Here the files on pastebin :
P3Shape.m on pastebin
And a manager who create the ogl program
P3GameManager.m on pastebin
Any ideas ?
EDIT : maybe a problem with my shaders ?
vertex :
attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
uniform mat4 Projection;
uniform mat4 Modelview;
attribute vec2 TexCoordIn; // New
varying vec2 TexCoordOut; // New
void main(void) {
DestinationColor = SourceColor;
gl_Position = Projection * Modelview * Position;
TexCoordOut = TexCoordIn; // New
}
and fragment :
varying lowp vec4 DestinationColor;
varying lowp vec2 TexCoordOut; // New
uniform sampler2D Texture; // New
void main(void) {
gl_FragColor = DestinationColor * texture2D(Texture, TexCoordOut); // New
}
I recommend using GLKViewController and GLKView to simplify your setup. Similarly, replace your texture loading with GLKTextureLoader.
I haven't seen any issues with GLKBaseEffect leaking memory and causing crashes. I would suggest posting a new question about this particular problem, and returning to using GLKBaseEffect if you don't need custom shaders (it looks like you don't). Will make your code a lot easier to manage and debug.

Simple gradient issue with OpenGL on iphone simulator

i was following the tutorial from raywenderlich website, the gradient seems not to work perfectly, is it only because of the iphone simulator, or is it something else? I can't try myself with an iphone. Here is the image :
And the code :
-(CCSprite *)spriteWithColor:(ccColor4F)bgColor textureSize:(float)textureSize {
// 1: Create new CCRenderTexture
CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize height:screenSize.height];
// 2: Call CCRenderTexture:begin
[rt beginWithClear:bgColor.r g:bgColor.g b:bgColor.b a:bgColor.a];
// 3: Draw into the texture
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
float gradientAlpha = 0.5;
CGPoint vertices[4];
ccColor4F colors[4];
int nVertices = 0;
vertices[nVertices] = CGPointMake(0, 0);
colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
vertices[nVertices] = CGPointMake(textureSize, 0);
colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
vertices[nVertices] = CGPointMake(0, screenSize.height);
colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
vertices[nVertices] = CGPointMake(textureSize, screenSize.height);
colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_FLOAT, 0, colors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
// 4: Call CCRenderTexture:end
[rt end];
// 5: Create a new Sprite from the texture
return [CCSprite spriteWithTexture:rt.sprite.texture];
}
Thanks

making an iphone cube class

I followed this tutorial and I'm currently trying to create a class to draw cubes. The class populates the buffers in a function called setup and draws them in a function called draw.
There are no errors however nothing is displayed on the screen.
My question is basicly how do I go about making a class from which I can render cubes.
Using Xcode 4.2, Opengl and Objective-C.
[edit]
-(void)SetShader:(GLuint)InShader
{
glUseProgram(InShader);
_positionSlot = glGetAttribLocation(InShader, "Position");
_colorSlot = glGetAttribLocation(InShader, "SourceColor");
glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);
_projectionUniform = glGetUniformLocation(InShader, "Projection");
_modelViewUniform = glGetUniformLocation(InShader, "Modelview");
_texCoordSlot = glGetAttribLocation(InShader, "TexCoordIn");
glEnableVertexAttribArray(_texCoordSlot);
_textureUniform = glGetUniformLocation(InShader, "Texture");
}
-(void)SetupVBO
{
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(CubeVertices), CubeVertices, GL_STATIC_DRAW);
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeIndices), CubeIndices, GL_STATIC_DRAW);
}
-(void)draw
{
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));
glDrawElements(GL_TRIANGLES, sizeof(CubeIndices)/sizeof(CubeIndices[0]), GL_UNSIGNED_BYTE, 0);
}
-(void)render:(CADisplayLink*)displayLink
{
glClearColor(0, 0, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(0, 0, -7)];
[modelView translateBy:CC3VectorMake(0,5,0)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
[m_cube draw];
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
It was as mentioned a problem with the arrangement of my viewport and perspective setups.

How to change the perspective on a 3D cube in OpenGL ES?

I draw a cube with OpenGL ES and I want to have an above perspective on it.But I don't know what should I modify...Maybe you can help.
- (void)render:(CADisplayLink*)displayLink {
//-(void) render{
glClearColor(255.0, 255.0/255.0, 255.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//projection
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:8];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
// _currentScaling += displayLink.duration * 1.0;
[modelView populateFromTranslation:CC3VectorMake(sin(30), _currentScaling , -7)];
[modelView rotateBy:CC3VectorMake(0, -15, 0)];
// [modelView scaleByY:_currentScaling];
if(_currentScaling > 2 ){
[displayLink invalidate];
_running = 1;
// scale = 0;
}
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
// 1
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
// 2
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
// 3
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]),
GL_UNSIGNED_BYTE, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
I recently started looking into OpenGL myself and found this same example. I believe what you are looking for is to change this line to change the perspective:
[modelView rotateBy:CC3VectorMake(0, -15, 0)];
and specify the degrees of rotation you want for the x,y,z axes.

app running on simulator but not in ipad..!

A'm stucked with a very strange problem.....really cant get how to come out with that..!!
I am developing iPad application in which I am drawing a 3d cube with openGL programming for ipad....Everything is fine....I drawn the cube and also colored it with different colors....This all things I have tested on simulator, everything is great. But when I tried to test on iPad, my cube is being drawn but coloring part is not working...!!!
Thanks in advance for help..!!
UPDATE : My sample code
MyViewController.h
CubeView *cubeView;
MyViewController.m
cubeView = [[CubeView alloc] initWithFrame:CGRectMake(250, 60, 450, 600)];
cubeView.multipleTouchEnabled = YES;
cubeView.backgroundColor = [UIColor clearColor];
[self.view addSubview:cubeView];
CubeView.m
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
[self setMultipleTouchEnabled:YES];
// self.frame = CGRectMake(20, 30, 500, 500);
renderer = nil;
if (!renderer)
{
renderer = [[CubeRenderer alloc] init];
if (!renderer)
{
[self release];
return nil;
}
}
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
[self setMultipleTouchEnabled:YES];
renderer = nil;
if (!renderer)
{
renderer = [[CubeRenderer alloc] init];
if (!renderer)
{
[self release];
return nil;
}
}
}
return self;
}
CubeRenderer.m
- (id)init
{
if ((self = [super init]))
{
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context])
{
[self release];
return nil;
}
currentCalculatedMatrix = CATransform3DIdentity;
// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
glGenFramebuffersOES(1, &defaultFramebuffer);
glGenRenderbuffersOES(1, &colorRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
}
return self;
}
static const GLfloat cubeVertices[] = {
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
};
static const GLushort cubeIndicesFaceFront[] = {
0, 1, 2, 3, 0
};
static const GLushort cubeIndicesFaceBack[] = {
4, 5, 6, 7, 4
};
static const GLushort cubeIndicesFaceLeft[] = {
0, 4, 7, 3, 0
};
static const GLushort cubeIndicesFaceRight[] = {
1, 5, 6, 2, 1
};
static const GLushort cubeIndicesFaceTop[] = {
3, 2, 6, 7, 3
};
static const GLushort cubeIndicesFaceBottom[] = {
0, 1, 5, 4, 0
};
[EAGLContext setCurrentContext:context];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-2.0, 2.0, -2.0 * 480.0 / 320.0, 2.0 * 480.0 / 320.0, -3.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeColorsBlueFace);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceFront);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceFront);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceBack);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBack);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBack);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceLeft);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceLeft);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceLeft);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceRight);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceRight);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceRight);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceTop);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceTop);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceTop);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceBottom);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBottom);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBottom);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];