Hello everyone! I am beginner in OpenGL and I am having problems making an object rotate using the glutTimerFunc. I expect to see this cube rotating around the vertical axis, which passes through its center, but I can’t see it rotate. My code is
#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};
int r=1, theta=0, phi=0, delay=300, angle; //delay in msec
float xe, ye, ze;
void InitWindow()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0.9,0.9,0.9,0);
glMatrixMode(GL_PROJECTION);
glFrustum(-2,2,-2,2,10,1500);
// glOrtho(-4,5,-3,5,-13,14);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
GLfloat lighting[]={0.7,0.7,0.7,1};
GLfloat lightPos[] = {2, 2, -14, 1};
glLightfv(GL_LIGHT0,GL_DIFFUSE,lighting);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glMatrixMode(GL_MODELVIEW);
}
void Cube()
{
glBegin(GL_QUADS); //left cube
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();
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// xe=r*cos(theta*pi/180.)*sin(phi*pi/180.)+0.5; //(xe,ye,ze) the direction of vision
// ye=r*sin(theta*pi/180.)*sin(phi*pi/180.); // (0.5,0,0) the focus point
// ze=r*cos(phi*pi/180.);
gluLookAt(0,0,0,0.,0.,-14.,0.,1.,0.);// (0,1,0) the angle of the camera
Cube();
glRotatef(angle,0,1,0);
glutSwapBuffers();
}
void StepRot(int n) //the "glutTimerFunc"
{
n++;
angle+=5;
printf("n",n);
glutPostRedisplay();
glutTimerFunc(delay,StepRot,n);
if(n<100) glutTimerFunc(delay,StepRot,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 light");
InitWindow();
glutDisplayFunc(Display);
glutTimerFunc(delay,StepRot,0);
glutMainLoop();
}
Thanks a lot for every help!