简体   繁体   English

鼠标移动opengl

[英]mouse movement opengl

I am creating a pool game written in C++ using plain OpenGL (no external tools), but I can use GLUT. 我正在使用普通的OpenGL创建一个用C ++编写的池游戏(没有外部工具),但我可以使用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 eg 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();
}

NeHe has a tutorial that covers the basics of rotation in OpenGL: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=04 NeHe有一个教程,涵盖了OpenGL中的旋转基础知识: http//nehe.gamedev.net/data/lessons/lesson.asp?withouton = 04

If you want to rotate around another point see How to change the Center-of-Rotation in OpenGL . 如果要围绕另一个点旋转,请参阅如何更改OpenGL中的旋转中心 It basically comes down to this: 它基本上归结为:

Translate it so that the point you want to rotate around is at the origin, then rotate, then translate it to the location you want it at. 翻译它以便您想要旋转的点位于原点,然后旋转,然后将其转换为您想要的位置。

Also use glPushMatrix/glPopMatrix or glLoadIdentity to isolate transformations. 还可以使用glPushMatrix / glPopMatrix或glLoadIdentity来隔离转换。

Glut has a few mouse callback function Glut有一些鼠标回调功能
Mouse callback 鼠标回调
Motion callback 动作回调

You could use the callback to figure out the movement of the mouse, the rest is pure math. 您可以使用回调来计算鼠标的移动,其余的是纯数学。

For clarification: OpenGL is neither a modelling library, not a scene graph. 为了澄清:OpenGL既不是建模库,也不是场景图。 All that OpenGL does is throwing points, lines and triangles to a image buffer. OpenGL所做的就是将点,线和三角形投射到图像缓冲区。

So doing it purely with OpenGL is not possible. 因此,使用OpenGL完全无法实现。 You'll need some kind of modelling code to create models made of triangles. 您需要某种建模代码来创建由三角形组成的模型。 Do yourself a favour and: 帮自己一个忙,并:

  • model your objects with a 3D modeller 使用3D建模器为您的对象建模
  • export them into a file format you can read 将它们导出为您可以阅读的文件格式
  • in your program read those files and send the meshes to OpenGL from there 在你的程序中读取这些文件并从那里将网格发送到OpenGL

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. 正如已经提到的,OpenGL没有建模,所以无论你用OpenGL绘制什么,只需将它发送到OpenGL就不会让它可以被物理模拟所接受。

Your code is wrong anyway: Only vertex specification calls are allowed between glBegin(...) and glEnd() . 无论如何你的代码都是错误的: glBegin(...)glEnd()之间只允许顶点规范调用。 Your calls of glRotate , glTranslate and glutSolidCone are not allowed there. 那里不允许你调用glRotateglTranslateglutSolidCone

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. 考虑到opengl / glut的当前结构,在没有它们的情况下这样做似乎非常困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM