Sign in to follow this  

Problem rotating camera with mouse

This topic is 3409 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 have this working with the arrow keys but the mouse is just crazy. can someone look at this code and tell me what I need to do to get the camera to rotate correctly on the x axis and on the y axis using the mouse with glut passive motion fuction. Also is there a way for me to be able to move forward or backward and turn at the same time with the arrow keys? Here is the code I have Much thanks, L.J. Bush
 #include <math.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>
//#define pi = 3.14159;
using namespace std;
 bool lbuttonDown;
 float angle=0.0,ratio;
 float x=0.0f,y=0.0f,z=0.5f;
 float lx=0.0f,ly=0.0f,lz=-0.1f;
 float upx=0.0f,upy=0.0f,upz=0.0f;
 float _angle = 30.0f;
 float _cameraAngle = 0.0f;
//GLfloat LookUpDown = 0.0f;
 void RotY(float ang) {

	lx = sin(ang);
	lz = -cos(ang);
	glLoadIdentity();
	gluLookAt(x, y, z, 
		      x + lx,y + ly,z + lz,
			  0.0f,1.0f,0.0f);
}

void moveFB(int direction) {
	x = x + direction*(lx)*0.1;
	z = z + direction*(lz)*0.1;
	glLoadIdentity();
	gluLookAt(x, y, z, 
		      x + lx,y + ly,z + lz,
			  0.0f,1.0f,0.0f);
}

void LookUpDown(float ang) {
	ly = sin(ang);
	lz = -cos(ang);
	glLoadIdentity();
	gluLookAt(x, y, z, 
		      x + lx,y + ly,z + lz,
			  0,1,0);
if (angle > 90) {
  	angle = 90; 
  }
 else if(angle < -90) {
angle = -90; 
  }

}




void initRendering() {
	glEnable(GL_DEPTH_TEST);
}

void handleKeypress(unsigned char key,
					int x, int y) {
	switch (key) {
		case 27:
		exit(0);
		
	}
}

void handleSpecialKey(int key, int x, int y) {
    
	switch (key) {
		case GLUT_KEY_LEFT : 
			angle -= 0.1f;
			RotY(angle);
			break;
		case GLUT_KEY_RIGHT : 
			angle +=0.1f;
			RotY(angle);
			break;
		case GLUT_KEY_UP : 
			moveFB(1);
			break;
		case GLUT_KEY_DOWN : 
			moveFB(-1);
			break;
	    /*case GLUT_KEY_UP && GLUT_KEY_RIGHT:
		    angle+=0.1f
			((moveFB(1), RotY(angle));
			break;
		case GLUT_KEY_UP && GLUT_KEY_LEFT:              
		    angle-=0.1f
			(moveFB(1), RotY(angle));
			break;
		case GLUT_KEY_DOWN && GLUT_KEY_RIGHT:
		    angle+=0.1f
			(moveFB(-1), RotY(angle));
			break;
		case GLUT_KEY_DOWN && GLUT_KEY_LEFT:
		    angle-=0.1f
			(moveFB(-1), RotY(angle));
			break;*/
		case GLUT_KEY_PAGE_UP : 
			angle +=0.01f;
			LookUpDown(angle);
			break;
		case GLUT_KEY_PAGE_DOWN : 
			angle -=0.01f;
			LookUpDown(angle);
			break;
       
	
	}
}
void motionPassive(int x, int y)
{   
	glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR);  

	cout << "Mouse moved at "
		<< "(" << x << "," << y << ")" << endl;
   		 
	if(x > ((GLUT_WINDOW_WIDTH)/2)){
	 angle += 0.1f;
	RotY(angle);
	}
	else if(x < ((GLUT_WINDOW_WIDTH)/2)){
	 angle -= 0.1f;
	RotY(angle);
	}
	
	if(y > ((GLUT_WINDOW_HEIGHT)/2)){
	 angle += 0.1f;
	LookUpDown(angle);
	}
	else if(y < ((GLUT_WINDOW_HEIGHT)/2)){
	angle -= 0.1f;
	LookUpDown(angle);
	}
    

}
void mouse(int button, int state, int x, int y)
{
	if (button == GLUT_RIGHT_BUTTON)
	{
		if (state == GLUT_DOWN)
			glScalef(2.0,2.0,2.0);
		else
			cout << "right button lifted at (" <<  x << "," << y << ")" << endl;
	}
	else if (button == GLUT_LEFT_BUTTON)
	{
		if (state == GLUT_DOWN)
			cout << "you shot me" << endl;
		else
			lbuttonDown = false;
	}
}




void handleResize(int w, int h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0,
				   (double)w / (double)h,
				   0.25,
				   200.0);
}

void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glEnable(GL_COLOR_MATERIAL);  
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
    gluLookAt(x, y, z, 
		  	x + lx,y + ly,z + lz,
			0.0f,1.0f,0.0f);

	
	//glRotatef(LookUpDown,1.0,0,0);      
	glPushMatrix();
	
	glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid
	glRotatef(_angle, 0.0f, 1.0f, 0.0f);
	glScalef(0.01f, 0.01f, 0.01f);
	glBegin(GL_QUADS);
	//Cross back
	glColor4f(1.0f,1.0f, 0.0f, 0.9f);
	glVertex3f(-0.5, 2.5, -0.5);
	glVertex3f(-0.5, -3.0, -0.5);
	glVertex3f(0.5, -3.0, -0.5);
	glVertex3f(0.5, 2.5, -0.5);
	
	glVertex3f(-2.0, 1.0, -0.5);
	glVertex3f(-2.0, 0.0, -0.5);
	glVertex3f(2.0, 0.0, -0.5);
	glVertex3f(2.0, 1.0, -0.5);
  
	
	//Cross front
	glColor4f(1.0f, 1.0f, 0.0f, 0.5f);
	glVertex3f(-0.5, 2.5, 0.5);
	glVertex3f(-0.5, -3.0, 0.5);
	glVertex3f(0.5, -3.0, 0.5);
	glVertex3f(0.5, 2.5, 0.5);
	
	glVertex3f(-2.0, 1.0, 0.5);
	glVertex3f(-2.0, 0.0, 0.5);
	glVertex3f(2.0, 0.0, 0.5);
	glVertex3f(2.0, 1.0, 0.5);

      //Cross top
	glVertex3f(0.5, 2.5, 0.5);
	glVertex3f(-0.5, 2.5, 0.5);
	glVertex3f(-0.5, 2.5, -0.5);
	glVertex3f(0.5, 2.5, -0.5);
	//Cross left top side
	glVertex3f(-0.5, 2.5, 0.5);
	glVertex3f(-0.5, 2.5, -0.5);
	glVertex3f(-0.5, 1.0, -0.5);
	glVertex3f(-0.5, 1.0, 0.5);		
	//Cross left side  top
	glVertex3f(-0.5, 1.0, 0.5);
	glVertex3f(-0.5, 1.0, -0.5);
	glVertex3f(-2.0, 1.0, -0.5);
	glVertex3f(-2.0, 1.0, 0.5);		
	
    //Cross left side  end
	glVertex3f(-2.0, 1.0, 0.5);
	glVertex3f(-2.0, 1.0, -0.5);
	glVertex3f(-2.0, 0.0, -0.5);
	glVertex3f(-2.0, 0.0, 0.5);		
	
	//Cross left side bottom
	glVertex3f(-2.0, 0.0, 0.5);
	glVertex3f(-0.5, 0.0, 0.5);
	glVertex3f(-0.5, 0.0, -0.5);
	glVertex3f(-2.0, 0.0, -0.5);		
	//Cross left bottom side 
	glVertex3f(-0.5, 0.0, 0.5);
	glVertex3f(-0.5, 0.0, -0.5);
	glVertex3f(-0.5, -3.0, -0.5);
	glVertex3f(-0.5, -3.0, 0.5);
	//Cross  bottom 
	glVertex3f(0.5, -3.0, 0.5);
	glVertex3f(0.5, -3.0, -0.5);
	glVertex3f(-0.5, -3.0, -0.5);
	glVertex3f(-0.5, -3.0, 0.5);
	
	//Cross right bottom side 
	glVertex3f(0.5, 0.0, 0.5);
	glVertex3f(0.5, -3.0, 0.5);
	glVertex3f(0.5, -3.0, -0.5);
	glVertex3f(0.5, 0.0, -0.5);
	
	//Cross right side bottom
	glVertex3f(0.5, 0.0, 0.5);
	glVertex3f(0.5, 0.0, -0.5);
	glVertex3f(2.0, 0.0, -0.5);
	glVertex3f(2.0, 0.0, 0.5);
	//Cross right side end
	glVertex3f(2.0, 1.0, 0.5);
	glVertex3f(2.0, 0.0, 0.5);
	glVertex3f(2.0, 0.0, -0.5);
	glVertex3f(2.0, 1.0, -0.5);
	
	//Cross right side top
	glVertex3f(0.5, 1.0, 0.5);
	glVertex3f(2.0, 1.0, 0.5);
	glVertex3f(2.0, 1.0, -0.5);
	glVertex3f(0.5, 1.0, -0.5);
	//Cross right side top
	glVertex3f(0.5, 1.0, 0.5);
	glVertex3f(0.5, 1.0, -0.5);
	glVertex3f(0.5, 2.5, -0.5);
	glVertex3f(0.5, 2.5, 0.5);
	glEnd();
	glPopMatrix(); 
	
	glPushMatrix();
	glBegin(GL_QUADS);
	glTranslatef(0.0f, -1.5f, 0.0f);
	glRotatef(_angle,0.0,0.0,0.0);
	//floor
	glColor3f(0.5, 1.0, 0.0);
	glVertex3f(-10.0,-0.5,10);
	glVertex3f(-10.0,-0.5,-10);
	glVertex3f(10.0,-0.5,-10);
	glVertex3f(10.0,-0.5,10);
    	glEnd();
    glPopMatrix(); 
	glutSwapBuffers();
}
void update(int value) {
	_angle += 4.0f;
	if (_angle > 360) {
		_angle -= 360;
	}
	
    glutPostRedisplay();
    glutTimerFunc(25, update, 0);
}	
	int main(int argc, char** argv) {
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(500,500);
	glutCreateWindow("Rotaion Test");
	initRendering();
	
	//glutFullScreen();
	glutDisplayFunc(drawScene);
	glutKeyboardFunc(handleKeypress);
	glutSpecialFunc(handleSpecialKey);
	glutReshapeFunc(handleResize);
	glutTimerFunc(25,update,0);
	glutMouseFunc(mouse);
	glutPassiveMotionFunc(motionPassive);
	glutMainLoop();
	return 0;
}

[Edited by - bushimports on August 14, 2008 10:32:39 PM]

Share this post


Link to post
Share on other sites
It looks like you turn the camera _every time_ the mouse moves even a little. And the camera movement does not even depend on the direction of the mouse, just the mouse cursor position.

I'm not that familiar with glut, but I think it handles keyboard messages in the usual way. It means that you get about 10-20 keyboard events per second, when keeping the key down. Mouse messages, on the other hand, come much more rapidly. (Ten times faster? A hundred? I'm not really sure about the numbers.)

So, by looking at the code, I'd say that you end up spinning wildly in unexpected directions when using the mouse.

My suggestion is: In your motionPassive() function, calculate _how much_ the mouse has moved from the previous position, and use that as the turning angle.

Share this post


Link to post
Share on other sites
You will want to save the previous cursor position so you can compare how much the cursor has moved since the last time you were there.
Then you need to find the difference and scale it down to slow it.
And before leaving, save the new position into the old position for next time.

Share this post


Link to post
Share on other sites

This topic is 3409 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this