鼠标移动opengl
我正在使用纯 OpenGL(无外部工具)创建一个用 C++ 编写的台球游戏,但我可以使用 GLUT。我画了一个台球杆,我想跟随鼠标光标,但我不知道如何做到这一点。
我知道如何使用键盘输入来移动物体,例如相机位置或绘制对象,但我 我不确定如何使用鼠标输入移动对象。
这是我试图通过鼠标输入移动的提示:
void cue () {
glBegin;
glTranslatef(-10,5,0);
glRotatef(90,0,1,0);
glutSolidCone(0.25, 15, 20, 20);
glEnd();
}
I am creating a pool game written in C++ using plain OpenGL (no external tools), but I can use GLUT. I have drawn a pool cue which I want to follow the mouse cursor but I am not sure how to do this.
I know how to use keyboard input to move things e.g camera position or draw an object but I
m not sure how to move an object using mouse input.
This is the cue i am trying to move via mouse input:
void cue () {
glBegin;
glTranslatef(-10,5,0);
glRotatef(90,0,1,0);
glutSolidCone(0.25, 15, 20, 20);
glEnd();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Glut有几个鼠标回调函数
鼠标回调
运动回调
您可以使用回调来弄清楚鼠标的移动,剩下的就是纯数学了。
Glut has a few mouse callback function
Mouse callback
Motion callback
You could use the callback to figure out the movement of the mouse, the rest is pure math.
NeHe 有一个教程,涵盖 OpenGL 中的旋转基础知识: http:// /nehe.gamedev.net/data/lessons/lesson.asp?lesson=04
如果您想围绕另一个点旋转,请参阅如何更改 OpenGL 中的旋转中心 。基本上可以归结为:
还可以使用 glPushMatrix/glPopMatrix 或 glLoadIdentity 来隔离转换。
NeHe has a tutorial that covers the basics of rotation in OpenGL: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=04
If you want to rotate around another point see How to change the Center-of-Rotation in OpenGL . It basically comes down to this:
Also use glPushMatrix/glPopMatrix or glLoadIdentity to isolate transformations.
澄清一下:OpenGL 既不是建模库,也不是场景图。 OpenGL 所做的就是将点、线和三角形扔到图像缓冲区中。
所以纯粹用 OpenGL 来实现是不可能的。您将需要某种建模代码来创建由三角形组成的模型。帮自己一个忙:
任何物理模拟(并且您的撞球游戏需要一个)都需要访问物体的几何形状。正如已经提到的,OpenGL 不建模,因此无论您使用 OpenGL 绘制什么,仅将其发送到 OpenGL 都无法使其可供物理模拟访问。
无论如何,您的代码都是错误的:在
glBegin(...)
和glEnd()
之间只允许顶点规范调用。那里不允许您调用glRotate
、glTranslate
和glutSolidCone
。For clarification: OpenGL is neither a modelling library, not a scene graph. All that OpenGL does is throwing points, lines and triangles to a image buffer.
So doing it purely with OpenGL is not possible. You'll need some kind of modelling code to create models made of triangles. Do yourself a favour and:
Any physics simulation (and your pool game requires one) needs to access the objects' geometry. And as already mentioned OpenGL does not model, so whatever you draw with OpenGL, just sending it to OpenGL will not make it accessible to a physics simulation.
Your code is wrong anyway: Only vertex specification calls are allowed between
glBegin(...)
andglEnd()
. Your calls ofglRotate
,glTranslate
andglutSolidCone
are not allowed there.使用一个全局变量来保留鼠标光标的位置,然后在两个函数中使用它。
全局变量似乎是 glut 所需的不同函数之间进行通信的唯一方式。考虑到 opengl/glut 当前的结构,如果没有它们,要做到这一点似乎非常困难。
use a global variable that keeps the position of mouse cursor and then use it in both functions.
global variables seem to be the only way to communicate between the different functions required by glut. To do this without them seems very difficult given the current structure of opengl/glut.