3
votes

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();
}
4
Did you mean queue when you say cue? I didn't dare to edit it.orlp
no it is meant to be cue...the name of the stick you use in snooker or pool :)DK10
@nightcracker: pool cuegenpfault
Please don't use glutSolidCone, it's awkward. Also I suggest you stay clear of GLU. Both GLU and GLUT are seriously outdated, hardly anyone uses them these days. GLUT is not part of OpenGL BTW (neither is GLU, but they tend to ship in common).datenwolf
There is a very similar question here: stackoverflow.com/questions/5835372/moving-a-pool-cue Is this by any chance a homework assignment?Bart

4 Answers

4
votes

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.

4
votes

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.

2
votes

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.

1
votes

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.