compiles fine with
g++ colourfulrotatingcube.cpp -lglut -lGL -lGLU -o colourfulrotatingcube
but the arrow keys are not moving the cube. Why?
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
#include<math.h>
void keyboard(int key, int x, int y);
void drawCube();
// Rotate X
double rX=0;
// Rotate Y
double rY=0;
// The coordinates for the vertices of the cube
double x = 0.6;
double y = 0.6;
double z = 0.6;
void drawCube()
{
// Set background Colour
glClearColor(0.4,0.4,0.4,1.0);
// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Rotate when user changes rX and rY
glRotatef(rX,1.0,0.0,0.0);
glRotatef(rY,0.0,1.0,0.0);
// BACK
glBegin(GL_TRIANGLES);
glColor3f(0.4,0.3,0.5);
glVertex3f(x,y,z);
glVertex3f(x,-y,z);
glVertex3f(-x,y,z);
glEnd();
// FRONT
// Using 4 triangles!
glBegin(GL_TRIANGLES);
glColor3f(0.1,0.5,0.3);
glVertex3f(-x,y,z);
glVertex3f(0,0,-z);
glVertex3f(-x,-y,-z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.0,0.5,0.0);
glVertex3f(-x,-y,-z);
glVertex3f(-0,0,-z);
glVertex3f(-x,-y,-z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.1,0.3,0.3);
glVertex3f(-x,y,-z);
glVertex3f(x,y,-z);
glVertex3f(0,0,-z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.2,0.2,0.2);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(-x,y,-z);
glEnd();
// LEFT
glBegin(GL_TRIANGLES);
glColor3f(0.3,0.5,0.6);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(-x,y,-z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.5,0.5,0.5);
glVertex3f(-x,y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,y,-z);
glEnd();
//RIGHT
glBegin(GL_TRIANGLES);
glColor3f(0.2,0.2,0.2);
glVertex3f(x,y,z);
glVertex3f(x,y,-z);
glVertex3f(x,-y,z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.0,0.0,0.0);
glVertex3f(x,y,z);
glVertex3f(x,y,-z);
glVertex3f(-x,y,-z);
glEnd();
//TOP
glBegin(GL_TRIANGLES);
glColor3f(0.6,0.0,0.0);
glVertex3f(x,y,z);
glVertex3f(x,y,-z);
glVertex3f(-x,y,-z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.6,0.1,0.2);
glVertex3f(-x,y,z);
glVertex3f(x,y,z);
glVertex3f(-x,y,-z);
glEnd();
//BOTTOM
glBegin(GL_TRIANGLES);
glColor3f(0.4,0.0,0.4);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(x,-y,z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.3,0.0,0.3);
glVertex3f(x,-y,-z);
glVertex3f(-x,-y,-z);
glVertex3f(x,-y,z);
glEnd();
glFlush();
glutSwapBuffers();
}
void keyboard(int key, int x, int y)
{
if(key == GLUT_KEY_RIGHT)
{
rY +=15;
}
else if(key == GLUT_KEY_LEFT)
{
rY -=15;
}
else if(key ==GLUT_KEY_DOWN)
{
rX -=15;
}
else if(key ==GLUT_KEY_UP)
{
rX +=15;
}
}
int main(int argc, char **argv)
{
// Initializr GLUT and process parameters
glutInit(&argc, argv);
// Request double buffered true colour window with Z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700,700);
glutInitWindowPosition(100,100);
// Create window
glutCreateWindow("Linux Journal Open GL Cube");
//Enable Z-buffer depth test
glEnable(GL_DEPTH_TEST);
//Callback functions
glutDisplayFunc(drawCube);
glutSpecialFunc(keyboard);
// Pass control to GLUT for events
glutMainLoop();
return 0;
}