# move camera with keyboard function

Hello! I have two cubes rotating here, and I additionally want to move my camera on a sphere to watch this from different angles. I use keyboard function, but camera doesn’t move. I don’t know what’s wrong… My code is this:

``````
#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 xe=0., ye=0., ze=0., 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 c,d;

srand(1);

glEnable(GL_POINT_SMOOTH);

glColor3f(1.,1.,0.);

glPointSize(1);

glBegin(GL_POINTS);

for (int i=1;i<=1000;i++){

c=200*rand()/RAND_MAX-100;

d=200*rand()/RAND_MAX-100;

glVertex3i(c,d,-300);

}

glEnd();

glPointSize(4);

glBegin(GL_POINTS);

for (int i=1;i<=100;i++){

c=200*rand()/RAND_MAX-100;

d=200*rand()/RAND_MAX-100;

glVertex3i(c,d,-300);

}

glEnd();

glPointSize(8);

glBegin(GL_POINTS);

for (int i=1;i<=10;i++){

c=200*rand()/RAND_MAX-100;

d=200*rand()/RAND_MAX-100;

glVertex3i(c,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.))*xe+(cos(c*pi/180.)*cos(b*pi/180.)*sin(a*pi/180.)+sin(c*pi/180.)*cos(a*pi/180.))*ye-cos(c*pi/180.)*sin(b*pi/180.)*ze;

ye=-(sin(c*pi/180.)*cos(b*pi/180.)*cos(a*pi/180.)-cos(c*pi/180.)*sin(a*pi/180.))*xe+(-sin(c*pi/180.)*cos(b*pi/180.)*sin(a*pi/180.)+cos(c*pi/180.)*cos(a*pi/180.))*ye+sin(c*pi/180.)*sin(b*pi/180.)*ze;

ze=sin(b*pi/180.)*cos(a*pi/180.)*xe+sin(b*pi/180.)*sin(a*pi/180.)*ye+cos(b*pi/180.)*ze;

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

glTranslatef(0,0,-14);

glRotatef(angle,0,1,0);

glTranslatef(0,0,14);

Cubes();

glutSwapBuffers();

}

void Camera(unsigned char key, int x, int y)

{

//    float move=20.;

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_DEPTH);

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();

}

``````

Thank you for helping

Your code is regular OpenGL and not OpenGL ES, but I think you need to switch the matrix mode back to GL_PROJECTION mode before you call gluLookAt().

Try adding this before the gluLookat() call:

`````` glMatrixMode(GL_PROJECTION);
``````

Regards, Clay

Thank you for helping. I tried it but it doesn’t work… I think there is problem because I’m using Euler angles to rotate the object, but I’m not sure this is the reason.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.