Hello,
First I’m sorry for my English, French student
I fight like crazy to be able to move the view (or camera) in a scene. I know it’s the scene that moves and not the view but still, it makes me sad for not succeeding this stuff (or that there is no glu function from the manage all this)
Question 0: Is there a way of defining Identity as actual position (and angles), a kind of glSaveIdentity, so that the followed glLoadIdentity will now load position that we just saved?
So I use 2 ways: one is based gluLookAt, the other based on a series of glRotated and glTranslated. I did not succeed with neither way, I had few wrong hopes …
Note: For keyboard interaction, I use a QWERTY keyboard and ideally, I would like to have (compared to the current view):
turn left a - d turn right to
look up w - s look down
swivels left q - e swivels right
Go left j - l go right
go up i - k go down
go ahead o - u go back
Question 1: What is the magic formula for cosine and sine that gluLookAt works properly for my demand?
const GLdouble PI = 3.1415926535898;
GLvoid Keyboard(unsigned char , int , int);
//View locate parameters
GLdouble eyex=0.;
GLdouble eyey=0.;
GLdouble eyez=0.;
GLdouble centrex=0.;
GLdouble centrey=0.;
GLdouble centrez=0.;
GLdouble upx=0.;
GLdouble upy=1.;
GLdouble upz=0.;
GLdouble anglexz=0;
GLdouble angley=0;
GLvoid DrawScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
/*In whole for centrex-centerz, I add a sine-cosine so that we look at the fictitious object on the trigonometric circle whose the center is the current view, and it works well to turn left-right
note: our keyboard interactions always attribute eyei = centrei*/
gluLookAt(eyex,eyey,eyez,centrex+sin(anglexz),centrey,centrez+cos(anglexz),]sin(angley)*cos(anglexz),cos(angley)*cos(anglexz),sin(angley)*cos(anglexz));
DessineSalle();
glutPostRedisplay();
glutSwapBuffers();
glFlush();
glPopMatrix();
}
GLvoid
Keyboard(unsigned char key , int x, int y)
{
switch(key)
{
////////////////// ROTATE
case 'e':
angley-=PI/144;
break;
case 'q':
angley+=PI/144;
break;
case 'd' :
anglexz-=PI/144;
break;
case 'a' :
anglez+=PI/144;
break;
case 's' :
anglex-=PI/144;
break;
case 'w' :
anglex+=PI/144;
break;
//////////////TRANSLATE
case 'j' :
centrex-=0.25;
eyex-=0.25;
break;
case 'l':
centrex+=0.25;
eyex+=0.25;
break;
case 'k' :
centrey-=0.25;
eyey-=0.25;
break;
case 'i' :
centrey+=0.25;
eyey+=0.25;
break;
case 'o' :
centrez-=0.25;
eyez-=0.25;
break;
case 'u' :
centrez+=0.25;
eyez+=0.25;
break;
case 'V':
glLoadIdentity();
break;
case 27:
exit(1);
break;
}
glutPostRedisplay();
}
Question 2: Why this trick of the function in glRotated glTranslated does not work at all?
GLvoid Keyboard(unsigned char , int , int);
GLvoid Camera(GLfloat , GLfloat , GLfloat , GLfloat , GLfloat , GLfloat );//move the camera
GLfloat angleX = 0;
GLfloat angleY = 0;
GLfloat angleZ = 0;
GLfloat posX = 0;
GLfloat posY = 0;
GLfloat posZ = 0;
GLvoid DrawScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
DessineSalle();
glutPostRedisplay();
glutSwapBuffers();
glFlush();
glPopMatrix();
}
GLvoid
Keyboard(unsigned char key , int x, int y)
{
switch(key)
{
////////////////// ROTATE
case 'e':
Camera(0.,0.,1.,0.,0.,0.);
break;
case 'q':
Camera(0.,0.,-1.,0.,0.,0.);
break;
case 's' :
Camera(1.,0.,0.,0.,0.,0.);
break;
case 'w' :
Camera(-1.,0.,0.,0.,0.,0.);
break;
case 'd' :
Camera(0.,1.,0.,0.,0.,0.);
break;
case 'a' :
Camera(0.,-1.,0.,0.,0.,0.);
break;
//////////////TRANSLATE
case 'j' :
Camera(0.,0.,0.,1.,0.,0.);
break;
case 'l':
Camera(0.,0.,0.,-1.,0.,0.);
break;
case 'k' :
Camera(0.,0.,0.,0.,1.,0.);
break;
case 'i' :
Camera(0.,0.,0.,0.,-1.,0.);
break;
case 'o' :
Camera(0.,0.,0.,0.,0.,1.);
break;
case 'u' :
Camera(0.,0.,0.,0.,0.,-1.);
break;
case 'V':
glLoadIdentity();
break;
case 27:
exit(1);
break;
}
glutPostRedisplay();
}
GLvoid Camera(GLfloat rotX, GLfloat rotY, GLfloat rotZ, GLfloat transX , GLfloat transY, GLfloat transZ)
{
//je ne pourrais pas expliquer ce que j'observe, en tout cas c'est pas ce que j'attend...
glTranslated(posX,posY,posZ);
glRotated(angleX,1.,0.,0.);
glRotated(angleY,0.,1.,0.);
glRotated(angleZ,0.,0.,1.);
angleX-=rotX;
angleY-=rotY;
angleZ-=rotZ;
posX-=transX;
posY-=transY;
posZ-=transZ;
glRotated(-angleZ,0.,0.,1.);
glRotated(-angleY,0.,1.,0.);
glRotated(-angleX,1.,0.,0.);
glTranslated(-posX,-posY,-posZ);
}