*      
* Obj3d test code
*
* SLJ 4/99
*

         ORG $1000

ObjCX    = 0
ObjCY    = 2
ObjCZ    = 4
ObjData  = 6
ObjID    = 8
ObjUser  = 9
ObCenPos = 10
ObjCXRem = 11
ObjCYRem = 12
ObjCZRem = 13
ObjMat   = 14
ObjSize  = 32             ;32 bytes total

                          ;Jump table

Init3D   = $5000          ;Initialize lib3d
AddObj   = Init3D+3       ;Add object to object list
DelObj   = AddObj+3       ;Delete object from list
SetCurOb = DelObj+3       ;Set current object
GetCurOb = SetCurOb+3     ;Get current object
GetNextOb = GetCurOb+3    ;Get next object in list
GetObj   = GetNextOb+3    ;Get pointer to object

SetMat   = GetObj+3       ;Calculate and set object matrix
Pitch    = SetMat+3       ;Pitch - rotate object around x-axis
Yaw      = Pitch+3        ;Yaw - rotate around y-axis
Roll     = Yaw+3          ;Roll - rotate around z-axis
MoveSide = Roll+3         ;Move object
MoveUp   = MoveSide+3
MoveForwards = MoveUp+3
GetSideVec = MoveForwards+3 ;Orientation vectors
GetUpVec = GetSideVec+3   ;(length=64)
GetFrontVec = GetUpVec+3

SetParms = GetFrontVec+3  ;Set rendering parameters
SetVisParms = SetParms+3  ;Set visibility parameters
CalcView = SetVisParms+3  ;Set viewpoint = object
SortVis  = CalcView+3     ;Compute and sort visible objects
DrawAllVis = SortVis+3    ;Draw all visible objects
DrawNextVis = DrawAllVis+3 ;Draw next objesible object list
RotDraw  = DrawNextVis+3  ;Rotate and draw object
DrawFace = RotDraw+3;Draw single face (polygon)

* lib3d stuff

CALCMAT  EQU $8800
ACCROTX  EQU $8803
ACCROTY  EQU $8806
ACCROTZ  EQU $8809
GLOBROT  EQU $880C
ROTPROJ  EQU $880F
POLYFILL EQU $8812
PLOT     EQU $8815
DRAWLINE EQU $8818
VERSION  EQU $881B
PQ       EQU $0200        ;Point queue


*===============================

temp     = $58            ;two bytes
point    = $5a            ;two bytes

X1       = $60            ;Line x-coord
Y1       = $62
X2       = $64
Y2       = $66


*
* Test code
*
* Nice pair of tets
*

OBS      = $0800          ;Object records
GETIN    = $FFE4

TestCode 
         LDA $D011
         ORA #$20         ;Bitmap mode
         STA $D011
         LDA #$08         ;Bitmap -> $6000
         STA $D018
         LDA $DD00
         AND #$F8
         ORA #$02         ;Bank 1
         STA $DD00
         LDA #$80
         STA $028A        ;All keys repeat

         JSR VERSION      ;If multicolor...
         BPL :hires
         LDA $D016
         ORA #$10
         STA $D016
:hires   
         JSR ClrColMap

         LDA #<OBS        ;Object records
         LDY #>OBS
         JSR Init3D

         LDA #<TETDAT     ;Object data
         LDY #>TETDAT
         LDX #01          ;ID
         JSR AddObj
         STX VOB          ;View object

         LDA #<STARDAT
         LDY #>STARDAT
         LDX #02
         JSR AddObj
         STX ROB          ;Rotate object
         STA POINT        ;Object pointer
         STY POINT+1
         LDY #5           ;Set center
:l1      LDA OCEN,Y
         STA (POINT),Y
         DEY
         BPL :l1

:sec     SEC              ;Solid polygons
:setp    LDX #$60         ;Bitmap at $6000
         LDA #<PATS       ;Pattern table
         LDY #>PATS
         JSR SetParms
:loop    
         JSR ClrBitmap

         LDX VOB          ;Calculate view
         JSR CalcView
         JSR SortVis      ;Sort objects
         JSR DrawAllVis   ;Draw objects

         LDX ROB          ;Set rotation object
         JSR SetCurOb

:wait    JSR GETIN
         BEQ :wait
         CMP #'a'
         BEQ :pitchdn
         CMP #'z'
         BEQ :pitchup
         CMP #'q'
         BEQ :rolll
         CMP #'w'
         BEQ :rollr
         CMP #'s'
         BEQ :yawl
         CMP #'d'
         BEQ :yawr
         CMP #'@'
         BEQ :movf
         CMP #'/'
         BEQ :movb
         CMP #'='
         BEQ :wire
         CMP #'*'
         BEQ :sec
         CMP #' '
         BNE :wait

         LDX VOB          ;Swap viewpoint
         JSR SetCurOb
         JSR GetNextOb
         STX VOB
         JMP :loop

:wire    CLC
         BCC :setp

:yawr    CLC
         DFB $24
:yawl    SEC
         JSR Yaw
         JMP :loop

:pitchdn CLC
         DFB $24
:pitchup SEC
         JSR Pitch
         JMP :loop

:rolll   CLC
         DFB $24
:rollr   SEC
         JSR Roll
         JMP :loop

:movf    LDA #$07
         DFB $2C
:movb    LDA #$F9
         JSR MoveForwards
         JMP :loop

ClrBitmap
         LDY #00
         TYA
:loop    STA $6000,Y
         STA $6100,Y
         STA $6200,Y
         STA $6300,Y
         STA $6400,Y
         STA $6500,Y
         STA $6600,Y
         STA $6700,Y
         STA $6800,Y
         STA $6900,Y
         STA $6A00,Y
         STA $6B00,Y
         STA $6C00,Y
         STA $6D00,Y
         STA $6E00,Y
         STA $6F00,Y
         STA $7000,Y
         STA $7100,Y
         STA $7200,Y
         STA $7300,Y
         STA $7400,Y
         STA $7500,Y
         STA $7600,Y
         STA $7700,Y
         STA $7800,Y
         STA $7900,Y
         STA $7A00,Y
         STA $7B00,Y
         STA $7C00,Y
         STA $7D00,Y
         STA $7E00,Y
         STA $7F00,Y
         INY
         BNE :loop
         RTS

ClrColMap                 ;Only need to call once
         LDX #00
         LDA #$12
:loop    STA $4000,X
         STA $4100,X
         STA $4200,X
         STA $4300,X
         INX
         BNE :loop

         JSR VERSION
         BPL :rts
         LDA #00          ;Multicolor mode
         STA $D020
         STA $D021
         LDA #14
:l2      STA $D800,X
         STA $D900,X
         STA $DA00,X
         STA $DB00,X
         INX
         BNE :l2
:rts     RTS


VOB      DFB 00           ;View object
ROB      DFB 00           ;2nd object

OCEN     DA 00            ;X-coord
         DA 00            ;Y-coord
         DA $0100         ;Z-coord

PATS                      ;Pattern table
SOLID    = 0
         HEX FFFFFFFFFFFFFFFF
DITHER1  = 1
         HEX 55AA55AA55AA55AA
DITHER2  = 2
         HEX AA55AA55AA55AA55
ZIGS     = 3
         HEX EEDDBB77EEDDBB77
ZAGS     = 4
         HEX 77BBDDEE77BBDDEE
ZIGZAG   = 5
         HEX BB55EEFFBB55EEFF
CROSSSM  = 6
         HEX 55EE55BB55EE55BB
BRICK    = 7
         HEX 00EEEEEE00BBBBBB
SQUARES  = 8
         HEX FFAAFFAAFFAAFFAA
INVSQ    = 9
         HEX 0055005500550055
HOLES    = 10
         HEX BD7EE7DBDBE77EBD
HSTRIPES = 11
         HEX FF00FF00FF00FF00
VSTRIPES = 12
         HEX AAAAAAAAAAAAAAAA


*
* Test object 1: simple tetrahedron
*
TETDAT   
         DFB 0            ;Normal object
         DFB 4            ;Number of points
         DFB 4            ;Number of faces

* Point list

TETX     DFB 45,45,0-45,0-45
TETY     DFB 45,0-45,45,0-45
TETZ     DFB 45,0-45,0-45,45

* Face list

FACE1    DFB 3            ;Number of vertices
         DFB SOLID        ;Fill pattern
         DFB 0,1,2,0      ;Vertices

FACE2    DFB 3
         DFB ZIGS
         DFB 3,2,1,3

FACE3    DFB 3
         DFB CROSSSM
         DFB 3,0,2,3

FACE4    DFB 3
         DFB HOLES
         DFB 3,1,0,3

*
* Test object 2: a compound object,
* spaceship-kind of thing (essentially
* cool world stars)
*
STARDAT  

         DFB $80          ;Compound object
         DFB 14           ;Number of points
         DFB 6            ;Number of oblets

* Point list

         DFB 50,0-50,0,0,0,0,15,15,15,15,0-15,0-15,0-15,0-15
         DFB 0,0,16,0-26,0,0,10,10,0-10,0-10,10,10,0-10,0-10
         DFB 0,0,0,0,94,0-22,15,0-15,0-15,15,15,0-15,0-15,15

* Oblet list: reference points

         DFB 0            ;First 6 points
         DFB 1
         DFB 2
         DFB 3
         DFB 4
         DFB 5

* Oblet 1

         DFB 26           ;26 bytes
         DFB 4            ;4 faces

* faces
         DFB 3            ;4 points
         DFB SOLID        ;pattern
         DFB 0,8,7,0      ;Star 2, Tine 0, face 1

         DFB 3
         DFB ZIGS
         DFB 0,7,6,0

         DFB 3
         DFB ZAGS
         DFB 0,6,9,0

         DFB 3
         DFB DITHER1
         DFB 0,9,8,0

* Oblet 2

         DFB 26           ;26 bytes
         DFB 4            ;4 faces


         DFB 3            ;4 points
         DFB ZIGS
         DFB 1,11,12,1

         DFB 3
         DFB BRICK
         DFB 1,12,13,1

         DFB 3
         DFB DITHER2
         DFB 1,13,10,1

         DFB 3
         DFB ZAGS
         DFB 1,10,11,1

* Oblet 3
         DFB 26           ;26 bytes
         DFB 4            ;4 faces

         DFB 3
         DFB SQUARES
         DFB 2,7,11,2

         DFB 3
         DFB INVSQ
         DFB 2,6,7,2

         DFB 3
         DFB DITHER1
         DFB 2,10,6,2

         DFB 3
         DFB DITHER2
         DFB 2,11,10,2

* Oblet 4
         DFB 26
         DFB 4

         DFB 3
         DFB CROSSSM
         DFB 3,12,8,3

         DFB 3
         DFB HOLES
         DFB 3,8,9,3

         DFB 3
         DFB VSTRIPES
         DFB 3,9,13,3

         DFB 3
         DFB SOLID
         DFB 3,13,12,3

* Oblet 5
         DFB 26           ;26 bytes
         DFB 4            ;4 faces

         DFB 3
         DFB DITHER1
         DFB 4,9,6,4

         DFB 3
         DFB SOLID
         DFB 4,6,10,4

         DFB 3
         DFB DITHER2
         DFB 4,10,13,4

         DFB 3
         DFB HSTRIPES
         DFB 4,13,9,4

* Oblet 6
         DFB 26           ;26 bytes
         DFB 4            ;4 faces

         DFB 3
         DFB ZIGZAG
         DFB 5,8,12,5

         DFB 3
         DFB HSTRIPES
         DFB 5,12,11,5

         DFB 3
         DFB VSTRIPES
         DFB 5,11,7,5

         DFB 3
         DFB DITHER2
         DFB 5,7,8,5

