Sign in to follow this  
takis

RayTracer: Texture coordinates for intersectionpoint

Recommended Posts

Hello, I am creating a raytracer. I am using a texture atlas. Can i calculate the texture coordinates of the intersectionpoint P as follow: vec3 Triangle::barycentric(vec3 P) { float F_abc, F_pbc, F_apc; vec3 bary(0,0,0); F_abc = tri_area(_A,_B,_C); F_pbc = tri_area(P,_B,_C); F_apc = tri_area(_A,P,_C); if (F_abc == 0.0f) return bary; bary.x = F_pbc/F_abc; bary.y = F_apc/F_abc; bary.z = 1.0f- bary.x-bary.y; return bary; } // Texturecoordinates for point P void Triangle::uv(vec3 P, float &u, float &v) { vec3 Pb = barycentric(P); u = _A.texcoord.x * Pb.x + _B.texcoord.x * Pb.y + _C.texcoord.x * Pb.z; v = _A.texcoord.y * Pb.x + _B.texcoord.y * Pb.y + _C.texcoord.y * Pb.z; }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this