Creating a 2D text box

I’m currently working on an assignment for university where we have to simulate a rocket on the moon, but that’s not really an issue for me. What’s wrong is that we have to display a box in the top-right corner displaying data about the rocket such as height, fuel, velocity etc. I’ve been told to use a 2D Ortho view to do this. So far I’ve been playing with this for a few days and end up going around in circles and getting nowhere. So if anyone could possibly point me into the right direction for creating this box I would be most appreciative

Thanks

Here goes:

glViewport ( 0, 0, m_Width, m_Height );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ();
glOrtho ( 0.0f, m_Width, 0.0, m_Height, 1.0, -1.0 );
glMatrixMode ( GL_MODELVIEW );	
glLoadIdentity ();

Now, the origin should be at the bottom left and the extents of your space will be 0…m_Width - 1 and 0…m_Height - 1.

So, your text could be displayed at, say, 0, 128 and should be visible (you must also render text using OpenGL of course!).

Thanks for that but I still can’t get it working, now I just get a black screen. I’ll post what code I have as it might help show where I’m going wrong.

static void drawInfo()
{
glViewport ( 0, 0, 600.0, 600.0 );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ();
glOrtho ( 0.0f, 600.0, 0.0, 600.0, 1.0, -1.0 );
BitmapString(AppFont, 0, 100, “Blah”);
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ();
}

//Main draw function.
static void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Move and zoom to position to draw map based on mouse movement.
gluLookAt(1.0, 1.0, ZoomZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(0.02, 0.02, 0.02);
//Rotate camera based on mouse movement.
if (SpinX != 0.0)
glRotatef(SpinX, -1.0, 0.0, 0.0);
if (SpinY != 0.0)
glRotatef(SpinY, 0.0, 1.0, 0.0);
drawInfo();
//Draw lighting.
drawLight();
//Move to position and draw moon surface.
glTranslatef(0.0, -7.5, 0.0);
drawMoon();
//Move to ship’s current position to draw it.
glTranslatef(PosX, PosY, PosZ);
if (PosY > -1.0)
{
//PosY -= GravityAcceleration;
drawShip(); //Draw ship.
}
else
{
drawExplosion();
}
CheckGL();
glutSwapBuffers();
}

Your code go something like this…

setup 3D projection
setup camera position (gluLookat)
setup lighting
draw 3D stuff… moon rocket etc

setup 2D projection
disable lighting (maybe?)
draw 2D stuff

swap buffers

You’re drawing stuff out of order. You set up the camera with gluLookat and then move it with the rotations (you can do this with gluLookat only). Then you blow all that away with the drawinfo function which calls a glLoadIdentity resetting the camera to the origin.

Your code go something like this…

setup 3D projection
setup camera position (gluLookat)
setup lighting
draw 3D stuff… moon rocket etc

setup 2D projection
disable lighting (maybe?)
draw 2D stuff

swap buffers

You’re drawing stuff out of order. You set up the camera with gluLookat and then move it with the rotations (you can do this with gluLookat only). Then you blow all that away with the drawinfo function which calls a glLoadIdentity resetting the camera to the origin.

Really I appreciate your choice of an OpenGL API for this use case. As my hope your problem has been fixed in one of the use cases of
Computer Graphics Projects posted in a YouTube channel named “Azzonika Tutorials”!