【发布时间】:2010-06-17 02:37:58
【问题描述】:
如果没有 Glu,我怎么能像 GluPerspective 一样做呢?谢谢
例如:gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
【问题讨论】:
如果没有 Glu,我怎么能像 GluPerspective 一样做呢?谢谢
例如:gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
【问题讨论】:
void gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
{
GLdouble xmin, xmax, ymin, ymax;
ymax = zNear * tan( fovy * M_PI / 360.0 );
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
}
【讨论】:
gluPerspective 文档中对此进行了相当清楚的解释。您只需构建适当的 4x4 变换矩阵并使用 glMultMatrix 将其乘以当前变换:
void myGluPerspective(double fovy, double aspect, double zNear, double zFar)
{
double f = 1.0 / tan(fovy * M_PI / 360); // convert degrees to radians and divide by 2
double xform[16] =
{
f / aspect, 0, 0, 0,
0, f, 0, 0,
0, 0, (zFar + zNear)/(zNear - zFar), -1,
0, 0, 2*zFar*zNear/(zNear - zFar), 0
};
glMultMatrixd(xform);
}
请注意,OpenGL 以 column-major 顺序存储矩阵,因此上述数组元素的顺序与gluPerspective 文档中的内容相反。
【讨论】:
gluPerspective 文档说明了等式 f = cotangent(fovy / 2);当您进行度数到弧度的转换时,fovy / 2 变为 fovy * M_PI / 360。