有一个Quad跟随鼠标指针(没有Glut)

C++ OpenGL Have a Quad follow the mouse pointer (without Glut)

本文关键字:没有 Glut 鼠标指针 Quad 跟随 有一个      更新时间:2023-10-16

我试图有一个四轴跟随鼠标指针。基本上我需要将屏幕坐标转换为世界坐标。我看过很多关于这个的帖子,还有NeHe和其他地方的教程,但没有一个适合我。几乎每次,我都会遇到某种缩放问题,即我将鼠标从窗口中心移动得越远,鼠标的四轴偏移量就越大。

我当前正在尝试的代码来自http://www.3dbuzz.com/forum/threads/191296-OpenGL-gluUnProject-ScreenCoords-to-WorldCoords-problem但它仍然存在缩放问题,即使链接中的人说代码对他有效。

我怎样才能让四轴飞行器跟随鼠标?

下面是代码(来自渲染框架函数):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
POINT mouse;
GetCursorPos(&mouse);
ScreenToClient(WindowHandle, &mouse);
GLdouble
    posX1,
    posY1,
    posZ1,
    posX2,
    posY2,
    posZ2,
    modelview[16],
    projection[16];
GLint
    viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
gluUnProject( mouse.x, viewport[1] + viewport[3] - mouse.y, 0, modelview, projection, viewport, &posX1, &posY1, &posZ1);
gluUnProject( mouse.x, viewport[1] + viewport[3] - mouse.y, 1, modelview, projection, viewport, &posX2, &posY2, &posZ2);
GLfloat t = (posZ1 - 0) / (posZ1 - posZ2);
GLfloat
    fX = posX1 + (posX2 - posX1) * t,
    fY = posY1 + (posY2 - posY1) * t;
OutputDebugFloat(fX);
OutputDebugString(", ");
OutputDebugFloat(fY);
OutputDebugString("n");
glTranslatef(fX, fY, 0);
DrawTile(test.Tex()); // Draws the quad with the specified texture
SwapBuffers(hDC);

好吧,我似乎已经弄明白了。问题是我没有背景资料。(我删除了它,所以我可以有一个空白的屏幕,而测试这一点。坏主意。)我尝试的大多数教程实际上都是有效的,只是因为它被破坏了。

编辑:我发现'glReadPixels'降低FPS相当多。我通过每帧/60帧只更新四边形位置来修复这个问题。