r/opengl • u/miki-44512 • 11h ago
Calculating the tangent of a sphere
Hello everyone hope you have a lovely day
I was following learnopengl pbr chapter and I encountered the code that render the sphere, ofc I want my sphere to be textured, but when The renderSphere function doesn't calculate the tangent and bitangent of the sphere, it calculates it's position, indices and normals and calculate the tangent in the fragment shader.
I decided to calculate the tangent from the cpu side for a better performance and also because I'm working with integrated gpu laptop.
I didn't find so much information after doing some research so I went with the option I don't like tbh, asking Ai about the theory
P = (cos φ sin θ, cos θ, sin φ sin θ)- Tangent = direction of increasing U, which is
∂P/∂φ:(-sin φ sin θ, 0, cos φ sin θ). This vanishes at the poles because of thesin θfactor, but its direction(-sin φ, 0, cos φ)is still well defined, so drop the factor and use that directly. - Bitangent = direction of increasing V, which is
∂P/∂θ:(cos φ cos θ, -sin θ, sin φ cos θ). P = (cos φ sin θ, cos θ, sin φ sin θ) Tangent = direction of increasing U, which is ∂P/∂φ: (-sin φ sin θ, 0, cos φ sin θ). This vanishes at the poles because of the sin θ factor, but its direction (-sin φ, 0, cos φ) is still well defined, so drop the factor and use that directly. Bitangent = direction of increasing V, which is ∂P/∂θ: (cos φ cos θ, -sin θ, sin φ cos θ).
this was the answer so calculating the tangent based on opengl renderSphere function will be this
tangents.emplace_back(glm::vec3(-std::sin(xSegment * 2 * PI), 0, std::cos(xSegment * 2 * PI)));
and this was the result

I don't trust Ai so much I decided to test how learnopengl generate TBN matrix using partial derivative and this was the result

So Did I calculate the tangent correctly or what? how did you guys overcome that?
Thanks appreciate your help and your time!


