我目前正在使用OpenGL开发一个小型的2D平台游戏项目,尝试从头开始做大部分事情作为学习练习。glfwSwapInterval()导致闪烁/破碎的图形
我现在遇到的问题似乎与使用glfwSwapInterval(n);有关。当我将交换间隔设置为0(即,没有vsync)时,我正在渲染的GameObject呈现得非常好,并且顺利移动。将其设置为1会导致该对象不被渲染,或闪烁并且角落四处跳动。将其设置为更高的值会使问题恶化。
我认为相关的代码是相关的如下。
game.cpp(主游戏类):
void Game::runGame()
{
// Some init things here but not important
while (!glfwWindowShouldClose(_disp.getWindow()))
{
// delta is set up here
glfwPollEvents();
for (auto& obj : _gobs) { obj->update(delta) };
_disp.checkInput(_cont.getKeys()); // Only checks for Esc press to close window
_disp.render(_gobs);
}
_disp.close();
}
display.cpp(句柄窗口创建):
bool Display::initGL()
{
// Majority of window creation code is above here, but generic and probably irrelevant
glViewport(0, 0, _scrWidth, _scrHeight);
glfwSwapInterval(0); // Seems to cause flickering with nonzero values
glfwSetInputMode(_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
return true;
}
void Display::render(std::vector<std::shared_ptr<GameObject>> gobs)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto& obj : gobs) { obj->render(); }
glfwSwapBuffers(_window);
}
gameobject.cpp(对象类别):
void GameObject::render()
{
if (_vaoid)
{
_shader.use();
glBindVertexArray(_vaoid);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
}
有没有人有什么想法可能导致这一点?这感觉就像我一定错过了窗口提示或类似的东西,但我找不到任何提及的文档。
编辑:着色器代码导致了问题: basicvert.vs:
#version 330 core
layout (location = 0) in vec2 position;
void main()
{
gl_Position.xy = position;
gl_Position.z = 0.0f;
}