3 ; a 256-byte intro by Rrrola <rrrola@gmail.com>
5 ; greets to everyone who's computer is too fast :)
7 ; This is loosely based on my intro 'Gem' (shown on Demobit),
8 ; but the code is much better.
10 ; Vector3: X right, Y down, Z forward.
11 ; On the FP stack it looks like {Y X Z} (Y is often used in comparisons).
12 ; In memory it looks like {Z X Y}, which saves a displacement byte.
13 ; (u'v) is the dot product: ux*vx + uy*vy + uz*vz.
24 ; assume al=0 bx=0 sp=di=-2 si=0100h bp=09??h
26 ;Set video mode and earth+sky palette
27 dec di ; u16[100h] = -20401, u16[10Ch] = -30515
29 dec di ; initial pixel_adr@di = -4
31 P:shr cl,1 ; B@cl = 0..8..31,31..0
33 int 10h ; set video mode / color: bx=index dh=R ch=G cl=B
35 movsx cx,bl ; 0..127,128..255 (palette index)
36 xor ch,cl ; 0..127,127..0
39 mul ax ; R@dh = 0..16..63,63..16..0
41 shr cx,1 ; G@ch = 0..63,63..0
43 inc bl ; keep default color 0
44 js Q ; R@dh = 0..63,63..16..0
45 xchg cl,dh ; B@cl = 0..16..63,63..0
49 ; Constants expected at a fixed address below.
67 ;Each frame: Generate normals to p0..p11=[bp+200h,300h,...].
68 M:mov ax,0x4731 ; highest 9 bits: float32 exponent 1/256 (for T)
69 ; lower byte = 2*number of rotations+1
70 ; lowest 4 bits must be 0x1 for 'test cl,al'
71 mov dx,0xA000-10-20-20-4
72 mov es,dx ; dx:bx = YX:XX = 0x9fca:0
73 pusha ; adr: -18 -16 -14 -12 -10 -8 -6 -4 -2
74 ; stack: di si bp sp bx dx cx ax 0
75 ; data: -4 100 9?? -2 0 9fca {T/256}
77 G:add bp,si ; i@cx = 12...1; bp points to p[12-i]; carry=0
80 ;Generate 12 planes with unit normals.
82 ; fld1 ; platonic dodecahedron: exact is atan((1+sqrt5)/2)=1.017rad
83 fld dword[di-2] ;|t=T/256: morphing shape: cube, platonic12, rhombic12
87 fldz ;|a=0 b c (a*a+b*b+c*c = 1)
88 ; fldlg2 ;irregular shape
90 N:test cl,al ;=1 ;|a b c
93 K:fstp st3 ;|b c +-a (scramble so that all 12 planes are generated)
96 ;Do a bunch of slow rotations. z x y -> cx-sy cy+sx z
99 Z:fld st1 ;|y x y z ;|x sy x cy z
100 fld dword[di-2] ;|t=T/256
101 fsincos ;|c=cos(t) s=sin(t) y x y z ;|c s x sy x cy z
102 fmulp st4 ;|s y x cy z ;|s x sy cx cy z
103 fmulp ;|sy x cy z ;|sx sy cx cy z
104 add al,0x7F ; loop 2x
106 faddp st3 ;|sy cx cy+sx z
107 fsubp ;|new.z=cx-sy .x=cy+sx .y=z
110 S:fstp dword[bp+si] ;[bp+100]=.z [bp+104]=.x [bp+108]=.y
118 ; the visible pixels are A0000..AF9FF, I want X=0 Y=0 in the center
119 ;Each pixel: cx=T dx:bx=YX:XX(init=9fca:0) di=adr(init=-4)
120 X:inc dx ; part of "dx:bx += 0x0000CCCD"
124 pusha ; adr: -18 -16 -14 -12 -10 -8 -6 -4 -2
125 fninit ; stack: di si bp sp bx dx cx ax 0
126 mov bx,es ; s16: pixadr 100 9?? -2 ..X..Y T result
127 mov di,-4 ;di = address of pushed ax
129 ;Compute ray direction.
130 fild word[BIG] ; store 28799 as a double, read as two floats
131 fst qword[bx] ; t_front@float[bx] = 0, t_back@float[bx+4] = 6.879
133 fild word[di+4-8] ;|y=Y x=X z=BIG
135 ;Intersect the pyrite.
137 popa ; color -> pushed ax
138 ; mov al,dl ; show only palette
140 ;; Faster, but lower quality: draw each pixel twice.
142 ; add bx,0xCCCD; dx:bx = YXX += 0000CCCD
145 add bx,0xCCCD; dx:bx = YXX += 0000CCCD
147 jnz X ; do 65536 pixels
150 dec ax ; ah=0 (checkboard uses positive color indices)
158 ; Faster (+4 or +8 bytes): test the shape only in the center of the screen
164 ;Ray-plane intersection.
165 ;Find the front plane with maximum t and back plane with minimum t.
166 ; tf@[bx], tb@[bx+4] ray parameter t
167 ; pf@[bx+si], pb@[bx+4+si] pointer to plane
168 mov cx,12 ; i@cx = 12...1
169 I:add bp,si ; bp points to p[i]
170 fldlg2 ;|pd=0.301 y x z
171 fadd dword[bp+si] ;|N=pd-(ro'p[i]) y x z ; ro = 0 0 -1
173 push si ; Dot product:
174 D:fld dword[bp+si] ;|p[i].z ...
175 fmul st4 ;|rd.z*p[i].z ...
176 sub si,di ; 100 104 108
177 jpo D ;|(rd*p[i]).y .x .z N rd.y .x .z
180 faddp ;|D=(rd'p[i]) N y x z
182 ;If we hit the plane from the front (D<0), update tf. Otherwise update tb.
184 fst dword[bp+di]; -> p[i].dot_rd (will be read later)
185 test [bp+di+2],sp ; sf=1 if we're in front of the plane
187 sub bx,di ; bx = address of tf?tb
188 FRONT: ; D<0: if tf*D < N { tf=N/D; pf=current; } maximalize tf
189 fld st0 ; D>=0: if tb*D < N { tb=N/D; pb=current; } minimalize tb
190 fmul dword[bx] ;|(tf?tb)*D D N y x z
192 ;;DosBOX-compatible FPU comparison, +5 bytes (+3 but we need ax)
194 ; fcomp st2 ;|D N y x z
196 ; sahf ; cf = (tf?tb)*D < N
203 ;another alternative, +6 bytes
206 ; test [bp-6],sp ; sf=1 if <0
210 fdivr st1 ;|t=N/D N y x z
211 fst dword[bx] ; -> tf?tb
212 mov [bx+si],bp ; pf?pb = current
218 cmp dx,[bx+2] ; if tf>tb { no_hit: early exit }
219 jng B ;si=100 ;|y x z
223 ;Reflect: reflect(i,n) = i - 2*n*(i'n)
225 Y:fld dword[bx+di] ;|(rd'pf) rd.y .x .z ; reads pf->dot_rd
226 fmul dword[bx+si] ;|(rd'pf)*pf.z rd.y .x .z
227 fadd st0 ;|2*(rd'pf)*pf.z rd.y .x .z
228 fsubr st3 ;|R.z=rd.z-2*(rd'pf)*pf.z rd.y .x .z
229 sub si,di ;100 104 108
230 jpo Y ;si=10C ;|(R=i-2*n(i'n)).y R.x R.z rd.y .x .z
232 ;Environment map: chessboard below, sky gradient above.
234 ; Subtle highlight on the pyrite.
236 fimul word[C] ; 16993 (background) or 20036 (pyrit)
237 fistp dword[di] ;|y x z
238 sar dword[di],22 ; if y>=-0.5 { chessboard } else { sky }
239 js E ; the sky is just y (= y^2 after gamma)
241 ; Everything the same brightness. (-6 bytes)
242 ; fist word[di] ;|y x z
243 ; sar word[di],8 ; if y>=-0.5 { chessboard } else { sky }
244 ; js E ; the sky is just y (= y^2 after gamma)
246 ; Dark background version.
247 ; fist word[di] ;|y x z
249 ; xor cl,9 ; hit?8:9 - make the background darker
250 ; sar word[di],cl ; if y>=-0.5 { chessboard } else { sky }
251 ; js E ; the sky is just y (= y^2 after gamma)
253 fidivr word[si] ;|C/y x z (C = hit?-30515:-20401)
255 fmul st2 ;|u=z*C/y v=x*C/y z
260 xor al,[bp+di+1] ; xortex@ax = (T-u) XOR v
262 ; aam -32-24 ; more interesting floor texture
264 add al,10<<3 ; tex = (xortex AND 0b1001) + 10 [10|11|18|19]
266 mov [di],ah ; pushed al = tex*y