I’m learning OpenGL and have worked through creating a basic animation: 2 cubes rotating (woo!)
My problem is that the back faces are not rendering, and as the cubes rotate it gives the impression that they are rotating back and forth rather than going the full 360 degrees.
Here’s the code for my program:
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <stdio.h>
static GLfloat spin = 0.0;
// COORDINATE COLUMNS :: X Y Z
// box 1 coordinates (left box)
static GLint box1verts[] = { -30, -10, 10, //point 1
-10, -10, 10, //point 2
-10, 10, 10, //etc
-30, 10, 10,
-30, -10, -10,
-10, -10, -10,
-10, 10, -10,
-30, 10, -10};
// box 2 coordinates (right box)
static GLint box2verts[] = { 10, -10, 10, //point 1
30, -10, 10, //point 2
30, 10, 10, //etc
10, 10, 10,
10, -10, -10,
30, -10, -10,
30, 10, -10,
10, 10, -10
};
// COLOUR COLUMNS :: R G B
static GLfloat colors[]= { 1.0, 0.0, 0.0, //point 1 colors
0.0, 1.0, 0.0, //etc
0.0, 0.0, 1.0,
0.5, 0.0, 0.0,
0.0, 0.5, 0.0,
0.0, 0.0, 0.5,
1.0, 1.0, 1.0,
1.0, 0.0, 1.0};
//cube indices refs in one array
// 3___________2
// / /|
// / / |
// 7/___________/6 | RIGHT
// | | | |
// | 0 _ | |1
// | / | /
// | | /
// |__________|/
// 4 FRONT 5
static GLubyte boxIndices[]= {4,5,6,7,1,2,6,5,0,1,5,4,0,3,2,1,0,4,7,3,2,3,7,6};
void initLights(void)
{
// Light properties
GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat diffuse[] = { 0.2, 0.0, 0.6, 1.0 };
GLfloat specular[] = { 0.40, 0.4, 0.4, 1.0 };
GLfloat position[] = { -5.0, 20.0, 30.0, 0.0 };
GLfloat lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat local_view[] = { 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
/* glFrontFace (GL_CW); */
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(3,GL_FLOAT,0,colors);
glPushMatrix();
glRotatef(spin,1.0,1.0,0.2);
// draw the lot at once
glVertexPointer(3,GL_INT,0,box1verts);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, boxIndices);
glVertexPointer(3,GL_INT,0,box2verts);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, boxIndices);
glPopMatrix();
glutSwapBuffers();
}
void spinDisplay(void)
{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0,50.0,-50.0,50.0,-50.0,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Mouse Control :: Starts and stops animation
void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Window");
init();
//initLights();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Please can someone take a look and tell me what the problem is?
Thanks