PDA

View Full Version : My cube again :)



nergal
10-27-2002, 07:08 AM
I got some pictures of my cube I made, it looks like this, why is it wrong?
First it look like this, when rotating: Cube Pic 1 (http://spectre.swt.nu/cube1.jpg)
Then after rotating some more it looks like this:
Cube Pic 2 (http://spectre.swt.nu/cube2.jpg)
And after even some more rotating like this:
Cube Pic 3 (http://spectre.swt.nu/cube3.jpg)

Why?
My code is like this and last in the code u can see my Perspective:

void createRoom(float langd, float hojd, float bredd, GLuint texture_id)
{
glPushMatrix();
glBindTexture ( GL_TEXTURE_2D, texture_id);
glBegin(GL_QUADS);
/*front*/
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); //bottom left
glTexCoord2f(1.0f, 1.0f);
glVertex3f(langd, 0.0f, 0.0f); //bottom right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(langd, hojd, 0.0f); // top right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, hojd ,0.0f); //top left

/*Back*/
glTexCoord2f(0.0f, 0.0f);
glVertex3f(langd, 0.0f, bredd); //bottom right
glTexCoord2f(1.0f, 0.0f);
glVertex3f(langd, hojd, bredd); //top right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0.0f, hojd, bredd); // top left
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, bredd); //bottom left

/*Top*/
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, hojd, 0.0f); //top left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0.0f, hojd, bredd); //bottom left
glTexCoord2f(1.0f, 1.0f);
glVertex3f(langd, hojd,bredd); //bottom right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(langd, hojd, 0.0f); //top right

/*botten*/
glTexCoord2f(1.0f, 0.0f);
glVertex3f(langd, 0.0f, 0.0f); //top right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f); //top left
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, bredd); //bottom left
glTexCoord2f(0.0f, 0.0f);
glVertex3f(langd, 0.0f, bredd); //bottom right

/*Right*/
glTexCoord2f(1.0f, 0.0f);
glVertex3f(langd, 0.0f, bredd); // bottom right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(langd, hojd, bredd); // top right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(langd, hojd, 0.0f); // top left
glTexCoord2f(0.0f, 0.0f);
glVertex3f(langd, 0.0f, 0.0f); // bottom left

/*Left*/
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); //bottom left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, bredd); //bottom right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0.0f, hojd, bredd); //top right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, hojd, 0.0f); //top left

glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}

----------------------------
Code in my resize function:
-----------------------------
...
gluPerspective(45.0f,(GLfloat) width / (GLfloat) height, 0.01f, 10000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
...
--------------------------------
This is how I create the cube
--------------------------------
createRoom(1.0f,1.0f,1.0f,texture_id[WALL1_TEXTURE]);

Hope to get a answer and if the code are wrong?

Bob
10-27-2002, 07:19 AM
Becuase some sides of the cube has it's vertices winded in the wrong order and you have back face culling enabled.

Take the front and back side of the cube as an example. When looking at the front side of the front face, you will see the vertices in counter clockwise order. This is the default order OpenGL's face culling treats as front face. Now, if you look at the back face, you will see it's vertices is ordered in clockwise order (remember that it's front is facing the oposite direction, so you have to rotate the cube 180 degrees before looking at it's winding order), which is the default order for a back face, and it will be removed if you have back face culling enabled.

nergal
10-27-2002, 08:10 AM
aha, how to disable back face culling than?

Bob
10-27-2002, 09:01 AM
glDisable(GL_CULL_FACE) maybe.