iDevGames Forums
OpenGL ES - Drawing a simple cube help. - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenGL ES - Drawing a simple cube help. (/thread-2432.html)



OpenGL ES - Drawing a simple cube help. - MattCairns - Aug 31, 2008 03:57 PM

Ok so I am trying to draw a cube in OpenGL and I just can't seem to get something right. When I run the program in the simulator or the phone it looks as if the cube is missing pieces and parts are attached to the wrong places. The whole cube almost seems semi-transparent. It creates an odd looking optical illusion though.

At first I thought it might be the vertices but later I tried exporting a cube model from Blender and I got the exact same results.

Maybe someone else can offer insight into this? Below is the code drawing my cube.

Code:
- (void)drawView {
    
    // Replace the implementation of this method to do your own custom drawing
    
    const GLfloat squareVertices[] = {
        // FRONT
        -0.5f, -0.5f,  0.5f,
        0.5f, -0.5f,  0.5f,
        -0.5f,  0.5f,  0.5f,
        0.5f,  0.5f,  0.5f,
        // BACK
        -0.5f, -0.5f, -0.5f,
        -0.5f,  0.5f, -0.5f,
        0.5f, -0.5f, -0.5f,
        0.5f,  0.5f, -0.5f,
        // LEFT
        -0.5f, -0.5f,  0.5f,
        -0.5f,  0.5f,  0.5f,
        -0.5f, -0.5f, -0.5f,
        -0.5f,  0.5f, -0.5f,
        // RIGHT
        0.5f, -0.5f, -0.5f,
        0.5f,  0.5f, -0.5f,
        0.5f, -0.5f,  0.5f,
        0.5f,  0.5f,  0.5f,
        // TOP
        -0.5f,  0.5f,  0.5f,
        0.5f,  0.5f,  0.5f,
        -0.5f,  0.5f, -0.5f,
        0.5f,  0.5f, -0.5f,
        // BOTTOM
        -0.5f, -0.5f,  0.5f,
        -0.5f, -0.5f, -0.5f,
        0.5f, -0.5f,  0.5f,
        0.5f, -0.5f, -0.5f,
        
        
    };
    
    [EAGLContext setCurrentContext:context];
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glRotatef(3.0f, 3.0f, 0.0f, 1.0f);
    
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
    
    glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
    glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
    glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
    
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
    glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
    
    glFlush ();
    
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

Thanks


OpenGL ES - Drawing a simple cube help. - arekkusu - Aug 31, 2008 05:01 PM

For depth testing to work, you need to create a depth renderbuffer and attach it to the view's FBO.

Alternatively, for a convex object like a cube, you don't need a depth buffer. Turn on backface culling.

Alternatively, for blended objects, sort your drawing manually.


OpenGL ES - Drawing a simple cube help. - MattCairns - Aug 31, 2008 05:08 PM

arekkusu Wrote:For depth testing to work, you need to create a depth renderbuffer and attach it to the view's FBO.

Alternatively, for a convex object like a cube, you don't need a depth buffer. Turn on backface culling.

Alternatively, for blended objects, sort your drawing manually.

Thanks for the help mate! Works perfectly now. Such a simple fix too.


OpenGL ES - Drawing a simple cube help. - mdonahoe - Oct 8, 2008 05:03 PM

Perhaps there is a better place for this, but has anyone else noticed difference between OpenGL on the simulator and on the iPhone?

I am using the OpenGL template application (the one with the spinning square) and just commenting out the line glClear(GL_COLOR_BUFFER_BIT); in EAGLView drawView:

I was making a drawing application, so I don't want to clear the buffer every frame. On the simulator, it works as expected, but on the iPhone it seems to cycle between 3 different buffers.

Any idea what is going on?


OpenGL ES - Drawing a simple cube help. - OneSadCookie - Oct 8, 2008 05:45 PM

Typically no guarantee is made about preservation of buffer contents (they could be cycled, flipped, copied, etc.). If you need the contents preserved, you might have to draw to an FBO, which will make such guarantees.


OpenGL ES - Drawing a simple cube help. - arekkusu - Oct 8, 2008 07:09 PM

Read the documentation for kEAGLDrawablePropertyRetainedBacking.
On the desktop the equivalent is kCGLPFABackingStore.


OpenGL ES - Drawing a simple cube help. - mdonahoe - Oct 10, 2008 02:07 PM

GLPaint seems to use kEAGLDrawablePropertyRetainedBacking to preserve the buffer, and that works great.

I now have a problem with glReadPixels. The color values from glReadPixels are from a delayed version of what is currently showing onscreen. Almost as if glReadPixels reads from an older buffer.

I tried doing an extra buffer swap right before a did a pixel read, but that seems to have no effect.

Is there a way to sync up the buffers so glReadPixels works? Or perhaps an alternative method of reading a pixel's color from the current buffer?


OpenGL ES - Drawing a simple cube help. - Frogblast - Oct 10, 2008 05:26 PM

mdonahoe Wrote:GLPaint seems to use kEAGLDrawablePropertyRetainedBacking to preserve the buffer, and that works great.

I now have a problem with glReadPixels. The color values from glReadPixels are from a delayed version of what is currently showing onscreen. Almost as if glReadPixels reads from an older buffer.

I tried doing an extra buffer swap right before a did a pixel read, but that seems to have no effect.

Is there a way to sync up the buffers so glReadPixels works? Or perhaps an alternative method of reading a pixel's color from the current buffer?

That sounds like a bug. Please report it at https://bugreport.apple.com. If at all possible, please include an example program that shows this issue.