Simple OpenGL ES problem

Apprentice
Posts: 12
Joined: 2009.04
Post: #1
I am trying to do some basic OpenGL ES with my iPhone, but I have some problems doing so. In my Xcode, I have created a new OpenGL ES based application project and I have changed the drawView function in the EAGLView.m file to look like this:

Code:
- (void)drawView {
    
    glEnable(GL_DEPTH_TEST);
    
    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    glOrthof(-100.0f, 100.0f, -150.0f, 150.0f, -100.0f, 100.0f);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    static GLfloat rotation = 0.0f;
    
    glRotatef(rotation, 0.0f, 1.0f, 0.0f);
    rotation += 1.0f;

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    const GLfloat squareVertices[] = {
        -50.0f, -50.0f, -1.0f,
        50.0f,  -50.0f,    -1.0f,
        -50.0f,  50.0f, -1.0f,
        50.0f,   50.0f, -1.0f,
    };
    
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    
    
    
    const GLubyte squareColors[] = {
        255, 255,   0, 255,
        255, 255,   0, 255,
        0,     0,   0,   0,
        255,   0, 255, 255,
    };
    
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    glEnableClientState(GL_COLOR_ARRAY);
    
    const GLfloat squareVertices2[] = {
        -40.0f, -40.0f, -12.0f,
        40.0f,  -40.0f, -12.0f,
        -40.0f,  40.0f, -12.0f,
        40.0f,   40.0f, -12.0f,
    };
    
    glVertexPointer(3, GL_FLOAT, 0, squareVertices2);
    glDrawArrays(GL_TRIANGLE_STRIP,0, 4);
    
    
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

Although I can get my two rotating squares, when the rotation is 180 degrees, the smaller square still seems to be in front of the bigger one, but this should not happen... I know the answer is simple but I can't find it! I have already enabled GL_DEPTH_TEST as you can see. The same thing happens if I try setting the z value of the smaller square to something else (like a positive value).

Can anyone help?
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #2
You need to enable the depth buffer (you'll find this at the top of EAGLView.m):

#define USE_DEPTH_BUFFER 1

And you need to clear it every frame (do this in drawView):

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2009.04
Post: #3
Yes, that was indeed simple. Sorry for wasting your time.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #4
No problem, it is often very easy to miss simple details, especially when learning OpenGL. Wink
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Alpha Channel Problem Moganza 1 2,704 Jan 19, 2013 08:25 AM
Last Post: sealfin
  iPad, OpenGL ES, and XCode Instruments problem! Bandit 0 3,703 Dec 13, 2010 01:21 PM
Last Post: Bandit
  OpenGL global and local coordinate problem. mikey 4 3,656 May 26, 2009 08:48 AM
Last Post: mikey
  Opengl picking problem (zip file) papillon68 1 4,312 Mar 1, 2009 08:49 PM
Last Post: chronus
  Simple OpenGL 2d test - nothing displaying SaxMan 8 6,080 Jan 19, 2009 12:04 AM
Last Post: TythosEternal