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.