Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Background image behind 3D scene

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2012
    Location
    Bergamo
    Posts
    3

    Background image behind 3D scene

    Hi all!!
    I'm trying to create a simple 3D game, and I want to put behind my scene a fixed background image.
    I read that I need to draw a textured quad in glOrtho for the background, and to draw my 3D objects in glPerspective.
    I tried to write my display function in this way:

    void display () {
    glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION );
    glLoadIdentity();
    glOrtho(0,1,0,1,-1,1);
    glPushMatrix();
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glDepthMask(GL_FALSE);
    glMatrixMode(GL_MODELVIEW);
    //draw 2D image
    glDepthMask(GL_TRUE);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT );
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat) W_WIDTH / (GLfloat) W_HEIGHT, 0.1, 1000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(
    CameraTemp.Pos.X,CameraTemp.Pos.Y, CameraTemp.Pos.Z,
    CameraTemp.Target.X,CameraTemp.Target.Y, CameraTemp.Target.Z,
    0.0, 1.0, 0.0);
    //draw 3D objects
    glutSwapBuffers();
    }
    ...but I see only the 2D background image. Anybody can say what am I wrong?

  2. #2
    Junior Member Regular Contributor tksuoran's Avatar
    Join Date
    Mar 2008
    Location
    Cambridge, UK
    Posts
    223

    Re: Background image behind 3D scene

    Do not glClear() after drawing the 2D image.

    Check the bitmask you send to glClear(). Use bitwise or instead of logical or.

    Check where your code actually draws the 3D object. I would suggest setting camera target to 0, 0, 0 and draw your object there first and then see how to make your object move properly.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2012
    Location
    Bergamo
    Posts
    3

    Re: Background image behind 3D scene

    Thanks for your reply.
    I changed in this way:
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION );
    glLoadIdentity();
    glOrtho(0,1,0,1,-1,1);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glDepthMask(GL_FALSE);
    glMatrixMode(GL_MODELVIEW);
    //draw 2D image
    glDepthMask(GL_TRUE);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat) W_WIDTH / (GLfloat) W_HEIGHT, 0.1, 1000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(
    CameraTemp.Pos.X,CameraTemp.Pos.Y, CameraTemp.Pos.Z,
    CameraTemp.Target.X,CameraTemp.Target.Y, CameraTemp.Target.Z,
    0.0, 1.0, 0.0);
    //draw 3D objects
    glutSwapBuffers();

    ...but now I see only the 3D scene, without the background...

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2012
    Location
    Bergamo
    Posts
    3

    Re: Background image behind 3D scene

    Ops, it was my fault!
    I forgot to insert the reset of model view matrix when I draw the 2D image.
    Now it's working! Thank you thank you thank you!!
    Here's the complete code, if somebody need it:

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION );
    glLoadIdentity();
    glOrtho(0,1,0,1,-1,1);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glDepthMask(GL_FALSE);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //draw 2D image
    glDepthMask(GL_TRUE);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat) W_WIDTH / (GLfloat) W_HEIGHT, 0.1, 1000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(
    CameraTemp.Pos.X,CameraTemp.Pos.Y, CameraTemp.Pos.Z,
    CameraTemp.Target.X,CameraTemp.Target.Y, CameraTemp.Target.Z,
    0.0, 1.0, 0.0);
    //draw 3D objects
    glutSwapBuffers();

  5. #5
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    1
    Thank you, Ivan! Your code helped me a lot!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •