OpenGL在3D游戏上绘制HUD
Opengl Draw HUD over 3D game
我正在创建一个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));
我也建议您阅读此内容。
相关文章:
- QuadTree只在窗口的右上角绘制
- 绘制旋转的三角形
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 如何在QT中的自定义视频小工具t上绘制矩形
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 无法使用VAO和EBO(openGL)绘制多个对象
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 绘制顺时针三角形,重新排序点
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- SFML 文本未绘制在窗口上
- 如何在快板的屏幕中显示子位图的绘制?
- 如何绘制自定义形状的元素?
- QPainter 将 QBrush 设置为 在 QT/C++ 中绘制文本
- 除非重新绘制大小,否则SDL_Texture在调整大小后呈现黑色
- 绘制文本问题
- 在 QT C++中绘制/操作多边形
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 在现代OpenGL中,绘制GL_TRIANGLE奇怪的z轴行为的填充圆
- OpenGL在3D游戏上绘制HUD
- HUD被绘制出来,但是OpenGL 3D场景消失了