座標我想使用glFrustum
的透視投影,並按照我的理解glFrustum
像glOrtho
可以用來修改我們所期望的座標系的實際屏幕座標映射(如在藍皮書讀取)。所以,如果我做glFrustum(-1,1,-1,1,1,1000);
,它改變了座標
左= -1,右= 1,底部= -1,在直角座標的形式頂部= 1。變化與glFrustum
我試圖通過調用下面的函數在我的拉伸方法繪製在該座標系統中的簡單的房間(含2個側壁)和它出來在屏幕上適當地繪製。
void drawRoomWalls(){
//Left wall
glBegin(GL_QUADS);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, -0.4);
glVertex3f(-1.0, -1.0, -0.4);
glVertex3f(-1.0, -1.0, 0.0);
glEnd();
//Right wall
glBegin(GL_QUADS);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(1.0, 1.0, -0.4);
glVertex3f(1.0, -1.0, -0.4);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
}
該函數被稱爲如下:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(1.0, -1.0, -1.0, 1.0, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -1.0);
drawRoomWalls();
隨後,我試圖做離軸投影(通過取鼠標作爲輸入,而不是用戶的頭部)。代碼如下: glMatrixMode(GL_PROJECTION); glPushMatrix(); glMatrixMode(GL_MODELVIEW); glPushMatrix();
double fov, near, far;
double headX, headY, headZ;
float aspectRatio;
near = 0.5f; far = 1000.0f; aspectRatio = ofGetWidth()/ofGetHeight();
fov = tan(DEG_TO_RAD * 30/2); //tan accepts angle in radians. tan of the half of the fov angle
fov = 0.5; //taken constant for now
double msX = (double)ofGetMouseX();
double msY = (double)ofGetMouseY();
double scrWidth = (double)ofGetWidth();
double scrHeight = (double)ofGetHeight();
headX = (msX/scrWidth) - 0.5;
headY = ((scrHeight - msY)/scrHeight) - 0.5;
headZ = -2.0;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(near * (-fov * aspectRatio + headX),
near * (fov * aspectRatio + headX),
near * (-fov + headY),
near * (fov + headY),
near,
far);
leftValue = near * (-fov * aspectRatio + headX); //for printing out on screen
rightValue = near * (fov * aspectRatio + headX); //for printing out on screen
bottomValue = near * (-fov + headY); //for printing out on screen
topValue = near * (fov + headY); //for printing out on screen
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(headX * headZ, headY * headZ, 0, headX * headZ, headY * headZ, -1, 0, 1, 0);
glTranslatef(0.0, 0.0, headZ);
drawRoomWalls();
我印刷的leftValue
,rightValue
,bottomValue
,topValue
的值,並且當鼠標在屏幕的中心(在調用glFrustum
看起來像glFrustum(-0.25,0.25,-0.25,0.25,1.0,1000.0)
則根據上面呼叫,我期望的座標系的left=-0.25, right=0.25, bottom=-0.25, top=0.25
和我期待的牆壁消失(因爲它們是在(1.0,1.0,0.0平局)的例子),但牆壁不斷出現在屏幕兩側(與場景歪斜,並在該中心與離軸投影基本相同)爲什麼牆壁仍然位於側面的位置(即使是在你的面前) h座標變成了-0.25,0.25)還是在glFrustum調用中我在這裏丟失了關於座標系的內容?
您的值不能正確。 「left == bottom」和「right == top」意味着'aspectRatio = 1'(因爲'headX'和'headY'爲0)。你把'fov'設置爲'0.5'。這意味着'接近== 0.5'。但是,您將其設置爲「1」。必須有一些不同於你預期的值。如果所有變量都包含正確的值,請檢查。 – 2013-05-13 16:37:02
@NicoSchertler:我將headX和headY設置爲零,並截取了截圖。 - > http://i.imgur.com/dTjfazb.png看上去很好。長寬比確實是一個因爲我沒有做的類型鑄造加倍分裂的寬度/高度。你可以添加這個答案?我會在這些評論中詢問一些後續問題。謝謝 – user1240679 2013-05-13 16:48:39
如果你認爲這是答案,我會這樣做。 – 2013-05-13 16:54:09