RayTracer: Texture coordinates for intersectionpoint

Started by
0 comments, last by Eelco 18 years, 10 months ago
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; }
Advertisement
yep. not sure if its the fastest way for your specific siituation, but it will work.

This topic is closed to new replies.

Advertisement