0

My 3D world draws perfectly every time but the 2D text never draws. The code below features my latest effort using a tutorial from lighthouse3D. I get the feeling its something stupidly simple and im just not seeing it.

Rendering code :

void ScreenGame::draw(SDL_Window * window) 
{

glClearColor(0.5f,0.5f,0.5f,1.0f);

glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);

// Set up projection matrix
glm::mat4 projection(1.0);
projection = glm::perspective(60.0f,800.0f/600.0f,1.0f,150.0f);
rt3d::setUniformMatrix4fv(shaderProgram, "projection", glm::value_ptr(projection));

GLfloat scale(1.0f); // just to allow easy scaling of complete scene

glm::mat4 modelview(1.0); // set base position for scene
mvStack.push(modelview);

mvStack.top() = glm::lookAt(camera->getEye(),camera->getAt(),camera->getUp());

glm::vec4 tmp = mvStack.top()*lightPos;
light0.position[0] = tmp.x;
light0.position[1] = tmp.y;
light0.position[2] = tmp.z;

rt3d::setLightPos(shaderProgram, glm::value_ptr(tmp));

glUseProgram(skyBoxShader); // Switch shaders, reset uniforms for skybox
rt3d::setUniformMatrix4fv(skyBoxShader, "projection", glm::value_ptr(projection));
glDepthMask(GL_FALSE); // make sure depth test is off
glm::mat3 mvRotOnlyMat3 = glm::mat3(mvStack.top());
mvStack.push( glm::mat4(mvRotOnlyMat3) );

skyBox->draw(mvStack); // drawing skybox

mvStack.pop();

glDepthMask(GL_TRUE); // make sure depth test is on
mvStack.top() = glm::lookAt(camera->getEye(),camera->getAt(),camera->getUp());

glUseProgram(shaderProgram); // Switch back to normal shader program
rt3d::setUniformMatrix4fv(shaderProgram, "projection", glm::value_ptr(projection));
rt3d::setLightPos(shaderProgram, glm::value_ptr(tmp));
rt3d::setLight(shaderProgram, light0);
// Draw all visible objects...
Ball->draw(mvStack);
ground->draw(mvStack);

building1->draw(mvStack);
building2->draw(mvStack);

setOrthographicProjection();

glPushMatrix();
glLoadIdentity();
renderBitmapString(5,30,1,GLUT_BITMAP_HELVETICA_18,"Text Test");
glPopMatrix();

restorePerspectiveProjection();

SDL_GL_SwapWindow(window); // swap buffers


}

using the following methods :

void setOrthographicProjection() {

// switch to projection mode
glMatrixMode(GL_PROJECTION);
// save previous matrix which contains the
//settings for the perspective projection
glPushMatrix();

// reset matrix
glLoadIdentity();

// set a 2D orthographic projection
glOrtho(0.0F, 800, 600, 0.0F, -1.0F, 1.0F);

// switch back to modelview mode
glMatrixMode(GL_MODELVIEW);
}

void restorePerspectiveProjection() {

glMatrixMode(GL_PROJECTION);
// restore previous projection matrix
glPopMatrix();

// get back to modelview mode
glMatrixMode(GL_MODELVIEW);
}

void renderBitmapString(

        float x,
        float y,
        int spacing,
        void *font,
        char *string) {

  char *c;
  int x1=x;

  for (c=string; *c != '\0'; c++) {

glRasterPos2f(x1,y);
glutBitmapCharacter(font, *c);
x1 = x1 + glutBitmapWidth(font,*c) + spacing;
}
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Have you checked the final value of the x1 variable to see if it actually incremented as much as you would expect? – Jere Feb 07 '15 at 01:49
  • 3
    Looks like a wild mix of different feature levels. Partly you use glm to build matrices, and your own matrix stack. The you use the legacy matrix stack for rendering the font. While you still have your shader bound. Something probably goes very wrong in this back and forth. – Reto Koradi Feb 07 '15 at 04:25

0 Answers0