簡體   English   中英

在OpenGL中移動相機時顯示錯誤

[英]Display error when moving the camera in opengl

我的項目中有一個問題,當我將相機移動到靠近圖像的位置時,這對我來說是完美的,但是就我移動或旋轉相機而言,這給了我這樣的錯誤。 我不知道我的錯誤在哪里。 我該如何糾正?

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

碼:

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;
}
   float DefaultFus=0.001f;
   glFrustum (-DefaultFus, DefaultFus, -DefaultFus, DefaultFus, DefaultFus,SIZE_KHUNG_CANH*3 );

那就是您的問題所在:與場景的比例相比,近裁剪位置的距離非常小。 但是,“場景比例” /“近片段”比率越大,有效的Z緩沖區分辨率越低。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM