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 7 of 7

Thread: Combining 2d and 3d?

  1. #1
    Intern Contributor
    Join Date
    Jan 2001
    Location
    Irving TX USA
    Posts
    60

    Combining 2d and 3d?

    Greetings all,

    Are you supposed to be able to combine 2d and 3d with OpenGL? For example, the border around the screen in Quake, or the little translator messages that pop up in Unreal?

    I can't get glOrtho and glPerspective to work together - whatever I draw in one gets erased when I switch to the other. If glOrtho isn't the answer, how can you do this?

    Thanks in advance.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,070

    Re: Combining 2d and 3d?

    It works if done correctly, without seeing your code can not point out your error.

    Code should look something like this:

    void display(void)
    {
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen
    // Set perspective view
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, 1, 1, 30);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    Draw_3d();

    //Set ortho view
    glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
    glLoadIdentity(); // Clear the matrix
    glOrtho(-9.0, 9.0, -9.0, 9.0, 0.0, 30.0); // Setup an Ortho view
    glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
    glLoadIdentity(); // Clear the model matrix

    Draw_2d();

    }

    Originally posted by DalTXColtsFan:
    Greetings all,

    Are you supposed to be able to combine 2d and 3d with OpenGL? For example, the border around the screen in Quake, or the little translator messages that pop up in Unreal?

    I can't get glOrtho and glPerspective to work together - whatever I draw in one gets erased when I switch to the other. If glOrtho isn't the answer, how can you do this?

    Thanks in advance.

  3. #3
    Intern Contributor
    Join Date
    Jan 2001
    Location
    Irving TX USA
    Posts
    60

    Re: Combining 2d and 3d?

    Below is just the render routine, the app is at http://home.att.net/~tennisman5/TryingToCombine.zip.

    I can get one or the other, I can switch the order of the 2d and the 3d and get whichever I render 2nd, but I can't get them both on the screen at the same time.

    Any ideas? They will be appreciated.


    void Render()
    {
    // clear screen and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glViewport(0, 0, width, height); // reset the viewport to new dimensions

    glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
    glLoadIdentity(); // reset projection matrix
    gluPerspective(54.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

    glMatrixMode(GL_MODELVIEW); // set modelview matrix
    glLoadIdentity(); // reset modelview matrix

    float angle = rotangle + 90.0f;
    if (angle < 0.0f) angle = angle + 360.0f;
    if (angle >= 360.0f) angle = angle - 360.0f;
    glRotatef(angle, 0.0f, 1.0f, 0.0f);

    //Now the translation. Remember, the camera stays put
    //and the world's axes move. So to simulate moving the
    //camera 8 to the right and 8 down, we would move the
    //world's axes 8 to the left and 8 up.
    float translatex = -personx;
    float translatez = -personz;
    glTranslatef(translatex, -2.0f, translatez); // perform transformations

    // draw our smooth shaded triangle
    glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f); // red vertex
    glVertex3f(-1.0f, -1.0f, -.5f);
    glColor3f(0.0f, 1.0f, 0.0f); // green vertex
    glVertex3f(2.0f, -1.0f, -.5f);
    glColor3f(0.0f, 0.0f, 1.0f); // blue vertex
    glVertex3f(-1.0f, 2.0f, -.5f);
    glEnd();

    myRobot.DrawRobot();

    glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
    glLoadIdentity(); // reset projection matrix

    glOrtho(0.0f, width - 1.0, 0.0, height - 1.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW); // set modelview matrix
    glLoadIdentity(); // reset modelview matrix

    glRasterPos2i(200,200);
    glDrawPixels(bitmapData->sizeX, bitmapData->sizeY, GL_RGB, GL_UNSIGNED_BYTE, bitmapData->data);

    glFlush();
    SwapBuffers(g_HDC); // bring backbuffer to foreground
    }

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Jan 2003
    Location
    Virginia
    Posts
    586

    Re: Combining 2d and 3d?

    Try extending the depth (far) of your glOrtho call so you can use glRaster3i and set a depth beyond the depth of your other drawings. I just tested it. Works.

    I've never used glDrawPixels so I'm not sure why, but I had to set the raster pos back nearly as far as the far clip plane. Looking at your code, your objects are only at -50 so I don't understand it. Perhaps someone else does... or I didn't look far enough into your code.

    Hope it helps.

    Code :
    	glOrtho(0.0f, width - 1.0, 0.0, height - 1.0, -1.0, 1000.0);
     
    	glMatrixMode(GL_MODELVIEW);			// set modelview matrix
    	glLoadIdentity();					// reset modelview matrix		
     
    	glRasterPos3i(200,200, -999);
    	glDrawPixels(bitmapData->sizeX, bitmapData->sizeY, GL_RGB, GL_UNSIGNED_BYTE, bitmapData->data);
     
    	glFlush();
    	SwapBuffers(g_HDC);			// bring backbuffer to foreground

  5. #5
    Intern Contributor
    Join Date
    Jan 2001
    Location
    Irving TX USA
    Posts
    60

    Re: Combining 2d and 3d?

    Thanks shin, I'll try that when I have a chance. (gotta go back to my DAY JOB right now, woohoo)

    So is the generally accepted method for combining 2d and 3d, for doing things like putting a border around the screen like wolf 3d with the player's health and other data, or displaying messageboxes on the screen like Unreal? If not, what are some other methods that are used in this case?

    Thanks

  6. #6
    Intern Newbie
    Join Date
    Jun 2002
    Posts
    33

    Re: Combining 2d and 3d?

    Hi,

    don't know if it is useful but there is a demo of using 2D and 3D on HeNe site. He has a 3D rotating box with words around the outside.
    It maybe the depth buffer is full up when you swap perspective. Why not try turning depth checking off or clearing the depth buffer before doing your second drawing bit,

    fringe

    [This message has been edited by fringe (edited 03-18-2003).]

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,070

    Re: Combining 2d and 3d?

    I notice you are using drawpixels, you will get some speed increase if you used a texture quad to map you 2D graphics.

    Also by using a texture quad you can map transparent areas in your 2D image using the alpha channel, this make you image work more like a overlay image.

    On other advantage to a texture quad is on screen re-size, you just stretch the quad to fill the screen vs. on a pixel operation you will have to have extra code to take care of screen size change.

Posting Permissions

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