TGA2.PAS 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. Uses UVector, ObjTrace, Gtypes,Objects, Dos;
  2. CONST
  3. CANJITTER = [F_OBJ_SPHERE,F_TRI_PLANE];
  4. IfJitter = TRUE;
  5. JitterConst = 0.05;
  6. TGAHeader : array [1..12] of byte = (0,0,2, 0,0,0, 0,0,0, 0,0,0);
  7. MAXRecur = 5;
  8. type
  9. PTGATrace = ^TGATrace;
  10. TGATrace = object (TTracer)
  11. outF : FILE;
  12. ver : longint;
  13. Constructor Init(OutPut : string);
  14. Procedure GetReflPointColor(var aC:RGB;aRay:TRay;Num:word);
  15. {
  16. � ¡®â îâ ®âà ¦¥­¨ï !!!!
  17. Num - ⥪ãé ï £«ã¡¨­  ४ãàᨨ (¤«ï ¯¥à¢®£® § ¯ã᪠ - 1)
  18. }
  19. Procedure Trace(ResX,ResY:word);
  20. Destructor Done;virtual;
  21. end;
  22. {TGATrace}
  23. Constructor TGATrace.Init(OutPut : string);
  24. var res:word;
  25. begin
  26. Inherited Init;
  27. Ver := CurVer;
  28. Assign(outF,OutPut);
  29. Rewrite(outF,1);
  30. BlockWrite(outF,TGAHeader,12,Res);
  31. if Res <> 12 then
  32. begin
  33. WriteLn('Writing to ',OutPut,' error!');
  34. Halt(255);
  35. end;
  36. end;
  37. {-------------------------}
  38. Procedure TGATrace.GetReflPointColor(var aC:RGB;aRay:TRay;Num:word);
  39. var
  40. Lt : POmni;
  41. Ob : PSurfObj;
  42. N,P,R : tVec;
  43. ReflCol,oC : RGB;
  44. L,Refl : TRay;
  45. Pd,tmp : float;
  46. i : word;
  47. begin
  48. SetColor(aC,0,0,0);
  49. if Num >= MAXRecur then Exit;
  50. oB := PSurfObj(FindISect(aRay,Pd));
  51. if oB = nil then exit;
  52. aC := Ambient;
  53. aRay.GetPoint(P,Pd);
  54. oB^.GetNormal(N,P);
  55. {NEW 25.06.01}
  56. {Added jitter feature}
  57. if IfJitter and (oB^.ID in CanJitter) then
  58. begin
  59. VRandomize(N,JitterConst);
  60. VNorm(N,1);
  61. end;
  62. for i := 1 to LightCount do
  63. if Pos('OMNI',Lights^[i]^.OType) <> 0 then
  64. begin
  65. Lt := POmni(Lights^[i]);
  66. L.Init(P,P);
  67. VLinear(L.Dir,Lt^.Pos,P,1,-1);
  68. if (FindISect(L,tmp) = nil) or (tmp>1.0) then
  69. begin
  70. tmp := Vcos(N,L.Dir) * oB^.Surface^.Kd
  71. {New 2 Prev} / VDot(L.Dir,L.Dir); {„¥«¨¬ ­  ª¢ ¤à â à ááâ®ï­¨ï
  72. ¤® ¨áâ®ç­¨ª }
  73. CAddMul(aC,Lt^.Color,tmp);
  74. VLinear(R,N,L.Dir,-2*VDot(N,L.Dir),1);
  75. tmp := Pow(VCos(R,aRay.Dir),Ob^.Surface^.Ns)*Ob^.Surface^.Ks
  76. {New} / VDot(L.Dir,L.Dir); {„¥«¨¬ ­  ª¢ ¤à â à ááâ®ï­¨ï
  77. ¤® ¨áâ®ç­¨ª }
  78. CAddMul(aC,Lt^.Color,tmp);
  79. end;
  80. end;
  81. {Calculate Reflection vec}
  82. Refl.Init(P,P);
  83. VLinear(Refl.Dir,N,aRay.Dir,-2*VDot(N,aRay.Dir),1);
  84. GetReflPointColor(ReflCol,Refl,Num+1);
  85. CAddMul(aC,ReflCol,Ob^.Surface^.Ks);
  86. { CAddMul(aC,ReflCol,1);}
  87. Ob^.GetColor(oC,P);
  88. aC.R := aC.R * OC.R;
  89. aC.G := aC.G * OC.G;
  90. aC.B := aC.B * OC.B;
  91. end;{TGATrace.GetPointColor}
  92. {-------------------------}
  93. Procedure TGATrace.Trace;
  94. var
  95. Ray : tRay;
  96. i,j : longint;
  97. ColP : LongInt;
  98. DX,DY : tVec;
  99. C : tVec;
  100. T,cur : float;
  101. Col : RGB;
  102. begin
  103. FillChar(Ray.Org,vecsize,0);
  104. FillChar(Ray.Dir,vecsize,0);
  105. FillChar(DX,vecsize,0);
  106. FillChar(DY,vecsize,0);
  107. BlockWrite(outF,ResX,2);
  108. BlockWrite(outF,ResY,2);
  109. i:=24;
  110. BlockWrite(outF,i,2);
  111. C := View.LLPos;
  112. VLinear(DX,View.Right,View.Right,1/ResX,0); {� áç¥â ¢¥ªâ®à  ¯à¨à®áâ  ¯® X & Y}
  113. VLinear(DY,View.Up, View.Right,1/ResY,0); {}
  114. for i := ResY-1 downto 0 do
  115. begin
  116. for j:= 0 to ResX-1 do
  117. begin
  118. Ray.Init(View.CamPos,View.CamPos);
  119. VLinear(Ray.Dir,C,View.CamPos,1,-1);
  120. VNorm(Ray.Dir,1);
  121. GetReflPointColor(Col,Ray,1); {Try This Stuff (It's work!)}
  122. ColP:=Pack24(Col);
  123. BlockWrite(outF,ColP,3);
  124. VLinear(C,C,DX,1,1);
  125. end;
  126. VLinear(C,C,DX,1,-ResX);
  127. VLinear(C,C,DY,1,1);
  128. Write(#13'Line: ',ResY-i,'/',ResY,' ');
  129. end;
  130. end;{TGATrace.Trace}
  131. {-------------------------}
  132. Destructor TGATrace.Done;
  133. begin
  134. Close(outF);
  135. Inherited Done;
  136. end;
  137. {-------------------------}
  138. Var
  139. TGA : TGATrace;
  140. begin
  141. randomize;
  142. TGA.Init('ballpln.tga');
  143. TGA.Load('ballpln.tra');
  144. {
  145. New(aTex,Init(54,64));
  146. aTex^.LoadTGA('Rod.tga');
  147. PTriangle(TGA.Objects^[2])^.SetTexture(aTex,1/5,1/5);}
  148. { VSet(v1,-40,100,-130);
  149. VSet(v2,0,-10,0);
  150. TGA.SetCamera(v1,v2,0,50,1);
  151. VSet(v1,0,-30,0);
  152. VSet(v2,5,-30,0);
  153. VSet(v3,0,-30,5);
  154. aC.R:=0.3;aC.G:=0.4;aC.B:=1;
  155. New(PTri,Init(v1,v2,v3,aC));
  156. PTri^.SetSurf(0.6,0.9,1);
  157. TGA.AddObject(PTri);
  158. aC.R:=7000;aC.G:=10000;aC.B:=7000;
  159. New(m_gOmni,Init(30,100,-70,aC));
  160. TGA.AddLight(m_gOmni);
  161. TGA.Save('new.tra',true);
  162. {
  163. VSet(v1,-100,2,0);
  164. VSet(v2,100,20,0);
  165. VSet(v3,0,100,50);
  166. aC.R:=0.3;aC.G:=0.4;aC.B:=1;
  167. New(PTri,Init(v1,v2,v3,aC));
  168. PTri^.SetSurf(0.6,0.5,1);
  169. TGA.AddObject(PTri);
  170. }
  171. TGA.Trace(500,500);
  172. Writeln;
  173. TGA.Done;
  174. { TGA.Load('cool.tra');}
  175. { TGA.Load('planes.tra');}
  176. { TGA.Save('cool.tra',true);}
  177. end.