Direct replacement for the trig unit, it was trying to do inverse cos of 1.001.
Should texture everthing correctly (or near enough), there seems to be some small scaling issue with the textures on some models, but they might be badly scaled like that anyway.
One thing you might notice is that the tops of the trees have a badly mapped triangle, i thought this was my error, BUT, it is actually mapped like that, go look at the oak trees in the game…
unit trig;
interface
uses
Windows, OpenGL,SysUtils, FileRSM, OpenGl1x, Math;
type
Tex3Vx2UV = array[0..2] of TVector2d;
function SinRule(lenb,angA,angB : Double) : Double;
function len3D(x1,y1,z1,x2,y2,z2 : Integer) : Double;
function CosRule(len1,len2,len3 : Double) : Double;
procedure DoStuff;
var
UV: array of Tex3Vx2UV;
implementation
// procedure to calculate all of the UV coordinates in the model file
// Call after loading the model and before displaying
procedure DoStuff;
var
ti,i,k,testflag,s0,su,sv : Integer;
lenau,lenaq,lenuq,lenus,lenav,lenvq,lentv : Double;
Angle_U,Angle_V,U_number,V_number,Angle_A,Angle_B : Double;
Error_Angle : Double;
begin
SetLength(UV, 0); SetLength(UV, GNumTriangles);
// for each triangle in model
for i := 0 to high(F) do
begin
testflag := 0;
//for each point in model triangle.
for k := 0 to 2 do
begin
if M[i] >1 then
begin //E1
ti := ( (M[i] + 2) div 4) -1;
lenau := len3D(V[Tex[ti][0]][0], V[Tex[ti][0]][1], V[Tex[ti][0]][2], V[Tex[ti][2]][0], V[Tex[ti][2]][1], V[Tex[ti][2]][2]) ;
lenaq := len3D(V[Tex[ti][0]][0], V[Tex[ti][0]][1], V[Tex[ti][0]][2], V[F[i][k]][0], V[F[i][k]][1], V[F[i][k]][2]) ;
lenuq := len3D(V[Tex[ti][2]][0], V[Tex[ti][2]][1], V[Tex[ti][2]][2], V[F[i][k]][0], V[F[i][k]][1], V[F[i][k]][2]) ;
lenav := len3D(V[Tex[ti][0]][0], V[Tex[ti][0]][1], V[Tex[ti][0]][2], V[Tex[ti][1]][0], V[Tex[ti][1]][1], V[Tex[ti][1]][2]) ;
lenvq := len3D(V[Tex[ti][1]][0], V[Tex[ti][1]][1], V[Tex[ti][1]][2], V[F[i][k]][0], V[F[i][k]][1], V[F[i][k]][2]) ;
Error_angle := 0;
//get angle A and B for error correction.
If (lenaq <> 0) AND (lenau <> 0) AND (lenav <> 0) AND (lenaq <> 0) then
begin
Angle_A := CosRule(lenaq,lenau,lenuq) ;
Angle_B := CosRule(lenav,lenaq,lenvq) ;
Error_angle := (Angle_A + Angle_B) - 90;
end;
//calculate U number
if (lenau = lenaq ) AND (lenuq = 0) then U_number := 1.0
else if (lenaq = 0) OR (lenau-lenaq = 0 ) then U_number := 0.0
else
begin
Angle_U := CosRule(lenau,lenuq,lenaq) ;
lenus := SinRule(lenuq,(90-Angle_U - Error_Angle),90) ;
If lenau <> 0 then U_number := (lenau - lenus) * (1 / lenau)
else MessageBox(0, PChar('division by Zero'), PChar('NaN'), MB_OK);
end;
//calculate V_number
if (lenav =lenaq) AND (lenvq = 0) then V_number := 1.0
else if (lenaq = 0) OR (lenav-lenaq = 0) then V_number := 0.0
else
begin
Angle_V := CosRule(lenvq,lenav,lenaq);
lentv := SinRule(lenvq, (90 - Angle_V - Error_Angle), 90);
If lenav <> 0 then V_number := (lenav - lentv) * (1 / lenav)
else MessageBox(0, PChar('division by Zero'), PChar('NaN'), MB_OK);
end;
UV[i][k][0] := V_number;
UV[i][k][1] := 1-U_number;
end else //end E1
begin
//for when no texture is used
UV[i][k][0] := 0;
UV[i][k][1] := 0;
end ;
end; //end of k loop
end; //end of i loop
end; //end of procedure
function len3D(x1,y1,z1,x2,y2,z2 : Integer) : Double;
begin
Result := Sqrt( ((x1-x2)*(x1-x2))+ ((y1-y2)*(y1-y2))+ ((z1-z2)*(z1-z2)) );
end;
function CosRule(len1,len2,len3 : Double) : Double;
var
temp1, radtemp : Double;
// len1 = a, len2 = b, len3 = c
begin
If (len1 <= 0) OR (len2 <= 0) then MessageBox(0, PChar('division by Zero'), PChar('NaN'), MB_OK);
temp1 := ((len1*len1)+(len2*len2)-(len3*len3))/(2*len1*len2);
if (temp1 < 1.0) AND (temp1 > -1.0) then begin
//if (temp1 < 1.0) then begin
radtemp := ArcCos(temp1);
Result := RadToDeg(ArcCos(temp1));
end else if (temp1 >= 1.0) then Result := 0
else if (temp1 <= -1.0) then result := 180;
//Result := ArcCos(temp1);
end;
function SinRule(lenb,angA,angB: Double) : Double;
var
temp1, radtemp : Double;
begin
// a / Sin(A) = b / Sin(B)
// therefore a = (b * Sin(A))/ Sin(B)
if (Sin(DegToRad(angB))) <= 0 then MessageBox(0, PChar('division by Zero in sin'), PChar('NaN'), MB_OK);
Result := (lenb * Sin(DegToRad(angA)) ) / (Sin(DegToRad(angB)));
end;
end.