2

我的项目中有一个问题,当我将相机移动到图像附近的位置对我来说是完美的,但就我移动或旋转相机而言,它会给我这样的错误。我不知道我的错误在哪里。我该如何纠正?

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

代码:

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;
}
4

1 回答 1

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

这就是您的问题所在:与场景的比例相比,剪裁附近的距离非常小。然而,“场景比例”/“近剪辑”比率越大,有效的 Z 缓冲区分辨率越低。

解决方法:选择近裁剪平面距离尽可能大,即尽可能远。理想情况下,您可以根据可见场景的内容和视点的位置动态选择值。

于 2012-10-25T18:15:35.797 回答