raytrace-retro/turbo-pascal/rayt2.pas
2026-04-08 21:02:41 -07:00

255 lines
No EOL
5.4 KiB
ObjectPascal

program RayTracingWithUltraRayTr;
{$X+}
uses Crt,Vga13h,RayTr;
type
TgaHeader = record { 18 bytes total }
who_knows: array[1..12] of Byte;
Width: Word;
Height: Word;
BitsPerPixel: Byte;
who_knows2: Byte;
end;
const
DefaultHeader: TgaHeader = (
who_knows: (0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Width: 320;
Height: 200;
BitsPerPixel: 24;
who_knows2: 32
);
var
tf: File of byte;
ttf: File absolute tf;
procedure StartTga(s: String);
begin
Assign(tf,s);
Rewrite(tf);
BlockWrite(ttf,DefaultHeader,18);
end;
procedure EndTga;
begin
Close(tf);
end;
procedure TgaDot(r,g,b: Integer);
begin
if r > 255 then r := 255;
if g > 255 then g := 255;
if b > 255 then b := 255;
if r < 0 then r := 0;
if g < 0 then g := 0;
if b < 0 then b := 0;
Write(tf,Byte(b),Byte(g),Byte(r));
end;
{ r,g,b:0-255 }
function Rgb(r,g,b: Integer): Integer;
begin
if r > 255 then r := 255;
if g > 255 then g := 255;
if b > 255 then b := 255;
if r < 0 then r := 0;
if g < 0 then g := 0;
if b < 0 then b := 0;
Rgb := (b shr 6) or ((g shr 3) and $1c) or (r and $e0);
end;
procedure RgbDot(x,y,r,g,b: Integer);
var
r1,g1,b1,r2,g2,b2,c1,c2: Integer;
begin
if r > 255 then r := 255;
if g > 255 then g := 255;
if b > 255 then b := 255;
if r < 0 then r := 0;
if g < 0 then g := 0;
if b < 0 then b := 0;
r1 := r and $e0; g1 := g and $e0; b1 := b and $c0;
r2 := r1 + (r mod 32)+16;
g2 := g1 + (g mod 32)+16;
b2 := b1 + (b mod 64)+32;
c1 := Rgb(r1,g1,b1); c2 := Rgb(r2,g2,b2);
if (x mod 2) = (y mod 2) then PutPixel(x,y,c1) else PutPixel(x,y,c2);
end;
procedure SetUpRgb;
var
Clrs: array[0..255,0..2] of Byte;
i: Integer;
begin
for i := 0 to 255 do begin
Clrs[i,0] := (i and $e0) shr 2;
Clrs[i,1] := (i and $1c) shl 1;
Clrs[i,2] := (i and 3) shl 4;
case Clrs[i,2] of
15..31: Inc(Clrs[i,2],4);
32..63: Inc(Clrs[i,2],8);
end;
end;
SetColorBlock(0,256,Clrs);
end;
procedure Save(var u: TUniverse; st: String);
var
x,y: Integer;
c: RgbTriple;
r,g,b: Integer;
begin
StartTga(st);
x := 0;
for y := 0 to u.ScrHeight-1 do
for x := 0 to u.ScrWidth-1 do begin
u.TracePoint(x,y,c);
r := Trunc(c.r*255);
g := Trunc(c.g*255);
b := Trunc(c.b*255);
RgbDot(x,u.ScrHeight-1-y,r,g,b);
TgaDot(r,g,b);
end;
EndTga;
end;
procedure Draw(var u: TUniverse);
var
x,y: Integer;
c: RgbTriple;
r,g,b: Integer;
begin
x := 0;
while (x < u.ScrWidth) and (not KeyPressed) do begin
for y := 0 to u.ScrHeight-1 do begin
u.TracePoint(x,u.ScrHeight-1-y,c);
r := Trunc(c.r*255);
g := Trunc(c.g*255);
b := Trunc(c.b*255);
RgbDot(x,y,r,g,b);
end;
Inc(x);
end;
end;
procedure Qwik(var u: TUniverse);
var
x,y: Integer;
c: RgbTriple;
r,g,b: Integer;
begin
x := 0;
while (x < (u.ScrWidth div 2)) and (not KeyPressed) do begin
for y := 0 to (u.ScrHeight div 2)-1 do begin
u.TracePoint(x*2,u.ScrHeight-1-(y*2),c);
r := Trunc(c.r*255);
g := Trunc(c.g*255);
b := Trunc(c.b*255);
RgbDot(x*2,y*2,r,g,b); RgbDot(x*2+1,y*2,r,g,b);
RgbDot(x*2+1,y*2+1,r,g,b); RgbDot(x*2,y*2+1,r,g,b);
end;
Inc(x);
end;
end;
procedure Zoom(var u: TUniverse);
var
x,y,i,j: Integer;
c: RgbTriple;
r,g,b: Integer;
begin
x := 0;
while (x < (u.ScrWidth div 4)) and (not KeyPressed) do begin
for y := 0 to (u.ScrHeight div 4)-1 do begin
u.TracePoint(x*4,u.ScrHeight-1-(y*4),c);
r := Trunc(c.r*255);
g := Trunc(c.g*255);
b := Trunc(c.b*255);
for i := 0 to 3 do
for j := 0 to 3 do
RgbDot(x*4+i,y*4+j,r,g,b);
end;
Inc(x);
end;
end;
procedure Beep;
begin
Sound(220); { Beep }
Delay(200); { For 200 ms }
NoSound; { Relief! }
end;
type
TPlane1 = object(TPlane)
procedure GetInfo(Ray: TRay; var Inf: TRayInfo); virtual;
end;
TransSphere = object(TSphere)
Col: RgbTriple;
constructor Init(x,y,z,rr,cr,cg,cb: Real);
procedure GetInfo(Ray: TRay; var Inf: TRayInfo); virtual;
end;
procedure TPlane1.GetInfo(Ray: TRay; var Inf: TRayInfo);
begin
TPlane.GetInfo(Ray,Inf);
MakeTrip(0,0,0,Inf.Reflectivity);
end;
constructor TransSphere.Init(x,y,z,rr,cr,cg,cb: Real);
begin
TSphere.Init(x,y,z,rr);
MakeTrip(cr,cg,cb,Col);
end;
procedure TransSphere.GetInfo(Ray: TRay; var Inf: TRayInfo);
begin
TSphere.GetInfo(Ray,Inf);
MakeTrip(0,0,0,Inf.Reflectivity);
MakeTrip(1-Col.r,1-Col.g,1-Col.b,Inf.Opacity);
Inf.Color := Col;
end;
procedure TraceIt;
var
u: TUniverse;
p1,p2: TPlane1;
l1,l2,l3: TLightSource;
s1,s2,s3: TransSphere;
begin
u.Init(320,200,10, 0,0,0, 0,0,1, 0,1,0, 1, 40,30, 0.5,0.5,0.5, 0,0,0);
l1.Init(-8.5,3,50, 20,20,20);
l2.Init(0,3,65, 20,20,20);
l3.Init(8.5,3,50, 20,20,20);
u.InsertLight(@l1);
u.InsertLight(@l2);
u.InsertLight(@l3);
s1.Init(-8.5,3,50,2, 1,0,0);
s2.Init(0,3,65,2, 0,1,0);
s3.Init(8.5,3,50,2, 0,0,1);
p1.Init(0,-8,0, 0,1,0);
p2.Init(0,15,0, 0,-1,0);
u.Insert(@p1);
u.Insert(@p2);
u.Insert(@s1);
u.Insert(@s2);
u.Insert(@s3);
Save(u,'brooke8.tga');
{Zoom(u);}
Beep;
u.DeleteAll;
u.Done;
end;
begin
Set13h;
SetUpRgb;
TraceIt;
ReadKey;
BiosMode(3);
end.