2013-01-24 78 views
2

當我試圖將座標系統的座標軸渲染到場景中時,我當前正面臨一些透視問題。對於這些軸,我繪製了三條穿過我的3D立方體中心的正交線。 這個問題很難解釋,所以我想最有說服力的方式就是發表一些圖片。OpenGL中的3D座標系統的透視問題

1)上的整個場景視圖:click here

2)鑑於放大上的座標系的原點:click here

3)當我在一個很小的點點放大進一步,兩個軸消失,另一個似乎由於某種原因被移位:click here

爲什麼會發生這種情況,我該如何預防它?

我的模型視圖和投影矩陣看以下幾點:

// Set ProjectionMatrix 
projectionMatrix = glm::perspective(90.0f, (GLfloat)width/(GLfloat) height, 0.0001f, 1000.f); 
glBindBuffer(GL_UNIFORM_BUFFER, globalMatricesUBO); 
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(projectionMatrix)); 
glBindBuffer(GL_UNIFORM_BUFFER, 0); 

// Set ModelViewMatrix 
glm::mat4 identity  = glm::mat4(1.0); // Start with the identity as the transformation matrix 
glm::mat4 pointTranslateZ = glm::translate(identity, glm::vec3(0.0f, 0.0f, -translate_z)); // Zoom in or out by translating in z-direction based on user input 
glm::mat4 viewRotateX  = glm::rotate(pointTranslateZ, rotate_x, glm::vec3(1.0f, 0.0f, 0.0f)); // Rotate the whole szene in x-direction based on user input 
glm::mat4 viewRotateY  = glm::rotate(viewRotateX, rotate_y, glm::vec3(0.0f, 1.0f, 0.0f)); // Rotate the whole szene in y-direction based on user input 
glm::mat4 pointRotateX = glm::rotate(viewRotateY, -90.0f, glm::vec3(1.0f, 0.0f, 0.0f)); // Rotate the camera by 90 degrees in negative x-direction to get a frontal look on the szene 
glm::mat4 viewTranslate = glm::translate(pointRotateX, glm::vec3(-dimensionX/2.0f, -dimensionY/2.0f, -dimensionZ/2.0f)); // Translate the origin to be the center of the cube 
+2

Frustrum撲殺? – Alexander

+0

這將解釋爲什麼2軸消失,對吧?但是當我放大一點點時,爲什麼第三張圖像中的另一張會完全移位? – Schnigges

+1

也許它的一部分被渲染,並且你看到由於接近而導致的運動不自然。 – Alexander

回答

2

這就是所謂的 「剪裁」。該軸正在碰撞近夾平面並因此被裁剪。第三軸不「移位」;它只是部分剪裁。拍攝第二張照片並掩蓋大部分內容,以便只看到對角軸的一部分;這就是你所得到的。

有幾個通用的解決方案。首先,你可能不允許用戶放大。或者您可以在相機靠近目標物體時向內調整近夾平面。這也會對遠處的物體造成精度問題,所以你可能也想調整你的遠剪平面。

或者,您可以打開深度鉗位(假設您有GL 3.x +或訪問ARB_depth_clampNV_depth_clamp)。這不是一個完美的解決方案,因爲當它們落在相機後面時它們仍然會被剪輯。如果兩個這樣的對象重疊,那麼與近剪切平面相交的事物將不再具有適當的深度緩衝。但它通常足夠好。