I’m new to OpenGL. As a matter of fact this is the first program I attempted. It’s supposed to be a spinning cube. And a cube it is. If I set glPolygonMode(GL_FRONT_AND_BACK,GL_LINE) it looks very much like I want it to look. The problems begin when I use polygon mode GL_POLYGON.
You see, it appears as if back-faces are culled, even though I haven’t turned culling on. I even tried using glDisable() to turn it off in case it was on by default, but that didn’t do much.
It sorta looks like the polygons are semitransparent too. That’s weird. I haven’t gotten to the blending part of my OpenGL book so I don’t know how to turn off blending (if it is on by default for some reason).
It’s very weird and I can’t for the life of me figure out what the problem is.
Here’s the code. It’s fairly simple and straight-forward.
#include <GL\glut.h>
GLfloat xRotated, yRotated, zRotated;
GLfloat cubeverts = {
-1.0,-1.0,-1.0, //vertex no 1
-1.0,1.0,-1.0, //2
-1.0,1.0,1.0, //3
-1.0,-1.0,1.0, //4
1.0,-1.0,1.0, //5
1.0,-1.0,-1.0, //6
1.0,1.0,1.0, //7
1.0,1.0,-1.0 //8
};GLfloat cubecolors = { //colors corresponding to vertices
0.5,0.2,0.8,1.0,
0.8,0.1,0.7,1.0,
0.1,0.9,0.6,1.0,
0.4,0.8,0.6,1.0,
0.1,0.7,0.8,1.0,
0.8,0.6,0.4,1.0,
0.9,0.5,0.2,1.0,
0.2,0.5,0.6,1.0};
unsigned char cubeindeces = { //indeces in the vertex array to draw the cube
0,3,2,1, //4 indeces per polygon. We use GL_QUADS
2,3,4,6,
0,5,4,3, //All are numbered CCW as seen from the outside
1,2,6,7,
7,6,4,5,
1,7,5,0
};void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-10.5);glRotatef(xRotated,1.0,0.0,0.0);
glRotatef(yRotated,0.0,1.0,0.0);
glRotatef(zRotated,0.0,0.0,1.0);
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeindeces);
glFlush(); //Finish rendering
glutSwapBuffers();
}void Reshape(int w, int h)
{
if (h == 0 | | w == 0) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Angle of view:30 degrees
//Near clipping plane distance: 0.5
//Far clipping plane distance: 20.0
gluPerspective(30.0,(GLdouble)w/(GLdouble)h,0.0,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,w,h); //Use the whole window for rendering
}void Idle(void)
{
xRotated += 0.093;
yRotated += 0.061;
zRotated += -.08;
glutPostRedisplay();
}
int main (int argc, char **argv)
{
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //For animations you should use double buffering
glutInitWindowSize(320,200);
//Create a window with rendering context and everything else we need
glutCreateWindow(“Mitt första OGL progg”);
glPolygonMode(GL_FRONT_AND_BACK,GL_POLYGON);
xRotated = yRotated = zRotated = 0.0;
glClearColor(0.0,0.0,0.0,0.0);
//Assign the three used Msg-routines
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutIdleFunc(Idle);
//Let GLUT get the msgs//Load vertex arrays and color arrays with cube data
//for super-fast rendering
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);glVertexPointer( 3, //x,y,z-coordinates
GL_FLOAT, //data type is float
0, //No stride
cubeverts);
glColorPointer( 4, //RGBA components
GL_FLOAT, //data type is float
0, //No stride
cubecolors);
//Start GLUT-loopglDisable(GL_CULL_FACE);
glutMainLoop();
return 0;
}
If you can help me I would be really grateful! Thank you in advance.
[This message has been edited by Caesar (edited 06-21-2000).]