我正在尝试使用 C++ 为 OpenGL 创建一个简单的 Camera 类。所以它基本上使用了以下功能:
void setModelviewMatrix () {
float m[16];
Vector3 eVec(eye.x, eye.y, eye.z);
m[0]=u.x; m[4]=u.y; m[8]=u.z; m[12]=-eVec.dotProduct(u);
m[1]=v.x; m[5]=v.y; m[9]=v.z; m[13]=-eVec.dotProduct(v);
m[2]=n.x; m[6]=n.y; m[10]=n.z; m[14]=-eVec.dotProduct(n);
m[3]=0; m[7]=0; m[11]=0; m[15]=1.0;
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(m);
}
void set (Point3 newEye, Point3 newLook, Vector3 newUp) {
eye.set(newEye);
n.set(eye.x-newLook.x, eye.y-newLook.y, eye.z-newLook.z);
u.set(newUp.crossProduct(n));
n.normalize();
u.normalize();
v.set(n.crossProduct(u));
setModelviewMatrix();
}
void setShape (float vAng, float asp, float nearD, float farD) {
viewAngle = vAng;
aspect = asp;
nearDist = nearD;
farDist = farD;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(viewAngle, aspect, nearDist, farDist);
}
所以我通过用我的新相机设置(也如下)替换工作标准相机设置(见下文)来测试它。从我的角度来看,新设置与旧设置完全相同。但是,结果仍然不同:在新设置下,我唯一能看到的是空白屏幕,而不是之前显示的对象。这是为什么?
旧相机设置(工作):
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-64/48.0, 64/48.0, -1.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.3, 1.3, 2, 0, 0.25, 0, 0, 1, 0);
新设置(不工作):
Point3 eye = Point3(2.3, 1.3, 2);
Point3 look = Point3(0, 0.25, 0);
Vector3 up = Vector3(0, 1, 0);
cam.setShape(30.0f, 64.0f/48.0f, 0.1f, 100.0f);
cam.set(eye, look, up);