PDA

View Full Version : Mouse Rotations



kokoko
10-12-2002, 02:01 PM
Hi all,
I am implementing mouse rotations to rotate a small scene consisting of a sphere and a plane. The scene rotates around its local axes, which gives weird rotations. I want to rotate the scene in a fixed coordinate system. Please suggest any ideas. I am attaching the code below:

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>

void drawScene(void);

GLfloat eyex = 0.0, eyey = 0.0, eyez = 5.0;
int check = 0;
float stateX, stateY, rotateX = 0.0, rotateY = 0.0;

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);

GLfloat light_position[] = {16.0, 3.0, 4.0, 1.0};
GLfloat light_ambient[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};



glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);


glRotatef(rotateY, 0.0, 1.0, 0.0);
glRotatef(rotateX, 1.0, 0.0, 0.0);
drawScene();


glutSwapBuffers();
glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 100.0);

}


void drawScene(void)
{
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_amb_diff[] = {0.4, 0.0, 0.0, 1.0};
GLfloat mat_shininess[] = {100.0};

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

glutSolidSphere(0.5, 30, 30);


glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-2.0, -1.0, 2.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(2.0, -1.0, 2.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(2.0, -1.0, -2.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-2.0, -1.0, -2.0);
glEnd();
}

void mouse(int button, int state, int i, int j)
{
if(state == GLUT_DOWN)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
{
check = 1;
stateX = i;
stateY = j;
}
break;

case GLUT_RIGHT_BUTTON:
check = 2;
break;

default:
break;
}
}
}


void motion(int i, int j)
{
if(check == 1)
{

rotateX = rotateX > 360.0 ? rotateX-360.0 : rotateX < -360.0 ? rotateX+=360.0 : rotateX;

rotateY = rotateY > 360.0 ? rotateY-360.0 : rotateY < -360.0 ? rotateY+=360.0 : rotateY;


if(j!=stateY)
rotateX = rotateX + (j - stateY) * 0.1;
if(i!=stateX)
rotateY = rotateY + (i - stateX) * 0.1;


printf("rotateX, rotateY: %f %f\n", rotateX, rotateY);

stateX = i;
stateY = j;
glutPostRedisplay();
}
else if(check == 2)
{
printf("Rotate y-axis\n");
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);

init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
// glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();

return 0;
}

Gavin
10-14-2002, 03:35 AM
Try a trackball, there is code for these out there or there is some on my site (in C) www.cs.cf.ac.uk/user/G.R.Powell (http://www.cs.cf.ac.uk/user/G.R.Powell)
->code
->kalman filter

OgliX
10-14-2002, 06:02 AM
use x and y to check whether the mouse has moved to cordinate of a circle
x = radius * cos(angel);
y = radius * sin(angel);

radius can be radius of rotation of camera.
angel is critical term as it will determine something we call mouse sensitivity(i.e. movement on screen corresponding to that of the mouse).
if the coordinate doesn't satisfies the equation either make it a co-ordinate on the circle or discard it.

I don't know whether this works> But u can try on this one.

<> OGLIX <>