PDA

View Full Version : Cube rotation question

jaysocrates
06-24-2004, 08:33 PM
Sorry, this is probably a dumb question but I just started w/ openGL this week!

I'm trying to draw a cube and rotate it in various directions responding to keyboard input. The problem is that the surfaces don't seem to display correctly--eg, when I rotate, surfaces appear and disappear in ways I don't expect from a rotating cube. (But I tried using GL_LINES instead of GL_QUADS to draw an "empty" cube and things looked ok...)

I was thinking maybe the problem was the order I was drawing the squares--does this matter at all, or is it another problem? Here's my code for the drawing function:

void DrawCube(HDC hDC,float x,float y,float z,float len)
{
clear(); //clears buffer
glPushMatrix();
glRotatef(rotate[0].angle,1.0f,0,0); //x,y,z rotations
glRotatef(rotate[1].angle,0,1.0f,0);
glRotatef(rotate[2].angle,0,0,1.0f);
//front face:
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(x, y, z);
glVertex3f(x+len, y, z);
glVertex3f(x+len, y-len, z);
glVertex3f(x, y-len, z);

//back face:
glColor3f(0.5f,0.5f,0.0f);
glVertex3f(x, y, z-1);
glVertex3f(x+len, y, z-1);
glVertex3f(x+len, y-len, z-1);
glVertex3f(x, y-len, z-1);

//left face:
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(x,y,z-1);
glVertex3f(x,y-len,z-1);
glVertex3f(x,y-len,z);
glVertex3f(x,y,z);

//right face:
glColor3f(0.0f,0.5f,0.5f);
glVertex3f(x+len,y,z);
glVertex3f(x+len,y,z-1);
glVertex3f(x+len,y-len,z-1);
glVertex3f(x+len,y-len,z);

//top face:
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(x,y,z);
glVertex3f(x,y,z-1);
glVertex3f(x+len,y,z-1);
glVertex3f(x+len,y,z);

//bottom face:
glColor3f(0.5f,0.0f,0.5f);
glVertex3f(x,y-len,z);
glVertex3f(x,y-len,z-1);
glVertex3f(x+len,y-len,z-1);
glVertex3f(x+len,y-len,z);
glEnd();
glPopMatrix();
SwapBuffers(hDC);
}

Thanks,
Jay

mikael_aronsson
06-24-2004, 10:22 PM
Hi !

glPolygonMode defines if it render both sides of a polygon, if you have set this to only render GL_FRONT facing polygons and you manage to get the vertices in wrong order then those polygons will not be visible.

Try to set glPolygonMode( GL_FRONT_AND_BACK, GL_FILL); and see if that helps, if it does, then check your polygons.

It can also be the normals, if you get the normals facing in the wrong direction you can get some weird things going on.

Mikael

brucemangy
06-25-2004, 02:48 AM
Are you sure you are using the depth buffer ?
if you don't, the wireframe should be correct but in solid, the faces are drawn in a precise order so the last square cover the others.

clear(); == glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ?

//clears buffer (no s ? -> buffers ^^ )

is GL_DEPTH_TEST enabled ?

--
brucemangy

plasmonster
06-25-2004, 02:50 AM
Also, you can tell the GL which winding is front facing with this:
glFrontFace( GL_CW or GL_CCW );

To select which side to cull, use this:
glCullFace( GL_FRONT or GL_BACK );

You enable/disable culling with GL_CULL_FACE.

Say you want a clockwise vertex winding to face the camera, and you want to cull back faces. You might do this:

glFrontFace( GL_CW );
glCullFace( GL_BACK );
glEnable( GL_CULL_FACE );

or equivalently

glFrontFace( GL_CCW );
glCullFace( GL_FRONT );
glEnable( GL_CULL_FACE );

jaysocrates
06-25-2004, 10:39 AM
Aaah, I didn't have GL_DEPTH_TEST enabled. Thanks, that fixed it perfectly.

(Thanks to the other responders as well--I'm sure they were good suggestions but I'm not there yet...:-))

Originally posted by brucemangy:
Are you sure you are using the depth buffer ?
if you don't, the wireframe should be correct but in solid, the faces are drawn in a precise order so the last square cover the others.

clear(); == glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ?

//clears buffer (no s ? -> buffers ^^ )

is GL_DEPTH_TEST enabled ?

--
brucemangy

jaysocrates
06-25-2004, 10:24 PM

I tried texture-mapping one side of the cube, and that worked fine--except that now the other sides of the cube have screwed-up colors. Then when I rotate, the texture itself becomes discolored (the other sides still don't have the right colors). I haven't gotten to using lighting yet so I don't think that's the problem...

ZbuffeR
06-26-2004, 04:31 AM
If your want to texture only a part of the geometry, be sure to :

1) glEnable(GL_TEXTURE_2D) , then specify texcoords (or use texgen) for each vertex of the textured geometry
2) glDisable(GL_TEXTURE_2D) before draw not textured geometry

jaysocrates
06-26-2004, 09:12 AM
I tried that, but it doesn't have any effect. Maybe I have it in the wrong place? In the function I have:

glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(...);
glBegin() [...draw textured side...]
glEnd();
glDisable(GL_TEXTURE_2D);
glBegin()[...draw untextured...]
glEnd();
glPopMatrix();
SwapBuffers();

It's like the texture is picking up color info from the other parts of the cube, even though I clear and rebind every time the function is called...?

jaysocrates
06-26-2004, 07:54 PM
When I put a texture on another face of the cube I'm drawing, it picks up a different color from one of the other sides of the cube--so I can change the tint of the texture by changing the color of a different face of the cube. Neat, but not what I want to do! Any ideas why this is happening?

brucemangy
06-27-2004, 12:03 AM

http://gorre.nerim.net/TexturedCube.rar

use 1 2 3 4 5 6 (not on keypad) to Enable/Disable the texturing of one face.
(action on Key Release)

(thx to nehe for the template and the drawing of the cube ^^)

--
brucemangy

jaysocrates
06-28-2004, 07:58 PM
Thanks, but that doesn't actually answer my question...I don't have a problem texturing an entire cube--the problem is, when I texture *part* of a cube, the texture gets tinted by whatever the most recent glColor call was...I can do the same thing in your program...in your function drawcube(), around line 150, replace the first part of the fn (that draws the front face of the cube) with this:

//tex_face(1); //don't texture
// Front Face
glColor3f(1.0,0,0);
glNormal3f( 0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();

So I took out the glTexture functions, didn't call tex_face(), and instead just colored the face red. I left all the other sides of the cube as-is (textured). If you run it now, you'll see that all the other textures are tinted red. I tried calling glclear and glclear color at various points and that doesn't help. Any other ideas?

Thanks,
Jay

Originally posted by brucemangy:

http://gorre.nerim.net/TexturedCube.rar

use 1 2 3 4 5 6 (not on keypad) to Enable/Disable the texturing of one face.
(action on Key Release)

(thx to nehe for the template and the drawing of the cube ^^)

--
brucemangy

yaooo
06-28-2004, 11:12 PM
:) That's not a problem. If you define the color to white before render texture, it will look fine. Because you didn't enable any light, so the color of the face is the mixture of the texture color and the current defined color. If you enable light, the defined color will not affect at all.

jaysocrates
06-29-2004, 06:40 AM
Aaaah, perfect! Thanks much!

Originally posted by yaooo:
:) That's not a problem. If you define the color to white before render texture, it will look fine. Because you didn't enable any light, so the color of the face is the mixture of the texture color and the current defined color. If you enable light, the defined color will not affect at all.

jabe
06-29-2004, 08:48 AM
instead of setting the color to white, you could set the texture environment mode to REPLACE. it is set to MODULATE as default.
the texture enironment defines the behaviour of texturing. when MODULATE is enabled, the color value from the texture is modulated (mixed) with the color value you set by calling glColor.
when REPLACE is set, the color value from the texture is used, nothing else.
you can do it by calling

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

before drawing the cubes side. the color from the glColor call is ignored then. after disabling texturing it will be used again.