problem with triangle strip rotation

Hi,

I’ve written small program that displays V-shaped triangle strip. User can rotate this strip using cursor keys.

The problem is that the front face always covers the back face, even though we rotate the model 180 degrees or more.

Can anybody tell me what I’m doing wrong?

here’s the code:

code

or here:


#include <GL/freeglut.h>

const GLdouble left = -10.0;
const GLdouble right = 10.0;
const GLdouble bottom = -10.0;
const GLdouble top = 10.0;
const GLdouble near = 50.0;
const GLdouble far = 70.0;


float rotatex = 0;
float rotatey = 0;

float x;
float y;
float z;


void display()
{
	glClearColor(1.0, 1.0, 1.0, 1.0);
    
	glClear(GL_COLOR_BUFFER_BIT);
    
	glMatrixMode(GL_MODELVIEW);
    
	glLoadIdentity();
    
	glTranslatef(x, y, z);
	glRotatef(rotatex, 1.0, 0.0, 0.0);
	glRotatef(rotatey, 0.0, 1.0, 0.0);

	glBegin(GL_TRIANGLE_STRIP);
		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(-3, -3, 3);
		glVertex3f(3, -3, 3);

		glColor3f(0.0, 1.0, 0.0);
		glVertex3f(-3, 3, 0);
		glVertex3f(3, 3, 0);

		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(-3, -3, -3);

		glColor3f(0.0, 0.0, 1.0);
		glVertex3f(3, -3, -3);
	glEnd();
		
	glFlush();	
	glutSwapBuffers();	
}


void reshape(int width, int height)
{
	glViewport (0,0,width,height);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity();
	glOrtho(left,right,bottom,top,near,far);
	display();
}


void special_keys(int key, int x, int y) 
{
	switch (key) 
	{
		case GLUT_KEY_UP:
			rotatex += 1;
			break;
		case GLUT_KEY_DOWN:
			rotatex -= 1;
			break;
		case GLUT_KEY_LEFT:
			rotatey -= 1;
			break;
		case GLUT_KEY_RIGHT:
			rotatey += 1;
			break;
	}

	reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
}

int main(int argc, char** argv) 
{
	x = (left + right) / 2.0;
	y = (top + bottom) / 2.0 + 5;
	z = -(near + far) / 2.0;

	glutInit(&argc,argv);

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

	glutInitWindowSize(550,500);

	glutCreateWindow("Sample");

	glutDisplayFunc (display);

	glutReshapeFunc (reshape);

	glutSpecialFunc(special_keys);

	glutMainLoop();

	return 0;
}

The depth test is not enabled.
You can replace your codes like this:


    glClear(GL_COLOR_BUFFER_BIT);

to


    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);