这是一个简单的程序,它按照鼠标光标的位置绘制一个三角形。
我(希望您)会注意到,三角形落后于光标,它不像拖动整个窗口时那样紧。
所以我的问题是:我在做什么错?是什么导致这种滞后?
我意识到的一件事是,足以移动三角形的实际像素值,而不必一次又一次地对其进行栅格化。但是光栅化这个三角形真的那么贵吗?我还尝试使用glTranslate而不是在变化的坐标处进行绘制,但是滞后没有得到改善。因此,我希望您能启发我如何有效地进行绘制。
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
float x = 0.0f;
float y = 0.0f;
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
static void cursor_callback(GLFWwindow *window, double xpos, double ypos)
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
float ratio = width / (float) height;
x = ratio*(2*xpos/(float)width - 1);
y = 2*-ypos/(float)height + 1;
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Following Triangle", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
// Callbacks
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, cursor_callback);
// geometry for the equal sided triangle
float r = 0.1f; // outer circle radius
float u = r * sin(M_PI_2/3.0f);
float l = 2.0f * r * cos(M_PI_2/3.0f);
while (!glfwWindowShouldClose(window))
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
float ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.0f, 1.0f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(x+0, y+r, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(x-l/2.0f, y-u, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(x+l/2.0f, y-u, 0.f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
所以是的,将渲染与帧速率同步的常用方法是使用,SwapInterval(1)而您没有这样做,但这不是滞后的来源。事实上,通常SwapInterval设置为 0 时,滞后比设置为 1 时要少,所以我怀疑它实际上一直设置为 1。所以,我正在写这个假设的其余部分SwapInterval被设置为 1。
您看到这种滞后有两个独立的原因。
忽略一些细节,在 OpenGL 中渲染的幼稚方法是这样的循环:
while (!exitCondition()) {
pollEvents();
render();
swap();
}
Run Code Online (Sandbox Code Playgroud)
这个循环每帧运行一次。如果render()速度很快,大部分时间都花在swap(). swap()然而,直到它返回的那一刻才真正发送新帧。新的鼠标和键盘事件可能会在整个时间内发生,但它们直到下一帧才会生效。这意味着鼠标和键盘信息在到达屏幕时已经是一到两帧旧了。为了获得更好的延迟,您不应该在swap()返回后立即轮询事件和渲染。等待尽可能多的新事件,渲染,然后及时发送帧以使其显示。应该花在等待上的时间越少越好swap()。这可以通过向循环添加延迟来实现。认为tFrame是帧之间的时间量(对于 60Hz 屏幕为 1s/60 .= 16.67ms)并且tRender该时间量通常大于render()运行所需的时间量。具有延迟延迟的循环如下所示:
while(!exitCondition()) {
sleep(tFrame - tRender);
pollEvents();
render();
swap();
}
Run Code Online (Sandbox Code Playgroud)
事实证明,耐心也是计算中的一种美德。
新手希望glfwSwapBuffers()等到 vsync,然后将新渲染的帧发送到屏幕并返回,类似于swap()我在原因 0 中使用的函数。它实际上所做的,实际上是将先前渲染的帧发送到屏幕然后返回,给你留下一整帧的延迟。为了解决这个问题,您必须获得一种单独的同步渲染方法,因为 OpenGL 的机制不够好。这种机制是特定于平台的。Wayland 有这样一个机制,它被称为presentation-time。GLFW 目前不支持这个,但我被这个同步问题困扰,所以我添加了它。结果如下:

如您所见,确实可以将渲染同步到系统光标。这真的很难。
您的更新纯粹是事件驱动的。尝试替换glfwPollEvents为glfwWaitEvents. 然后我会重新实现glfwSwapInterval(1)。通过比显示器刷新率更频繁的更新,您不会获得任何好处 - 只是在周期中撕裂和烧毁。