您是否在此 Wiki 页面上获得此代码:http://www.opengl.org/wiki/GluPerspective_code?
为什么会创建 temp、temp2、temp3 和 temp4?
是的,未使用的变量。很可能是从glhFrustumf2 实现中复制而来的。
为什么会有注释代码?我想是指定glFrustum的一些参数。
是的,可能是为了解释传递给函数glhFrustumf2的参数值。
但是,为什么 xmin = -ymax * aspectRatio;而不是 xmin = -max;
是的,xmin = -xmax 是正确的,并且避免了重复。
我通常采用类似的方法:透视是根据截锥体实现的,截锥体本身是通过简单地设置矩阵的所有系数来实现的(这允许隐藏存储格式,即行优先与列优先) .与 OpenGL 手册页中相同的参数命名约定也有助于您以后需要查阅代码。
void
setPerspectiveMatrix4x4f(float fovy, float aspect, float zNear, float zFar, float* m)
{
const float bottom = -zNear * tanf(0.5f * fovy * M_PI / 180.0f);
const float top = -bottom;
const float left = aspect * bottom;
const float right = -left;
setFrustumMatrix4x4f(left, right, bottom, top zNear, zFar, m);
}
void
setFrustumMatrix4x4f(float left, float right, float bottom, float top, float zNear, float zFar, float* m)
{
const float dx = right - left;
const float dy = top - bottom;
const float dz = zFar - zNear;
const float mx = 0.5f * (left + right);
const float my = 0.5f * (bottom + top);
const float mz = 0.5f * (zNear + zFar);
const float n = zNear;
const float nf = zNear * zFar;
setMatrix4x4f(2.0f * n / dx, 0.0f, 2.0f * mx / dx, 0.0f,
0.0f, 2.0f * n / dy, 2.0f * my / dy, 0.0f,
0.0f, 0.0f, -2.0f * mz / dz, -2.0f * nf / dz,
0.0f, 0.0f, -1.0f, 0.0f,
m);
}
void
setMatrix4x4f(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33,
float* m)
{
// Fill m with m00, ... in row-major or column-major order
}