Orthographic Projection

Member
Posts: 215
Joined: 2008.06
Post: #16
arekkusu Wrote:Did you allocate a depth buffer?

Would that look something like this?:

glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #17
Your rotations are all messed up. Try this instead:

Code:
- (void)drawView
{
    
    const GLfloat squareVertices[] =
        {
        -0.5f, 0.0f, -0.5f,
        0.5f, 0.0f,  -0.5f,
        -0.5f, 0.0f,  0.5f,
        0.5f, 0.0f,  0.5f,
    };
    
    /**
    const GLubyte squareColors[] =
    {
        255, 255,   0, 255,
        0,   255, 255, 255,
        0,     0,   0,   0,
        255,   0, 255, 255,
    };
     */
    
    const GLfloat squareTextureVerts[] =
    {
        0.0f, 0.0f,
        1.0f, 0.0f,
        0.0f, 1.0f,
        1.0f, 1.0f
    };
    
    [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);
    
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, [splashScreenTex name]);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    
    // only need to do these once
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glTexCoordPointer(2, GL_FLOAT, 0, squareTextureVerts);
    glEnable(GL_DEPTH_TEST);
    
    // push and load identity for camera
    glPushMatrix();
    glLoadIdentity();
    
    static GLfloat        rot = 0.0f;
    
    rot += 0.3f;
    while (rot > 360.0f)
        rot -= 360.0f;
    glRotatef(rot * 2.0f, 1.0f, 0.0f, 0.0f);
    glRotatef(rot, 0.0f, 1.0f, 0.0f);
    
    //Bottom
    glPushMatrix();
    glTranslatef(0.0f, -0.5f, 0.0f); // move down along y axis
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    //Top
    glPushMatrix();
    glTranslatef(0.0f, 0.5f, 0.0f); // move up along y axis
    glRotatef(180.0f, 1.0f, 0.0f, 0.0f); // flip it so it faces opposite of bottom
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    
    //Front
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, 0.5f); // move toward viewer on z axis
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f); // tilt up
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // blue
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    //Back
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, -0.5f); // move into the screen along the z axis
    glRotatef(180.0f, 0.0f, 1.0f, 0.0f); // rotate heading to face back
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f);    // tilt up
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // red
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    //Left
    glPushMatrix();
    glTranslatef(-0.5f, 0.0f, 0.0f); // move left on x axis
    glRotatef(90.0f, 0.0f, 1.0f, 0.0f); // rotate heading to face left
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f);    // tilt up
    glColor4f(0.0f, 1.0f, 0.0f, 1.0f); // green
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    //Right
    glPushMatrix();
    glTranslatef(0.5f, 0.0f, 0.0f); // move right on x axis
    glRotatef(-90.0f, 0.0f, 1.0f, 0.0f); // rotate heading to face right
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f);    // tilt up
    glColor4f(0.0f, 1.0f, 1.0f, 1.0f); // cyan
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    // pop the camera
    glPopMatrix();
    
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #18
AnotherJake Wrote:Your rotations are all messed up. Try this instead:

Code:
- (void)drawView
{
    
    const GLfloat squareVertices[] =
        {
        -0.5f, 0.0f, -0.5f,
        0.5f, 0.0f,  -0.5f,
        -0.5f, 0.0f,  0.5f,
        0.5f, 0.0f,  0.5f,
    };
    
    /**
    const GLubyte squareColors[] =
    {
        255, 255,   0, 255,
        0,   255, 255, 255,
        0,     0,   0,   0,
        255,   0, 255, 255,
    };
     */
    
    const GLfloat squareTextureVerts[] =
    {
        0.0f, 0.0f,
        1.0f, 0.0f,
        0.0f, 1.0f,
        1.0f, 1.0f
    };
    
    [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);
    
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, [splashScreenTex name]);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    
    // only need to do these once
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glTexCoordPointer(2, GL_FLOAT, 0, squareTextureVerts);
    glEnable(GL_DEPTH_TEST);
    
    // push and load identity for camera
    glPushMatrix();
    glLoadIdentity();
    
    static GLfloat        rot = 0.0f;
    
    rot += 0.3f;
    while (rot > 360.0f)
        rot -= 360.0f;
    glRotatef(rot * 2.0f, 1.0f, 0.0f, 0.0f);
    glRotatef(rot, 0.0f, 1.0f, 0.0f);
    
    //Bottom
    glPushMatrix();
    glTranslatef(0.0f, -0.5f, 0.0f); // move down along y axis
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    //Top
    glPushMatrix();
    glTranslatef(0.0f, 0.5f, 0.0f); // move up along y axis
    glRotatef(180.0f, 1.0f, 0.0f, 0.0f); // flip it so it faces opposite of bottom
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    
    //Front
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, 0.5f); // move toward viewer on z axis
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f); // tilt up
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // blue
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    //Back
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, -0.5f); // move into the screen along the z axis
    glRotatef(180.0f, 0.0f, 1.0f, 0.0f); // rotate heading to face back
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f);    // tilt up
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // red
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    //Left
    glPushMatrix();
    glTranslatef(-0.5f, 0.0f, 0.0f); // move left on x axis
    glRotatef(90.0f, 0.0f, 1.0f, 0.0f); // rotate heading to face left
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f);    // tilt up
    glColor4f(0.0f, 1.0f, 0.0f, 1.0f); // green
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    //Right
    glPushMatrix();
    glTranslatef(0.5f, 0.0f, 0.0f); // move right on x axis
    glRotatef(-90.0f, 0.0f, 1.0f, 0.0f); // rotate heading to face right
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f);    // tilt up
    glColor4f(0.0f, 1.0f, 1.0f, 1.0f); // cyan
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    // pop the camera
    glPopMatrix();
    
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

Why do you have the "tilt up" commands after the heading changes? How is this significantly different than what I had? Works great though, thanks!

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #19
Talyn Wrote:How is this significantly different than what I had?

The order of transforms is *critical* with OpenGL, so yes, that makes it significantly different. You'll get better at it after a while, but right now you'll have to do a lot of mental twisting to get a mind's eye on it. And even once you get it down better, it's still easy to muck up. Wink

The way I did the adjustments for you is:

- pretend you leave the cube unrotated and at the origin.
- move each side into position
- rotate each face around the y axis so it will point toward the correct direction
- then tilt each face up (if needed)

The way you have your vertex coordinates set up by default leaves each face flat down in the x-z plane, which is fine for the top and bottom, but the four sides have to be tilted up.

If you think about this just a little more, you'd realize that it would be better to have your default vertices in a vertical plane instead. ... and then thinking a little further, it would be better to pre-calculate *all* of the coordinates and put them in one array and call glDrawArrays only one time, without all the extra transform calls, for the entire cube.

But one step at a time. Wink
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #20
AnotherJake Wrote:The order of transforms is *critical* with OpenGL, so yes, that makes it significantly different. You'll get better at it after a while, but right now you'll have to do a lot of mental twisting to get a mind's eye on it. And even once you get it down better, it's still easy to muck up. Wink

The way I did the adjustments for you is:

- pretend you leave the cube unrotated and at the origin.
- move each side into position
- rotate each face around the y axis so it will point toward the correct direction
- then tilt each face up (if needed)

The way you have your vertex coordinates set up by default leaves each face flat down in the x-z plane, which is fine for the top and bottom, but the four sides have to be tilted up.

If you think about this just a little more, you'd realize that it would be better to have your default vertices in a vertical plane instead. ... and then thinking a little further, it would be better to pre-calculate *all* of the coordinates and put them in one array and call glDrawArrays only one time, without all the extra transform calls, for the entire cube.

But one step at a time. Wink

Right right! I had forgotten that I had changed my vertex coordinates to be x-z oriented. Thank you, this makes a lot more sense now. I had originally thought that I was doing things in the precise order you specified above, but I only did it for some planes. I agree that pre-calculated vertexes for the entire cube would be ideal, but the context in which I plan to apply these lessons will not allow for that. In any case, thank you for your help, AJ, and be looking for more questions on similar subjects sometime soon.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #21
Thank you guys so much for the help! I got my isometric orthographic scene working great in OGL ES, so thank you to everyone who posted. Don't think I'm out of the woods yet, but thanks for getting me closer!

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPhone Texture Projection GLphysX 2 2,511 Apr 28, 2010 02:32 PM
Last Post: aBabyRabbit