/[projet1]/users/chema/1337intro/oobj3d/obj3d.s
Defence Force logotype

Contents of /users/chema/1337intro/oobj3d/obj3d.s

Parent Directory Parent Directory | Revision Log Revision Log


Revision 379 - (show annotations)
Tue Aug 31 10:21:36 2010 UTC (9 years, 7 months ago) by Chema
File size: 24574 byte(s)
1337 rotating logo added.
1 ;
2 ; lib3d extensions
3 ;
4 ; A collection of routines which extends
5 ; the 3d library, adding routines for creating,
6 ; manipulating, and rendering objects.
7 ;
8 ; SLJ March/April 1999
9 ;
10 ; Obj3d
11 ; Oric port Chema 2008
12 ;
13 ;
14 ; Idea Maintain a list of objects.
15
16 ; Get constants in an include file..
17
18 #include "obj3d.h"
19 #include "params.h"
20
21 #define X1 _LargeX0
22 #define Y1 _LargeY0
23 #define X2 _LargeX1
24 #define Y2 _LargeY1
25
26 #define MAXOBJS 8
27 #define MAXVERTEX 64
28
29
30
31 ; Now some internal (not visible) variables
32
33 _NumObjs
34 NUMOBJS .byt 00 ;Number of objects
35 NUMCENTS .byt 00 ;Number of object senter
36 CUROBJ .byt 00 ;Current object
37 LASTOBJ .byt 00
38 ZMAX .word $2300 ;Maximum range
39 ZMIN .byt 50 ;Minimum range
40 OBJECTS .word $0800 ;Object records 1227 = 3456 bytes = $0D80
41
42
43 NUMLINES .byt 00 ;This is for the line-drawing... I think. Defined below in the original
44
45 ;===============================
46
47 ; Zero page variables
48
49 .zero
50
51 __obj3d_zeropage_start
52
53 RTEMPA .byt 00
54 RTEMPX .byt 00
55 RTEMPY .byt 00
56 TEMP .word 00 ;two bytes
57 POINT .word 00 ;two bytes
58
59 ; This is for the line-plotting routine
60 ; Coordinates are 16-bit
61
62 ;#define X1 _LargeX0
63 ;#define X2 _LargeX1
64 ;#define Y1 _LargeY0
65 ;#define Y2 _LargeY1
66
67 ; This is for polygon drawing or C64 specific ???
68
69 ;BITMAP .word $B9 ;Page of bitmap base
70 ;FILLPAT .word $BB ;Pointer to fill pattern
71
72
73 __obj3d_zeropage_end
74
75 .text
76
77 __obj3d_start
78
79 ;Lines drawn
80 ;speeds up wireframe
81 ; Oric port This is the same I already used.
82 ; 100 bytes instead of $67.
83
84 #define MAXLINES 39;100
85 LINELO .dsb 40;100
86 LINEHI .dsb 40;100
87
88 #ifdef USEOBLETS
89 #define OBHEAD $2F-1 ;Head of list = $55FF Oric port Just the size of OBLETS
90 OBLETS .dsb OBHEAD+1 ;Oblet list
91 #endif
92
93 .dsb 256-(*&255)
94 PLISTX .dsb (MAXVERTEX*2) ;Point lists (projected)
95 PLISTY .dsb (MAXVERTEX*2)
96 PLISTZ .dsb (MAXVERTEX*2)
97
98 // META
99 VISOBJS .dsb 129
100 OBCEN .dsb MAXOBJS ;Center object #
101 ;Note will bug if 128 vis objs
102
103 OBJLO .dsb MAXOBJS ;Object pointers
104 OBJHI .dsb MAXOBJS ;if 0 then empty
105
106 CX .dsb MAXOBJS ;Rotated/relative centers
107 HCX .dsb MAXOBJS
108 CY .dsb MAXOBJS
109 HCY .dsb MAXOBJS
110 CZ .dsb MAXOBJS
111 HCZ .dsb MAXOBJS
112
113
114 ; Some stuff from lib3D
115
116 #define MATRIX A11 ; Local rotation Matrix
117 #define VIEWMAT VA11 ; Viewpoint rotation matrix
118
119
120 ; Now let's go to the code...
121
122
123 ;
124 ; Initialize lib3d pointers, variables
125 ;
126 ; On entry .AY = pointer to object records
127 ;
128
129 Init3D
130 .(
131 STA OBJECTS
132 STY OBJECTS+1
133
134 jsr _lib3dInit ; Initialize lib3D Oric Version
135 /*
136 lda #<PLISTX
137 sta PLISTXLO
138 lda #<PLISTY
139 sta PLISTYLO
140 lda #<PLISTZ
141 sta PLISTZLO
142
143 lda #>PLISTX
144 sta PLISTXLO+1
145 lda #>PLISTY
146 sta PLISTYLO+1
147 lda #>PLISTZ
148 sta PLISTZLO+1
149
150
151 lda #<(PLISTX+MAXVERTEX)
152 sta PLISTXHI
153 lda #<(PLISTY+MAXVERTEX)
154 sta PLISTYHI
155 lda #<(PLISTZ+MAXVERTEX)
156 sta PLISTZHI
157
158 lda #>(PLISTX+MAXVERTEX)
159 sta PLISTXHI+1
160 lda #>(PLISTY+MAXVERTEX)
161 sta PLISTYHI+1
162 lda #>(PLISTZ+MAXVERTEX)
163 sta PLISTZHI+1
164 */
165 LDX #<CX
166 STX CXLO
167 LDX #<CY
168 STX CYLO
169 LDX #<CZ
170 STX CZLO
171
172 LDX #>CX
173 STX CXLO+1
174 LDX #>CY
175 STX CYLO+1
176 LDX #>CZ
177 STX CZLO+1
178
179
180 LDX #<HCX
181 STX CXHI
182 LDX #<HCY
183 STX CYHI
184 LDX #<HCZ
185 STX CZHI
186
187 LDX #>HCX
188 STX CXHI+1
189 LDX #>HCY
190 STX CYHI+1
191 LDX #>HCZ
192 STX CZHI+1
193
194 ;LDX #159 ;(0,0) = center of screen
195 ;LDY #99
196
197 ; Oric port Let's put center in center )
198
199 ldx #CENTER_X
200 ldy #CENTER_Y
201 NOM STX XOFFSET
202 STY YOFFSET
203
204
205 +_EmptyObj3D
206 LDA #$FF
207 STA CUROBJ
208 LDA #00
209 STA NUMOBJS
210 STA LASTOBJ
211 sta NUMCENTS
212
213 LDY #00 ;Zero out object pointers
214 TYA
215 L1 STA OBJHI,Y
216 INY
217 ;BPL L1 ;128 entries
218 cpy #MAXOBJS
219 bne L1
220
221 RTS
222 .)
223
224 ;
225 ; AddObj -- Add object to object list
226 ;
227 ; On entry .AY = pointer to object point data
228 ; .X = optional ID byte
229 ;
230 ; On exit .AY = pointer to object
231 ; .X = object number
232 ; C set indicates an error
233 ;
234
235 AddObj
236 .(
237 sta RTEMPA
238 sty RTEMPY
239 stx RTEMPX
240
241 ldx #00 ;Find open slot
242 txa
243 sta tmp
244 clc
245 loop ldy OBJHI,x
246 beq found
247 adc #ObjSize ;32 bytes per record
248 bcc c1
249 inc tmp
250 clc
251 c1 inx
252 bpl loop
253 sec
254 rts
255
256 found adc OBJECTS
257 sta POINT
258 sta OBJLO,x
259 lda tmp
260 adc OBJECTS+1
261 sta POINT+1
262 sta OBJHI,x
263 inc NUMOBJS
264 cpx LASTOBJ
265 bcc init
266 stx LASTOBJ
267 init
268 ldy #ObjSize-1 ;Init object
269 lda #00
270 l2 sta (POINT),y
271 dey
272 bpl l2
273
274 lda #64 ;Identity matrix
275 ldy #ObjMat
276 sta (POINT),y
277 ldy #ObjMat+4
278 sta (POINT),y
279 ldy #ObjMat+8
280 sta (POINT),y
281
282 ldy #ObjID
283 lda RTEMPX ;ID
284 sta (POINT),y
285
286 lda RTEMPY ;Obj data pointer
287 dey
288 sta (POINT),y
289 lda RTEMPA
290 dey
291 sta (POINT),y
292
293 +SetCurOb stx CUROBJ
294 +GetCurOb ldx CUROBJ
295 +GetObj lda OBJLO,x
296 ldy OBJHI,x
297 clc
298 rts
299
300 .)
301
302
303 ;
304 ; DelObj -- Remove object from list
305 ; by setting high byte=0
306 ;
307 ; On entry .X = object number
308 ; C set means serious error!
309 ;
310
311 DelObj
312 .(
313 LDA #00
314 STA OBJHI,X
315 LDA NUMOBJS
316 BEQ end
317 DEC NUMOBJS
318 CPX LASTOBJ ;Last object in list
319 BCC end
320 L1 LDA OBJHI,X
321 BNE save_X
322 DEX
323 BPL L1
324 SEC ;Out of objects!
325 INX
326 save_X STX LASTOBJ
327 end RTS
328 .)
329
330
331 ;
332 ; GetNextOb -- Get next object in list
333 ;
334 ; On exit .X = object number
335 ; .AY= object pointer
336 ; C set indicates error
337 ; Current object set to .X
338 ;
339 GetNextOb
340 .(
341 ;LDA NUMOBJS
342 ;BEQ ERR
343 LDX CUROBJ
344 ;BMI ERR
345 CPX LASTOBJ
346 BCC C1
347 LDX #$FF
348 C1 INX
349 LDY OBJHI,X
350 BEQ C1
351 LDA OBJLO,X
352 STX CUROBJ
353 ;CLC
354 RTS
355 /*
356 ERR SEC
357 RTS
358 */
359 .)
360
361
362
363 ;-------------------------------
364 ;
365 ; Object manipulation routines
366 ;
367 ;-------------------------------
368
369 ;
370 ; SetMat -- Calculate matrix for current object
371 ;
372 ; On entry .A = Angle around z-axis
373 ; .Y = Angle around y-axis
374 ; .X = Angle around x-axis
375 ;
376 SetMat
377 .(
378 JSR CALCMAT ;Result -> MATRIX
379 LDX CUROBJ
380 BMI done
381 LDA OBJLO,X
382 CLC
383 ADC #ObjMat ;Matrix
384 STA POINT
385 LDA OBJHI,X
386 ADC #00
387 STA POINT+1
388
389 LDA #00
390 LDY #17
391 L2 STA (POINT),Y ;Zero remainder
392 DEY
393 CPY #8
394 BNE L2
395
396 LOOP LDA MATRIX,Y
397 STA (POINT),Y
398 DEY
399 BPL LOOP
400
401 done RTS
402 .)
403
404
405 ;
406 ; Yaw/Pitch/Roll -- Accumulate a rotation on current object
407 ;
408 ; On entry C clear -> positive rotation
409 ; C set -> negative rotation
410 ;
411
412 GetCurMat
413 .(
414 PHP
415 JSR GetCurOb
416 CLC
417 ADC #ObjMat
418 BCC save_P
419 INY
420 save_P PLP
421 RTS
422
423 +Pitch
424 JSR GetCurMat
425 JMP ACCROTX
426 +Yaw
427 JSR GetCurMat
428 JMP ACCROTY
429 +Roll
430 JSR GetCurMat
431 JMP ACCROTZ
432 .)
433
434
435 ;
436 ; MoveDown/Side/Forwards -- Move object along its
437 ; (local) X Y or Z axis.
438 ;
439 ; Note that orientation matrix is inverse of object matrix
440 ;
441 ; On entry .A = velocity (signed)
442 ;
443
444 .(
445 MOVRTS RTS
446 +MoveDown
447 LDY #ObjMat ;Row 1
448 .byt $2C
449 +MoveSide
450 LDY #ObjMat+3 ;Row 2
451 .byt $2C
452 +MoveForwards
453 LDY #ObjMat+6
454 STY RTEMPY ;Object offset
455 CMP #00
456 BEQ MOVRTS
457 STA MultLo1 ;Plain signed multply
458 STA MultHi1
459 EOR #$FF
460 CLC
461 ADC #01
462 STA MultLo2
463 STA MultHi2
464
465 LDX CUROBJ
466 BMI MOVRTS
467 LDA OBJLO,X
468 STA POINT
469 LDA OBJHI,X
470 STA POINT+1
471
472 LDA (POINT),Y
473 TAY
474 LDA #ObjCXRem
475 LDX #ObjCX
476 JSR MovMult
477
478 LDY RTEMPY
479 INY
480 STY RTEMPY
481 LDA (POINT),Y
482 TAY
483 LDA #ObjCYRem
484 LDX #ObjCY
485 JSR MovMult
486
487 LDY RTEMPY
488 INY
489 LDA (POINT),Y
490 TAY
491 LDA #ObjCZRem
492 LDX #ObjCZ
493
494 MovMult
495 STX RTEMPX ;Coord
496 STA RTEMPA ;Remainder
497 LDA (MultLo1),Y
498 SEC
499 SBC (MultLo2),Y
500 STA TEMP
501 LDA (MultHi1),Y
502 SBC (MultHi2),Y
503 CPY #$80 ;Fix up signs
504 BCC POS1
505 SBC MultLo1
506 POS1 LDX MultLo1
507 BPL DIV
508 STY TEMP+1
509 SEC
510 SBC TEMP+1
511
512 DIV STA TEMP+1 ;Now div by 16
513 LDY #00
514 AND #$FF
515 BPL POSA
516 DEY ;High byte
517 POSA TYA
518 ASL TEMP
519 ROL TEMP+1
520 ROL
521 ASL TEMP
522 ROL TEMP+1
523 ROL
524 ASL TEMP
525 ROL TEMP+1
526 ROL
527 ASL TEMP
528 ROL TEMP+1
529 ROL
530
531 TAX
532 LDA TEMP
533 LDY RTEMPA ;Remainder
534 CLC
535 ADC (POINT),Y
536 STA (POINT),Y
537 LDY RTEMPX ;Coord
538 LDA TEMP+1
539 ADC (POINT),Y
540 STA (POINT),Y
541 INY
542 TXA
543 ADC (POINT),Y
544 STA (POINT),Y
545 RTS
546 .)
547
548 ;
549 ; GetDown/Side/FrontVec
550 ;
551 ; Get orientation vector
552 ; On entry Current object set
553 ; On exit (.X,.Y,.A) = (X,Y,Z) signed
554 ; direction vector, length=64
555 ;
556
557 GetDownVec
558 LDY #ObjMat ;Row 1
559 .byt $2C
560 GetSideVec
561 LDY #ObjMat+3 ;Row 2
562 .byt $2C
563 GetFrontVec
564 .(
565 LDY #ObjMat+6
566
567 LDX CUROBJ
568 BMI done
569 LDA OBJLO,X
570 STA POINT
571 LDA OBJHI,X
572 STA POINT+1
573
574 LDA (POINT),Y
575 TAX
576 INY
577 INY
578 LDA (POINT),Y
579 PHA
580 DEY
581 LDA (POINT),Y
582 TAY
583 PLA
584 done RTS
585
586 .)
587
588
589
590 ;-------------------------------
591 ;
592 ; Visualization routines
593 ;
594 ;-------------------------------
595
596 ViewObj .byt 00 ;Viewpoint object
597 ;SOLIDPOL .byt 00 ;Solid/wireframe polygons
598
599 ; Oric Port There is NO Pattern table, so point anywhere...
600 ;PATTAB .word $1000 ;Pattern table
601
602 ;
603 ; SetParms -- Set rendering parameters
604 ;
605 ; On entry .AY = Pointer to pattern table
606 ; .X = Bitmap page
607 ; C set -> solid polygons
608 ; C clear -> wireframe
609 ;
610
611 ;SetParms
612 ; ROR SOLIDPOL
613 ; STA PATTAB
614 ; STY PATTAB+1
615 ; STX BITMAP
616 ; RTS
617
618 ;
619 ; SetVisParms -- Set visibility parms
620 ;
621 ; On entry .AY = Maximum object range
622 ; .X = Minimum object range
623 ;
624
625 ;SetVisParms
626 ; STA ZMAX
627 ; STY ZMAX+1
628 ; STX ZMIN
629 ; RTS
630
631 ;
632 ; CalcView -- Calculate view (Set viewpoint, translate and rotate centers)
633 ;
634 ; On entry .X = viewpoint object
635 ;
636 ; On exit: translated rotated centers
637 ; in CX/CY/CZ
638 ;
639
640 CalcView
641 .(
642 STX ViewObj
643 JSR SetCurOb
644 STA POINT
645 STY POINT+1
646
647 LDX #8
648 LDY #ObjMat+8
649 loop
650 LDA (POINT),Y ;Viewpoint matrix
651 STA VIEWMAT,X
652 DEY
653 DEX
654 BPL loop
655
656 LDX #11 ;Set up pointers
657 cl LDA CXLO,X
658 STA C0XLO,X
659 DEX
660 BPL cl
661
662 LDX #00
663 STX RTEMPX ;Object index
664
665 getloop JSR GetNextOb
666 CPX ViewObj
667 BEQ done
668
669 STA TEMP
670 STY TEMP+1
671 TXA
672 LDX RTEMPX
673 STA OBCEN,X ;Object number
674 TXA
675 LDY #ObjCenPos ;Position in center list
676 STA (TEMP),Y
677
678 LDY #00
679 LDA (TEMP),Y ;Translate
680 SEC
681 SBC (POINT),Y
682 STA CX,X
683 INY
684 LDA (TEMP),Y
685 SBC (POINT),Y
686 STA HCX,X
687
688 INY
689 LDA (TEMP),Y ;Translate
690 SEC
691 SBC (POINT),Y
692 STA CY,X
693 INY
694 LDA (TEMP),Y
695 SBC (POINT),Y
696 STA HCY,X
697
698 INY
699 LDA (TEMP),Y ;Translate
700 SEC
701 SBC (POINT),Y
702 STA CZ,X
703 INY
704 LDA (TEMP),Y
705 SBC (POINT),Y
706 STA HCZ,X
707
708 INC RTEMPX
709 BPL getloop
710 done
711 LDY RTEMPX ;# of objects
712 STY NUMCENTS
713
714 JMP GLOBROT ;off she goes!
715 .)
716
717
718
719 ;
720 ; SortVis -- Compute and sort all visible objects
721 ;
722 ; On entry centers stored in CX etc.
723 ;
724 ; On exit VisObjs = linked list of visible objects
725 ; (farthest objects at start of list)
726 ;
727 ; Visibility conditions
728 ; x+z>0, x-z<0 (within view area)
729 ; same for x+y/x-y
730 ; z > 8192 will be treated as too far away to see
731 ; z < 180 is treated as being too near.
732 ;
733
734 NUMVIS .byt 00
735
736 SortVis
737 .(
738 LDA #00
739 STA NUMVIS
740 LDA #$80
741 STA VISOBJS+$80
742 LDX NUMCENTS
743 DEX
744 bpl loop
745 rts
746 loop
747
748 LDA CZ,X ;low byte
749 STA TEMP
750 LDA HCZ,X ;high byte
751 STA TEMP+1
752 bmi SKIP
753
754 LDA TEMP
755 CMP ZMIN
756 LDA TEMP+1
757 SBC #00
758 BCC SKIP
759
760 LDA TEMP ;x+z>0
761 CLC
762 ADC CX,X
763 LDA TEMP+1
764 ADC HCX,X
765 BMI SKIP
766
767 LDA TEMP ;z-x>0
768 CMP CX,X
769 LDA TEMP+1
770 SBC HCX,X
771 BMI SKIP
772
773 LDA TEMP ;y+z>0
774 CLC
775 ADC CY,X
776 LDA TEMP+1
777 ADC HCY,X
778 BMI SKIP
779
780 LDA TEMP ;z-y>0
781 CMP CY,X
782 LDA TEMP+1
783 SBC HCY,X
784 BMI SKIP
785
786 LDY #$80 ;Head of list
787 l1 LDA VISOBJS,Y ;Linked list of objects
788 BMI link
789 STY RTEMPY
790 TAY ;Next object
791 LDA CZ,Y ;If farther, then
792 CMP TEMP ;move down list
793 LDA HCZ,Y
794 SBC TEMP+1
795 BCS l1
796 ;Nearest objects last in list
797 TYA
798 LDY RTEMPY ;Insert into list
799
800 link STA VISOBJS,X ;X -> rest of list
801 TXA
802 STA VISOBJS,Y ;beginning of list -> X
803 SKIP DEX
804 BPL loop
805 LDY #$80
806 STY COB ;Current object
807 done RTS
808
809 .)
810
811
812
813 ;
814 ; DrawAllVis -- Draw all visible objects
815 ; in linked list.
816 ;
817 COB .byt 00 ;Current object
818 .(
819 DrawLoop
820 STY COB
821 ldx OBCEN,y ;Actual object number
822
823 JSR RotDraw
824
825 ldy COB
826 ldx OBCEN,y
827
828 +DrawAllVis
829 LDX COB ;Head = #$80
830 LDY VISOBJS,X
831 BPL DrawLoop
832 done
833 DrawRTS RTS
834
835
836
837 ;
838 ; GetNextVis -- Get next object in
839 ; visible list.
840 ;
841 ; On exit N set indicates end of list
842 ;
843 +GetNextVis
844 LDX COB
845 LDY VISOBJS,X
846 BMI DrawRTS
847 php
848 STY COB
849 ldx OBCEN,y
850 LDA #00
851 STA NUMLINES
852 jsr SetCurOb
853 plp
854 rts
855 .)
856
857 ; .dsb 256-(*&255)
858
859
860 ;
861 ; RotDraw -- Rotate and draw an object
862 ;
863 ; On entry Object number in .X
864 ; SetParms already called.
865 ;
866 DATAP .word 0 ;Temp pointer
867 NPOINTS .byt 00
868 OBTYPE .byt 00
869
870 NFACES .byt 00 ;Faces/Oblets
871
872 RotDraw
873 .(
874 LDA OBJLO,X
875 STA POINT
876 LDA OBJHI,X
877 STA POINT+1
878
879 LDY #ObjMat ;Matrix -> ZP
880 LDX #00
881 STX NUMLINES
882 mli STX TEMP
883 LDA #3
884 STA TEMP+1
885 ml LDA (POINT),Y ;Take transpose to get
886 STA MATRIX,X ;orientation matrix
887 INY
888 INX
889 INX
890 INX
891 DEC TEMP+1
892 BNE ml
893 LDX TEMP
894 INX
895 CPX #3
896 BNE mli
897
898 LDY #ObjCenPos
899 LDA (POINT),Y
900 STA RTEMPA ;Position of center in list
901
902 LDY #ObjData ;Get data pointer for later
903 LDA (POINT),Y
904 STA DATAP
905 TAX
906 INY
907 LDA (POINT),Y
908 STA DATAP+1
909 STA POINT+1
910 STX POINT
911 LDY #00
912 LDA (POINT),Y
913 STA OBTYPE ;Normal/compound
914 normalmodel
915 INY
916 LDA (POINT),Y ;Number of points
917 ;BEQ done
918 bne cont
919 rts
920 cont
921 STA NPOINTS
922 INY
923 LDA (POINT),Y
924 STA NFACES
925 iny
926 tya
927 clc
928 adc POINT
929 sta P0X
930 ldx POINT+1
931 bcc cc1
932 inx
933 clc
934 cc1
935 stx P0X+1
936 adc NFACES
937 sta P0Y
938 bcc cc2
939 inx
940 clc
941 cc2
942 stx P0Y+1
943 adc NFACES
944 sta P0Z
945 bcc cc3
946 inx
947 clc
948 cc3
949 stx P0Z+1
950
951 lda NFACES
952 beq end_prepare_normals
953 jmp prepare_normals
954 +end_prepare_normals
955
956 lda NFACES ; Point pointers
957 clc
958 adc P0Z
959 sta P0X
960 ldx P0Z+1
961 BCC c1
962 INX
963 CLC
964 c1 STX P0X+1
965 ADC NPOINTS
966 STA P0Y
967 BCC c2
968 INX
969 CLC
970 c2 STX P0Y+1
971 ADC NPOINTS
972 STA P0Z
973 BCC c3
974 INX
975 CLC
976 c3 STX P0Z+1
977 ADC NPOINTS
978 STA FACEPTR
979 TXA
980 ADC #00
981 STA FACEPTR+1
982
983 LDY NPOINTS
984 LDX RTEMPA ;Center index
985 SEC ;Rot and proj
986 JSR ROTPROJ
987
988 LDA NFACES ;Maybe just want tointsate p
989 BEQ done
990 LDA FACEPTR
991 LDY FACEPTR+1
992 ;LDX OBTYPE ; Draw depending on object type.
993
994 dloop
995 jmp DrawFace
996 +draw_face_end
997 DEC NFACES
998 BNE dloop
999 done RTS
1000
1001 .)
1002
1003
1004 ;
1005 ; DrawFace -- Draw a polygon
1006 ;
1007 ; On entry .AY points to face data
1008 ; On exit .AY points to next face
1009 ;
1010 ; Wireframe C clear -> face not drawn
1011 ;
1012
1013 NVERTS .byt 00 ;Number of vertices
1014 FACEPTR .word 00 ;Pointer to face
1015
1016 DrawFace
1017 .(
1018 STA POINT
1019 STY POINT+1
1020
1021 LDY #00
1022 LDA (POINT),Y
1023 STA NVERTS
1024 SEC ;N+1 in list
1025 ADC #1 ;(closes on itself)
1026 ADC POINT
1027 STA FACEPTR ;Next face
1028 LDA #00
1029 STA TEMP
1030 ADC POINT+1
1031 STA FACEPTR+1
1032
1033 ;Wireframe routine
1034 Wire
1035 lsr facevis+2
1036 ror facevis+1
1037 ror facevis
1038 bcc exit
1039
1040 ;LDY #2 ;Connect the dots...
1041 ldy #1
1042 l2 LDA (POINT),Y
1043 TAX
1044 LDA PLISTX,X
1045 STA X1
1046 LDA PLISTX+MAXVERTEX,X
1047 STA X1+1
1048 LDA PLISTY,X
1049 STA Y1
1050 LDA PLISTY+MAXVERTEX,X
1051 STA Y1+1
1052 STX RTEMPA
1053
1054 INY
1055 LDA (POINT),Y
1056 TAX
1057 LDA PLISTX,X
1058 STA X2
1059 LDA PLISTX+MAXVERTEX,X
1060 STA X2+1
1061 LDA PLISTY,X
1062 STA Y2
1063 LDA PLISTY+MAXVERTEX,X
1064 STA Y2+1
1065 STY RTEMPY
1066
1067
1068 ; Check for line connections
1069 ; Points in RTEMPA and .X
1070 LDY #00
1071 loop CPY NUMLINES
1072 BEQ store
1073 TXA
1074 CMP LINELO,Y
1075 BNE test2
1076 LDA RTEMPA
1077 CMP LINEHI,Y
1078 BEQ skip
1079 incy INY
1080 BPL loop
1081 store LDA RTEMPA
1082 STA LINELO,Y
1083 TXA
1084 STA LINEHI,Y
1085 CPY #MAXLINES
1086 BEQ godraw
1087 INC NUMLINES
1088
1089 godraw
1090 JSR _DrawClippedLine
1091
1092 skip LDY RTEMPY
1093 DEC NVERTS
1094 BNE l2
1095
1096
1097 SEC
1098 BCS exit
1099 test2 CMP LINEHI,Y
1100 BNE incy
1101 LDA RTEMPA
1102 CMP LINELO,Y
1103 BNE incy
1104 BEQ skip
1105
1106 #endif
1107
1108 exit LDA FACEPTR
1109 LDY FACEPTR+1
1110 ;RTS
1111 jmp draw_face_end
1112 .)
1113
1114
1115 #define cx_ tmp3
1116 #define cy_ tmp4
1117 #define cz_ tmp5
1118 #define tempx op2
1119
1120 prepare_normals
1121 .(
1122 ; Prepare call to ROTPROJ in mode "Rotation only"
1123 ldy NFACES
1124 ldx RTEMPA ;Center index
1125 clc ;Rot and NOT proj
1126 jsr ROTPROJ
1127
1128 ; Save center coordinates in cx_, cy_, cz_ for later.
1129 ldx RTEMPA
1130 lda CX,x
1131 sta cx_
1132 lda HCX,x
1133 sta cx_+1
1134
1135 lda CY,x
1136 sta cy_
1137 lda HCY,x
1138 sta cy_+1
1139
1140 lda CZ,x
1141 sta cz_
1142 lda HCZ,x
1143 sta cz_+1
1144
1145 ; If too far we patch code below and avoid calculating the
1146 ; dot product between the normal and the view vector.
1147 ; Just will use the normal Z sign.
1148
1149 cmp #5
1150 bcc nopatch
1151
1152 lda #$18 ; clc opcode
1153 sta _patch_code
1154 jmp startcheck
1155
1156 nopatch
1157 lda #$38 ; sec opcode
1158 sta _patch_code
1159
1160 ; Adjust center coordinates, so they are 7-bit signed and SMULT can be used
1161 loopadj
1162 lda cx_+1
1163 beq conty
1164 bpl shift
1165 eor #$ff
1166 bne shift
1167 conty
1168 lda cy_+1
1169 beq contz
1170 bpl shift
1171 eor #$ff
1172 bne shift
1173 contz
1174 lda cz_+1
1175 beq endloop
1176 shift
1177 lda cx_+1
1178 cmp #$80
1179 ror cx_+1
1180 ror cx_
1181
1182 lda cy_+1
1183 cmp #$80
1184 ror cy_+1
1185 ror cy_
1186
1187 ; Z is allways positive (or it won't be visible)
1188 lsr cz_+1
1189 ror cz_
1190
1191 jmp loopadj
1192 endloop
1193
1194 ; We need two more adjustments
1195 ; To be sure the amount is 7-bit signed (-64..64).
1196
1197 lda cx_+1
1198 cmp #$80
1199 ror
1200 ror cx_
1201 cmp #$80
1202 ror
1203 ror cx_
1204 sta cx_+1
1205
1206 lda cy_+1
1207 cmp #$80
1208 ror
1209 ror cy_
1210 cmp #$80
1211 ror
1212 ror cy_
1213 sta cy_+1
1214
1215 ; Z is allways positive (or it wouldn't be visible)
1216 lsr cz_+1
1217 ror cz_
1218 lsr cz_+1
1219 ror cz_
1220
1221 startcheck
1222 ; Prepare facevis bitfield.
1223
1224 lda #0
1225 sta facevis
1226 sta facevis+1
1227 sta facevis+2
1228
1229
1230 ; Loop through face list
1231 ldx NFACES
1232 dex
1233 stx tempx
1234 loop
1235 ; First check. If all normals are zero, face is visible
1236 ldx tempx
1237 lda PLISTX,x
1238 ora PLISTY,x
1239 ora PLISTZ,x
1240 bne cont
1241 sec
1242 jmp vis
1243 cont
1244
1245 _patch_code
1246 sec ; This is patched to clc or sec
1247 bcs cont2
1248 lda PLISTZ,x
1249 bpl noneg
1250 clc
1251 jmp vis
1252 noneg
1253 sec
1254 jmp vis
1255 cont2
1256
1257
1258 ; For each face we need to calculate the dot-product
1259 ; of the center and the normal.
1260 ; <cx_,cy_,cz_>*<nx,ny,nz>=
1261 ; cx_*nx+cy_*ny+cz_*nz.
1262 ; All within range of SMULT
1263 ; op1 (2-byte) will store the 16-bit result
1264
1265 lda #0
1266 sta op1+1
1267
1268 lda PLISTX,x
1269 ldy cx_
1270 SMULT
1271 sta op1
1272 bpl doY
1273 dec op1+1
1274 doY
1275 lda #0
1276 sta tmp
1277 lda PLISTY,x
1278 ldy cy_
1279 SMULT
1280 bpl noneg1
1281 dec tmp
1282 noneg1
1283 clc
1284 adc op1
1285 sta op1
1286 lda tmp
1287 adc op1+1
1288 sta op1+1
1289
1290 doZ
1291 lda #0
1292 sta tmp
1293 lda PLISTZ,x
1294 ldy cz_
1295 SMULT
1296 bpl noneg2
1297 dec tmp
1298 noneg2
1299 clc
1300 adc op1
1301 sta op1
1302 lda tmp
1303 adc op1+1
1304 sta op1+1
1305
1306 doneall
1307 sec
1308 bmi novis
1309 lda op1+1
1310 ora op1
1311 bne vis
1312 ;bpl vis
1313 ;beq vis
1314 novis
1315 clc
1316 vis
1317 rol facevis
1318 rol facevis+1
1319 rol facevis+2
1320 dec tempx
1321 bmi end
1322 jmp loop
1323 end
1324 ;rts
1325 jmp end_prepare_normals
1326 .)
1327
1328
1329
1330 .zero
1331 facevis .dsb 3
1332 .text
1333
1334
1335
1336
1337 __obj3d_end
1338
1339 #echo obj3d: Memory usage
1340 #echo obj3d: Zero page:
1341 #print (__obj3d_zeropage_end - __obj3d_zeropage_start)
1342 #echo obj3d: Normal memory:
1343 #print (__obj3d_end - __obj3d_start)
1344
1345
1346
1347
1348

  ViewVC Help
Powered by ViewVC 1.1.26