Initial commit of 1992 raytracing code
This commit is contained in:
commit
3f46e7dd82
42 changed files with 8483 additions and 0 deletions
262
turbo-pascal/ray11.pas
Normal file
262
turbo-pascal/ray11.pas
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
program mmmmmmmmmmmmm;
|
||||
{$X+}
|
||||
uses Crt,Vga13h,RayTrace;
|
||||
|
||||
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;
|
||||
tff: File absolute tf;
|
||||
|
||||
procedure StartTga(s: String);
|
||||
begin
|
||||
Assign(tf,s);
|
||||
Rewrite(tf);
|
||||
BlockWrite(tff,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;
|
||||
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: Universe; st: String);
|
||||
var
|
||||
x,y: Integer;
|
||||
c: LongInt;
|
||||
r,g,b: Integer;
|
||||
begin
|
||||
StartTga(st);
|
||||
x := 0;
|
||||
for y := 0 to u.Height-1 do
|
||||
for x := 0 to u.Width-1 do begin
|
||||
c := u.TracePoint(x,y);
|
||||
r := (c and $ff0000) shr 16;
|
||||
g := (c and $ff00) shr 8;
|
||||
b := c and $ff;
|
||||
RgbDot(x,u.Height-1-y,r,g,b);
|
||||
TgaDot(r,g,b);
|
||||
end;
|
||||
EndTga;
|
||||
end;
|
||||
|
||||
procedure Draw(var u: Universe);
|
||||
var
|
||||
x,y: Integer;
|
||||
c: LongInt;
|
||||
begin
|
||||
x := 0;
|
||||
while (x < u.Width) and (not KeyPressed) do begin
|
||||
for y := 0 to u.Height-1 do begin
|
||||
c := u.TracePoint(x,u.Height-1-y);
|
||||
if c <> 0 then
|
||||
RgbDot(x,y,(c and $ff0000) shr 16,(c and $ff00) shr 8,c and $ff);
|
||||
end;
|
||||
Inc(x);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure Qwik(var u: Universe);
|
||||
var
|
||||
x,y: Integer;
|
||||
c: LongInt;
|
||||
r,g,b: Byte;
|
||||
begin
|
||||
x := 0;
|
||||
while (x < (u.Width div 2)) and (not KeyPressed) do begin
|
||||
for y := 0 to (u.Height div 2)-1 do begin
|
||||
c := u.TracePoint(x*2,u.Height-1-(y*2));
|
||||
if c <> 0 then begin
|
||||
r := (c and $ff0000) shr 16;
|
||||
g := (c and $ff00) shr 8;
|
||||
b := c and $ff;
|
||||
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;
|
||||
end;
|
||||
Inc(x);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure Zoom(var u: Universe);
|
||||
var
|
||||
x,y,i,j: Integer;
|
||||
c: LongInt;
|
||||
r,g,b: Byte;
|
||||
begin
|
||||
x := 0;
|
||||
while (x < (u.Width div 4)) and (not KeyPressed) do begin
|
||||
for y := 0 to (u.Height div 4)-1 do begin
|
||||
c := u.TracePoint(x*4,u.Height-1-(y*4));
|
||||
if c <> 0 then begin
|
||||
r := (c and $ff0000) shr 16;
|
||||
g := (c and $ff00) shr 8;
|
||||
b := c and $ff;
|
||||
for i := 0 to 3 do
|
||||
for j := 0 to 3 do
|
||||
RgbDot(x*4+i,y*4+j,r,g,b);
|
||||
end;
|
||||
end;
|
||||
Inc(x);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure Beep;
|
||||
begin
|
||||
Sound(220); { Beep }
|
||||
Delay(200); { For 200 ms }
|
||||
NoSound; { Relief! }
|
||||
end;
|
||||
|
||||
type
|
||||
pplane = object(Plane)
|
||||
function GetColor(aray: Ray; time: Real): LongInt; virtual;
|
||||
end;
|
||||
BlankSphere = object(Sphere)
|
||||
function GetColor(aray: Ray; time: Real): LongInt; virtual;
|
||||
end;
|
||||
|
||||
const
|
||||
AmbientInt = 10;
|
||||
PointInt = 100;
|
||||
LightX = 15;
|
||||
LightY = 5;
|
||||
LightZ = 100;
|
||||
CoeffReflect = 0.85;
|
||||
|
||||
function pplane.GetColor(aray: Ray; time: Real): LongInt;
|
||||
var
|
||||
costheta,d,x,y,z,I: Real;
|
||||
N,L: Ray;
|
||||
c: Integer;
|
||||
begin
|
||||
SurfNormal(aray,time,N);
|
||||
d := Sqrt(Sqr(n.dx)+Sqr(n.dy)+Sqr(n.dz));
|
||||
if d <> 0 then begin
|
||||
n.dx := n.dx / d;
|
||||
n.dy := n.dy / d;
|
||||
n.dz := n.dz / d;
|
||||
end;
|
||||
x := LightX - (time * aray.dx + aray.ox);
|
||||
y := LightY - (time * aray.dy + aray.oy);
|
||||
z := LightZ - (time * aray.dz + aray.oz);
|
||||
d := Sqrt(Sqr(x)+Sqr(y)+Sqr(z));
|
||||
if d <> 0 then begin
|
||||
x := x / d;
|
||||
y := y / d;
|
||||
z := z / d;
|
||||
end;
|
||||
costheta :=
|
||||
(n.dx*x+n.dx*y+n.dx*z+
|
||||
n.dy*x+n.dy*y+n.dy*z+
|
||||
n.dz*x+n.dz*y+n.dz*z);
|
||||
if costheta < 0 then costheta := 0;
|
||||
I := (CoeffReflect * AmbientInt) +
|
||||
((CoeffReflect * PointInt) / (d + 0.001)) * costheta;
|
||||
c := Round(I*15);
|
||||
GetColor := GetRgb(c,c,c);
|
||||
end;
|
||||
|
||||
function BlankSphere.GetColor(aray: Ray; time: Real): LongInt;
|
||||
begin
|
||||
GetColor := $ffffff;
|
||||
end;
|
||||
|
||||
procedure TraceIt;
|
||||
var
|
||||
u: Universe;
|
||||
p,p2,p3: Pplane;
|
||||
s: BlankSphere;
|
||||
begin
|
||||
u.Init(320,200);
|
||||
s.Init(LightX,LightY,LightZ,1); {15,5,100}
|
||||
p.Init(0,-8,0, 0.25,1,0);
|
||||
p2.Init(0,0,150, 0,0,-1);
|
||||
p3.Init(-20,0,0, 1,0,0);
|
||||
u.Insert(@p);
|
||||
u.Insert(@p2);
|
||||
u.Insert(@p3);
|
||||
u.Insert(@s);
|
||||
Zoom(u);
|
||||
Beep;
|
||||
u.Done;
|
||||
end;
|
||||
|
||||
begin
|
||||
Set13h;
|
||||
SetUpRgb;
|
||||
TraceIt;
|
||||
ReadKey;
|
||||
BiosMode(3);
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue