0

I'm creating a 3D Maze Game and trying to draw a HUD. I'm using openGL, GLSL, and C++.

Im drawing the maze, switching to an orthographic projection, and then drawing the HUD (currently a test triangle). The world rendering works correctly, but the HUD isn't rendering anything.

I'm misunderstanding something conceptually and am not sure where to look for my error(s).

My HUD uses a 2D camera class:

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

where the function implementation is

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

In MainGame.cpp I initialize the HUD and set it's position.

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

Update it in the game loop

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

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

        draw();
    }
}

And finally draw it"

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

My vertex shader is this:

in vec4 vertexPosition;
in vec4 vertexColor;

out vec4 fragmentColor;

uniform mat4 MVP;

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

I have a feeling that I'm incorrectly translating in the update() function.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Luke John Southard
  • 117
  • 1
  • 3
  • 10

1 Answers1

0

There are two mistakes in your code that could cause the problem:

  • glm::mat4 projMatrix = _hud.getCameraMatrix();
    getCameraMatrix() does not return your projection matrix

  • glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &(projMatrix[0][0]));
    You're assigning your "projection matrix" to your model view projection matrix (MVP)

The fixed code could look like this:

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

I also recommend you read this and this.

Community
  • 1
  • 1
Axalo
  • 2,953
  • 4
  • 25
  • 39