PDA

View Full Version : rotate object in 3d using camera

marito
03-19-2013, 10:58 AM
Hello! I am trying to make a camera rotate around an object that is already rotating. I want to move the camera using keyboard. Each time I push a button I change one of the three angles which show where the camera is. The camera rotates bythe Euler angles. I don't understand what I am doing wrong. Please help, I have searched and tried many things, but nothing works...

#include <stdio.h>
#include <stdlib.h>
#include <glut.h>
#include <math.h>

#define pi 3.14159265

typedef GLfloat point3d[3];
point3d p0={-1,-1,-13}, p1={1,-1,-13}, p2={-1,1,-13}, p3={1,1,-13};
point3d p4={-1,-1,-15}, p5={1,-1,-15}, p6={-1,1,-15}, p7={1,1,-15};
point3d p20={1,1,-13}, p21={1.5,1,-13}, p22={1,1.5,-13}, p23={1.5,1.5,-13};
point3d p24={1,1,-14}, p25={1.5,1,-14}, p26={1,1.5,-14}, p27={1.5,1.5,-14};
int delay=100, angle; //delay in msec
float xea=0., yea=0., zea=0., xe, ye, ze, a=0., b=0., c=0., move=20.;

void InitWindow()
{
GLfloat ambientLight[] = {0.3, 0.3, 0.3, 1};//ambient grey light
GLfloat diffuseLight[] = {0.7, 0.7, 0.7, 1};//diffuse white light
GLfloat lightPos[] = {1, 1, -12, 1};

//ENABLE SPECULAR LIGHT
//GLfloat specularLight[] = {0.8, 0.8, 0.8, 1};
//GLfloat specref[] = {1, 1, 1, 1};//full reflectance
//GLfloat spotDir[]={0, 0, -14};
//glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);
//glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION, spotDir);
//glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,15);
//glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,specref);
//glMateriali(GL_FRONT,GL_SHININESS,128);

glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

glClearColor(0.,0.,40./255.,0); //blue sky
glMatrixMode(GL_PROJECTION);
glFrustum(-2,2,-2,2,10,15000);
// glOrtho(-4,5,-3,5,-13,15000);
glMatrixMode(GL_MODELVIEW);
}

void Stars()
{
float d,e;
srand(1);
glEnable(GL_POINT_SMOOTH);
glColor3f(1.,1.,0.);
glPointSize(1);
glBegin(GL_POINTS);
for (int i=1;i<=1000;i++){
e=200*rand()/RAND_MAX-100;
d=200*rand()/RAND_MAX-100;
glVertex3i(e,d,-300);
}
glEnd();
glPointSize(4);
glBegin(GL_POINTS);
for (int i=1;i<=100;i++){
e=200*rand()/RAND_MAX-100;
d=200*rand()/RAND_MAX-100;
glVertex3i(e,d,-300);
}
glEnd();
glPointSize(8);
glBegin(GL_POINTS);
for (int i=1;i<=10;i++){
e=200*rand()/RAND_MAX-100;
d=200*rand()/RAND_MAX-100;
glVertex3i(e,d,-300);
}
glEnd();
}

void Cubes()
{
glColor3f(0.3,0.3,0.9);//blue
glVertex3fv(p0); glVertex3fv(p1); glVertex3fv(p3); glVertex3fv(p2); //front
glColor3f(0.1,0.6,0.9);
glVertex3fv(p1); glVertex3fv(p5); glVertex3fv(p7); glVertex3fv(p3); //right
glColor3f(0,0.3,0.9);
glVertex3fv(p5); glVertex3fv(p4); glVertex3fv(p6); glVertex3fv(p7); //back
glColor3f(0.3,0,0.9);
glVertex3fv(p4); glVertex3fv(p0); glVertex3fv(p2); glVertex3fv(p6); //left
glColor3f(0.3,0.3,0);
glVertex3fv(p2); glVertex3fv(p3); glVertex3fv(p7); glVertex3fv(p6); //top
glColor3f(1,0.3,1);
glVertex3fv(p1); glVertex3fv(p0); glVertex3fv(p4); glVertex3fv(p5); //bottom
glEnd();
glColor3f(0.3,0.3,0.9);//blue
glVertex3fv(p20); glVertex3fv(p21); glVertex3fv(p23); glVertex3fv(p22); //front
glColor3f(0.1,0.6,0.9);
glVertex3fv(p21); glVertex3fv(p25); glVertex3fv(p27); glVertex3fv(p23); //right
glColor3f(0,0.3,0.9);
glVertex3fv(p25); glVertex3fv(p24); glVertex3fv(p26); glVertex3fv(p27); //back
glColor3f(0.3,0,0.9);
glVertex3fv(p24); glVertex3fv(p20); glVertex3fv(p22); glVertex3fv(p26); //left
glColor3f(0.3,0.3,0);
glVertex3fv(p22); glVertex3fv(p23); glVertex3fv(p27); glVertex3fv(p26); //top
glColor3f(1,0.3,1);
glVertex3fv(p21); glVertex3fv(p20); glVertex3fv(p24); glVertex3fv(p25); //bottom
glEnd();
}

void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
Stars();
glPopMatrix();
xe=(cos(c*pi/180.)*cos(b*pi/180.)*cos(a*pi/180.)-sin(c*pi/180.)*sin(a*pi/180.))*xea+(cos(c*pi/180.)*cos(b*pi/180.)*sin(a*pi/180.)+sin(c*pi/180.)*cos(a*pi/180.))*yea-cos(c*pi/180.)*sin(b*pi/180.)*zea;
ye=-(sin(c*pi/180.)*cos(b*pi/180.)*cos(a*pi/180.)-cos(c*pi/180.)*sin(a*pi/180.))*xea+(-sin(c*pi/180.)*cos(b*pi/180.)*sin(a*pi/180.)+cos(c*pi/180.)*cos(a*pi/180.))*yea+sin(c*pi/180.)*sin(b*pi/180.)*zea;
ze=sin(b*pi/180.)*cos(a*pi/180.)*xea+sin(b*pi/180.)*sin(a*pi/180.)*yea+cos(b*pi/180.)*zea;
gluLookAt(xe,ye,ze,0,0,-14,0,1,0);// (0,1,0) the angle of the camera,(xe,ye,ze) the camera,(0,0,-14) the focus point
xea=xe;
yea=ye;
zea=ze;

glTranslatef(0,0,-14);
glRotatef(angle,0,1,0);
glTranslatef(0,0,14);

Cubes();
glutSwapBuffers();
}

void Camera(unsigned char key, int x, int y)
{
switch(key){
case 'q': a+=move; break;
case 'w': a-=move; break;
case 'a': b+=move; break;
case 's': b-=move; break;
case 'z': c+=move; break;
case 'x': c-=move; break;
case '0': a=0.; b=0.; c=0.; //go to the initial state
}
if(key==27) exit(0); //turn off camera
glutPostRedisplay();
}

void Rotate(int n) // the glutTimerFunc
{
n++;
angle+=5;
glutPostRedisplay();
glutTimerFunc(delay,Rotate,n);
}

void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);
glutInitWindowPosition(200,100);
glutInitWindowSize(400,400);
glutCreateWindow("3D-cubes rotating in space and hit by light");
InitWindow();

glutDisplayFunc(Display);
glutTimerFunc(delay,Rotate,0);
glutKeyboardFunc(Camera);
glutMainLoop();
}