Opengl在3D游戏中绘制HUD

时间:2015-05-20 01:25:41

标签: c++ opengl

我正在创建一个3D迷宫游戏并尝试绘制HUD。我使用的是openGL,GLSL和C ++。

我绘制迷宫,切换到正交投影,然后绘制HUD(目前是测试三角形)。世界渲染工作正常,但HUD不能渲染任何东西。

我在概念上误解了某些内容,并且不确定在哪里查找我的错误。

我的HUD使用2D相机类:

class Camera2D
{
public:
    Camera2D();
    ~Camera2D();

    void init(int screenWidth, int screenHeight);
    void update();
    void draw();

    void setPosition(glm::vec2& newPosition){ _position = newPosition; _needsMatrixUpdate = true; };

    glm::mat4 getCameraMatrix() { return _cameraMatrix; };

private:
    GLuint _vbo;
    GLuint _vao;

    int _screenWidth, _screenHeight;
    bool _needsMatrixUpdate;

    glm::vec2 _position;
    glm::mat4 _cameraMatrix;
    glm::mat4 _orthoMatrix;
};

函数实现

void Camera2D::init(int screenWidth, int screenHeight)
{
    _screenWidth = screenWidth;
    _screenHeight = screenHeight;
    _orthoMatrix = glm::ortho(0.0f, (float)_screenWidth, 0.0f, (float)_screenHeight);
}

void Camera2D::update()
{
    if (_needsMatrixUpdate)
    {
        //Camera Translation
        glm::vec3 translate(-_position.x + _screenWidth / 2, -_position.y + _screenHeight / 2, 0.0f);
        _cameraMatrix = glm::translate(_orthoMatrix, translate);

        _needsMatrixUpdate = false;
    }
}

void Camera2D::draw()
{

    float _points[] = { 0, 0, 0, 0, 5, 0, 5, 0, 0 };
    float _colors[] = { 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1 };

    if (_vbo == 0)
    {
        glGenBuffers(1, &_vbo);
    }
    glBindBuffer(GL_ARRAY_BUFFER, _vbo);

    glBufferData(GL_ARRAY_BUFFER, sizeof(_points) + sizeof(_colors), nullptr, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_points), _points);
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(_points), sizeof(_colors), _colors);

    if (_vao == 0)
    {
        glGenVertexArrays(1, &_vao);
    }
    glBindVertexArray(_vao);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(_points)));

    glDrawArrays(GL_TRIANGLES, 0, 9);
}

在MainGame.cpp中,我初始化HUD并设置它的位置。

void MainGame::initSystems()
{
    GameEngine3D::init();
    _window.create("Maze Runner", _screenWidth, _screenHeight);

    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    glCullFace(GL_BACK);

    initShaders();

    //Trap mouse within window
    //If SDL_SetRelativeMouseMode fails exit game
    if (SDL_SetRelativeMouseMode(SDL_TRUE))
    {
        _gameState = GameState::EXIT;
    }

    _hud.init(_screenWidth, _screenHeight);
    _hud.setPosition(glm::vec2(_screenWidth / 2, _screenHeight / 2));

    //Generate Maze
    mazeAlgor.generateMazeWeights();
    mazeAlgor.generateMaze();
    mazeAlgor.printMaze();
}

在游戏循环中更新

void MainGame::gameLoop()
{
    while (_gameState != GameState::EXIT)
    {
        processInput();

        //update the camera model-view-projection matrix
        _camera.Update();
        _hud.update();

        draw();
    }
}

最后画出来"

void MainGame::draw()
{
    glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    _shaderProgram.use();

    //locate the location of "MVP" in the shader
    GLint mvpLocation = _shaderProgram.getUniformLocation("MVP");

    //pass the camera matrix to the shader
    glm::mat4 cameraMatrix = _camera.getMVPMatrix();
    glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &(cameraMatrix[0][0]));

    mazeAlgor.drawMaze();

    glm::mat4 projMatrix = _hud.getCameraMatrix();
    glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &(projMatrix[0][0]));

    _hud.draw();

    _shaderProgram.unuse();

    _window.swapBuffer();
}

我的顶点着色器是这样的:

in vec4 vertexPosition;
in vec4 vertexColor;

out vec4 fragmentColor;

uniform mat4 MVP;

void main()
{
    gl_Position = MVP * vertexPosition;
    fragmentColor = vertexColor;
}

我感觉我在update()函数中错误地进行了翻译。

1 个答案:

答案 0 :(得分:0)

您的代码中有两个可能导致问题的错误:

  • glm::mat4 projMatrix = _hud.getCameraMatrix();
    getCameraMatrix()不会返回您的投影矩阵

  • glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &(projMatrix[0][0]));
    您正在分配您的"投影矩阵"到您的模型视图投影矩阵(MVP)

固定代码可能如下所示:

glm::mat4 modelviewMatrix = _hud.getCameraMatrix(); // _hud._cameraMatrix
glm::mat4 projMatrix = _hud.getProjectionMatrix(); // _hud._orthoMatrix
glm::mat4 mvp = projMatrix * modelviewMatrix;
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, glm::value_ptr(mvp));

我还建议您阅读thisthis