Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Raytracer prototype!

20 views
Skip to first unread message

luser- -droog

unread,
Mar 12, 2013, 11:16:16 PM3/12/13
to
Don't get too excited. It just draws a white circle in a black rectangle.

%!
%http://code.google.com/p/xpost/downloads/detail?name=mat.ps
(mat.ps) run
/div { dup 0 eq { pop 10000 }{ div } ifelse } bind def
/normalize{[1 index mag 2 index length 1 sub{dup}repeat]{div}vop}def
/tan { dup sin exch cos div } def

<<
/Cam [ 0 0 0 ]
/Lookat [ 0 0 -1 ]
/Up [ 0 1 0 ]
/fovx 45
/W 320
/H 200
/Sc [ 1 1 -5 ]
/Sr 2
>> begin

<<
/buf W string
/fovy H W div fovx mul
>> {def} forall

/y 0 def
320 200 8
[ 1 0 0 1 0 0 ]
{
0 1 W 1 sub { /x exch def
[ x 2 mul W sub W div fovx tan mul
y 2 mul H sub H div fovy tan mul
-1 ]
Cam {sub} vop
normalize /R exch def

Cam Sc {sub} vop
dup R dot 2 mul /B exch def
dup dot Sr dup mul sub /C exch def

B dup mul C 4 mul sub /disc exch def

% ray misses sphere?
disc 0 lt { 0 }{
disc sqrt /sdisc exch def
B neg sdisc sub 2 div /t exch def
t 0 le { B neg sdisc add 2 div /t exch def } if
255
} ifelse

buf x 3 2 roll put
} for
/y y 1 add def
buf
}
image
showpage

luser- -droog

unread,
Mar 13, 2013, 10:42:45 PM3/13/13
to
On Tuesday, March 12, 2013 10:16:16 PM UTC-5, luser- -droog wrote:
> Don't get too excited. It just draws a white circle in a black rectangle.
>
>

Added calculations for intersection point and angle from light
ray. Now shows a nicer gradient at the edges.

Spins the light around the y-axis with a for-loop.

Makes a 63k gif with:
$ convert -crop 320x200 -limit memory 32 ray.ps ray.gif
nb. remove the `150 200 translate` for this.


%!
%http://code.google.com/p/xpost/downloads/detail?name=mat.ps
(mat.ps) run
/div { dup 0 eq { pop 10000 }{ div } ifelse } bind def
/normalize{[1 index mag 2 index length 1 sub{dup}repeat]{div}vop}def
/tan { dup sin exch cos div } def

<<
/Cam [ 0 0 0 ]
/Lookat [ 0 0 -1 ]
/Up [ 0 1 0 ]
/fovx 45
/W 320
/H 200
/Sc [ 1 1 -5 ]
/Sr 2
>> begin

<<
/buf W string
/fovy H W div fovx mul
>> {def} forall
/ang 0 def
/pos 3 def
/setLight { /Light [ ang cos pos mul 3 ang sin pos mul ] def } def

0 30 360 { /ang exch def
setLight
150 200 translate % remove this line to `convert -crop 320x200`
/y 0 def
W H 8
[ 1 0 0 1 0 0 ]
{
0 1 W 1 sub { /x exch def
[ x 2 mul W sub W div fovx tan mul
0 0 ]
[ 0 y 2 mul H sub H div fovy tan mul
0 ]
Lookat {add} vop {add} vop
Cam {sub} vop
normalize /R exch def

Cam Sc {sub} vop
dup R dot 2 mul /B exch def
dup dot Sr dup mul sub /C exch def

B dup mul C 4 mul sub /disc exch def

% ray misses sphere?
disc 0 lt { 0 }{
disc sqrt /sdisc exch def
B neg sdisc sub .5 mul /t exch def
t 0 le { B neg sdisc add .5 mul /t exch def } if
R [ t t t ] {mul} vop
Cam {add} vop /ri exch def %intersection point
ri Sc {sub} vop
[ 1 Sr div dup dup ] {mul} vop /rn exch def %surface normal
Light ri {sub} vop rn dot % normal . light-ray
.3 mul
dup 0 lt {pop 0} if
dup 1 gt {pop 1} if
255 mul truncate cvi
} ifelse

buf x 3 2 roll put
} for
/y y 1 add def
buf
}
%exec
image
showpage

} for
0 new messages