简体   繁体   中英

Finding theta and phi for spherical texture mapping

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!

Since proj is not normalized, you get a wrong result for theta .

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 xy-plane is called phi.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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