OpenGL ES How to Correctly Combine Orthof and Frustum

741 Views Asked by At

I am beginning to learn OpenGL ES 1.1 for iPhone and would like to draw a 2D image in orthographic projection behind a few 3D objects. Using Jeff Lamarche's tutorials and the book Pro OpenGL ES for iPhone and I've come up with the following couple methods to attempt to do this. If I disable the call to drawSunInRect the 3D objects are rendered just fine and I can move them with touch controls etc. If I uncomment that call and try to draw the sun image, the image appears in the CGRect I supply, but I cannot see any of my other 3D objects - the rest of the screen is black. I've tried to disable/enable depth testing in various places, pass different parameters to glOrthof(), and move around the rectangle but I keep getting the sun image only when the drawSunInRect method is called. I'm assuming it is covering my 3D objects.

// Draw Sun in Rect and with Depth
- (void)drawSunInRect:(CGRect)rect withDepth:(float)depth {

    // Get screen bounds
    CGRect frame = [[UIScreen mainScreen] bounds];

    // Calculate vertices from passed CGRect and depth
    GLfloat  vertices[] = 
    {
        rect.origin.x, rect.origin.y, depth,

        rect.origin.x + rect.size.width , rect.origin.y, depth,

        rect.origin.x, rect.size.height+rect.origin.y , depth,

        rect.origin.x + rect.size.width , rect.size.height+rect.origin.y ,depth
    };

    // Map the texture coords - no repeating
    static  GLfloat textureCoords[] = 
    {               
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0
    };

    // Disable DEPTH test and setup Ortho
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrthof(0,frame.size.width,frame.size.height,0,0.1f,1000.0); 

    // Enable blending and configure
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);

    // Enable VERTEX and TEXTURE client states
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    // Enable Textures
    glEnable(GL_TEXTURE_2D); 

    // Projection Matrix Mode for ortho and reset
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();     
    glLoadIdentity();      

    // No lighting or Depth Mask for 2D - no Culling
    glDisable(GL_LIGHTING);                                                                                          
    glDepthMask(GL_FALSE);
    glDisable(GL_CULL_FACE);
    glDisableClientState(GL_COLOR_ARRAY);   

    // Define ortho
    glOrthof(0,frame.size.width,frame.size.height,0,0.1f,1000); 

     // From Jeff Lamarche Tutorials
     // glOrthof(-1.0,                                          // Left
     // 1.0,                                                    // Right
     // -1.0 / (rect.size.width / rect.size.height),            // Bottom
     // 1.0 / (rect.size.width / rect.size.height),             // Top
     // 0.01,                                                   // Near
     // 10000.0);                                               // Far

    // Setup Model View Matrix
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    // Bind and draw the Texture
    glBindTexture(GL_TEXTURE_2D,sunInt);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT,0,textureCoords);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glMatrixMode(GL_MODELVIEW); 
    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    // Re-enable lighting 
    glEnable(GL_LIGHTING);  
    glDisable(GL_TEXTURE_2D);   
    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDepthMask(GL_TRUE);

}


// Override the draw in rect function
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{

    // Initialize and init the rotation stuff for the object
    // Identity Matrix
    glLoadIdentity();
    static GLfloat rot = 0.0;

    // Clear any remnants in the drawing buffers
    // and fill the background with black
    glClearColor(0.0f,0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Render the Sun - 2D Sun Image in CGRect
    //[self drawSunInRect:CGRectMake(100, 100, 50, 50) withDepth:-30.0]; // 2D Sun Image

    // Render the BattleCruiser - 3D Battlecruiser
    [self renderTheBattleCruiser];

    // Calculate a time interval to use to rotate the cruiser lives
    static NSTimeInterval lastDrawTime;
    if (lastDrawTime)
    {
        NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
        rot += 75 * timeSinceLastDraw;                
    }
    lastDrawTime = [NSDate timeIntervalSinceReferenceDate];

}

UPDATE

I modified the draw sun method to have only 1 push and 1 pop for the GL_PROJECTION. I am still having the same issue. The sun image appears but I cannot see my 3D Objects. I have tried to re-arrange the calls to the render methods so the 3D objects are rendered first but I get the same results. I would appreciate other ideas on how to see my orthographic texture and 3D objects together.

// Draw Sun in Rect and with Depth
- (void)drawSunInRect:(CGRect)rect withDepth:(float)depth {

    // Get screen bounds
    CGRect frame = [[UIScreen mainScreen] bounds];

    // Calculate vertices from passed CGRect and depth
    GLfloat  vertices[] = 
    {
        rect.origin.x, rect.origin.y, depth,

        rect.origin.x + rect.size.width , rect.origin.y, depth,

        rect.origin.x, rect.size.height+rect.origin.y , depth,

        rect.origin.x + rect.size.width , rect.size.height+rect.origin.y ,depth
    };

    // Map the texture coords - no repeating
    static  GLfloat textureCoords[] = 
    {               
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0
    };

    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D); 

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();     
    glLoadIdentity();      

    glDisable(GL_LIGHTING);                                                                                          
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glDisableClientState(GL_COLOR_ARRAY);   

    glOrthof(0,frame.size.width,frame.size.height,0,0,1000);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glColor4f(1,1,1,1);

    glBindTexture(GL_TEXTURE_2D,sunInt);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT,0,textureCoords);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glMatrixMode(GL_MODELVIEW); 
    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    glEnable(GL_LIGHTING);  
    glDisable(GL_TEXTURE_2D);   
    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1

There are 1 best solutions below

0
On BEST ANSWER

I realized I was calling setClipping only once in my viewController viewDidLoad. I moved it to my glkView method and now I get my Sun image and my 3d object.