PDA

View Full Version : Lightmapping rectangle faces...



M109uk
27-11-2003, 05:21 PM
I have finally got my lightmapper working, but i have a bigproblem with it!
For some reason it doesnt like rectangle (eg. 200 x 100) shaped faces :s it works like a dream if it is square (eg. 100 x 100)..

I cant see anywhere that it assumes the face to be square :s heres the code im using to calculate the lightmaps ::


const
Dimension = 32;
var
PolyNormal,ZeroPosition,PlaneNormal: TVector3f;
UVVector,Vect1,Vect2,Lumel,Tempv: TVector3f;
Edge1,Edge2,nEdge1,nEdge2: TVector3f;
i,iX,iY,r,g,b,lm: Integer;
LightmapBMP: TBitmap;
Plane: ToxPlane;
PlaneDistance,Distance,X,Y,Z,uFactor,vFactor: Single;
PolyPlane: ToxPlaneAlignment;
begin
lm := Add;
ZeroPosition := Lightmaps[lm].FindZeroUV;
Plane := oxPlaneFromPoints(Triangle.Vertex);
PolyPlane := oxTriangleAlignement(Triangle.Vertex);
PolyNormal := oxNormal(Triangle.Vertex);
Lightmaps[lm].SetupUVs(Triangle);

PlaneNormal := Vector3f(Plane.a, Plane.b, Plane.c);
oxNormalize(PlaneNormal);
PlaneDistance := oxDot(PlaneNormal, Triangle.Vertex[0]);
With Lightmaps[lm] Do
Begin
Case PolyPlane Of
paXZ: Begin
y := -(PolyNormal[0]*uMin+PolyNormal[2]*vMin+PlaneDistance)/PolyNormal[1];
UVVector := Vector3f(uMin, y, vMin);
y := -(PolyNormal[0]*uMax+PolyNormal[2]*vMin+PlaneDistance)/PolyNormal[1];
Vect1 := Vector3f(uMax, y, vMin);
y := -(PolyNormal[0]*uMin+PolyNormal[2]*vMax+PlaneDistance)/PolyNormal[1];
Vect2 := Vector3f(uMin, y, vMax);
End;
paYZ: Begin
x := -(PolyNormal[2]*uMin+PolyNormal[1]*vMin+PlaneDistance)/PolyNormal[0];
UVVector := Vector3f(x, uMin, vMin);
x := -(PolyNormal[2]*uMax+PolyNormal[1]*vMin+PlaneDistance)/PolyNormal[0];
Vect1 := Vector3f(x, uMin, vMax);
x := -(PolyNormal[2]*uMin+PolyNormal[1]*vMax+PlaneDistance)/PolyNormal[0];
Vect2 := Vector3f(x, uMax, vMin);
End;
paXY: Begin
z := -(PolyNormal[0]*uMin+PolyNormal[1]*vMin+PlaneDistance)/PolyNormal[2];
UVVector := Vector3f(uMin, vMin, z);
z := -(PolyNormal[0]*uMax+PolyNormal[1]*vMin+PlaneDistance)/PolyNormal[2];
Vect1 := Vector3f(uMax, vMin, z);
z := -(PolyNormal[0]*uMin+PolyNormal[1]*vMax+PlaneDistance)/PolyNormal[2];
Vect2 := Vector3f(uMin, vMax, z);
End;
End;

Edge1 := oxSub(Vect1, UVVector);
Edge2 := oxSub(Vect2, UVVector);

LightmapBMP := TBitmap.Create;
LightmapBMP.Width := Dimension;
LightmapBMP.Height := Dimension;

For iX := 0 To Dimension Do
For iY := 0 To Dimension Do
Begin
r := 0;
g := 0;
b := 0;
uFactor := iX/Dimension;
vFactor := iY/Dimension;
nEdge1 := Vector3f(Edge1[0]*uFactor, Edge1[1]*uFactor, Edge1[2]*uFactor);
nEdge2 := Vector3f(Edge2[0]*vFactor, Edge2[1]*vFactor, Edge2[2]*vFactor);
TempV := oxAdd(UVVector, nEdge1);
Lumel := oxAdd(TempV, nEdge2);

If Lumel[0] < 0 Then Lumel[0] := -lumel[0];
If Lumel[2] < 0 Then Lumel[2] := -lumel[2];
If Lumel[1] < 0 Then Lumel[1] := -lumel[1];

For i := 0 To High(PointLights) Do
With PointLights[i] Do
Begin
Distance := oxDistance(Lumel, Position);
If Distance < Radius Then
Begin
If Distance = 0 Then
Distance := 1 Else
Distance := (Radius-Distance)/Distance;
r := r+Round(Distance*Colour[0]);
g := g+Round(Distance*Colour[1]);
b := b+Round(Distance*Colour[2]);
If r > 255 Then r := 255;
If g > 255 Then g := 255;
If b > 255 Then b := 255;
End;
End;
LightmapBMP.Canvas.Pixels[iX, iY] := RGB(r,g,b);
End;
LightmapBMP.PixelFormat := pf24bit;
LightmapBMP.SaveToFile(InttoStr(Triangle.LightmapI D)+'.bmp');
LightmapBMP.Free;
Result := lm;
End;


Here some screenshots of the results ::
Square Face :: http://www.biocoders.org/oxw_lm_sqr.jpg
Rectangle Face :: http://www.biocoders.org/oxw_lm_rec.jpg

I coule be wrong, i have been staring at this code for far too long and im now seeing dots :s

Thanx for any help...

Edit ::
The only thing that i can think that is suggesting its a square is the Bitmap, but if this is the problem how can i solve it??

Alimonster
02-12-2003, 12:59 AM
I'll bump this in the (probably vain) hope that I'll find some time to investigate for ya.

I really wish I had "bullet time" -- use it, answer all the questions on forums, code some, get something to eat, and by the time it's finished I'd still have spare time. :( Of course, the only problem is that I'd have to kill random thugs to get it. Heh.

M109uk
03-12-2003, 02:25 PM
Thanx, i think i know where i need to change it but im just guessing again lol

towards the bottom of the code it is using dimension to size the bitmap and to calculate the edges, i have had a quick try at changing this to the distance of the required points, but it didnt work very well.. i will try again soon and any input will be very welcome :D..

On another note, do you know where i can find some maths for calculating the different light types (Point light, spot light, etc)?

hehe i know what you mean :D it seems spare time is getting more and more rarer :( lol