有一个Quad跟随鼠标指针(没有Glut)
C++ OpenGL Have a Quad follow the mouse pointer (without Glut)
我试图有一个四轴跟随鼠标指针。基本上我需要将屏幕坐标转换为世界坐标。我看过很多关于这个的帖子,还有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帧只更新四边形位置来修复这个问题。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 如果没有malloc,链表实现将失败
- 数组索引的值没有增加
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 变量没有改变?通过向量的函数调用
- 没有名称的C++模板参数
- 在线编译器中的分段C++没有打印消息
- 没有为自己的结构调用列表推回方法
- OPENgl - GL/glut.h 没有这样的文件或目录
- 控制台窗口有图标,但 OpenGL/GLUT 窗口没有。为什么?
- 在Visual Studio中使用GLUT库,没有错误,但输出控制台不起作用
- 如何在一个没有主要功能的项目中使用OpenGL(例如MFC / Qt),而不使用GLUT
- 在没有SDL或GLUT的情况下检测按键的最佳方法
- 有一个Quad跟随鼠标指针(没有Glut)
- 在OSX上浮动没有标题栏的GLUT窗口
- OpenGL+GLUT没有填充最左边的多边形