2012-10-25 40 views
2

我的項目中有一個問題,當我將相機移動到圖像附近的位置對我而言是完美的,但是至於移動或旋轉相機,它會給我帶來像這個。 我不知道我的錯誤在哪裏。我該如何解決這個問題?在opengl中移動相機時顯示錯誤

http://nr7.upanh.com/b5.s32.d2/b7f577d38c0b23cbb35ca367e253b716_50312137.orr.png http://nr9.upanh.com/b2.s11.d1/0be16f4300e3a84aba3a56e5e2bf008d_50312139.perfec.png

代碼:

ButtonEndRollVV KeyPessUp; 
VeKhungCanh Draw; 
GLint ListDraw[100]; 


void glintIn() 
{ 
    glutWarpPointer(400,300); 
    KeyPessUp.lastx=400; 
    KeyPessUp.lasty=300; 
    KeyPessUp.xrot=ROLL_X; 
    KeyPessUp.yrot=ROLL_Y; 
    KeyPessUp.xpos=-DEFAUTCAMERAX; 
    KeyPessUp.ypos=-DEFAUTCAMERAY; 
    KeyPessUp.zpos=-DEFAUTCAMERAZ; 
/////////////////////////////////set defual///////////////////////////////////// 
    glShadeModel(GL_SMOOTH); 
    glEnable(GL_DEPTH_TEST);// Enables Depth Testing 
    glClearDepth(1.0f);       // Depth Buffer Setup       
    glDepthFunc(GL_LEQUAL);        // The Type Of Depth Testing To Do 
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
    glClearStencil(0);//gia phong bo dem dat no bang 0 
    glEnable(GL_STENCIL_TEST);//kich hoat bo dem stencil 
////////////////////////////////create list grid/////////////////////////////// 
    ListDraw[0]=glGenLists(1); 
    glNewList(ListDraw[0],GL_COMPILE); 
    { 
     Draw.CreateKhongGian(); 
    } 
    glEndList(); 
/////////////////////////////////create cong hang hai////////////////////////// 
    ListDraw[1]=glGenLists(1); 
    glNewList(ListDraw[1],GL_COMPILE); 
    { 
     Draw.CreateCongHangHai(); 
    } 
    glEndList(); 
/////////////////////////////create hang giao///////////////////////////////// 
    ListDraw[2]=glGenLists(1); 
    glNewList(ListDraw[2],GL_COMPILE); 
    { 
     Draw.CreateHangGiao(); 
    } 
    glEndList(); 
//////////////////////////////////anh sang va thong so vat lieu//////////////////////////// 

    float Al[4] = {0.6f, 0.6f, 0.6f, 1.0f }; 
    glLightfv(GL_LIGHT0, GL_AMBIENT, Al); 

    float Dl[4] = {1.0f, 1.0f, 1.0f, 1.0f }; 
    glLightfv(GL_LIGHT0, GL_DIFFUSE, Dl); 

    float Sl[4] = {1.0f, 1.0f, 1.0f, 1.0f }; 
    glLightfv(GL_LIGHT0, GL_SPECULAR, Sl); 

     float Am[4] = {0.6f,0.6f, 0.6f, 1.0f }; 
    glMaterialfv(GL_FRONT, GL_AMBIENT, Am); 

    float Dm[4] = {1.0f, 1.0f, 1.0f, 1.0f }; 
    glMaterialfv(GL_FRONT, GL_DIFFUSE, Dm); 

    float Sm[4] = {1.0f, 1.0f, 1.0f, 1.0f }; 
    glMaterialfv(GL_FRONT, GL_SPECULAR, Sm); 

    glMaterialf(GL_FRONT, GL_SHININESS, 45); 
    glColorMaterial(GL_FRONT, GL_DIFFUSE);//bo cai nay neu bumapping 
    glEnable(GL_COLOR_MATERIAL); 

    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
} 
void display() 
{ 
    glClearColor(0,0,0,0); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
    glLoadIdentity(); 
    glPushMatrix(); 


    glRotatef(KeyPessUp.xrot,1.0,0.0,0.0); 
    glRotatef(KeyPessUp.yrot,0.0,1.0,0.0); 
    glTranslatef(KeyPessUp.xpos,KeyPessUp.ypos,KeyPessUp.zpos); 
    glCallList(ListDraw[0]);//draw grid 
    glColor3f(1,1,0); 
    glCallList(ListDraw[1]);//draw Cong Hang Hai pt parabol: 
    glCallList(ListDraw[2]);//draw Hang Giao 


    glPopMatrix(); 

    glFinish(); 
    glutSwapBuffers(); 
} 
void resize(int w,int h) 
{ 
     glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
     glMatrixMode (GL_PROJECTION); 
     glLoadIdentity(); 
     //SIZE_KHUNG_CANH*2 
     float DefaultFus=0.001f; 
     glFrustum (-DefaultFus, DefaultFus, -DefaultFus, DefaultFus, DefaultFus,SIZE_KHUNG_CANH*3); 
     glMatrixMode (GL_MODELVIEW);   
     glutPostRedisplay(); 
     KeyPessUp.sizeH=h; 
     KeyPessUp.sizeW=h; 
} 
void idle() 
{ 
    if(KeyPessUp.keyStates[GLUT_KEY_UP]==true) 
    { 
      float xrotrad, yrotrad; 
      yrotrad = (KeyPessUp.yrot/180 * pi); 
      xrotrad = (KeyPessUp.xrot/180 * pi); 
      KeyPessUp.xpos -= float(sin(yrotrad)/SPEED) ; 
      KeyPessUp.zpos += float(cos(yrotrad)/SPEED) ; 
      if(DISABLEUPDOWNCAMERA==0) 
       KeyPessUp.ypos += float(sin(xrotrad)/SPEED) ;//bỏ đi nếu nhập vai 
    } 
    if(KeyPessUp.keyStates[GLUT_KEY_LEFT]==true) 
    { 
      float xrotrad, yrotrad; 
      yrotrad = ((KeyPessUp.yrot+90)/180 * pi); 
      xrotrad = ((KeyPessUp.xrot)/180 * pi); 
      KeyPessUp.xpos += float(sin(yrotrad)/SPEED) ; 
      KeyPessUp.zpos -= float(cos(yrotrad)/SPEED) ; 
      if(DISABLEUPDOWNCAMERA==0) 
       KeyPessUp.ypos -= float(sin(xrotrad)/SPEED) ;//bỏ đi nếu nhập vai 
    } 
    if(KeyPessUp.keyStates[GLUT_KEY_DOWN]==true) 
    { 

      float xrotrad, yrotrad; 
      yrotrad = (KeyPessUp.yrot/180 * pi); 
      xrotrad = (KeyPessUp.xrot/180 * pi); 
      KeyPessUp.xpos += float(sin(yrotrad)/SPEED); 
      KeyPessUp.zpos -= float(cos(yrotrad)/SPEED) ; 
      if(DISABLEUPDOWNCAMERA==0) 
       KeyPessUp.ypos -= float(sin(xrotrad)/SPEED); //bỏ đi nếu nhập vai 
    } 
    if(KeyPessUp.keyStates[GLUT_KEY_RIGHT]==true) 
    { 
     float xrotrad, yrotrad; 
      yrotrad = ((KeyPessUp.yrot+90)/180 * pi); 
      xrotrad = ((KeyPessUp.xrot)/180 * pi); 
      KeyPessUp.xpos -= float(sin(yrotrad)/SPEED); 
      KeyPessUp.zpos += float(cos(yrotrad)/SPEED) ; 
      if(DISABLEUPDOWNCAMERA==0) 
       KeyPessUp.ypos += float(sin(xrotrad)/SPEED);//bỏ đi nếu nhập vai 
    } 
    glutPostRedisplay(); 
} 
void KeyPess(int Key,int x,int y) 
{ 
    if(KeyPessUp.acted==0) 
    { 
     glutIdleFunc(idle); 
     KeyPessUp.acted=1; 
    } 
    KeyPessUp.keyStates[Key]=true; 
} 
void KeyUp(int Key,int x,int y) 
{ 
    int check=0; 
    KeyPessUp.keyStates[Key]=false; 
    for(int i=0;i<255;i++) 
    { 
     if(KeyPessUp.keyStates[i]==true) 
     { 
      check=1; 
     } 
    } 
    if(KeyPessUp.acted==1&&check==0) 
    { 
     glutIdleFunc(NULL); 
     KeyPessUp.acted=0; 
    } 


} 
void KeyExit(unsigned char Key,int x,int y) 
{ 
    if(Key==27) 
    { 
     exit(0); 
    } 
} 
void MouseMove(int x,int y) 
{ 
    int xpoint=x-KeyPessUp.lastx; 
    int ypoint=y-KeyPessUp.lasty; 
    KeyPessUp.lastx=x; 
    KeyPessUp.lasty=y; 
    KeyPessUp.yrot += xpoint*.3; 
    KeyPessUp.xrot += ypoint*.3; 
    if (KeyPessUp.xrot >=89) KeyPessUp.xrot = 89; 
    if (KeyPessUp.xrot <= -89) KeyPessUp.xrot =-89; 
    if (KeyPessUp.yrot >360) KeyPessUp.yrot -= 360; 
    if (KeyPessUp.yrot < -360)KeyPessUp.yrot += 360; 
    glutPostRedisplay(); 
    if(x>450 || x<350 ||y>350 || y<250) 
    { 
     glutWarpPointer(400,300); 
     KeyPessUp.lastx=400; 
     KeyPessUp.lasty=300; 
    } 
} 
int main(int argc,char** argv) 
{ 

    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL); 
    glutInitWindowPosition(100,100); 
    glutInitWindowSize(800,600);  
    glutCreateWindow("De Tai Nghien Cuu Khoa Hoc"); 
    glintIn(); 
    glutDisplayFunc(display); 
    glutReshapeFunc(resize); 
    glutSpecialFunc(KeyPess); 
    glutSpecialUpFunc(KeyUp); 
    glutKeyboardFunc(KeyExit); 
    glutPassiveMotionFunc(MouseMove); 
    glutMotionFunc(MouseMove); 
    glutSetCursor(GLUT_CURSOR_NONE);   
    glewInit(); 
    if(ENDABLE_FULL_SCREEN==TRUE) 
    glutFullScreen(); 
    glutMainLoop(); 
    return 0; 
} 
+0

'SIZE_KHUNG_CANH'的值是多少?在閃爍區有多個共面三角形? – genpfault

+0

所有問題已經解決我設置的glFrustum值太小,我增加了0.05f它效果很好。 –

+0

想知道,你的znear和zfar值是什麼?如果這是問題,我可能會寫一個關於浮點數和深度緩存的答案。編輯:哦,我看到@genpfault正在問同樣的問題。那麼,你的SIZE_KHUNG_CANH值是多少? – Xymostech

回答

0
float DefaultFus=0.001f; 
    glFrustum (-DefaultFus, DefaultFus, -DefaultFus, DefaultFus, DefaultFus,SIZE_KHUNG_CANH*3); 

那是你的問題就在那裏:規模相比現場的近裁剪的地方距離非常小。然而,「場景比例」/「近剪輯」比例越大,則有效的Z緩衝區分辨率越低。

解決方案:選擇近夾平面的距離儘可能大,即儘可能遠。理想情況下,您根據可見場景的內容和視點的位置動態選擇值。

相關問題