对于一个学校项目,我需要找到球形纹理贴图的 theta 和 phi。许多用于纹理的实际 OpenGL 已经完成(带有起始代码)。起始代码提供了以下功能和注释。代码是我到目前为止所做的(除了给定的 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;
}
按照评论中的“说明”,这就是我想出的。纹理贴图不起作用(没有错误,坐标是错误的)。 getNormal
函数可能无法正常工作,但我认为问题出在我对球坐标缺乏理解。请让我知道您认为是什么问题,谢谢!
最佳答案
由于 proj
未规范化,您会得到错误的 theta
结果。
顺便说一句,您对 theta 和 phi 的命名是非常规的(起初让我感到困惑)。通常与 z 的夹角称为 theta,与 x-y 平面的夹角称为 phi。
关于c++ - 寻找球形纹理贴图的 theta 和 phi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8129140/