PDA

View Full Version : Normals :: Can't make back faces visible



Turbo
03-09-2005, 08:53 AM
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(&amp;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

Aeluned
03-09-2005, 10:25 AM
First of all, just to be sure, disable culling of polygons
glDisable(GL_CULL_FACE).

secondly, your depth func is a little weird - your fragments are always passing, so it's like you're not doing a depth test at all.
try: glDepthFunc(GL_LEQUAL).

that's just at a quick scan...let me know if the problem persists.

Turbo
03-09-2005, 11:50 AM
Hi thanks for your reply.

I tried glDepthFunc() before when I had lighting in the scene but it didn't solve the ptoblem, and with some settings gave me some weird interlaced-video-effect results. Anyway I'm afraid it didn't work!

Do you think it might have something to do with the order the quads are drawn? Any other ideas?

Aeluned
03-09-2005, 12:32 PM
The order in which the quads are drawn affect only lighting and culling (that's why I suggested disabling GL_CULL_FACE - just to be sure).

Could you post a screen shot of the problem?
They say a picture is worth a thousand words...

Turbo
03-09-2005, 12:57 PM
Sure, here's a screencap: Boxes! (http://www.m3ssy.co.uk/ogl.wmv)

The front faces are bright green. As it rotates you can see into the box through the back left and bottom (I think?) faces.

Aeluned
03-09-2005, 01:28 PM
hmmm...
Let's ensure the z-buffer is writable -
try: glDepthMask(GL_TRUE)

also, make sure you changed your depth func - it will never look correct using GL_ALWAYS.

edit: here's another problem -
glClear(GL_COLOR_BUFFER_BIT)

you need to or in the depth buffer bit, otherwise you'll get weird output as well
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

Turbo
03-09-2005, 01:44 PM
Ahhh nice work, GL_DEPTH_BUFFER_BIT did it!

Thanks very much for your help!

Aeluned
03-09-2005, 01:51 PM
Cheers.