3D场景中的2D Hud(OpenGL,SDL,C ++)

时间:2015-02-07 01:08:54

标签: c++ opengl 2d sdl hud

我的3D世界每次都画得很完美,但2D文字从不画画。下面的代码使用来自lighthouse3D的教程来展示我最近的努力。我感觉它的东西很愚蠢,我只是没有看到它。

渲染代码:

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


}

使用以下方法:

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;
}
}

0 个答案:

没有答案