2016-09-23 79 views
1

我有立方體,我試圖移動使用glTranslatef()函數。一切都很好,除了cubeb開始在z> 1消失。openGl物體消失在某些zCoord

這裏是我的代碼

void owidget::initializeGL() 
{ 
    glDepthRange(0,100); 
    glEnable(GL_DEPTH_TEST); 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
    glOrtho(-5.0,5.0,-5.0,5.0,-500.0,500.0); 
} 

void owidget::paintGL() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glPushMatrix(); 
     glTranslatef(xPos/100,yPos/100,zPos/100); 
     glRotatef(xRot,1,0,0); 
     glRotatef(yRot,0,1,0); 
     glRotatef(zRot,0,0,1); 
     cube(0.3); 
    glPopMatrix(); //Fuction for dislpaying cube 
} 

void owidget::resizeGL(int width, int height) 
{ 
    glViewport(0,0,width,height); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
} 

Here is a picture

+0

記住,以後一切都被轉化成最終的投影矩陣,事情應該當他們面向Z> 1或Z消失< - 1。對不起,如果你已經知道這一點,但從這個問題不清楚你是否做到了。 – Steven2163712

回答

2

你的設置是有點怪異。您在initializeGL中正確設置了zFar,gluPerspective,但它在paintGL中被glLoadIdentity覆蓋,這會將zFar重置爲默認值,即Normalized Device Coordinates中的1。

如果將glMatrixMode(GL_PROJECTION);更改爲glMatrixMode(GL_MODELVIEW);,則應該起作用。

我也推薦違約glMatrixModeGL_MODELVIEW。我的意思是,只要你完成編輯的投影矩陣,叫glMatrixMode(GL_MODELVIEW);

void owidget::initializeGL() 
{ 
    glDepthRange(0,100); 
    glEnable(GL_DEPTH_TEST); 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
    glMatrixMode(GL_PROJECTION); 
    gluPerspective(0,650/600,20.0,100.0); 
    glMatrixMode(GL_MODELVIEW); 
} 

void owidget::paintGL() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 
    glPushMatrix(); 
     glTranslatef(xPos/100,yPos/100,zPos/100); 
     glRotatef(xRot,1,0,0); 
     glRotatef(yRot,0,1,0); 
     glRotatef(zRot,0,0,1); 
     cube(0.3); 
    glPopMatrix(); //Fuction for dislpaying cube 
} 

void owidget::resizeGL(int width, int height) 
{ 
    glViewport(0,0,width,height); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glMatrixMode(GL_MODELVIEW); 
}