-
OpenGL Transformations
I've managed to load an image and texture map it. I am now trying to rotate this object. When I rotate it, it looks funny and I'm not sure why.
Here are the relevant lines of code:
Code:
void initGL(void){
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,640,480,0,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void drawBox(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D,tname);
glPushMatrix();
glRotatef(rcubex,1.0f,0.0f,0.0f);
// glRotatef(rcubey,0.0f,1.0f,0.0f);
// glRotatef(rcubez,0.0f,0.0f,1.0f);
glBegin(GL_QUADS);
//front
glTexCoord2f(0,0);
glVertex3f(270,190,0);
glTexCoord2f(1,0);
glVertex3f(370,190,0);
glTexCoord2f(1,1);
glVertex3f(370,290,0);
glTexCoord2f(0,1);
glVertex3f(270,290,0);
//back
glTexCoord2f(0,0);
glVertex3f(270,190,100);
glTexCoord2f(1,0);
glVertex3f(370,190,100);
glTexCoord2f(1,1);
glVertex3f(370,290,100);
glTexCoord2f(0,1);
glVertex3f(270,290,100);
//right
glTexCoord2f(0,0);
glVertex3f(370,190,0);
glTexCoord2f(1,0);
glVertex3f(370,190,100);
glTexCoord2f(1,1);
glVertex3f(370,290,100);
glTexCoord2f(0,1);
glVertex3f(370,290,0);
glEnd();
glPopMatrix();
rcubex+= 0.3f;
rcubey+= 0.2f;
rcubez+= 0.4f;
}
I think it might have to do with what is in initGL()
-
Could you be more specific than "funny"? Blurry textures? Wrong colors? Wrong shape? Missing faces?
-
I noticed you never set your prespective, a la glFrustrum or equivalent.
-
Ah, I see it now.
There is a call to set the projection
Code:
glOrtho(0,640,480,0,-1,1);
but znear and zfar are way too small to show the whole object; you'll only see the small slice that happens to be between -1 and 1.
-
...and that will only work if your entire object lies between -1 and 1. This will work fine for 2D where everything can be at z = 0.0f or z = 1.0f and objects are normally on the x/y plane but it will not work for anything else.