简体   繁体   English

找到用于球面贴图的theta和phi

[英]Finding theta and phi for spherical texture mapping

for a school project I need to find theta and phi for a spherical texture map. 对于学校项目,我需要找到球形纹理贴图的theta和phi。 A lot of the actual OpenGL for texturing is already completed (comes with the starter code). 许多实际的用于纹理化的OpenGL已经完成(随入门代码一起提供)。 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) : 该代码是我到目前为止所做的(除了给出的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;
}

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. getNormal函数可能无法正常工作,但我认为问题出在我对球形坐标缺乏了解。 Please let me know what you believe to be the issue, thanks! 请让我知道您认为是什么问题,谢谢!

Since proj is not normalized, you get a wrong result for theta . 由于未将proj标准化,因此您得到theta错误的结果。

BTW, your naming of theta and phi is unconventional (it confused me at first). 顺便说一句,您对theta和phi的命名是不合常规的(起初让我感到困惑)。 Usually the angle from z is called theta and the angle in the xy-plane is called phi. 通常将与z的角度称为theta,将xy平面中的角度称为phi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM