我有一个简单的 OpenGL 程序,它按如下方式设置相机:
void
SimRenderer::render() {
glDepthMask(true);
glClearColor(0.5f, 0.5f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glFrontFace(GL_CW);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 cameraTransformation;
cameraTransformation.rotate(mAlpha, 0, 1, 0); // mAlpha = 25
cameraTransformation.rotate(mBeta, 1, 0, 0); // mBeta = 25
QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, mDistance);
QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
mProgram.bind();
mProgram.setUniformValue(mMatrixUniformLoc, mProjMatrix * vMatrix * mMatrix );
// render a grid....
}
但结果是一个倒置的相机!
!1
当我将视图矩阵更改为: QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, -1, 0);
有用 !但是,当我的实际向上方向为正 Y 时,为什么我需要将向上方向设置为负 Y 呢?
在这里完成课程:https ://code.google.com/p/rapid-concepts/source/browse/trunk/simviewer/simrenderer.cpp
其他信息:我正在渲染到 QQuickFramebufferObject,它在调用渲染函数之前将 FBO 绑定到小部件表面。不要认为这将是一个问题,但无论如何。这根本不是纹理问题,没有要翻转的纹理等。似乎相机正在以相反的方式解释向上的方向!
http://doc.qt.digia.com/qt-maemo/qmatrix4x4.html#lookAt
更新 :
因此,由于同时使用 lookat 和 cameraTransformations 可能不起作用,我正在尝试:
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 cameraTransformation;
cameraTransformation.rotate(mAlpha, 0, 1, 0); // 25
cameraTransformation.rotate(mBeta, 1, 0, 0); // 25
cameraTransformation.translate(0, 0, mDistance);
vMatrix = cameraTransformation.inverted();
这会产生完全相同的结果:)
我认为需要以某种方式考虑相机上轴。