PDA

View Full Version : Rotation about Eye Co-ordinate's X and Y axis



Binod Kumar
07-10-2008, 07:08 PM
Hi,
I have drwan a cube at the point (1,1,-1). I want it rotate in eye co-ordinate X axis if I press key 'x' and it should rotate in eye co-ordinate Y axis when I press 'y'. I tried it following way but couldn't achieve the result.

-----------------------------------------

//press x to rotate about x axis press y to rotate about y axis.

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

#define PI_ 3.14159265358979323846

GLuint theTorus;
static GLfloat anglex =0.0, angley = 0.0;


static void draw_cube(void)
{

glutWireCube(1.0); //,2.0,10,10);
}

static void init(void)
{
glShadeModel(GL_FLAT);
glClearColor(0.0, 0.0, 0.0, 0.0);
}


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0,1.0,1.0,1.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(-5, 5, 10, 0, 0 , 0, 0, 1, 0);

glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(100.0,0.0,0.0);

glColor3f(0.0,1.0,0.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,100.0,0.0);

glColor3f(0.0,0.0,1.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,0.0,100.0);
glEnd();


glPushMatrix();
glRotatef(anglex,1.0,0.0,0.0);
glRotatef(angley,0.0,1.0,0.0);
glTranslatef(1.0,1.0,-1.0);
draw_cube();
glPopMatrix();
glFlush();
}

/* Handle window resize */
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30, (GLfloat) w/(GLfloat) h, 1.0, 100.0);

}

/* Rotate about x-axis when "x" typed; rotate about y-axis
when "y" typed; */

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'x':
case 'X':
//glPushMatrix();
//glRotatef(30.0,1.0,0.0,0.0);
anglex+=10.0;
glutPostRedisplay();
//glPopMatrix();
break;
case 'y':
case 'Y':
//glRotatef(30.,0.0,1.0,0.0);
angley+=10.0;
glutPostRedisplay();
break;

case 27:
exit(0);
break;
}
}

int main(int argc, char **argv)
{
glutInitWindowSize(200, 200);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGB);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

-------------------------------

Kindly help.

dletozeun
07-11-2008, 01:55 AM
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'x':
case 'X':
//glPushMatrix();
//glRotatef(30.0,1.0,0.0,0.0);
anglex+=10.0;
glutPostRedisplay();
//glPopMatrix();
break;
case 'y':
case 'Y':
//glRotatef(30.,0.0,1.0,0.0);
angley+=10.0;
glutPostRedisplay();
break;

case 27:
exit(0);
break;
}
}


This is really bad! :) I advise you to just modify anglex and angley values in this function, redisplay if you want but not modify modelviw matrix here. (And I don't understand anything in this function , just looking at this arbitrary 30 rotation...



glPushMatrix();
glRotatef(anglex,1.0,0.0,0.0);
glRotatef(angley,0.0,1.0,0.0);
glTranslatef(1.0,1.0,-1.0);
draw_cube();
glPopMatrix();


If you want to rotate the cube around the camera X and Y axis, you have to take care of the camera position that you set with:



gluLookAt(-5, 5, 10, 0, 0 , 0, 0, 1, 0);


I mean that you have to translate the scene from the oppsite of the camera position, so in your case from a vector ( 5, -5, -10)

This gives:



glPushMatrix();
glRotatef(anglex,1.0,0.0,0.0);
glRotatef(angley,0.0,1.0,0.0);
glTranslatef( 5, -5, -10); // go to camera space
glTranslatef(1.0,1.0,-1.0);
draw_cube();
glPopMatrix();


Note that your case, the camera view and vertical axis are no aligned with the world space (x,y,z) whereas you will compute these rotations in world space.