3D in Ortho

Member
Posts: 241
Joined: 2008.07
Post: #1
I'm trying to draw some 3D objects so that regardless of their position on screen, they always have the same perspective.

I tried pulling the camera waaaay far back and narrowing the FOV to a very narrow field but that didn't work and at that distance, even with the far clipping plane set far back enough, there occurred some z-fighting between the front and back of the objects.

The only other thing I can think of is drawing the 3D objects to an orthographic perspective, so that there will be no dimension other than the way the objects are oriented. I can scale them to represent zoom, if needed.

I'm not sure what else there is to do to set this up. I presume we'll need lighting and depth testing turned on as you would with any other 3D objects, so that it draws correctly so I'm turning those on before drawing the 3D and back off before the 2D.

I removed the camera and any camera transformations and repositioned the objects at the origin so that I can see them as a start. They draw right up close on the screen when I set the projection to perspective. Now, when I set the orthographic mode, I see the background and nothing else. It's not behind the background because when I turn off the background draw call, it's just the clear color.

Here's what my ortho call looks like:

Code:
    glOrthof(0.0f, WIDTH, HEIGHT, 0.0F, -1.0f, 100.0f);

Can anyone think what I'm doing wrong?
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #2
bmantzey Wrote:I'm not sure what else there is to do to set this up. I presume we'll need lighting and depth testing turned on as you would with any other 3D objects, so that it draws correctly so I'm turning those on before drawing the 3D and back off before the 2D.
There shouldn't need to be any difference other than the way you set up your projection matrix. I can't see anything obviously wrong from what you posted, so I'd be resorting to more basic troubleshooting. If you draw a quad facing the viewer at the origin, can you see it? If so, there's probably something wrong with how/where you're drawing your object(s). Since you're drawing them at the origin, maybe you're only seeing the inside of the objects and their backfaces are getting culled?
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #3
Maybe if I post my code, it will become apparent where the problem lies.

First I setup:

Code:
-(void)setup
{
    glClearColor(0.3f, 0.65f, 0.2f, 1.0f);
    glClearDepthf(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    
    // Set a blending function to use
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // Enable blending
    glEnable(GL_BLEND);
    glAlphaFunc(GL_GREATER, 0.1F);
    glEnable(GL_ALPHA_TEST);
    // Enable use of the texture
    glEnable(GL_TEXTURE_2D);

        ///  LIGHTING AND MATERIALS SETUP

        ///  Load mesh data and textures...

    glViewport(0, 0, WIDTH, HEIGHT);
    glDisable(GL_CULL_FACE);
}

Then I draw:

Code:
-(void)draw
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glBindBuffer(GL_ARRAY_BUFFER, (int)nil);

    glEnable(GL_TEXTURE_2D);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    {
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_LIGHTING);
        [self orthographic:-1.0f:100.0f];
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        {
            glLoadIdentity();
                        //  PERFORM 2D BACKGROUND DRAWING HERE

        } glPopMatrix();
        glMatrixMode(GL_PROJECTION);
    } glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    glPushMatrix(); // Identity
    {
        //[frustum Set:FOV :(float)WIDTH/(float)HEIGHT : 0.1f:100.0f];
            glOrthof(0.0f, 320, 480, 0.0F, -1.0F, 100.0F);
            glDepthMask(GL_FALSE);
        
        glLightfv(GL_LIGHT0, GL_POSITION, lightDirection);

                ///////  PERFORM 3D DRAWING HERE //////
                // Objects are centered at 0,0,0 and do appear when the frustum is perspective
                //  but not when it's glOrtho
        
    } glPopMatrix();

       ///// TODO:  SETUP 2D OBJECTS AND HUD DRAWING
}
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #4
Strange, I still can't see anything obviously wrong. One thing it might be is that your ortho projection has {0, 0} in the upper left corner, which may not center your objects where you expect them to be. It's also possible that 320 x 480 is large enough that your objects are too small to see, depending on how large they are themselves. Maybe try glOrthof(-((float) width / (float) height), ((float) width / (float) height), -1.0f, 1.0f, -1.0f, 100.0f)?
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #5
Update:

No luck with that. I even tried making the view bigger, scaling the models both ways, implementing a variable scale that goes up and down from 0.0 to 2.0...

It draws just as you would expect in the perspective.

I even put the 3D draw calls in with the 2D stuff just to see if it would work. newp Sad

The thing that really makes this painful is it was working last night and I went to change some stuff and ended up losing what I did. I don't have a backup either. Sad I remember doing nothing special to it and it worked right away. I merely changed the projection to ortho instead of projection.

Update:

Okay, I'm not sure why or how but I rotated 90 degrees it and then translated it a little bit and now it's right there laughing in my face. Ugh!

Update:

I'm not sure what's happening now. It's drawing but I realized that what it's drawing to is actually the projection identity matrix. Upon "fixing" it so that it's supposedly drawing to the orthographic perspective, it breaks.

Update:

It makes sense now. You were right. I scaled my model 300 times and it appeared down in the lower left corner. Yay!

(I'm using iPhone with landscape orientation, so it would be top left if I was holding it right side up.)
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #6
Be wary of setting your matrix modes correctly, too.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Ortho + Aspect Ratios jSTIN 2 2,985 Apr 6, 2005 09:21 AM
Last Post: jSTIN
  2D to 3D and back (Ortho and Perspective) Phate 5 5,509 Nov 30, 2003 10:56 AM
Last Post: AnotherJake
  Ortho problems Kattmat 6 3,446 Oct 12, 2003 12:15 PM
Last Post: Kattmat
  Interpolating Ortho projection to 3d arekkusu 8 5,832 Feb 27, 2003 04:53 AM
Last Post: codemattic