• Advertisement
Sign in to follow this  

Mouse Rotated Camera [Solved]

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a camera that rotates when the mouse's left button is clicked and when they move the mouse but I am having some trouble with it, for some reason it won't update whether I have my mouse buttons clicked or not clicked. Here's my code:
/*These variables are global and declared at the top of the file*/
Camera Cam; /*This is my own class that basically just holds 2 vectors for rotation and position*/
int xLast = 0, yLast = 0;
bool LMB = false;
void mouseClick(int Button, int State, int x, int y){
	if(Button == GLUT_LEFT_BUTTON && State == GLUT_DOWN)
		LMB = true;
	
	if(Button == GLUT_LEFT_BUTTON && State == GLUT_UP)
		LMB = false;
}

void mouseMovement(int x, int y){
	if(LMB){
		int xDiff, yDiff;
		xDiff = x - xLast;
		yDiff = y - yLast;
		xLast = x;
		yLast = y;
		
		Cam.Rotation.x += yDiff*0.3;
		Cam.Rotation.y += xDiff*0.3;
		
		glutPostRedisplay();
	}
}

And here's how I am assigning the functions to glut.
	glutMouseFunc(mouseClick);
	glutPassiveMotionFunc(mouseMovement);

[Edited by - MrPickle on May 10, 2008 7:04:43 AM]

Share this post


Link to post
Share on other sites
Advertisement
Sure :)

void drawScene(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Cam.Update();
glBegin(GL_QUADS);

glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);

glEnd();
glutSwapBuffers();
}


void Camera::Update(void){
UpdateRotation();
UpdatePosition();
}

void Camera::UpdateRotation(void){
glRotatef(Rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(Rotation.y, 0.0f, 1.0f, 0.0f);
}

void Camera::UpdatePosition(void){
glTranslatef(-Position.x, -Position.y, -Position.z);
}

Share this post


Link to post
Share on other sites
I don't know so much about glut, but I'd guess that this might be the problem:
glutPassiveMotionFunc(mouseMovement);

Is there some "active motion func" too? Just a hunch.
You can try this just by removing that "if (LMB)" line from your mouseMovement() function.


Also this might be the problem:
int xDiff,yDiff;
...
Cam.Rotation.x += yDiff*0.3;
Cam.Rotation.y += xDiff*0.3;

xDiff and yDiff are integers and you multiply them by 0.3.
What datatype Cam.Rotation.x and Cam.Rotation.y are? Hopefully they are floats, otherwise those lines above won't do anything.

Share this post


Link to post
Share on other sites
Yes, there is a active motion function. That was the problem :)

Thank you, I guess this is what you get when you don't read what the differences are between things :P

And yes, Rotation.x and Rotation.y are floats.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement