HUD根本不在屏幕上显示

问题描述 投票:0回答:1

我正在尝试在游戏中显示带纹理的HUD,但未显示,因此我试图将多维数据集显示为HUD,并且也未显示。我尝试了几件事,但仍然没有显示。

在主要功能中,我启用了2D并将其禁用。

void main(int argc, char** argv) {

// init GLUT and create window

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("Space 3D Game");


    // register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutIdleFunc(renderScene);
     // OpenGL Initialization
    loadTexture(argv[1]);
    init(argv[1]);
    setOrthographicProjection(windowWidth, windowHeight);//Enable 2D and draw objects in 2D
    resetPerspectiveProjection();//Disable 2D
    glutSpecialFunc(processSpecialKeys);
    glutTimerFunc(25, TimerFunc, 1);


    //// OpenGL init
    //glEnable(GL_DEPTH_TEST);

    // enter GLUT event processing cycle
    glutMainLoop();

}
//windowHeight = 500
//windowWidth = 800

void setOrthographicProjection(float windowWidth, float windowHeight) {
    // switch to projection mode
    glMatrixMode(GL_PROJECTION);
    // save the previous matrix which contains the
    //settings for the perspective projection
    glPushMatrix();
    // reset matrix
    glLoadIdentity();
    // set a 2D orthographic projection
    gluOrtho2D(0, windowWidth, 0, windowHeight);
    // invert the y axis, down is positive
    glScalef(1, -1, 1);
    // mover the origin from the bottom left corner
    // to the upper left corner
    glTranslatef(0, -windowWidth, 0);
    glMatrixMode(GL_MODELVIEW);
    //Draw HUD
    drawHud(windowWidth, windowHeight);
}

void drawHud(float windowWidth, float windowHeight)
{

    if (hudEnabled)
    {
                glDisable(GL_DEPTH_TEST);
        glBegin(GL_QUADS);
        glColor3f(255.0f, 0.0f, 0.0);
        glVertex2f(0.0, 0.0);
        glVertex2f(100.0, 0.0);
        glVertex2f(100.0, 100.0);
        glVertex2f(0.0, 100.0);
        glEnd();
        glEnable(GL_DEPTH_TEST);

    }
}

void resetPerspectiveProjection()
{

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);

}

有人可以告诉我此代码中的错误吗?

c++ visual-studio opengl 3d opengl-3
1个回答
0
投票

此问题是由以下代码行引起的:

gluOrtho2D(0, windowWidth, 0, windowHeight);
glScalef(1, -1, 1);
glTranslatef(0, -windowWidth, 0);

必须是

gluOrtho2D(0, windowWidth, 0, windowHeight);
glTranslatef(0, windowHeight, 0);
glScalef(1, -1, 1);

同样可以通过:

gluOrtho2D(0, windowWidth, windowHeight, 0);

说明:

[glTranslatef分别为glTranslatef,指定一个矩阵并将当前矩阵乘以新矩阵。因此,必须在glScale之前调用glScale

如果您想

[...]移动器从左下角到左上角的原点[...]

然后您必须翻译(0,glTranslatef,0)而不是(0,glScale,0)。

windowHeightleftrightbottomtop剪切平面。在您的情况下,bottom必须为-windowWidth,而top必须为0。

© www.soinside.com 2019 - 2024. All rights reserved.