TGA.PAS 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. Uses UVector, ObjTrace, Gtypes,Objects, Dos;
  2. CONST
  3. TGAHeader : array [1..12] of byte = (0,0,2, 0,0,0, 0,0,0, 0,0,0);
  4. MAXRecur = 5;
  5. type
  6. PTGATrace = ^TGATrace;
  7. TGATrace = object (TTracer)
  8. outF : FILE;
  9. ver : longint;
  10. Constructor Init(OutPut : string);
  11. Procedure GetReflPointColor(var aC:RGB;aRay:TRay;Num:word);
  12. {
  13. � ¡®â îâ ®âà ¦¥­¨ï !!!!
  14. Num - £«ã¡¨­  ४ãàᨨ (¤«ï ¯¥à¢®£® § ¯ã᪠ - 1)
  15. }
  16. Procedure Trace(ResX,ResY:word);
  17. Destructor Done;virtual;
  18. end;
  19. {TGATrace}
  20. Constructor TGATrace.Init(OutPut : string);
  21. var res:word;
  22. begin
  23. Inherited Init;
  24. Ver := CurVer;
  25. Assign(outF,OutPut);
  26. Rewrite(outF,1);
  27. BlockWrite(outF,TGAHeader,12,Res);
  28. if Res <> 12 then
  29. begin
  30. WriteLn('Writing to ',OutPut,' error!');
  31. Halt(255);
  32. end;
  33. end;
  34. {-------------------------}
  35. Procedure TGATrace.GetReflPointColor(var aC:RGB;aRay:TRay;Num:word);
  36. var
  37. Lt : POmni;
  38. Ob : PSurfObj;
  39. oc : RGB;
  40. N,P,R : tVec;
  41. ReflCol : RGB;
  42. L,Refl : TRay;
  43. Pd,tmp : float;
  44. i : word;
  45. begin
  46. SetColor(aC,0,0,0);
  47. if Num >= MAXRecur then Exit;
  48. oB := PSurfObj(FindISect(aRay,Pd));
  49. if oB = nil then exit;
  50. aC := Ambient;
  51. aRay.GetPoint(P,Pd);
  52. oB^.GetNormal(N,P);
  53. for i := 1 to LightCount do
  54. if Pos('OMNI',Lights^[i]^.OType) <> 0 then
  55. begin
  56. Lt := POmni(Lights^[i]);
  57. L.Init(P,P);
  58. VLinear(L.Dir,Lt^.Pos,P,1,-1);
  59. if (FindISect(L,tmp) = nil) or (tmp>1.0) then
  60. begin
  61. if OB^.ID in [F_OBJ_SURF_PLANE,F_TRI,F_QUAD] then
  62. tmp := aCos(N,L.Dir) * oB^.Surface^.Kd / VDot(L.Dir,L.Dir)
  63. else
  64. tmp := Vcos(N,L.Dir) * oB^.Surface^.Kd / VDot(L.Dir,L.Dir);
  65. CAddMul(aC,Lt^.Color,tmp);
  66. VLinear(R,N,L.Dir,-2*VDot(N,L.Dir),1);
  67. if OB^.ID in [F_OBJ_SURF_PLANE,F_TRI,F_QUAD] then
  68. tmp := Pow(aCos(R,aRay.Dir),Ob^.Surface^.Ns)*Ob^.Surface^.Ks
  69. / VDot(L.Dir,L.Dir)
  70. else
  71. tmp := Pow(VCos(R,aRay.Dir),Ob^.Surface^.Ns)*Ob^.Surface^.Ks
  72. {New} / VDot(L.Dir,L.Dir); {„¥«¨¬ ­  ª¢ ¤à â à ááâ®ï­¨ï
  73. ¤® ¨áâ®ç­¨ª }
  74. CAddMul(aC,Lt^.Color,tmp);
  75. end;
  76. end;
  77. {Calculate Reflection vec}
  78. Refl.Init(P,P);
  79. VLinear(Refl.Dir,N,aRay.Dir,-2*VDot(N,aRay.Dir),1);
  80. VNorm(Refl.Dir,1);
  81. GetReflPointColor(ReflCol,Refl,Num+1);
  82. { if Pd > VisRange then tmp := VisRange / Pd
  83. else tmp := 1;}
  84. CAddMul(aC,ReflCol,Ob^.Surface^.Ks);
  85. { CAddMul(aC,ReflCol,1);}
  86. Ob^.GetColor(oC,P);
  87. aC.R := aC.R * OC.R;
  88. aC.G := aC.G * OC.G;
  89. aC.B := aC.B * OC.B;
  90. end;{TGATrace.GetPointColor}
  91. {-------------------------}
  92. Procedure TGATrace.Trace;
  93. var
  94. Ray : tRay;
  95. i,j : longint;
  96. ColP : LongInt;
  97. DX,DY : tVec;
  98. C : tVec;
  99. T,cur : float;
  100. Col : RGB;
  101. begin
  102. BlockWrite(outF,ResX,2);
  103. BlockWrite(outF,ResY,2);
  104. i:=24;
  105. BlockWrite(outF,i,2);
  106. C := View.LLPos;
  107. VLinear(DX,View.Right,DX,1/ResX,0); {� áç¥â ¢¥ªâ®à  ¯à¨à®áâ  ¯® X & Y}
  108. VLinear(DY,View.Up, DY,1/ResY,0); {}
  109. for i := ResY-1 downto 0 do
  110. begin
  111. for j:= 0 to ResX-1 do
  112. begin
  113. Ray.Init(View.CamPos,View.CamPos);
  114. VLinear(Ray.Dir,C,View.CamPos,1,-1);
  115. VNorm(Ray.Dir,1);
  116. GetReflPointColor(Col,Ray,1); {Try This Stuff (It's work!)}
  117. { GetPointColor2(Col,Ray);}
  118. { GetPointColor(Col,Ray);}
  119. ColP:=Pack24(Col);
  120. BlockWrite(outF,ColP,3);
  121. VLinear(C,C,DX,1,1);
  122. end;
  123. VLinear(C,C,DX,1,-ResX);
  124. VLinear(C,C,DY,1,1);
  125. Write(#13'Line: ',ResY-i,'/',ResY,' ');
  126. end;
  127. end;{TGATrace.Trace}
  128. {-------------------------}
  129. Destructor TGATrace.Done;
  130. begin
  131. Close(outF);
  132. Inherited Done;
  133. end;
  134. {-------------------------}
  135. Var
  136. TGA : TGATrace;
  137. fIn,fOut : String;
  138. ResX,ResY,res : Integer;
  139. hb,mb,sb,ssb : word;
  140. he,me,se,sse : word;
  141. begin
  142. if ParamCount < 3 then
  143. begin
  144. WriteLn('Usage: TGA.exe <scene.tra> <ResX> <ResY> [out.tga]');
  145. Halt(255);
  146. end;
  147. fIn := ParamStr(1);
  148. if ParamStr(4) <> '' then fOut := ParamStr(4)
  149. else fOut := Copy(fIn,1,Length(fIn)-3)+'tga';
  150. Val(ParamStr(2),ResX,res);
  151. Val(ParamStr(3),ResY,res);
  152. TGA.Init(fOut);
  153. TGA.Load(fIn);
  154. GetTime(hb,mb,sb,ssb);
  155. TGA.Trace(resX,resY);
  156. GetTime(he,me,se,sse);
  157. if (sse<ssb) then
  158. begin
  159. dec(se);
  160. Inc(sse,100-ssb);
  161. end
  162. else Dec(sse,ssb);
  163. if (se<sb) then
  164. begin
  165. dec(me);
  166. Inc(se,60-sb);
  167. end
  168. else Dec(se,sb);
  169. if (me<mb) then
  170. begin
  171. dec(he);
  172. Inc(me,60-mb);
  173. end
  174. else Dec(me,mb);
  175. Dec(he,hb);
  176. WriteLn(#13'Elapsed ',he,' hours, ',me,' minutes, ',se,' seconds, ',
  177. sse,' 100th second');
  178. TGA.Done;
  179. end.