gpt4 book ai didi

c++ - 使用 glFrustum 改变坐标

转载 作者:太空狗 更新时间:2023-10-29 21:02:02 24 4
gpt4 key购买 nike

我正在尝试使用 glFrustum 进行透视投影,据我所知,glFrustumglOrtho 可用于修改我们的映射所需的坐标系到实际屏幕坐标(如 Blue book 中所读)。所以,如果我执行
glFrustum(-1,1,-1,1,1,1000);,它会将坐标更改为
笛卡尔坐标形式的left = -1, right = 1, bottom = -1, top = 1。

我尝试通过在我的绘制方法中调用以下函数在此坐标系中绘制一个简单的房间(有 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();


我打印了 leftValuerightValuebottomValuetopValue 的值,当鼠标位于屏幕中央(对 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) 为例)。然而,墙壁不断出现在屏幕的两侧(场景倾斜且位于中心,与离轴投影基本相同)。为什么墙壁仍然位于侧面的位置(即使坐标更改为 -0.25,0.25)或者 glFrustum 调用中我缺少有关坐标系的内容?

最佳答案

我刚刚意识到您还将 headZ 更改为 -2。这就解释了这种行为。

如果相机位于 z = 2 并且垂直半视野为 0.5,则在 z = 0 处您已经看到 1 个单位的正负y 方向。这就是您看到墙壁的原因。

Visualization

关于c++ - 使用 glFrustum 改变坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16521731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com