PDA

View Full Version : Car backface shows up and it shouldn't



pmatos
10-10-2003, 06:59 AM
Hi all,

I'm doing a simple exercise of building a simple car out of some polygons. The situation is that when I try to view the car after compiler the back window (Vertex 6,18,17,5) show up but they shouldn't because they are behind the side of the car. Any ideas on why this happens?
(code is just below)

Best regards,

Paulo Matos


Source code: (C++)
-------------------
#include <iostream>
#include <GL/glut.h>


#define WINDOW_POSITION_X 10
#define WINDOW_POSITION_Y 10
#define WINDOW_SIZE_H 600
#define WINDOW_SIZE_W 1000

// Macros para os vertices
#define VERTEX1 -5, 0, -15
#define VERTEX2 -5, 2, -15
#define VERTEX3 -5, 2, -14
#define VERTEX4 -5, 3, -14
#define VERTEX5 -5, 5, -10
#define VERTEX6 -5, 10, -7
#define VERTEX7 -5, 10, 3
#define VERTEX8 -5, 6, 7
#define VERTEX9 -5, 2, 14
#define VERTEX10 -5, 1, 14
#define VERTEX11 -5, 1, 15
#define VERTEX12 -5, 0, 15
#define VERTEX13 5, 0, -15
#define VERTEX14 5, 2, -15
#define VERTEX15 5, 2, -14
#define VERTEX16 5, 3, -14
#define VERTEX17 5, 5, -10
#define VERTEX18 5, 10, -7
#define VERTEX19 5, 10, 3
#define VERTEX20 5, 6, 7
#define VERTEX21 5, 2, 14
#define VERTEX22 5, 1, 14
#define VERTEX23 5, 1, 15
#define VERTEX24 5, 0, 15

void cbDisplay()
{

// Limpa viewport
glClear(GL_COLOR_BUFFER_BIT);

// Desenha Linha
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 10.0);
glVertex3f(10.0, 0.0, 10.0);
glVertex3f(10.0, 0.0, 20.0);
glVertex3f(20.0, 0.0, 20.0);
glVertex3f(20.0, 0.0, 10.0);
glVertex3f(30.0, 0.0, 10.0);
glVertex3f(30.0, 0.0, 0.0);
glVertex3f(40.0, 0.0, 0.0);
glVertex3f(40.0, 0.0, 10.0);
glEnd();

// Desenha Carro
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_POLYGON);
glVertex3i(VERTEX1);
glVertex3i(VERTEX2);
glVertex3i(VERTEX3);
glVertex3i(VERTEX4);
glVertex3i(VERTEX5);
glVertex3i(VERTEX6);
glVertex3i(VERTEX7);
glVertex3i(VERTEX8);
glVertex3i(VERTEX9);
glVertex3i(VERTEX10);
glVertex3i(VERTEX11);
glVertex3i(VERTEX12);
glEnd();

glBegin(GL_POLYGON);
glVertex3i(VERTEX13);
glVertex3i(VERTEX14);
glVertex3i(VERTEX15);
glVertex3i(VERTEX16);
glVertex3i(VERTEX17);
glVertex3i(VERTEX18);
glVertex3i(VERTEX19);
glVertex3i(VERTEX20);
glVertex3i(VERTEX21);
glVertex3i(VERTEX22);
glVertex3i(VERTEX23);
glVertex3i(VERTEX24);
glEnd();

// Criar o capot do carro!
glColor3f(0.8, 0.8, 0.8);
glBegin(GL_POLYGON);
glVertex3i(VERTEX12);
glVertex3i(VERTEX24);
glVertex3i(VERTEX23);
glVertex3i(VERTEX11);
glEnd();

glBegin(GL_POLYGON);
glVertex3i(VERTEX11);
glVertex3i(VERTEX23);
glVertex3i(VERTEX22);
glVertex3i(VERTEX10);
glEnd();

glBegin(GL_POLYGON);
glVertex3i(VERTEX10);
glVertex3i(VERTEX22);
glVertex3i(VERTEX21);
glVertex3i(VERTEX9);
glEnd();

glBegin(GL_POLYGON);
glVertex3i(VERTEX9);
glVertex3i(VERTEX21);
glVertex3i(VERTEX20);
glVertex3i(VERTEX8);
glEnd();

glBegin(GL_POLYGON);
glVertex3i(VERTEX8);
glVertex3i(VERTEX20);
glVertex3i(VERTEX19);
glVertex3i(VERTEX7);
glEnd();

glBegin(GL_POLYGON);
glVertex3i(VERTEX7);
glVertex3i(VERTEX19);
glVertex3i(VERTEX18);
glVertex3i(VERTEX6);
glEnd();

glBegin(GL_POLYGON);
glVertex3i(VERTEX6);
glVertex3i(VERTEX18);
glVertex3i(VERTEX17);
glVertex3i(VERTEX5);
glEnd();

//~ glBegin(GL_POLYGON);
//~ glVertex3i(VERTEX5);
//~ glVertex3i(VERTEX17);
//~ glVertex3i(VERTEX16);
//~ glVertex3i(VERTEX4);
//~ glEnd();

//~ glBegin(GL_POLYGON);
//~ glVertex3i(VERTEX4);
//~ glVertex3i(VERTEX16);
//~ glVertex3i(VERTEX15);
//~ glVertex3i(VERTEX3);
//~ glEnd();


glFlush();

}

void cbReshape(int w, int h)
{

float ratio;

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;

ratio = 1.0 * w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(50.0f, 20.0f, 50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glutPostRedisplay();

}

void setupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH);
glClear(GL_DEPTH_BUFFER_BIT);
}

int main(int argc, char *argv[])
{
// Main window ID.
int mainWindow;
// Setting up title for main window.
char *mainWindowTitle = "MVT - Laboratorio 2";

// Passing initial command line arguments to glut.
glutInit(&argc, argv);

// Setting up initial window position.
glutInitWindowPosition(WINDOW_POSITION_X, WINDOW_POSITION_Y);

// Setting up initial window size.
glutInitWindowSize(WINDOW_SIZE_W, WINDOW_SIZE_H);

// Creating main window.
mainWindow = glutCreateWindow(mainWindowTitle);

// Setting display mode.
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);

// Setting Opengl callbacks.
glutDisplayFunc(cbDisplay);
glutReshapeFunc(cbReshape);

setupRC();

glutMainLoop();

return (0);
}

Bob
10-10-2003, 07:29 AM
You don't enable depth test.




glEnable(GL_DEPTH);

If you try to enable it here, you will only generate an error, since GL_DEPTH is not a valid parameter to glEnable. You probably meant to pass GL_DEPTH_TEST instead.

pmatos
10-12-2003, 11:03 AM
Thanks a lot. I did that and the image appeared terribly distorced. With a lot of rendering problems.

Any ideas?

dawn
10-13-2003, 04:37 AM
Maybe u need besides glEnable(GL_DEPTH_TEST) also glDepthFunc(GL_LEQUAL);