Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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();
}
like image 904
DK10 Avatar asked Feb 05 '26 02:02

DK10


2 Answers

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.

like image 165
Kien Truong Avatar answered Feb 09 '26 09:02

Kien Truong


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.

like image 40
Jonas Bötel Avatar answered Feb 09 '26 08:02

Jonas Bötel