0
我想要一个四跟随鼠标指针。所以基本上我需要将屏幕坐标转换为世界坐标。我已经看到很多帖子和NeHe和其他地方的教程,但没有一个适合我。几乎每一次,我都会遇到一些缩放问题,在距离窗口中心越远的地方,我的鼠标越偏离四方。C++ OpenGL有一个四个跟随鼠标指针(无Glut)
目前,我想要的代码是从 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);