简体   繁体   English

OpenGL:通过给定的4×4矩阵以3D形式更改相机的视口

[英]OpenGL: Change Camera's view port in 3D by given 4-by-4 matrix

I am now trying to change camera's view port under the given 4-by-4 matrix as 我现在正在尝试在给定的4×4矩阵下将相机的视口更改为

R11 R12 R13  transx
R21 R22 R23  transy
R31 R32 R33 transz
0    0    0     1

R is the 3-by-3 rotation matrix while transx, transy, transz are translations along x,y and z axis in 3D space. R是3 x 3旋转矩阵,而transx,transy,transz是3D空间中沿x,y和z轴的平移。

My code is as following 我的代码如下

eyex = transx;
eyey = transy;
eyez = transz;

atx = transx;
aty = transy;
atz = transz+1;

gluLookAt (R11*eyex + R12*eyey + R13*eyez, R21*eyex + R22*eyey + R23*eyez, R31*eyex + R32*eyey + R33*eyez, atx, aty, atz,  0.0, 1.0, 0.0);

However, I can't get the correct result. 但是,我无法获得正确的结果。 (Rotatation seems ok but problems occurs on translation) (旋转似乎可以,但翻译时会出现问题)

Could someone can pick up the error in my code? 有人可以在我的代码中发现错误吗?

Not really sure what you want to do, but you seem to be confusing issues. 不太确定要做什么,但您似乎会感到困惑。 gluLookAt is a function that generate a cam-to-world matrix for you, by passing to the function, the eye position and a point you are looking at. gluLookAt是一个函数,它将传递给您的眼睛位置和您要看的点,从而为您生成一个“凸轮到世界”矩阵。 So in short, it's either you use a gluLooAt to build you matrix, or you build your matrix yourself for the camera, and use it to set the OpenGL cam-to-world matrix. 简而言之,要么是使用gluLooAt来构建矩阵,要么是您自己为相机构建矩阵,然后使用它来设置OpenGL cam-to-world矩阵。

In OpenGL4, this would be done on the CPU (building up that matrix) and then loading it in the vertex shader. 在OpenGL4中,这将在CPU上完成(建立该矩阵),然后将其加载到顶点着色器中。 At the end of you will to transform your object to world space, then transform the vertex in world space in camera space, and finally project using the perspective projection matrix. 最后,将对象转换为世界空间,然后将顶点空间转换为摄影机空间,最后使用透视投影矩阵进行投影。

You can find some info on this website: www.scratchapixel.com 您可以在以下网站上找到一些信息:www.scratchapixel.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM