-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRend3D.z80
295 lines (242 loc) · 5.33 KB
/
Rend3D.z80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
.nolist
.sym "Rend3D.sym" ; export all labels for debugger
; System variable addresses
GBuffA .equ $86EC ;saveSScreen
GBuffB .equ $9340 ;plotSScreen
ProgStart .equ $9D95
appData .equ $8000
ramCode .equ $8100
; I/O ports
KeyboardPort .equ $01
LCDinstPort .equ $10
LCDdataPort .equ $11
; System routine to pause until LCD is ready to accept commands
LCD_Busy_Wait .equ $000b
; Macro to fill memory to the next 'b'-byte boundary
#define align(b) .fill b-$&(b-1)
; t increment = 1 / 2^SlerpSteps
#define SlerpSteps 5
; 0 = 1 step
; 1 = 2 steps
; 2 = 4 steps
; 3 = 8 steps
; 4 = 16 steps
; 5 = 32 steps
; ...
; 1 TState = 1/6000000 s
; Max size: 8811
; Limit to ~150 pushes ... 100 to be safe (Will it ever get that large?)
; Program entry point
.org ProgStart-2
.db $BB,$6D ; asm program header
.list
call getVData
call LoadVData
di ; no interrupting, thanks.
exx
ex af,af'
push af
push bc
push de
push hl
push iy ; Store shadow & system registers
call MainLoop
pop iy ; Restore shadow & system registers
pop hl
pop de
pop bc
pop af
ex af,af'
exx
ei
ret
VDatName: ; Name of vertex data object to load -- value is written here by calling that program first
.db 0,0,0,0,0,0,0,0,0,0
VData: ; Pointer to vertex data memory
.dw $0000
; Loads vertex data pointers
LoadVData:
ld de,6 ; skip past VData header
ld (VData),hl
add hl,de ; hl = VData.VertexCount
ld de, CPos
ld bc, 14 ; sizeof(CPos) + sizeof(VCount) + sizeof(TCount)
ldir ; CPos = VData.CameraPos
; VertexCount = VData.VertexCount
; TriangleCount = VData.TriangleCount
ex de,hl ; de = VData.VListPtr
ld hl,(VData)
ex de,hl
ld c,(hl)
inc hl
ld b,(hl)
inc hl
ex de,hl
add hl,bc
ld (VertexList),hl ; VertexList = VData.VList
ld hl,(VData)
ex de,hl
ld c,(hl)
inc hl
ld b,(hl)
inc hl
ex de,hl
add hl,bc
ld (TriangleList),hl ; TriangleList = VData.TList
ld hl,(VData)
ex de,hl
ld c,(hl)
inc hl
ld b,(hl)
inc hl
ex de,hl
add hl,bc
ld (NormalList),hl ; NormalList = VData.NList
ld hl,(VData)
ex de,hl
ld c,(hl)
inc hl
ld b,(hl)
inc hl
ex de,hl
add hl,bc
ld (VertexListCalc),hl ; VertexListCalc = VData.VListCalc
ld hl,(VData)
ex de,hl
ld c,(hl)
inc hl
ld b,(hl)
;inc hl
ex de,hl
add hl,bc
ld (TriangleSort),hl ; TriangleSort = VData.TSort
ret
; Main program code
; All registers & shadow registers are available for use.
MainLoop:
ld hl,RotM
call RotXYZ ; randomize rotation matrix
call RotMtoQ1 ; Q1 = Quat(RotM)
ld ix,Q0
ld iy,Q1
; acc = (Q0·Q1)
ld a,(ix+Q_A)
ld l,(ix+Q_A+1)
ld h,(ix+Q_A+2)
ld c,(iy+Q_A)
ld e,(iy+Q_A+1)
ld d,(iy+Q_A+2)
call fMult ; Q0A * Q1A
exx \ ex af,af' ; acc = Q0A * Q1A
ld a,(ix+Q_B)
ld l,(ix+Q_B+1)
ld h,(ix+Q_B+2)
ld c,(iy+Q_B)
ld e,(iy+Q_B+1)
ld d,(iy+Q_B+2)
call fMult ; Q0B * Q1B
call fAddAcc
exx \ ex af,af' ; acc = Q0A * Q1A + Q0B * Q1B
ld a,(ix+Q_C)
ld l,(ix+Q_C+1)
ld h,(ix+Q_C+2)
ld c,(iy+Q_C)
ld e,(iy+Q_C+1)
ld d,(iy+Q_C+2)
call fMult ; Q0C * Q1C
call fAddAcc
exx \ ex af,af' ; acc = Q0A*Q1A + Q0B*Q1B + Q0C*Q1C
ld a,(ix+Q_D)
ld l,(ix+Q_D+1)
ld h,(ix+Q_D+2)
ld c,(iy+Q_D)
ld e,(iy+Q_D+1)
ld d,(iy+Q_D+2)
call fMult ; Q0D * Q1D
call fAddAcc ; AHLDE = Q0A*Q1A + Q0B*Q1B + Q0C*Q1C + Q0D*Q1D
ld b,$80 ; sign bit
and b ; acc < 0 ?
jr z,NoNeg
; (Q0·Q1) < 0; Invert Q1
ld a,(Q1+Q_B)
xor b ; invert sign
ld (Q1+Q_B),a
ld a,(Q1+Q_C)
xor b ; invert sign
ld (Q1+Q_C),a
ld a,(Q1+Q_D)
xor b ; invert sign
ld (Q1+Q_D),a
NoNeg:
ld a,$BF \ ld hl,$8000 ; theta = 0
SlerpLoop:
;T = (theta*(2-abs(theta))+1)/2
; approx 1-(1-sin(theta*pi/2))/2, for a nice smooth start and stop
cp $3F ; sgn == 0 && exp == 0
jr nz,calcTCont ; theta != $3F8000
; theta >= $3F8000, reset
ld hl,Q1
ld de,Q0
ld bc,12 ; sizeof(Q1)
ldir ; Q0 = Q1
ld a,(RotCount)
dec a
ret z
ld (RotCount),a
jp MainLoop ; loop forever
calcTCont
push af \ push hl ; preserve theta through SlerpLoop ...
push af \ push hl ; theta
or $80 ; -|theta|
ld c,$40 \ ld de,$8000 ; CDE = 2
call fAdd ; (2-|theta|)
pop de \ pop bc \ ld c,b ; CDE = theta
call fMult ; theta*(2-|theta|)
call fTrunc
ld c,$3F \ ld de,$8000 ; CDE = 1
call fAdd ; AHL + 1
call fDiv2 ;dec a ; (theta*(2-|theta|) + 1)/2
ld (T),a \ ld (T+1),hl ; T = AHL
;inline Slerp
; QC = Q0 * ((q0^-1)*q1)^t
#include "il_slerp.inc"
;inline Quat to RotM
#include "il_qtom.inc"
call ClearBuffer
;inline Render
#include "il_rend.inc"
call DrawBuffer
; Finish Loop
pop hl \ pop af ; restore theta
ld c,(64-SlerpSteps) ; SlerpSteps Exponent
ld de,$8000
call fAdd ; theta += 2^-(SlerpSteps-1)
ld b,a
;ld a,(Exit) ; exit value used if key detection is in interrupt handler
;or a
;ld a,b
; keyboard input?
ld a,$fd
out (KeyboardPort),a
in a,(KeyboardPort)
cp $bf
ld a,b
jp nz,SlerpLoop ; Exit == 0
ld a,$ff
out (KeyboardPort),a ; reset keyboard
ret
#include "float.inc"
#include "mult.inc"
#include "div.inc"
#include "add.inc"
#include "mat.inc"
#include "quat.inc"
#include "math.inc"
#include "draw.inc"
#include "findsym.inc"
#include "vars.inc"
; label points for debugger
#include "exports.inc"
; throws an error if program is too big
#include "fsizechk.inc"
.end