PDA

View Full Version : Shoe carton face problem



Penman
11-23-2013, 05:06 AM
Hello guys,
I'm new to openGL, so don't hit me.
I'm trying to draw a shoe carton where i want to put in things like cubes.

I'm drawing it with simple vertices, but when I rotate my view, it behaves weird.

You might understand my problem better with some stuff for your eyes:

abload.de/image.php?img=boxqcq0z.png
1. So this is the box. Its simple: 5 faces, 20 vertices, 5 colours. It looks quite how I want - from that perspective.
2. So let's move a bit around - oh no, what is this? We just moved some steps right and suddenly the red face (back side) is drawn over the green face (right side)! It's an impossible model now!
3. Back to our origin position from picture 1, we moved some steps down the y-axis, but what is that? The box's floor is now a cover!

And this is the code, producing this weird stuff


#define UPDATE_INTERVAL 16
float rotX = 0;
float rotY = 0;

void Box(GLfloat fSeitenL)
{
float alpha = 1.0f;
float xLeft = -fSeitenL / 2.0f;
float xRight = fSeitenL / 2.0f;
float yBottom = -fSeitenL / 2.0f;
float yTop = fSeitenL / 2.0f;
float zBack = -fSeitenL / 2.0f;
float zFront = fSeitenL / 2.0f;

// Floor
glColor4f(1.0, 1.0, 1.0, alpha);
glBegin(GL_POLYGON);
glVertex3f(xLeft, yBottom, zBack);
glVertex3f(xRight, yBottom, zBack);
glVertex3f(xRight, yBottom, zFront);
glVertex3f(xLeft, yBottom, zFront);
glEnd();

// Quarter Height
yTop = -fSeitenL / 4.0;

// Right side
glBegin(GL_POLYGON);
glColor4f(0.0, 1.0, 0.0, alpha);
glVertex3f(xRight, yBottom, zFront);
glVertex3f(xRight, yBottom, zBack);
glVertex3f(xRight, yTop, zBack);
glVertex3f(xRight, yTop, zFront);
glEnd();

// back side
glBegin(GL_POLYGON);
glColor4f(1.0, 0.0, 0.0, alpha);
glVertex3f(xRight, yTop, zBack);
glVertex3f(xRight, yBottom, zBack);
glVertex3f(xLeft, yBottom, zBack);
glVertex3f(xLeft, yTop, zBack);
glEnd();

// left side
glBegin(GL_POLYGON);
glColor4f(0.0, 0.0, 1.0, alpha);
glVertex3f(xLeft, yTop, zBack);
glVertex3f(xLeft, yBottom, zBack);
glVertex3f(xLeft, yBottom, zFront);
glVertex3f(xLeft, yTop, zFront);
glEnd();

// front side
glBegin(GL_POLYGON);
glColor4f(1.0, 1.0, 0, alpha);
glVertex3f(xLeft, yBottom, zFront);
glVertex3f(xRight, yBottom, zFront);
glVertex3f(xRight, yTop, zFront);
glVertex3f(xLeft, yTop, zFront);
glEnd();
}

void OnDraw(){
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, 640 / 480, 1.0, 16.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(rotX, rotY, 2.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
Box(1);
glutSwapBuffers();
}

void OnUpdate(int value){
glutPostRedisplay();
glutTimerFunc(UPDATE_INTERVAL, OnUpdate, 0);
}

int _tmain(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GL_DEPTH_BUFFER_BIT | GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow(WINDOW_TITLE);
glutDisplayFunc(OnDraw);
glutTimerFunc(UPDATE_INTERVAL, OnUpdate, 0);
glutMainLoop();
return EXIT_SUCCESS;
}


So what is the problem and how can I solve it?

Aleksandar
11-23-2013, 06:22 AM
Add glEnable(GL_DEPTH_TEST) somewhere in the initialization code (some PrepareScene() function), or in OnDraw() for the first help. ;)