Hi Guys, Could you help me out with this problem here? I’ve tried to create a simple objects (a fan made from blocks) to help me understand the transformation matrix. But, it’s got a problem that whenever I move or rotate the camera or objects , the objects that is closer to us (viewpoint) are overlapped by the objects farther from the viewpoint. How could this be happening anyway? I’ve been trying to figure it out for days, but still can’t find a clue. Please help me ( I’m using Visual C++ 6 for the code).
//-------------Cut here------------------//
#include <gl/glut.h>
int w_rotx,w_roty,w_rotz, // world rotation
w_transz;
float camx,camy,camz; // camera position to current object
void drawCube(void){
static GLfloat cube[]={
1.0,0.0,0.0, -1.0,-1.0, 1.0, //front vertexes
0.0,1.0,0.0, 1.0,-1.0, 1.0,
0.0,0.0,1.0, 1.0, 1.0, 1.0,
1.0,1.0,0.0, -1.0, 1.0, 1.0,
0.0,1.0,1.0, -1.0,-1.0,-1.0, //back vertexes
1.0,0.0,1.0, 1.0,-1.0,-1.0,
0.5,0.2,0.0, 1.0, 1.0,-1.0,
0.5,1.0,0.2, -1.0, 1.0,-1.0
};
static GLuint frontindices[] ={0,1,2,3};
static GLuint backindices[] ={7,6,5,4};// counterclockwise (back's front face)
static GLuint leftindices[] ={0,3,7,4};
static GLuint rightindices[] ={2,1,5,6};
static GLuint topindices[] ={3,2,6,7};
static GLuint bottomindices[]={4,5,1,0};// counterclockwise (bottom's front face)
glInterleavedArrays(GL_C3F_V3F,0,cube);
glDrawElements(GL_QUADS,4,GL_UNSIGNED_INT,frontindices);
glDrawElements(GL_QUADS,4,GL_UNSIGNED_INT,backindices);
glDrawElements(GL_QUADS,4,GL_UNSIGNED_INT,leftindices);
glDrawElements(GL_QUADS,4,GL_UNSIGNED_INT,rightindices);
glDrawElements(GL_QUADS,4,GL_UNSIGNED_INT,topindices);
glDrawElements(GL_QUADS,4,GL_UNSIGNED_INT,bottomindices);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
}
void init(void){
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_SMOOTH);
camx=-45.0;camy=90.0;camz=0.0; //set so you can see what is wrong
w_transz=-10.0;
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(w_rotx, 1.0f, 0.0f, 0.0f); //rotate world
glRotatef(w_roty, 0.0f, 1.0f, 0.0f);
glRotatef(w_rotz, 0.0f, 0.0f, 1.0f);
glTranslatef( 0,0, w_transz); //Translate world
// Create the fan
glPushMatrix();
//modeling transformation for the objects
glTranslatef(0.0,0.0,-5.0);
glRotatef(camx,0.0,1.0,0.0);
glRotatef(camy,1.0,0.0,0.0);
glRotatef(camz,0.0,0.0,1.0);
//center of the fan
drawCube();
for (int count=4,int i=0;i<count;i++){
glPushMatrix();
//draw hands relative to the center
glRotatef(i*360/count,0.0,0.0,1.0);
glTranslatef(6.0,0.0,0.0);
glScalef(3.0,1.0,1.0);
drawCube();
glPopMatrix();
}
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void keycheck(unsigned char key,int x,int y){
switch(key){
case 'w':camy-=1.0;break; //Pitch
case 's':camy+=1.0;break;
case 'a':camx-=1.0;break; //Yawn
case 'd':camx+=1.0;break;
case '8':camz+=1.0;break; //Roll
case '5':camz-=1.0;break;
//rotate world
case 'o':w_rotx-=1;break; //camera look up
case 'l':w_rotx+=1;break; //camera look down
case 'k':w_roty-=1;break; //camera look left
case ';':w_roty+=1;break; //camera look right
case 'j':w_transz+=1;break; //zoom forward
case 'm':w_transz-=1;break; //zoom backward
}
w_rotx%=360;
w_roty%=360;
glutPostRedisplay();
}
void reshape(int w,int h){
glViewport(0,0,(GLsizei) w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keycheck);
glutMainLoop();
return 0;
}