PDA

View Full Version : 2D HUD



James Lovejoy
05-26-2011, 11:20 AM
Hi,

I have my game set up drawing things in 3D but I would like to add a 2D HUD over the top. Here is my code that is not working:

gameloop


void gameLoop()
{

keychecker();

//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective


// Make camera follow keyboard and mouse
updateCamera();

RenderScene();
DrawHud();

glfwSwapBuffers();
}


updatecamera


void updateCamera()
{
glRotatef(xrot,1.0f,0.0f,0.0f); //rotate our camera on the x-axis (left and right)
glRotatef(yrot,0.0f,1.0f,0.0f); //rotate our camera on the y-axis (up and down)




glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera


}


renderscene


void RenderScene()
{
glPushMatrix();
//draw world objects
DrawCube(0.0f, 0.0f, 0.0f);
DrawCube(5.0f, 0.0f, 0.0f);
DrawCube(10.0f, 0.0f, 0.0f);
DrawCube(15.0f, 0.0f, 0.0f);
DrawCube(20.0f, 0.0f, 0.0f);
DrawCube(25.0f, 0.0f, 0.0f);
DrawCube(30.0f, 0.0f, 0.0f);
DrawCube(35.0f, 0.0f, 0.0f);
DrawCube(40.0f, 0.0f, 0.0f);
DrawCube(45.0f, 0.0f, 0.0f);
DrawCube(50.0f, 0.0f, 0.0f);
glPopMatrix();
}


drawcube


void DrawCube(float xPos, float yPos, float zPos)
{
glColor3f(0.7f, 0.0f, 0.8f);
glPushMatrix();
glBegin(GL_POLYGON);

/* This is the top face*/
glVertex3f(xPos + 0.0f,yPos + 0.0f,zPos + 0.0f);
glVertex3f(xPos + 0.0f,yPos + 0.0f,zPos + -5.0f);
glVertex3f(xPos + -5.0f,yPos + 0.0f,zPos + -5.0f);
glVertex3f(xPos + -5.0f,yPos + 0.0f,zPos + 0.0f);

/* This is the front face*/
glVertex3f(xPos + 0.0f,yPos + 0.0f,zPos + 0.0f);
glVertex3f(xPos + -5.0f,yPos + 0.0f,zPos + 0.0f);
glVertex3f(xPos + -5.0f,yPos + -5.0f,zPos + 0.0f);
glVertex3f(xPos + 0.0f,yPos + -5.0f,zPos + 0.0f);

/* This is the right face*/
glVertex3f(xPos + 0.0f,yPos + 0.0f,zPos + 0.0f);
glVertex3f(xPos + 0.0f,yPos + -5.0f,zPos + 0.0f);
glVertex3f(xPos + 0.0f,yPos + -5.0f,zPos + -5.0f);
glVertex3f(xPos + 0.0f,yPos + 0.0f,zPos + -5.0f);

/* This is the left face*/
glVertex3f(xPos + -5.0f,yPos + 0.0f,zPos + 0.0f);
glVertex3f(xPos + -5.0f,yPos + 0.0f,zPos + -5.0f);
glVertex3f(xPos + -5.0f,yPos + -5.0f,zPos + -5.0f);
glVertex3f(xPos + -5.0f,yPos + -5.0f,zPos + 0.0f);

/* This is the bottom face*/
glVertex3f(xPos + 0.0f,yPos + 0.0f,zPos + 0.0f);
glVertex3f(xPos + 0.0f,yPos + -5.0f,zPos + -5.0f);
glVertex3f(xPos + -5.0f,yPos + -5.0f,zPos + -5.0f);
glVertex3f(xPos + -5.0f,yPos + -5.0f,zPos + 0.0f);

/* This is the back face*/
glVertex3f(xPos + 0.0f,yPos + 0.0f,zPos + 0.0f);
glVertex3f(xPos + -5.0f,yPos + 0.0f,zPos + -5.0f);
glVertex3f(xPos + -5.0f,yPos + -5.0f,zPos + -5.0f);
glVertex3f(xPos + 0.0f,yPos + -5.0f,zPos + -5.0f);

glEnd();
glPopMatrix();

}


drawhud


void DrawHud()
{


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glTranslatef(0,0,0); //dont actually have to do since it is default 0
//also have rotation at 0
glOrtho(0.0f, width, height, 0.0f, -2.0f, 2.0f);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glPushMatrix();
glPushMatrix();
glBegin(GL_QUADS);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glEnd();
glPopMatrix();
glPopMatrix();
}


What is wrong? Thanks,

James

Alfonse Reinheart
05-26-2011, 11:53 AM
Here is my code that is not working:

In what way is it "not working"? That is, what are you expecting to see? What are you actually seeing? And how are the two different?

James Lovejoy
05-26-2011, 12:08 PM
Commenting out DrawHud(); in game loop means that a line of 10 cubes appear on the screen and I can move the camera. With DrawHud(); there the 10 cubes disappear and I am left with a small column in the top left hand corner.

drawhud left in:
http://stashbox.org/1120333/drawhud.GIF

drawhud taken out:
http://stashbox.org/1120338/nodrawhud.GIF

I'm expecting to see the same as what is in the second image with an extra square drawn on the screen in 2D.

James

Alfonse Reinheart
05-26-2011, 12:15 PM
You forgot to reset your GL_PROJECTION matrix before drawing the regular scene. So after the first frame of rendering, the projection matrix will be using glOrtho.

James Lovejoy
05-26-2011, 01:50 PM
Thanks,

I changed my gameloop so that it looks like this:


void gameLoop()
{

keychecker();

//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective


// Make camera follow keyboard and mouse
updateCamera();

RenderScene();
DrawHud();

glfwSwapBuffers();
}


But i still do not yet the desired effect i can no longer move around to see my row of cubes i just get the mass on the screen. I can "look" but using the WASD keys just throws the entire thing out of view. A I doing it right?

Screenshot:
http://stashbox.org/1120401/glprojection.GIF

Thanks,

James

Alfonse Reinheart
05-26-2011, 02:20 PM
Somewhere in the initialization of your code, you set the current matrix to GL_PROJECTION and then did some matrix operations. There's a good change that glFrustum or gluPerspective were among them. You need to put that code into your render loop.