简体   繁体   中英

mouse movement opengl

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 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

If you want to rotate around another point see How to change the Center-of-Rotation in 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.

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.

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:

  • model your objects with a 3D modeller
  • export them into a file format you can read
  • in your program read those files and send the meshes to OpenGL from there

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(...) and glEnd() . Your calls of glRotate , glTranslate and glutSolidCone are not allowed there.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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