Hi everybody :) Excuse my English...
I'm implementing LightMaps in my 3D engine using the planar mapping algorithm.
I will explain it for whoever is interested.
Steps (for each polygon):
1- Obtain the polygon normal and choose the greater element to generate the texture coordinates using the remaining elements:
For a polygon (suppose a triangle) [A B C], I get the normal [N] and compare:
if (Abs(N.x) > Abs(N.y)) (Abs(N.x) > Abs(N.z)) then
Make UV coordinates from the YZ elements of the polygon.
Set variable "Flag = 1"
if (Abs(N.y) > Abs(N.yx) (Abs(N.y) > Abs(N.z)) then
Make UV coordinates from the XZ elements of the polygon.
Set variable "Flag = 2"
else
Make UV coordinates from the XY elements of the polygon.
Set variable "Flag = 3"
2- The next step in the algorithm is to obtain the maximum and minimum of these coordinates, in order to clamp them to the range [0..1]:
For each coordinate:
Save the minimum U into U_Min
Save the minimum V into V_Min
PolygonVertex.U = (PolygonVertex.U - U_Min) / (U_Max - U_Min)
PolygonVertex.V = (PolygonVertex.V - V_Min) / (V_Max - V_Min)
- Then, the lightmap position vectors are transformed into world space by using the equation of the plane to obtain the remaining unknown element:
if Flag = 1 then
// Vertex 1
V1.x = - (N.y * U_Min + N.z * V_Min & D) / N.x; // Plane equation
V1.y = U_Min
V1.z = V_Min
// Vertex 2
V2.x = - (N.y * U_Max + N.z * V_Min & D) / N.x; // Plane equation
V2.y = U_Max
V2.z = V_Min
// Vertex 3
V3.x = - (N.y * U_Min + N.z * V_Max & D) / N.x; // Plane equation
V3.y = U_Min
V3.z = V_Max
if Flag = 2 then
// Vertex 1
V1.x = U_Min
V1.y = - (N.x * U_Min + N.z * V_Min & D) / N.y;
V1.z = V_Min
// Vertex 2
V2.x = U_Max
V2.y = - (N.x * U_Max + N.z * V_Min & D) / N.y;
V2.z = V_Min
// Vertex 3
...
if Flag = 3 then
...
Now, to achieve the standard per pixel lighting, I need to do the product N.L for every pixel (or lumel, in this case).
I can easily get the L factor from the previous algorithm, just interpolating between through the vertices:
Edge1 = V2 - V1
Edge2 = V3 - V1
for each pixel U
for each pixel V
Pixel = V1 + Edge1 * U + Edge2 * V
Finally (!), here comes my problem:
I don't know how to get the same interpolation factor for the N (normal) factor just like I did for the L factor. That's because I have only the normal of the plane, and what I need is the normal for each vertex V1, V2, and V3.
The Planar Mapping algorithm projects the vectors into the UV plane and changes its orientation depending on the normal N, so I can't see how can I get my original normals.
Is there another equation that allows me to get the original vectors from the equation of the plane, the normal of the plane and/or the transformed vectors (V1, V2, V3)?
Better still, is there a matrix that represents the transformation done, so I can get the original vectors from the new ones (V1, V2 and V3)?
I will appreciate any help :) Thanks guys!