查找球形纹理映射的 theta 和 phi
对于一个学校项目,我需要找到球形纹理贴图的 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 函数可能无法正常工作,但我相信问题在于我对球坐标缺乏了解。请告诉我您认为是什么问题,谢谢!
for a school project I need to find theta and phi for a spherical texture map. A lot of the actual OpenGL for texturing is already completed (comes with the starter code). The starter code provides the function and comments below. The code is what I have done so far (besides the initialization for x and z, which was given) :
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;
}
Following the "instructions" in the comments, this is what I came up with. The texture map does not work though (no errors, the coordinates are just wrong). It is possible that the getNormal
function is not working correctly but I believe the problem lies in my lack of understanding for spherical coordinates. Please let me know what you believe to be the issue, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
proj
未标准化,因此您会得到错误的theta
结果。顺便说一句,你对 theta 和 phi 的命名是非常规的(一开始它让我感到困惑)。通常,与 z 的角度称为 theta,与 xy 平面的角度称为 phi。
Since
proj
is not normalized, you get a wrong result fortheta
.BTW, your naming of theta and phi is unconventional (it confused me at first). Usually the angle from z is called theta and the angle in the x-y-plane is called phi.