[英]Change of coordinates with glFrustum
I am trying to use glFrustum
for a perspective projection and as I understand glFrustum
like glOrtho
can be used to modify the mapping of our desired coordinate system to real screen coordinates (as read in Blue book). 我试图用glFrustum
的透视投影,并按照我的理解glFrustum
像glOrtho
可以用来修改我们所期望的坐标系的实际屏幕坐标映射(如在蓝皮书读取)。 So, if I do 所以,如果我这样做
glFrustum(-1,1,-1,1,1,1000);
, it changes the coordinates to ,它将坐标更改为
left = -1, right = 1, bottom = -1, top = 1 in the form of cartesian coordinates. left = -1,right = 1,bottom = -1,top = 1,采用笛卡尔坐标的形式。
I tried to draw a simple room (with 2 side walls) in this coordinate system by calling the following function in my draw method and it comes out drawing appropriately on the screen. 我尝试通过在我的绘图方法中调用以下函数在此坐标系中绘制一个简单的房间(带有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();
}
The function was called as follows: 该函数调用如下:
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();
Subsequently, I tried to do an off-axis projection (by taking mouse as input instead of user's head). 随后,我尝试进行离轴投影(通过将鼠标作为输入而不是用户的头部)。 The code is as follows: glMatrixMode(GL_PROJECTION); 代码如下:glMatrixMode(GL_PROJECTION); glPushMatrix(); glPushMatrix(); glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW); glPushMatrix(); 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();
I printed the values of leftValue
, rightValue
, bottomValue
, topValue
and when the mouse is in the center of the screen (the call to glFrustum
looks like glFrustum(-0.25,0.25,-0.25,0.25,1.0,1000.0)
我打印了leftValue
, rightValue
, bottomValue
, topValue
以及当鼠标位于屏幕中心时(对glFrustum
的调用看起来像glFrustum(-0.25,0.25,-0.25,0.25,1.0,1000.0)
As per the above call, I expected the coordinate system's left=-0.25, right=0.25, bottom=-0.25, top=0.25
and I was expecting the walls to disappear (since they are draw at (1.0,1.0,0.0) for an example). 根据上面的调用,我预计坐标系的left=-0.25, right=0.25, bottom=-0.25, top=0.25
,我期待墙壁消失(因为它们是在(1.0,1.0,0.0)绘制的一个例子)。 However, the walls keep on appearing on the sides of the screen (with the scene being skewed and at the center being essentially the same as with off-axis projection). 然而,墙壁一直出现在屏幕的两侧(场景歪斜,中心与离轴投影基本相同)。 Why is it that the walls are still at the place on the sides (even though the coordinates changed to -0.25,0.25) or there's something in glFrustum call that I am missing here about the coordinate system? 为什么墙壁仍然在两侧的位置(即使坐标变为-0.25,0.25)或glFrustum调用中有什么东西我在这里缺少关于坐标系的东西?
I just realized that you also changed headZ
to -2
. 我才意识到你也将headZ
改为-2
。 That explains the behaviour. 这解释了这种行为。
If the camera is situated at z = 2
and you have a vertical half fov of 0.5, then at z = 0
you already see 1 unit in positive and negative y-direction. 如果摄像机位于z = 2
并且您的垂直半fov为0.5,则在z = 0
您已经看到正方向和负方向上的1个单位。 That's why you see the walls. 这就是你看到墙壁的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.