【发布时间】:2011-11-15 06:57:05
【问题描述】:
对于一个学校项目,我需要为球形纹理贴图找到 theta 和 phi。许多用于纹理的实际 OpenGL 已经完成(附带启动代码)。入门代码提供了下面的函数和 cmets。代码是我到目前为止所做的(除了给定的 x 和 z 的初始化):
Vec3f sphere::getTextureCoords(Vec3f eye, Vec3f dir)
{
// find the normal (getNormal)
Vec3f n = this->getNormal(eye, dir);
// use these to find spherical coordinates
Vec3f x(1, 0, 0);
Vec3f z(0, 0, 1);
// phi is the angle down from z
// theta is the angle from x curving toward y
// find phi using the normal and z
float phi = acos(n.Dot3(z));
// find the x-y projection of the normal
Vec3f proj(n.x(), n.y(), 0);
// find theta using the x-y projection and x
float theta = acos(proj.Dot3(x));
// if x-y projection is in quadrant 3 or 4, then theta = 2PI - theta
if (proj.y() < 0)
theta = TWOPI - theta;
Vec3f coords;
coords.set(theta / TWOPI, phi / PI, 0);
return coords;
}
按照 cmets 中的“说明”,这就是我想出的。纹理贴图不起作用(没有错误,坐标只是错误的)。 getNormal 函数可能无法正常工作,但我认为问题在于我对球坐标缺乏了解。请让我知道您认为是什么问题,谢谢!
【问题讨论】:
-
注意Spherical-cartesian conversions 上的维基百科文章中的公式,特别是它们对arctan 的使用以及关于atan2 的注释。
标签: c++ textures texture-mapping