OpenGL ES - Drawing a simple cube help.

Nibbie
Posts: 3
Joined: 2008.08
Post: #1
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
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #2
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.
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2008.08
Post: #3
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.
Quote this message in a reply
Member
Posts: 21
Joined: 2008.10
Post: #4
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?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
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.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #6
Read the documentation for kEAGLDrawablePropertyRetainedBacking.
On the desktop the equivalent is kCGLPFABackingStore.
Quote this message in a reply
Member
Posts: 21
Joined: 2008.10
Post: #7
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?
Quote this message in a reply
Member
Posts: 87
Joined: 2006.08
Post: #8
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Simple OpenGL ES problem soulstorm 3 3,966 May 14, 2009 03:53 PM
Last Post: AnotherJake
  Cube auto-rotate sakrist 2 2,905 May 5, 2009 03:31 AM
Last Post: Ingemar
  Simple OpenGL 2d test - nothing displaying SaxMan 8 6,073 Jan 19, 2009 12:04 AM
Last Post: TythosEternal
  opengl text/font drawing from CGContext fretmunky 11 12,433 Dec 7, 2008 11:29 AM
Last Post: fretmunky
  Very simple OpenGL problem evangs 10 5,303 Dec 10, 2007 01:32 PM
Last Post: wyrmmage