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