PDA

View Full Version : Rotate cube not working



openglnewbie01
01-27-2013, 07:21 AM
Hi,

I'm following the tutorial here: I was not able to insert a link so search google for "OpenGL Tutorial for 308-557"
Everything worked fine until number 6: Rotate cube. It compiles ok, but only displays a red face and doesn't respond to user inputs.
Here's the code:

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define WIDTH 480
#define HEIGHT 480

#define RED 0
#define GREEN 0
#define BLUE 0
#define ALPHA 1

#define KEY_ESC 27
#define KEY_UP 101
#define KEY_DOWN 103
#define KEY_X 120
#define KEY_Y 121
#define KEY_Z 122

#define DELTA 5
int x = 0;
int rotateX = 0;
int y = 0;
int rotateY = 0;
int z = 0;
int rotateZ = 0;
int speed = 0;

int squares = 0;
GLuint face;
GLuint cube;

GLvoid initGL();
void init_scene();
GLvoid window_reshape(GLsizei width, GLsizei height);
GLvoid window_display();
void render_scene();
GLvoid window_key(unsigned char key, int x, int y);
GLvoid window_special_key(int key, int x, int y);
GLvoid window_idle();

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitWindowPosition(0, 0);
glutCreateWindow("Square");

initGL();
init_scene();

glutIdleFunc(&window_idle);
glutSpecialFunc(&window_special_key);

glutDisplayFunc(&window_display);
glutReshapeFunc(&window_reshape);
glutKeyboardFunc(&window_key);
glutMainLoop();

return 1;
}

/**
*
*/
GLvoid initGL()
{
glClearColor(RED, GREEN, BLUE, ALPHA);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}

void init_scene()
{
face = glGenLists(2);
cube = face + 1;

glNewList(face, GL_COMPILE);
glBegin(GL_POLYGON);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(1, 1, 0);
glVertex3f(0, 1, 0);
glEnd();
glEndList();

glNewList(cube, GL_COMPILE);
glTranslatef(-0.5, -0.5, 0.5);
glColor3f(1, 0, 0);
glCallList(face);

glColor3f(1, 1, 0);
glPushMatrix();
glTranslatef(0, 0, -1);
glCallList(face);
glPopMatrix();

glColor3f(0, 1, 0);
glPushMatrix();
glRotatef(90, 0, 1, 0);
glCallList(face);
glPopMatrix();

glColor3f(0, 1, 1);
glPushMatrix();
glTranslatef(1, 0, 0);
glRotatef(90, 0, 1, 0);
glCallList(face);
glPopMatrix();

glColor3f(0, 0, 1);
glPushMatrix();
glRotatef(-90, 1, 0, 0);
glCallList(face);
glPopMatrix();

glColor3f(1, 0, 1);
glPushMatrix();
glTranslatef(0, 1, 0);
glRotatef(-90, 1, 0, 0);
glCallList(face);
glPopMatrix();

glEndList();
}

/**
* GLUT will call this function every time the window is resized.
*/
GLvoid window_reshape(GLsizei width, GLsizei height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (GLdouble)width/(GLdouble)height, 1, 10);
glMatrixMode(GL_MODELVIEW);
}

GLvoid window_display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
render_scene();

//glFlush();
glutSwapBuffers();
}

void render_scene()
{
glRotatef(x, 1, 0, 0);
glRotatef(y, 0, 1, 0);
glRotatef(z, 0, 0, 1);
glCallList(cube);
}

GLvoid window_key(unsigned char key, int x, int y)
{
switch(key) {
case KEY_ESC:
exit(1);
break;
case KEY_X:
rotateX = !rotateX;
glutPostRedisplay();
break;
case KEY_Y:
rotateY = !rotateY;
glutPostRedisplay();
break;
case KEY_Z:
rotateZ = !rotateZ;
glutPostRedisplay();
break;
default:
printf("Pressing %d doesn't do anything.\n", key);
break;
}
}

GLvoid window_special_key(int key, int x, int y)
{
switch(key) {
case KEY_UP:
speed = (speed + DELTA + 360) % 360;
glutPostRedisplay();
break;
case KEY_DOWN:
speed = (speed - DELTA + 360) % 360;
glutPostRedisplay();
break;
default:
printf("Pressing %d doesn't do anything.\n", key);
break;
}
}

GLvoid window_idle()
{
if(rotateX) x = (x + speed + 360) % 360;
if(rotateY) y = (y + speed + 360) % 360;
if(rotateZ) z = (z + speed + 360) % 360;
if(speed > 0 && (rotateX || rotateY || rotateZ))
glutPostRedisplay();
}

I'm using Ubuntu 11.10