OpenGL在3D游戏上绘制HUD

Opengl Draw HUD over 3D game

本文关键字:绘制 HUD 游戏 3D OpenGL      更新时间:2023-10-16

我正在创建一个3D迷宫游戏,并试图绘制HUD。我正在使用OpenGL,GLSL和C 。

IM绘制迷宫,切换到拼字图投影,然后绘制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 i初始化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()函数中错误翻译的感觉。

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

  • 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));

我也建议您阅读此内容。