;APS00002D36000083010000851800000FDA0000000000000AB400000000000000000000000000000000
;------------------------------------------------
; c2p Os v1.0 by Skjeggspir,Brekke and Nightraver
; Txture+Gouraud routine by Skjeggspir&Brekke
; Txture+plainshade by Skjeggspir
; Gouraud routine by Skjeggspir
; Plainshade by Skjeggspir
; Rotation by Skjeggspir
; Sorter by Ludde&Skjeggspir
; World routine by Skjeggspir&brekke
;-------------------------------------------------
;Engine code 1997-1998 SP&brekke
;            1998-1999 SP (SKJEGGSPIR)


DEBUG_MODE=1

;--------hvilke c2p'er som skal includes ------- 
_Cpu3blit=0		; 8 bpl 020/030 (040/060) cpu+blitter
_Cpu4=0			; 4 bpl 020-060 cpu only
_Cpuscr=0		; 8 bpl 020-060 cpu only
_Ham8scr=0		; 8 bpl 020-060 cpu only
_C2p060=0
_Ham6scr=1		; 4 bpl 020-060 cpu only
_Ham7scr=1		; 5 bpl 020-060 cpu only
_Cpu4scr=0
_Ham6=0
;----------------------------------------
Bit24=0			;scale 24bit->18bit ? i ham8scr\.fixscrham
Music=0
Manual_camera=1

**************** 3d strukturer 


	incdir	include:
	include	lvo/exec_lib.i
	include	lvo/dos_lib.i
	include	exec/types.i
 
	STRUCTURE	objekta,0		;ctz8

	LONG	Uobjstart
	LONG	Clipobjflags
	LONG	Coord3dstart
	LONG	Matrixstart
	LONG	Faceoptstart
	LONG	Linjer2start
	LONG	Flagstart
	LONG	Coord2dstart
	LONG	Header
	LONG	Antpunkter
	LONG	Antfaces
	LONG	Antlinjer
	LONG	Boundboxstart
	LONG	Coordstart
	LONG	Facestart
	LONG	Normstart
	LONG	Facenormstart
	LONG	Linjefacestart
	LONG	Linjerstart
	LONG	Fargerstart
	LONG	Navnstart

****************************************************************
;relative adresser til obj data:
;Uobjstart =-24
;Matrixstart  = -20
;Faceoptstart = -16
;Linjer2start = -12
;Flagstart = -8
;Coord2dstart = -4
;Antpunkter = 4
;Antfaces = 8 
;Antlinjer  = 12
;Maksradius = 16
;Coordstart = 20
;Facestart = 24
;Normstart = 28
;Linjefacestart = 32
;Linjerstart = 36
;Fargerstart = 40

    STRUCTURE	scene,0
	LONG	Sheader
	LONG	Numberofobjects
	LONG	Objnamesptr
	LONG	Objlistptr
	LONG	Objectinfoptr	
	LONG	Numberofframes

;30
    STRUCTURE	matrix_3x3,0
	WORD	M11
	WORD	M12
	WORD	M13
	WORD	M21
	WORD	M22
	WORD	M23
	WORD	M31
	WORD	M32
	WORD	M33
	WORD	Xangle
	WORD	Yangle
	WORD	Zangle
	WORD	Xrotpos
	WORD	Yrotpos
	WORD	Zrotpos
	WORD	Xpos
	WORD	Ypos
	WORD	Zpos
	WORD	Relxpos
	WORD	Relypos
	WORD	Relzpos
	WORD	Pivotx	
	WORD	Pivoty	
	WORD	Pivotz
	LONG	Uobjarray
	LONG	Matrixpre
	WORD	Matrix_size

**************************

********  3d konstanter ************
Maksantallfaces=5000
Maksantalllinjer=(Maksantallfaces*3)
Maksantallcoords=10000

Filselect=0
Control=1

E_bredde=320
E_hoyde=180	;180		;200	;156		;128	;156	;192
Zbehind=20
Id_objekt=1				; alloc id
Taakefarge=$0
******************************************************************


	section	CONTRAZ_ONLY,code_f

MALLOC	MACRO				; size,id,type -> peker i d0
	movem.l	d1-d7/a0-a6,-(sp)
	move.l	\1,d0
	move.l	\2,d1
	move.l	DEMOBASE,a6
	jsr	_LVOAlloc\3(a6)
	movem.l	(sp)+,d1-d7/a0-a6
	ENDM

READFILE MACRO		; a0,dennetxt,#256*256+1024
	movem.l	d0-d7/a0-a6,-(sp)

	move.l	DEMOVARS,a4
	move.l	\2,a1
	move.l	\1,a0
	move.l	\3,d0

	move.l	DEMOBASE,a6
        jsr     _LVOLoadFile(a6)
;A4=DEMOVARS|D0=Size|A0=Name|A1=Buffer
	movem.l	(sp)+,d0-d7/a0-a6

;	move.l	DEMOVARS,a1
;	move.l	LIB.DOSBASE(a1),a6
;;	move.l	dosbase,a6
;	clr.l	d0
;	move.l	\1,d1
;	move.l	#1005,d2
;	jsr	_LVOOpen(a6)
;	move.l	d0,-(sp)
;	move.l	d0,d1
;	move.l	\2,d2
;	move.l	\3,d3
;	jsr	_LVORead(a6)
;	move.l	(sp)+,d1
;	jsr	_LVOClose(A6)

	endm

	incdir	include:
	include	antisys/antisys+.i
	include	include:ctzc2p++.i
	IFNE	Music
	include	include:plays3m.i
	endb	a5
	even
	ENDIF

	CODESTART
 	SCREENOPEN #E_bredde*4,#E_hoyde,Superhires,Yx1,cmap,HAM7SCR,#0
;	VBLWAIT 222
;	move.l	#$dff000,a6		;interrupts må på pga loading
;	move.w	$1c(a6),kake2

	move.l	#$dff000,a6		;interrupts må på pga loading
	move.w	$1c(a6),d0
	or.w	#$8008,d0
	move.w	d0,Intena(a6)
start1

	bsr.w	INITSCENE
	bsr.w	MAKEOBJLIST
	bsr.w	INITSHADES
	bsr.w	INITOBJECTS

	move.l	#copper,$dff080
	move.w	#%1000001110001111,$dff096
	
	jsr	Readkey.on		;på med level2 interrupt
	clr.l	framecounter

;	bra END
	

HOVED:
;	rept 20
;	vblwait 255
;	endr
.neste
	CALL	scrswap

	bsr.w	TIMER\.init
	CALL	c2p
	bsr.w	TIMER\.done
	move.l	d0,c2pspeed

	bsr.w	clear
	bsr.w	TIMER\.init
 	bsr.w	MAKEMATRIX
	clr.l	(a3)+			;viktig!

	bsr.w	ROTATEPOINTS
	CALL_T	INSIDEOUT
	move.l	d0,backface

	bsr	ZCLIP

 	CALL_T	SORTPOLYS
	move.l	d0,qsorts


	bsr.w	ROTATENORMALS
	bsr.w	ROTATEFACENORMALS

	CALL_T	LAGLINJE
	move.l	d0,txtmappass1
	CALL_T	RENDER
	move.l	d0,txtmappass2
;	IFNE	Control
;	bsr.w	MUSKONTROL
 
;	ENDC

	btst #6,$bfe001
	bne.w	HOVED

	btst	#10,$dff016
	bne.w	HOVED
END:	
	btst	#6,$bfe001
	beq.b	END

	IFNE	Music
	bsr.w   s3end
	ENDIF
;	jsr	readkey.off
;	
;	CODEQUIT
;	bsr.w	SKRIVSTATS

	rts


KEYBOARDKONTROL:
	moveq	#0,d0
	move.b	key,d0
	btst	#7,d0
	bne.s	.exit

	cmp.b	#$6f,d0
	bhi.s	.exit

	move.l	.JumpTable(pc,d0.w*4),a0
	tst.l	a0
	beq.s	.exit
	jmp	(a0)

.exit	rts

.JumpTable	dc.l	0			;$00
		dc.l	0			;$01
		dc.l	0			;$02
		dc.l	0			;$03
		dc.l	0			;$04
		dc.l	0			;$05
		dc.l	0			;$06
		dc.l	0			;$07
		dc.l	0			;$08
		dc.l	0			;$09
		dc.l	0			;$0a
		dc.l	0			;$0b
		dc.l	0			;$0c
		dc.l	0			;$0d
		dc.l	0			;$0e
		dc.l	0			;$0f
		dc.l	0			;$10
		dc.l	0			;$11
		dc.l	0			;$12
		dc.l	0			;$13
		dc.l	0			;$14
		dc.l	0			;$15
		dc.l	0			;$16
		dc.l	0			;$17
		dc.l	0			;$18
		dc.l	0			;$19
		dc.l	0			;$1a
		dc.l	0			;$1b
		dc.l	0			;$1c
		dc.l	0			;$1d
		dc.l	0			;$1e
		dc.l	0			;$1f
		dc.l	.zup			;$20 a
		dc.l	.zdown			;$21
		dc.l	0			;$22
		dc.l	0			;$23
		dc.l	0			;$24
		dc.l	0			;$25
		dc.l	0			;$26
		dc.l	0			;$27
		dc.l	0			;$28
		dc.l	0			;$29
		dc.l	0			;$2a
		dc.l	0			;$2b
		dc.l	0			;$2c
		dc.l	0			;$2d
		dc.l	0			;$2e
		dc.l	0			;$2f
		dc.l	0			;$30
		dc.l	.sides2			;$31 z
		dc.l	.sides1			;$32 x
		dc.l	0			;$33
		dc.l	0			;$34
		dc.l	0			;$35
		dc.l	0			;$36
		dc.l	0			;$37
		dc.l	0			;$38
		dc.l	0			;$39
		dc.l	0			;$3a
		dc.l	0			;$3b
		dc.l	0			;$3c
		dc.l	0			;$3d
		dc.l	0			;$3e
		dc.l	0			;$3f
		dc.l	0			;$40
		dc.l	0			;$41
		dc.l	0			;$42
		dc.l	0			;$43	; -
		dc.l	0			;$44
		dc.l	0			;$45
		dc.l	0			;$46
		dc.l	0			;$47
		dc.l	0			;$48
		dc.l	0         		;$49
		dc.l	0			;$4a
		dc.l	0			;$4b
		dc.l	.backward		;$4c arrow up
		dc.l	.forward		;$4d arrow down
		dc.l	.right			;$4e arrow right
		dc.l	.left			;$4f arrow left
		dc.l	0			;$50
		dc.l	0			;$51
		dc.l	0			;$52
		dc.l	0			;$53
		dc.l	0			;$54
		dc.l	0			;$55
		dc.l	0			;$56
		dc.l	0			;$57
		dc.l	0			;$58
		dc.l	0			;$59
		dc.l	0			;$5a
		dc.l	0			;$5b
		dc.l	0			;$5c
		dc.l	0			;$5d
		dc.l	0			;$5e
		dc.l	0			;$5f
		dc.l	0			;$60
		dc.l	0			;$61	; right
		dc.l	0			;$62
		 dc.l	0			;$63	; left
		dc.l	0			;$64
		dc.l	0		;$65	; down
		dc.l	0			;$66
		dc.l	0		;$67	; up
		dc.l	0			;$68
		dc.l	0			;$69
		dc.l	0			;$6a
		dc.l	0			;$6b	; +
		dc.l	0			;$6c
		dc.l	0			;$6d
		dc.l	0			;$6e
		dc.l	0			;$6f
		dc.l	0			;$70
		dc.l	0			;$71
		dc.l	0			;$72
		dc.l	0			;$73
		dc.l	0			;$74
		dc.l	0			;$75
		dc.l	0			;$76
		dc.l	0			;$77
		dc.l	0			;$78
		dc.l	0			;$79
		dc.l	0			;$7a
		dc.l	0			;$7b
		dc.l	0			;$7c
		dc.l	0			;$7d
		dc.l	0			;$7e
		dc.l	0			;$7f
		dc.l	0			;$80
		dc.l	0			;$81
		dc.l	0			;$82
		dc.l	0			;$83
		dc.l	0			;$84
		dc.l	0			;$85
		dc.l	0			;$86
		dc.l	0			;$87
		dc.l	0			;$88
		dc.l	0			;$89
		dc.l	0			;$8a
		dc.l	0			;$8b
		dc.l	0			;$8c
		dc.l	0			;$8d
		dc.l	0			;$8e
		dc.l	0			;$8f
		dc.l	0			;$90
		dc.l	0			;$91
		dc.l	0			;$92
		dc.l	0			;$93
		dc.l	0			;$94
		dc.l	0			;$95
		dc.l	0			;$96
		dc.l	0			;$97
		dc.l	0			;$98
		dc.l	0			;$99
		dc.l	0			;$9a
		dc.l	0			;$9b	; x
		dc.l	0			;$9c
		dc.l	0			;$9d	; z
		dc.l	0			;$9e
		dc.l	0			;$9f
		dc.l	0			;$a0
		dc.l	0			;$a1
		dc.l	0			;$a2
		dc.l	0			;$a3
		dc.l	0			;$a4
		dc.l	0			;$a5
		dc.l	0			;$a6
		dc.l	0			;$a7
		dc.l	0			;$a8
		dc.l	0			;$a9
		dc.l	0			;$aa
		dc.l	0			;$ab
		dc.l	0			;$ac
		dc.l	0			;$ad
		dc.l	0			;$ae
		dc.l	0			;$af
		dc.l	0			;$b0
		dc.l	0			;$b1
		dc.l	0			;$b2
		dc.l	0			;$b3
		dc.l	0			;$b4
		dc.l	0			;$b5
		dc.l	0			;$b6
		dc.l	0			;$b7
		dc.l	0			;$b8
		dc.l	0			;$b9
		dc.l	0			;$ba
		dc.l	0			;$bb	; d
		dc.l	0			;$bc
		dc.l	0			;$bd	; z
		dc.l	0			;$be
		dc.l	0			;$bf

		dc.l	0			;$c0
		dc.l	0			;$c1
		dc.l	0			;$c2
		dc.l	0			;$c3
		dc.l	0			;$c4
		dc.l	0			;$c5
		dc.l	0			;$c6
		dc.l	0			;$c7
		dc.l	0			;$c8
		dc.l	0			;$c9
		dc.l	0			;$ca
		dc.l	0			;$cb	; d
		dc.l	0			;$cc
		dc.l	0			;$cd	; z
		dc.l	0			;$ce
		dc.l	0			;$cf

		dc.l	0			;$d0
		dc.l	0			;$d1
		dc.l	0			;$d2
		dc.l	0			;$d3
		dc.l	0			;$d4
		dc.l	0			;$d5
		dc.l	0			;$d6
		dc.l	0			;$d7
		dc.l	0			;$d8
		dc.l	0			;$d9
		dc.l	0			;$da
		dc.l	0			;$db	; d
		dc.l	0			;$dc
		dc.l	0			;$dd	; z
		dc.l	0			;$de
		dc.l	0			;$df

.MoveUP
	rts
.MoveDown
	rts

.forward
	move.l	camptr,a0
	sub.w	#8,Xrotpos(a0)
	rts
.backward
	move.l	camptr,a0
	add.w	#8,Xrotpos(a0)
	rts
.right
	move.l	camptr,a0
	add.w	#8,Yrotpos(a0)
	rts
.left
	move.l	camptr,a0
	sub.w	#8,Yrotpos(a0)	
	rts
.sides1
	move.l	camptr,a0
	move.w  M11(a0),d0
	move.w  M21(a0),d1
	move.w  M31(a0),d2
	moveq.l #12,d7
	asr.w   d7,d0
	asr.w   d7,d1
	asr.w   d7,d2
	sub.w   d0,Xpos(a0)
	sub.w   d1,Ypos(a0)
	sub.w   d2,Zpos(a0)
	rts
.sides2
	move.l	camptr,a0
	move.w  M11(a0),d0
	move.w  M21(a0),d1
	move.w  M31(a0),d2
	moveq.l #12,d7
	asr.w   d7,d0
	asr.w   d7,d1
	asr.w   d7,d2
	add.w   d0,Xpos(a0)
	add.w   d1,Ypos(a0)
	add.w   d2,Zpos(a0)

	rts
.zup
	move.l	camptr,a0
	move.w  M13(a0),d0
	move.w  M23(a0),d1
	move.w  M33(a0),d2
	moveq.l #12,d7
	asr.w   d7,d0
	asr.w   d7,d1
	asr.w   d7,d2
	sub.w   d0,Xpos(a0)
	sub.w   d1,Ypos(a0)
	sub.w   d2,Zpos(a0)

	rts
.zdown
	move.l	camptr,a0
	move.w  M13(a0),d0
	move.w  M23(a0),d1
	move.w  M33(a0),d2
	moveq.l #12,d7
	asr.w   d7,d0
	asr.w   d7,d1
	asr.w   d7,d2
	add.w   d0,Xpos(a0)
	add.w   d1,Ypos(a0)
	add.w   d2,Zpos(a0)

	rts

kake:	dc.l	0


ZCLIP:

;	move.l	antallfaces,.antallfaces
	move.l	#0,.antallfaces
	move.l	#0,.antcoords
	move.l	#0,.antlinjer
	

	lea	clipobject,a0
	move.l	Clipobjflags(a0),a1	;peker til poligoner som skal klippes
.cliploop:
	move.l	(a1)+,d0
	beq.w	.ut
.kake
	move.l	d0,a2			;objektpeker
	move.w	(a1)+,d0		;facepeker
	move.l	Coord2dstart(a2),a3	;	
	move.l	Coord3dstart(a2),a4	;	
	move.l	Facestart(a2),a5
	move.l	Coord2dstart(a2),a6	;Nye coord2d coords	


	movem.w	(a5,d0.w*8),d1-d3	;hent 3 cordinat pekere 	

	move.w	(a3,d1.w*8),d4		;z0
	move.w	(a3,d2.w*8),d5		;z1
	move.w	(a3,d3.w*8),d6		;z2
	
	cmp.w	d5,d6			;sorter
	bgt.b	.o			;d6>d5
	exg.l	d5,d6			;swap	z
	exg.l	d2,d3			;swap	peker
.o
	cmp.w	d4,d6			;sorter
	bgt.b	.o2			;d6>d4
	exg.l	d4,d6
	exg.l	d1,d3
.o2
	cmp.w	d4,d5			;sorter
	bgt.b	.o3			;d6>d5>d4
	exg.l	d4,d5
	exg.l	d1,d2
.o3
******* Minste z er nærmest kamera ***************

	cmp.w	#Zbehind,d5
	bge.b	.to_utenfor

	bra.w	.neste
.to_utenfor
	move.l	(a3,d3.w*8),(a6)+	;z1,x1,y1,farge
	move.l	4(a3,d3.w*8),(a6)+

	
				;To punkter utenfor, 1 nytt poligon.
				;finner skjæringen med z=Zbehind planet
				;x2-x1    Dx         (x2-x1)(zbh-z1)
				;----- = ------   -> - -------------- =Dx
				;z2-z1   zbehind-z1       z2-z1
				;(y2-y1) (zbh-z1)
				;   ---------------  = Dy
				;     z2-z1
	move.w	d5,.hoyz	;

	sub.w	d6,d5		;z2-z1
	ext.l	d5
	bne.b	.ok
	or.b	#1,d5
.ok

	move.l	#Zbehind,d7	;zbh-z1
	sub.w	d6,d7
	swap	d7
	
	divs.l	d5,d7		; (zbh-z1)<<16 / z2-z1

	move.w	(a4,d2.w*4),d5	;coord3d
	sub.w	(a4,d3.w*4),d5	;x2-x1
	ext.l	d5
	muls.l	d7,d5
	swap	d5
	add.w	(a4,d3.w*4),d5	;x+dx=X

	move.w	2(a4,d2.w*4),d6
	sub.w	2(a4,d3.w*4),d6	;y2-y1
	ext.l	d6
	muls.l	d7,d6
	swap	d6
	add.w	2(a4,d3.w*4),d6	;y+dy=Y
	
	move.l	#(2^11*ROTATEPOINTS\.dist)/Zbehind,d7		;dist/z
		
	muls.w	d7,d5
	muls.w	d7,d6

	moveq.l	#11,d7
	asr.l	d7,d5			;x
	asr.l	d7,d6			;y

	move.w	#Zbehind,(a6)+		;z2
	move.w	d5,(a6)+		;x2
	move.w	d6,(a6)+		;y2
 move.w	#$ffff,(a6)+	

	sub.w	(a3,d3.w*8),d6
	sub.w	d6,d4		;z3-z1
	ext.l	d4
	bne.b	.ok2
	or.b	#1,d4
.ok2


	move.l	#Zbehind,d7
	sub.w	d6,d7		;zbh-z1
	swap	d7
	divs.l	d4,d7

	move.w	(a4,d1.w*4),d4	;x3-x1
	sub.w	(a4,d3.w*4),d4
	ext.l	d4
	muls.l	d7,d4
	swap	d4
	add.w	(a4,d3.w*4),d4	;x1=x1+x

	move.w	2(a4,d1.w*4),d6	;y3-y1
	sub.w	2(a4,d3.w*4),d6
	ext.l	d6
	muls.l	d7,d6
	swap	d6
	add.w	2(a4,d3.w*4),d6	;y1=y1+y


	move.l	#(2^11*ROTATEPOINTS\.dist)/Zbehind,d7		;dist/z

	muls.w	d7,d4
	muls.w	d7,d6

	moveq.l	#11,d7
	asr.l	d7,d4			;x
	asr.l	d7,d6			;y

	move.w	#Zbehind,(a6)+		;z2
	move.w	d4,(a6)+		;x2
	move.w	d6,(a6)+		;y2
 move.w	#$ffff,(a6)+	

; lage linje tabell

	move.l	.antcoords,d6
	move.l	.antlinjer,d7		;øker med 4 hver gang
	move.l	Linjerstart(a0),a5

	lea	(a5,d7.w*4),a5		;nåverende linje

	move.w	d6,(a5)+		;p1
	addq.l	#1,d6
	move.w	d6,(a5)+		;p2
	add.l	#14,a5			;skip txturecoords

	move.w	d6,(a5)+		;p2
	addq.l	#1,d6
	move.w	d6,(a5)+		;p3
	add.l	#14,a5			;skip txturecoords

	move.w	d6,(a5)+		;p3
	subq.l	#2,d6
	move.w	d6,(a5)+		;p1

;lage faceopt. tabell

	move.l	.antallfaces,d5
	move.l	Faceoptstart(a0),a4
	lea	(a4,d5.w*8),a4

	move.l	Linjer2start(a0),a5
	lea.l	(a5,d7.w*8),a5   ;adressen til #linjer2+antlinjer*8
	move.l	a5,(a4)+
	add.l	#32,a5
	move.l	a5,(a4)+
	add.l	#32,a5
	move.l	a5,(a4)+


	move.l	Faceoptstart(a2),a5
	lsl.l	#1,d0
	move.l	12(a5,d0.w*8),(a4)

	sub.l	#12,a4		;starten av linja.

	move.l	.facepekerend,a5
	move.l	a4,(a5)+
	move.l	a5,.facepekerend		;lage sort liste

;	move.l	.sluttavsortbuffer,a5
;	move.w	.antallvisual,d5
;	swap	d5
;	move.w	.hoyz,d5
;	neg.w	d5
;	move.l	d5,(a5)+
;	move.l	a5,.sluttavsortbuffer

	addq.l	#1,.antallvisual
	addq.l	#2,.antallfaces
	addq.l	#3,.antcoords
	add.l	#3*4,.antlinjer


.neste
	bra.w	.cliploop
.ut:	
	move.l	.sluttavsortbuffer,a2

	move.w	#-$7fff,(a2)+
	move.w	#-$7fff,(a2)+


	move.l	Flagstart(a0),a1
	move.l	.antlinjer,d7
	subq.l	#1,d7
	bmi.b	.nei
	moveq.l	#0,d1
.cloop
	move.l	d1,(a1)+
	addq.l	#1,d1
	dbf	d7,.cloop
.nei
	rts

.antcoords:	dc.l	0
.antlinjer:	dc.l	0
;.faceopt:	dc.l	0
.antallfaces:	dc.l	0
.hoyz:		dc.l	0

.antallvisual:	dc.l	0
.facepekerend:	dc.l	0
.sluttavsortbuffer dc.l	0

INITSCENE:
	lea	scenen,a0
	cmp.l	#'SCNE',Sheader(a0)
	bne.w	END

	move.l	Objnamesptr(a0),d0
	add.l	a0,d0
	move.l	d0,Objnamesptr(a0)

	move.l	Objlistptr(a0),d0
	add.l	a0,d0
	move.l	d0,Objlistptr(a0)

	move.l	Objectinfoptr(a0),d0
	add.l	a0,d0
	move.l	d0,Objectinfoptr(a0)
	move.l	d0,camptr

	move.l	Numberofobjects(a0),d7
	subq.l	#1,d7
	bmi.w	END

	move.l	Numberofframes(a0),frames


******** Load objects *****************

	move.l	Objnamesptr(a0),a1
	move.l	Objlistptr(a0),a2

; a0 peker på objekt info struktur.
loadloop
	move.l	(a2)+,d0
	beq.b	.skipcamera
	cmp.l	#-1,d0
	beq.b	.endofhunk

; finn obj nummer
	move.l	a1,a4
	move.l	d0,d7
	move.l	d7,objektnr
	subq.l	#1,d7
	
.scan	cmp.b	#0,(a4)+
	bne.b	.scan
	dbf	d7,.scan

	movem.l	d0-d7/a0-a6,-(sp)
	bsr	LOADOBJ
	movem.l	(sp)+,d0-d7/a0-a6
	move.l	LOADOBJ\.objadresse,-4(a2)

******* sjekk om underobjekter *******************
.bah
	move.l	Objectinfoptr(a0),a4
	move.l	objektnr,d1
	muls.l	#Matrix_size,d1	
	add.l	d1,a4

	move.l	Matrixpre(a4),d0
	beq.b	.nope
	add.l	a0,d0
	move.l	d0,Matrixpre(a4)
.nope

	move.l	Uobjarray(a4),d0	
	beq.b	.null
	add.l	a0,d0
	move.l	d0,Uobjarray(a4)

	move.l	a2,-(sp)
	move.l	d0,a2
	bsr.b	loadloop
	move.l	(sp)+,a2

.null

.skipcamera
	bra.b	loadloop

.endofhunk:
	clr.l	-(a2)		;slutt av objekt liste ender med 0

	rts

objektnr:	dc.l	0

; krever filnavn på objekt i a4 
LOADOBJ:
;	kopier navn og add path.

	lea	.filnavn+9,a1
.o
	move.b	(a4)+,(a1)+
	bne.b	.o
	clr.l	(a1)+

;loader inn et objekt og allokerer nok minne til en objektstruktur.

        move.l  #.filnavn,a0
        move.l	DEMOBASE,a1	
	jsr	_LVOGetFileLength(a1)
	move.l	d0,.length		;størrelse på fil i bytes

  	add.l	#Header,d0		;reservere til header
	MALLOC	d0,#Id_objekt,Best	
	move.l	d0,.objadresse
	
	add.l	#Header,d0		;start riktig
	move.l	d0,a1
	move.l	DEMOVARS,a4
	move.l	.length,d0
        move.l  #.filnavn,a0
        move.l	DEMOBASE,a2	
	jsr	_LVOLoadFile(a2)

;A4=DEMOVARS|D0=Size|A0=Name|A1=Buffer

;****** allokere minne *********
;
;	dc.l	*o1matrix	;ligger i SCENEformat
;       dc.l    *coord3d        ;Pointer to numberofpoints*4  bytes
;	dc.l	*surfaces	;Pointer to numberoffaces*4*4 bytes
;	dc.l	*linjer2	;Pointer to numberoflines*32  bytes
;	dc.l	*linjepekere	;Pointer to numberoflines*2   bytes
;	dc.l	*coord2d	;Pointer to numberofpoints*8  bytes

;	move.l	a0,a1
	lea	scenen,a0
	move.l	Objectinfoptr(a0),d0
	move.l	objektnr,d1
	muls.l	#Matrix_size,d1	
	add.l	d1,d0
	move.l	.objadresse,a0		;start av objekt
	move.l	d0,Matrixstart(a0)

	move.l	Antfaces(a0),d0
	lsl.l	#4,d0			;numberoffaces*4*4	
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Faceoptstart(a0)
	move.l	Antlinjer(a0),d0
	lsl.l	#6,d0
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Linjer2start(a0)
	move.l	Antlinjer(a0),d0
	lsl.l	#1,d0
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Flagstart(a0)
	move.l	Antpunkter(a0),d0
	lsl.l	#3,d0

	MALLOC	d0,#Id_objekt,Best	
	move.l	d0,Coord2dstart(a0)

	move.l	Antpunkter(a0),d0
	lsl.l	#2,d0
	MALLOC	d0,#Id_objekt,Best	
	move.l	d0,Coord3dstart(a0)
	

;	move.l	(sp)+,d1
;	jsr	_LVOClose(A6)
.ut:

	rts

.length:	dc.l	0
.filnavn:	dc.b	  "objekter:leftarm.ctz7",0
		blk.b	32,0	
		even
.handle:	dc.l	0
.objadresse:	dc.l	0

MAKEOBJLIST:
;	lea	objekter,a0
	lea	scenen,a0
	move.l	Objlistptr(a0),a0	;peker på star av hoved obj liste.
	addq.l	#4,a0			;skip camera.
	moveq.l	#0,d0			;ant faces
	moveq.l	#0,d1			;ant coords
	moveq.l	#0,d7
	lea	visualobjectlist,a6
.loop
	move.l	(a0)+,a1
	tst.l	a1
	beq.b	.ut
	move.l	Antfaces(a1),d7
	add.l	d7,d0
	move.l	Antpunkter(a1),d7
	add.l	d7,d1
	move.l	Antlinjer(a1),d2
	add.l	d7,d2

	move.l	a1,(a6)+
	move.l	Matrixstart(a1),a2
	move.l	Uobjarray(a2),a2
	tst.l	a2
	beq.b	.loop

	move.l	a0,-(a7)
	move.l	a2,a0
	bsr.b	.loop
	move.l	(a7)+,a0

	bra	.loop
.ut
	move.l	d0,antallfaces
	move.l	d1,antallpunkter
	move.l	d2,antalllinjer

******* Lag clip objekt ************************

	lea	clipobject,a0
;LONG  Clipobjflags  antallfaces*6 bytes
	move.l	antallfaces,d0
	muls.l	#6,d0
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Clipobjflags(a0)
;LONG	Faceoptstart antallfaces*4*4*2	32
	move.l	antallfaces,d0
	lsl.l	#5,d0
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Faceoptstart(a0)
; LONG	Linjer2start antpoligoner*32*5 	160
	move.l	antallfaces,d0
	muls.l	#160,d0
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Linjer2start(a0)
; LONG	Flagstart antlinjer*2*5		;10
	move.l	antalllinjer,d0
	muls.l	#10,d0
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Flagstart(a0)
; LONG	Coord2dstart antcoords*2*4*4	;16
	move.l	antallpunkter,d0
	lsl.l	#5,d0
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Coord2dstart(a0)
; LONG	Linjerstart   antfaces*2*8*5	;80
	move.l	antallfaces,d0
	muls.l	#80,d0
	MALLOC	d0,#Id_objekt,Best
	move.l	d0,Linjerstart(a0)

************************************************

	rts
	

INITSHADES:
	lea	cmap,a1
	lea	visualobjectlist,a5
	lea	txturepekere+4,a2
	moveq.l	#0,d7	;ant objekter
.oloop:
	move.l	(a5)+,a6
	tst.l	a6
	beq.w	.exit
	move.l	Fargerstart(a6),a0
	add.l	a6,a0
	add.l	#Header,a0
.loop
	move.l	(a0)+,d0
	cmp.l	#-1,d0
	beq.b	.ut	
	lsr.l	#8,d0
	move.l	d0,(a1)+
	move.l	(a0)+,(a1)+	;specular flags
.skipit
	bra.b	.loop		
.ut
	move.l	Navnstart(a6),a0
	add.l	a6,a0
	add.l	#Header,a0

.loadtxtures:
	move.l	(a0),d0
	beq.b	.oloop

	move.l	#$dff000,a6
	move.w	$1c(a6),d0
	or.w	#$8008,d0
	move.w	d0,Intena(a6)

	MALLOC	#($10000+$10000/2),#Id_objekt,Best
	add.l	#128*256-1024,d0	;til txturestart
	move.l	d0,a3
	move.l	d0,dennetxt

	READFILE a0,dennetxt,#256*256+1024

	lea	kladdcmap,a4
	move.w	#1024/4-1,d7
.copy1024:
	move.l	(a3)+,(a4)+
	dbf	d7,.copy1024
	
.bah	move.l	a3,(a2)+		;peker på start av txture
	move.l	a3,a4
	sub.l	#64*256*2,a4

	movem.l	d0-d7/a0-a6,-(sp)
	lea	shadetable2,a0
	lea	kladdcmap,a6
	bsr	MAKE24BITSHADETABLE64

	lea	shadetable2,a3		;24bit shadetable	
	move.l	#64*256-1,d0
	move.l	scramblerout,a0
	jsr	(a0)			;bsr	HAM7SCR\.fixscrham

	movem.l	(sp)+,d0-d7/a0-a6

	addq.l	#1,a0	
.finnn:
	move.b	(a0)+,d1		;finnneste txturenavn
	bne.b	.finnn
	bra	.loadtxtures


.exit:
	move.l	#-1,(a1)+		;cmap   end
	move.l	#-1,(a2)+		;txture pekere end
	lea	cmap,a6
	lea	shadetable2,a0
	bsr	MAKE24BITGSTABLE

	lea	shadetable2,a3	
	lea	gouraudshades,a4
	move.l	#64*256-1,d0
	move.l	scramblerout,a0
	jsr	(a0)			;bsr	HAM7SCR\.fixscrham

	rts

dennetxt:
	dc.l	0
	
MAKE24BITGSTABLE:
.loop
        move.l  (a6)+,d1
	cmp.l	#-1,d1
	beq.b	.ut

        move.l  (a6)+,d2
        swap	d2		;specular flag 0-255
	tst.w	d2
	bne.b	.spec

	move.l	#Taakefarge,d0
        move.l  #64,d7
        bsr     PALSPREAD
	bra.b	.neste
.spec
	
	move.l	#Taakefarge,d0
        move.l  #64-16,d7
        bsr     PALSPREAD

        move.l  -8(a6),d0
        move.l  #$ffffffff,d1
        move.l  #16,d7
        bsr     PALSPREAD

.neste
	moveq.l	#0,d1
	move.l	#64-1,d7
.cloop	move.l	d1,(a0)+
	dbf	d7,.cloop

	bra	.loop

.ut:

;	lea	gouraudshades,a0	
	rts


*******Ctz 3d engine *************************************

INITOBJECTS:

;	init objekt
	lea	visualobjectlist,a6
	move.l	#txturepekere,d3

	moveq.l	#0,d1		;høyeste surface #
	moveq.l	#0,d2		;surface oppsamling reg.
	moveq.l	#0,d4		;txture oppsamling reg.
	moveq.l	#0,d5
.oloop
	move.l	(a6)+,d0
	beq.w	.exit
	move.l	d0,a0
	move.l	a0,a1
	add.l	#Header,a1

	move.l	Coordstart(A0),d0
	add.l	a1,d0
	move.l	d0,Coordstart(A0)

	move.l	Facestart(A0),d0
	add.l	a1,d0
	move.l	d0,Facestart(A0)

	move.l	Normstart(A0),d0
	beq.b	.nei
	add.l	a1,d0
	move.l	d0,Normstart(A0)
.nei
	move.l	Facenormstart(A0),d0
	beq.b	.nei2
	add.l	a1,d0
	move.l	d0,Facenormstart(A0)
.nei2
	move.l	Linjefacestart(A0),d0
	add.l	a1,d0
	move.l	d0,Linjefacestart(A0)

	move.l	Linjerstart(A0),d0
	add.l	a1,d0
	move.l	d0,Linjerstart(A0)

	move.l	Fargerstart(A0),d0
	add.l	a1,d0
	move.l	d0,Fargerstart(A0)

	move.l	Boundboxstart(A0),d0
	add.l	a1,d0
	move.l	d0,Boundboxstart(A0)

	move.l	Linjefacestart(a0),a0

.fixloop
	move.l	#3-1,d7

.cpy	move.w	(a0),d0
	bmi.b	.ut
	lsl.w	#1,d0
	move.w	d0,(a0)+
	dbf	d7,.cpy

	addq.l	#2,a0

	bra.b	.fixloop	
.ut

	add.l	d1,d2
	add.w	d4,d5
	moveq.l	#0,d1		;høyeste surface #
	moveq.l	#0,d4		;høyeste txture #

	
****** fix opt facetable ************

	move.l	-4(a6),a1
	move.l	Linjefacestart(a1),a2
	move.l	Linjer2start(a1),a3
	move.l	Faceoptstart(a1),a0
	move.l	Fargerstart(a1),d6
	move.l	Navnstart(a1),a1
.hehe
	lea	gouraudshades,a5
			;lea	txturepekere,a5	(d2)
.loop
	moveq.l	#0,d0
	move.w	(a2)+,d0		;facepeker
	bmi.b	.heltut
	lea	(a3,d0.w*8),a4
	move.l	a4,(a0)+
	move.w	(a2)+,d0		;facepeker
	lea	(a3,d0.w*8),a4
	move.l	a4,(a0)+
	move.w	(a2)+,d0		;facepeker
	lea	(a3,d0.w*8),a4
	move.l	a4,(a0)+

	move.w	(a2)+,d0		;surface info . txturepeker
	move.w	d0,d7
	and.w	#$c000,d7		;flags
	sub.w	d7,d0			;ta bort flags

	cmp.w	d0,d1
	bge.b	.j
	move.l	d0,d1			;hoyeste surface.
.j

	move.w	d7,(a0)+
	bpl.b	.gouraud		;er bit 15 ikke satt ->ingen txture

	move.l	d6,a4
	move.l	-8(a4,d0.w*8),d0
	and.l	#$ff,d0			;finn hvilken txture
	cmp.w	d0,d4
	bge.w	.o2
	move.w	d0,d4			;Hoyeste txture #
.o2
	add.w	d5,d0			;add ant txtures i forige obj
	move.w	d0,(a0)+

	bra.w	.loop

.gouraud
	add.l	d2,d0			;add in surf fra forige

	subq.w	#1,d0
	and.l	#$ffff,d0
	lsl.l	#5,d0		

	move.w	d0,(a0)+
	bra.w	.loop

.heltut


	bra.w	.oloop	
.exit

	rts

speedsize=256			;*2*2*2*2
shift=8				;256=>8 512=>7

SORTPOLYS:
	subq.l	#1,antvisualfaces
	bgt.b	.o
	rts
.o

;-----------Teller forekomster------------------------

	lea.l	visuallist,a0
	lea.l	counttable(pc),a1

	move.w	#$ffff>>shift,d5
	move.l	antvisualfaces,d1
	move.w	d1,d2
	lsr.w	#3,d1
	subq	#1,d1
	bmi.w	.loop2b
.loop2:
	rept	4
	move.l	(a0)+,d0
	move.l	(a0)+,d4
	move.l	d0,d3
	move.l	d4,d6
	and.w	d5,d0
	and.w	d5,d4
	lsr.w	#8,d3
	lsr.w	#8,d6
	addq	#1,(a1,d0.w*4)
	addq	#1,(a1,d4.w*4)
	addq	#1,2(a1,d3.w*4)
	addq	#1,2(a1,d6.w*4)
	endr
	
	dbra	d1,.loop2

	and.w	#$7,d2
.loop2b:
	move.l	(a0)+,d0
	move.l	d0,d3
	and.w	d5,d0
	lsr.w	#8,d3
	addq	#1,(a1,d0.w*4)
	addq	#1,2(a1,d3.w*4)

	dbra	d2,.loop2b

;----------Lager offsets av forekomstene------------------

	lea.l	offsettable(pc),a0
	lea.l	counttable(pc),a1
	moveq	#0,d0
	moveq	#0,d2
	move.w	#speedsize/32-1,d1
.loop3:
	rept	32
	move.l	d0,(a0)+
	add.l	(a1),d0
	move.l	d2,(a1)+
	endr
	dbra	d1,.loop3


;--------------Flytter tallene til rikitg plass--------------
	lea.l	offsettable(pc),a2
	lea.l   visuallist,a0
	lea.l   sorttable2,a1

	move.l	antvisualfaces,d3
	move.w	d3,d4
	lsr.w	#3,d3
	subq	#1,d3
	bmi.w	.loop4b
.loop4:
	rept	8
	move.l	(a0)+,d0
	move.l	d0,d1
	and.w	d5,d0
	move.w	(a2,d0.w*4),d2
	move.l	d1,(a1,d2.w*4)
	addq.w	#1,d2
	move.w	d2,(a2,d0.w*4)
	endr


	dbra	d3,.loop4

	and.w	#7,d4
.loop4b:
	move.l	(a0)+,d0
	move.l	d0,d1
	and.w	d5,d0
	move.w	(a2,d0.w*4),d2
	move.l	d1,(a1,d2.w*4)
	addq.w	#1,d2
	move.w	d2,(a2,d0.w*4)

	dbra	d4,.loop4b


;	rts
********* pass2 **************
pass2

;--------------Flytter tallene til rikitg plass--------------
	lea.l	offsettable(pc),a2
	lea.l   sorttable2,a0
	lea.l   visuallist,a1

	move.l	antvisualfaces,d3
	move.w	d3,d4
	lsr.w	#3,d3
	subq	#1,d3
	bmi.w	.loop4b
.loop4:
	rept	8
	move.l	(a0)+,d0
	move.l	d0,d1
	lsr.w	#shift,d0
	lea	2(a2,d0.w*4),a3
	move.w	(a3),d2
	move.l	d1,(a1,d2.w*4)
	addq.w	#1,d2
	move.w	d2,(a3)
	endr

	dbra	d3,.loop4

	and.w	#7,d4
.loop4b:
	move.l	(a0)+,d0
	move.l	d0,d1
	lsr.w	#shift,d0
	move.w	2(a2,d0.w*4),d2
	move.l	d1,(a1,d2.w*4)
	addq.w	#1,d2
	move.w	d2,2(a2,d0.w*4)
	dbra	d4,.loop4b

	rts
	
	cnop	0,16
counttable:	blk.l	speedsize+16,0
	cnop	0,16
offsettable:	blk.l	speedsize+16,0

INSIDEOUT:

	move.l	a7,.stack

	lea	clipobject,a0
	move.l	Clipobjflags(a0),.clipflags

	lea.l	visuallist,a2
	move.l	#visualobjectlist,.objektloop
	move.l	#facepekere,.facepekere
	moveq.l	#0,d7
	move.l	#0,.ant
.oloop:
	move.l	.objektloop(pc),a3
	move.l	(a3)+,d0
	beq.w	.exit	
	move.l	a3,.objektloop
	move.l	d0,a3

	move.l	Coord2dstart(a3),a0
	move.l	Facestart(a3),a1
	move.l	Flagstart(a3),.linjepekere	;move.l	#linjepekere,.linjepekere
	move.l	Linjefacestart(a3),.linjeface
	move.l	Faceoptstart(a3),.faceopt
	
	sub.l	a4,a4
	sub.l	a6,a6
	sub.l	a5,a5
	sub.l	a3,a3

	bra	.start
	cnop	0,8
.loop
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	addq	#2,a1

	movem.w	2(a0,d0.w*8),a3/a4	;x0
	movem.w	2(a0,d1.w*8),a5/a6	;x1

	move.l	a5,d5
	move.l	a6,d6

	sub.w	a3,d5
	sub.w	a4,d6

	movem.w	2(a0,d2.w*8),d3/d4

	sub.w	a5,d3	;x2
	sub.w	a6,d4	;y2

	muls	d3,d6
	muls	d4,d5
	
	sub.l	d5,d6
	bge.w	.o		; Sjekker om z verdien er neg ->(x1*y2-y1*x2)
;	blt.w	.o

	movem.w	2(a0,d2.w*8),d3/d4
.hehe
************* yakse **********************
	cmp.w	a6,a4
	blt.b	.klop		;a6 > a4
	exg.l	a6,a4
.klop
	cmp.w	a6,d4		;a6 > d4
	blt.b	.klop2
	exg.l	d4,a6
.klop2
	cmp.w	a4,d4		;a4 > d4
	blt.b	.klop3
	move.l	a4,d4
.klop3

	tst.w	a6
	blt.w	.o

	cmp.w	#E_hoyde,d4
	bgt.w	.o
************* x akse **********************

	cmp.w	a5,a3
	blt.b	.xklop
	exg.l	a5,a3
.xklop
	cmp.w	a5,d3
	blt.b	.xklop2
	exg.l	d3,a5
.xklop2
	cmp.w	a3,d3
	blt.b	.xklop3
	move.l	a3,d3
.xklop3


	tst.w	a5
	ble.w	.o

	cmp.w	#E_bredde,d3
	bgt.w	.o

******************************************************

	move.w	(a0,d0.w*8),d3
	move.w	(a0,d1.w*8),d4
	move.w	(a0,d2.w*8),d5
	
.zsorta
	cmp.w	d5,d3
	blt.b	.zklop		;d5 > d3
	exg.l	d5,d3
.zklop
	cmp.w	d5,d4		;d5 > d4
	blt.b	.zklop2
	exg.l	d5,d4
.zklop2
	cmp.w	d4,d3		;d4 > d3
	blt.b	.zklop3
	move.l	d4,d3
.zklop3

	cmp.w	#Zbehind,d5		; sjekk om z bak
	blt.b	.o

	cmp.w	#Zbehind,d3		; sjekk om z bak
	bgt.b	.ikkezclip

	move.l	.objektloop(pc),a5
	move.l	-4(a5),d6
	move.l	.clipflags(pc),a5
	move.l	d6,(a5)+
	move.w	d7,(a5)+
	move.l	a5,.clipflags

	bra.b	.o

.ikkezclip


	lea	.linjeface(pc),a3
	move.l	(a3)+,a7		;linjefacestart
	move.l	(a3)+,a5		;faceopt

	lea	(a5,d7.w*8),a5		;objektlinjefacestart + d7*4  -> facepekere
	move.l	(a3),a6			;facepekerstart
	move.l	a5,(a6)+		;Inn med facepeker
	move.l	a6,(a3)+	

	move.l	d7,d6
	neg.w	d5	
	move.w	d5,d6
	move.l	d6,(a2)+		; zcoord

	lea.l	linjesjekk,a5
	
	move.l	(a3)+,d6		;dennecmp
	addq.l	#1,(a3)+
	move.l	(a3),a6

	movem.w	(a7,d7.w*4),d1-d3		;hent en linje i l-facetbl

	cmp.l	(a5,d1.w),d6
	beq.s	.f
	move.l	d6,(a5,d1.w)
	move.w	d1,(a6)+		;inn med linjepeker
.f
	move.l	a6,(a3)

	cmp.l	(a5,d2.w),d6
	beq.s	.f2
	move.l	d6,(a5,d2.w)
	move.w	d2,(a6)+			;inn med linjepeker
.f2
	cmp.l	(a5,d3.w),d6
	beq.s	.f3
	move.l	d6,(a5,d3.w)
	move.w	d3,(a6)+			;inn med linjepeker
.f3
	move.l	a6,(a3)
	add.l	#$00010000,d7				; face nr.
.o
	addq.l	#$00000002,d7				; face nr.
.start:
	move.w	(a1)+,d0
	bge.w	.loop
.ut
	clr.w	d7
	move.l	.linjepekere,a6
	move.l	#-1,(a6)+
	move.l	.ant,antvisualfaces
	addq.l	#1,.dennecmp

	bra.w	.oloop

.exit:
	move.l	a2,ZCLIP\.sluttavsortbuffer

	move.w	#-$7fff,(a2)+
	move.w	#-$7fff,(a2)+


	move.l	.facepekere,ZCLIP\.facepekerend	;lagrer slutten av faceliste
	clr.w	d7
	swap	d7
	move.l	d7,ZCLIP\.antallvisual

	move.l	.clipflags(pc),a0
	move.l	#0,(a0)+
	move.l	#0,(a0)+

	move.l	.stack,a7

	rts

.linjeface:	dc.l	0
.faceopt:	dc.l	0
.facepekere:	dc.l	facepekere	;.linjesjekk	dc.l	linjesjekk
.dennecmp:	dc.l	1
.ant:		dc.l	0
.linjepekere	dc.l	linjepekere
.objektloop:	dc.l	0
.stack:		dc.l	0
.clipflags:	dc.l	0

test:		dc.l	0

MAKEMATRIX:
        move.l  camptr,a6

	moveq.l	#0,d0
	move.b	$dff00a,d0
;	neg.l	d0
;	sub.w	#128,d0
	lsl.l	#3,d0
;	move.w	d0,Xrotpos(a6)

	moveq.l	#0,d0
	move.b	$dff00b,d0
	;neg.l	d0
	sub.w	#128,d0
	lsl.l	#3,d0
;	move.w	d0,Yrotpos(a6)	

***********************************

	move.l	Matrixpre(a6),d0
	beq.b	.rotermanuelt

	IFNE Manual_camera
	bra.b	.rotermanuelt
	ENDIF


	move.l	d0,a2
	add.l	#scenen,a2
        move.l  framecounter,d0
        muls.l  #12*2,d0                ;finn riktig precalcmatrise
        lea     (a2,d0.l),a2
        move.l  a6,a1
        moveq.l #9-1,d7
.copycammatrix
        move.w  (A2)+,(a1)+     ;pecalca matriser til objektmatrisen.
        dbf     d7,.copycammatrix

	add.l	#6*2,a1
        move.w  (A2)+,Xpos(a6)		;(a1)+	; posisjon også
        move.w  (A2)+,Ypos(a6)
	move.w  (A2)+,d7
;	neg.w	d7
	move.w	d7,Zpos(a6)


	bra.w	.prerotert
.rotermanuelt

	move.w	Xrotpos(a6),d0		;xangle
	move.w	Yrotpos(a6),d2		;yangle
	move.w	Zrotpos(a6),d3		;zangle
	bsr.w	MAKEROTMATRIX

	lea	ymatrix,a0
	lea	xmatrix,a1
	move.l	a6,a2
	bsr.w	MULSMATRIX_3x3
	move.l	a6,a0
	lea	zmatrix,a1
	move.l	a6,a2
	bsr.w	MULSMATRIX_3x3

********** muskontr *********************
; fix kamera:

	btst	#10,$dff016
	bne.b	.o
	move.w	M13(a6),d0
	move.w	M23(a6),d1
	move.w	M33(a6),d2
	moveq.l	#8,d7
	asr.w	d7,d0
	asr.w	d7,d1
	asr.w	d7,d2
	add.w	d0,Xpos(a6)
	add.w	d1,Ypos(a6)
	add.w	d2,Zpos(a6)
.o
	btst #6,$bfe001
	bne.w	.o2
	move.w	M13(a6),d0
	move.w	M23(a6),d1
	move.w	M33(a6),d2
	moveq.l	#8,d7
	asr.w	d7,d0
	asr.w	d7,d1
	asr.w	d7,d2
	sub.w	d0,Xpos(a6)
	sub.w	d1,Ypos(a6)
	sub.w	d2,Zpos(a6)
.o2

.prerotert:

        move.l  camptr,a0
      	move.l	a0,a1
	bsr	INVMATRIX

        move.l  camptr,a0
	move.w	Relxpos(a0),d5
	move.w	Relypos(a0),d6
	move.w	Relzpos(a0),d7

	
	movem.l	a0-a6,-(a7)
	bsr.w	ROTATESINGLE
	movem.l	(a7)+,a0-a6

	move.w	d0,Relxpos(a0)
	move.w	d1,Relypos(a0)
	move.w	d5,Relzpos(a0)

******************
        move.l  camptr,a4
;        	lea	cammatrix,a4		;forige matrise
;	lea	objekter,a5
        lea     scenen,a5
        move.l  Objlistptr(a5),a5      ;peker på star av hoved obj liste.
;	move.l  12(a5),a5      		;peker på star av hoved obj liste.
        addq.l  #4,a5                   ;skip camera.

	lea	visualobjectlist,a3
.obloop
		move.l	(a5)+,a0
		tst.l	a0
		beq.w	.ut

		move.l	Matrixstart(a0),a6

;------------ test om objekt har keyframes matriser ---------------------------

		move.l	Matrixpre(a6),d0
		beq.b	.nei
	;	bra.b	.nei
.bah		move.l	d0,a2			
		move.l	framecounter,d0
		muls.l	#12*2,d0		;finn riktig precalcmatrise	
		lea	(a2,d0.l),a2
		move.l	a6,a1

                moveq.l #9-1,d7
.copymatrix
                move.w  (A2)+,(a1)+	;pecalca matriser til objektmatrisen.
                dbf     d7,.copymatrix

		add.l	#12,a1
                move.w  (A2)+,(a1)+	; posisjon også
                move.w  (A2)+,(a1)+
                move.w  (A2)+,(a1)+


		bra.b	.skiprotasjon
.nei:
		move.w	Xangle(a6),d0
		move.w	Yangle(a6),d1
		move.w	Zangle(a6),d2
		add.w	d0,Xrotpos(a6)
		add.w	d1,Yrotpos(a6)
		add.w	d2,Zrotpos(a6)

		move.w	Xrotpos(a6),d0
		move.w	Yrotpos(a6),d2
		move.w	Zrotpos(a6),d3
		bsr.w	MAKEROTMATRIX

		lea	ymatrix,a0
		lea	xmatrix,a1
		move.l	a6,a2		;object matrix
		bsr.w	MULSMATRIX_3x3
		move.l	a6,a0		;object matrix
		lea	zmatrix,a1
		move.l	a6,a2		;object matrix
		bsr.w	MULSMATRIX_3x3

.skiprotasjon:
		move.l	a4,a0		;forige matrise
		move.l	a6,a1

		lea.l	kladdmatrix,a2
		bsr.w	MULSMATRIX_3x3
		lea	kladdmatrix,a0
		move.l	a6,a1
		moveq.l	#9-1,d7
.loop	
		move.w	(A0)+,(a1)+
		dbf	d7,.loop	

		move.w	Pivotx(a6),d5
		move.w	Pivoty(a6),d6
		move.w	Pivotz(a6),d7

		movem.l	a0-a6,-(a7)
		move.l	a6,a0
		bsr	ROTATESINGLE
		movem.l	(a7)+,a0-a6

		movem.w	d0/d1/d5,-(sp)

		move.w	Xpos(a6),d5
		move.w	Ypos(a6),d6
		move.w	Zpos(a6),d7

;		add.w	Pivotx(a6),d5
;		add.w	Pivoty(a6),d6
;		add.w	Pivotz(a6),d7

		movem.l	a0-a6,-(a7)
		move.l	a4,a0
		bsr	ROTATESINGLE
		movem.l	(a7)+,a0-a6

		add.w	Relxpos(a4),d0
		add.w	Relypos(a4),d1
		add.w	Relzpos(a4),d5

		movem.w	(sp)+,d2-d4
		sub.w	d2,d0
		sub.w	d3,d1
		sub.w	d4,d5
		move.w	d0,Relxpos(a6)
		move.w	d1,Relypos(a6)
		move.w	d5,Relzpos(a6)		

		move.l	-4(a5),a0		;hent obj peker
		move.l	a0,(a3)+	
************** Bounding box sjekk *************************************
		movem.l	d0-a2/a4-a6,-(sp)
;		movem.l	d0-d7/a0-a6,-(sp)
		bsr	BOUNDINGBOX
;		movem.l	(sp)+,d0-d7/a0-a6
		movem.l	(sp)+,d0-a2/a4-a6
************************************************************************


		move.l	Uobjarray(a6),a1	;sjekk om under objekter	
		tst.l	a1
		beq.w	.obloop

		move.l	a5,-(a7)		;lagre objekter peker
		move.l	a4,-(a7)		;lagre forige matrise peker
		move.l	a1,a5			;bytt til under objekter
		move.l	a6,a4			;bytt til forige matrise

		bsr.w	.obloop

		move.l	(a7)+,a4		;hent matrisepeker
		move.l	(a7)+,a5		;hent forige objekter peker
			
		bra.w	.obloop

.ut		rts

BOUNDINGBOX:
; Bounding box sjekk objektpeker i a0
		move.l	Boundboxstart(a0),a1
;		move.l	Coordstart(a0),a1
		move.l	Matrixstart(a0),a0
		move.w	Relxpos(a0),.posx
		move.w	Relypos(a0),.posy
		move.w	Relzpos(a0),.posz

		clr.w	.zbh
		clr.w	.bigx
		clr.w	.bigy
		clr.w	.smallx
		clr.w	.smally	
		move.w	#1,.forste	


		move.w	(a0),a2
		move.w	2(a0),.matr12
		move.w	4(a0),.matr13		;a3
		move.w	1*6(a0),a4
		move.w	1*6+2(a0),.matr22		;d3
		move.w	1*6+4(a0),d4
		move.w	2*6(a0),a5
		move.w	2*6+2(a0),a6
		move.w	2*6+4(a0),a0

		move.w	#8-1,d3
; a0,a1 (d5,d5,d7) + 3  | 8 ledige
.gjenta:
		movem.w	(a1)+,d5-d7		; sette ut x,y,z

	;	addq.l	#2,a1

		move.w	d5,d0
		move.l	a2,d2
		muls.w	d2,d0		;a2 	(a3)
		move.w	d7,d1
		move.w	.matr12(pc),d2
		muls.w	d6,d2
		add.l	d2,d0
		move.w	.matr13,d2
		muls.w	d2,d1		;a3	4(a3)
		add.l	d1,d0		;x

		move.w	d5,d1
		move.l	a4,d2		;1*8(a3)
		muls.w	d2,d1		;a4
		move.w	d6,d2
		muls.w	.matr22,d2		;d3 1*8+2(a3)
		add.l	d2,d1
		move.w	d7,d2
		muls.w	d4,d2		;d4 1*8+4(a3)
		add.l	d2,d1		;y

 		move.l	a5,d2
		muls.w	d2,d5		;a5 2*8+(a3)
 		move.l	a6,d2
		muls.w	d2,d6		;a6 2*8+2(a3)
 		move.l	a0,d2
		muls.w	d2,d7		;a7 2*8+4(a3)
		add.l	d7,d6
		add.l	d6,d5		;z

		moveq.l	#14,d2
		lsr.l	d2,d0
		lsr.l	d2,d1
		lsr.l	d2,d5

		sub.w	.posx(pc),d0			;camerapos x,y,z
		sub.w	.posy(pc),d1
		sub.w	.posz(pc),d5

;32/16
; x=x3*dist / z3+dist
; y=y3*dist / z3+dist
;
; dz = dist/z
; x = x3*dz
.dist=192


		cmp.w	#Zbehind,d5
		bgt.b	.synlig
		addq.w	#1,.zbh	
;		or.w	#1,d5
		bra.w	.skip
.synlig:
		move.l	#2^11*.dist,d2
		divu.w	d5,d2		;dist/z
		
		muls.w	d2,d0
		muls.w	d2,d1

		moveq.l	#11,d5
		asr.l	d5,d0
		asr.l	d5,d1

.ok1
		add.w	#E_bredde/2,d0
		add.w	#E_hoyde/2,d1

;		cmp.w	#320,d0
;		bgt.b	.nope
;		cmp.w	#0,d0
;		blt.b	.nope
;
;		cmp.w	#e_hoyde,d1
;		bgt.b	.nope
;		cmp.w	#0,d1
;		blt.b	.nope
;
;	move.l	a3,-(sp)
;	move.w	d1,d5
;	muls.w	#320*2,d5
;	ext.l	d0
;	add.l	d0,d5
;	add.l	d0,d5
;	lea	kladd,a3
;	and.l	#$3ffff,d5
;	move.w	#$ffff,(a3,d5.l)
;	movem.l	(sp)+,a3
;.nope


********
		subq.w	#1,.forste	;
		blt.b	.nei	
		move.w	d0,.bigx
		move.w	d0,.smallx
		move.w	d1,.bigy
		move.w	d1,.smally
		bra.b	.skip
.nei
		cmp.w	.bigx(pc),d0	
		blt.b	.no
		move.w	d0,.bigx
.no
		cmp.w	.smallx(pc),d0	
		bgt.b	.no2
		move.w	d0,.smallx
.no2
		cmp.w	.bigy(pc),d1	
		blt.b	.no3
		move.w	d1,.bigy
.no3
		cmp.w	.smally(pc),d1	
		bgt.b	.no4
		move.w	d1,.smally
.no4

.skip
		dbf	d3,.gjenta

.bah

;
; Sjekk om box er innenfor kamera pos.
;
;		tst.w	.zbh
;		bne.b	.ok	
		cmp.w	#8,.zbh		;er alle z bak?
		beq.b	.remove


		move.l	#E_bredde,d0
		cmp.w	.smallx(pc),d0
		blt.b	.remove		;320>smallx ?
		tst.w	.bigx(pc)
		bmi.b	.remove

		move.l	#E_hoyde,d0
		cmp.w	.smally(pc),d0
		blt.b	.remove
		tst.w	.bigy(pc)
		bmi.b	.remove

.ok
		rts

.remove:	
		clr.l	-(a3)		;fjærn objektpekeren
		rts



.posx:		dc.l	0
.posy:		dc.l	0
.posz:		dc.l	0
.bigx		dc.l	0
.bigy		dc.l	0
.smallx		dc.l	0
.smally		dc.l	0
.zbh		dc.l	0		;when all z are behind camera = 8
.forste		dc.l	0
.matr22		dc.l	0
.matr12		dc.l	0
.matr13		dc.l	0

	
INVMATRIX:
;a0 matrise1
;a1 output patrise

	move.w	(a0)+,d0		;11
	move.w	(a0)+,d1		;12
	move.w	(a0)+,d2		;13
	move.w	(a0)+,d3		;21
	move.w	(a0)+,d4		;22
	move.w	(a0)+,d5		;23
	move.w	(a0)+,d6		;31
	move.w	(a0)+,d7		;32
	move.w	(a0)+,a2		;33
	
	move.w	d0,(a1)+
	move.w	d3,(a1)+
	move.w	d6,(a1)+
	move.w	d1,(a1)+
	move.w	d4,(a1)+
	move.w	d7,(a1)+
	move.w	d2,(a1)+
	move.w	d5,(a1)+
	move.w	a2,(a1)+

	sub.l	#9*2,a0
	sub.l	#9*2,a1

	move.w	Xpos(a0),d0
	neg.w	d0
	move.w	d0,Relxpos(a1)
	move.w	Ypos(a0),d0
	neg.w	d0
	move.w	d0,Relypos(a1)
	move.w	Zpos(a0),d0
	neg.w	d0
	move.w	d0,Relzpos(a1)

	rts

ROTATESINGLE:	
		move.w	(a0),a2
		move.w	4(a0),a3
		move.w	1*6(a0),a4
		move.w	1*6+2(a0),d3
		move.w	1*6+4(a0),d4
		move.w	2*6(a0),a5
		move.w	2*6+2(a0),a6
		move.w	2*6+4(a0),a1

		move.w	d5,d0
		move.l	a2,d2
		muls.w	d2,d0		;a2 	(a3)

		move.w	2(a0),d1
		muls.w	d6,d1
		add.l	d1,d0

		move.w	d7,d1
		move.l	a3,d2
		muls.w	d2,d1		;a3	4(a3)
		add.l	d1,d0		;x

		move.w	d5,d1
		move.l	a4,d2		;1*8(a3)
		muls.w	d2,d1		;a4
		move.w	d6,d2
		muls.w	d3,d2		;d3 1*8+2(a3)
		add.l	d2,d1
		move.w	d7,d2
		muls.w	d4,d2		;d4 1*8+4(a3)
		add.l	d2,d1		;y

 		move.l	a5,d2
		muls.w	d2,d5		;a5 2*8+(a3)
 		move.l	a6,d2
		muls.w	d2,d6		;a6 2*8+2(a3)
 		move.l	a1,d2
		muls.w	d2,d7		;a7 2*8+4(a3)
		add.l	d7,d6
		add.l	d6,d5		;z

		moveq.l	#14,d7
		asr.l	d7,d0		
		asr.l	d7,d1		
		asr.l	d7,d5		

		rts


MAKEROTMATRIX:
		and.l	#1024-1,d0
		and.l	#1024-1,d2
		and.l	#1024-1,d3

;x matrix rotasjon rundt zy planet
;|   1    0    0|
;|   0  cos -sin|
;|   0  sin  cos|

		lea	sintable,a1
		lea	costable,a2

		lea	xmatrix,a0

		move.w	(a2,d0.w*2),d1
		move.w	d1,M22(a0)	;cos	
		move.w	d1,M33(a0)	;cos

		move.w	(a1,d0.w*2),d1
		move.w	d1,M32(a0)	;sin	
		neg.w	d1		
		move.w	d1,M23(a0)	;-sin	
;| cos    0  sin|
;|   0    1    0|
;|-sin    0  cos|
		lea	ymatrix,a0

		move.w	(a1,d2.w*2),d1
		move.w	d1,M13(a0)
		neg.w	d1		
		move.w	d1,M31(a0)	;-sin	
	
		move.w	(a2,d2.w*2),d1
		move.w	d1,M11(a0)	;cos	
		move.w	d1,M33(a0)	;cos	

;| cos -sin    0|
;| sin  cos    0|
;|   0    0    1|
		lea	zmatrix,a0

		move.w	(a1,d3.w*2),d1
		move.w	d1,M21(a0)
		neg.w	d1		
		move.w	d1,M12(a0)	;-sin	

		move.w	(a2,d3.w*2),d1
		move.w	d1,M11(a0)	;cos	
		move.w	d1,M22(a0)	;cos	

		rts
		


MULSMATRIX_3x3:

;	lea	objmatrix1,a0
;	lea	objmatrix2,a1
;	lea	destmatrix,a2
;	a0 og a2 kan være like

	move.l	#3-1,d7
.loop

	move.w	(a0)+,d0		;en rad i source matrix
	move.w	(a0)+,d1
	move.w	(a0)+,d2

	move.w	M11(a1),d4
	muls.w	d0,d4
	move.w	M21(a1),d5
	muls.w	d1,d5
	move.w	M31(a1),d6
	muls.w	d2,d6

	add.l	d4,d6
	add.l	d5,d6

	asr.l	#8,d6
	asr.l	#6,d6

	move.w	d6,(a2)+

	move.w	M12(a1),d4
	muls.w	d0,d4
	move.w	M22(a1),d5
	muls.w	d1,d5
	move.w	M32(a1),d6
	muls.w	d2,d6

	add.l	d4,d6
	add.l	d5,d6
		
	asr.l	#8,d6
	asr.l	#6,d6
	
	move.w	d6,(a2)+

	move.w	M13(a1),d4
	muls.w	d0,d4
	move.w	M23(a1),d5
	muls.w	d1,d5
	move.w	M33(a1),d6
	muls.w	d2,d6

	add.l	d4,d6
	add.l	d5,d6

	asr.l	#8,d6
	asr.l	#6,d6
	move.w	d6,(a2)+

	dbf	d7,.loop

	rts

ROTATEPOINTS:
		move.l	a7,.stack

		lea	visualobjectlist,a6
		move.l	a6,.objpeker

.oloop:
		move.l	.objpeker(pc),a0
		move.l	(a0)+,d0
		beq.w	.exit
		move.l	a0,.objpeker
		move.l	d0,a0

;		lea	o1matrix,a1
		move.l	Matrixstart(a0),a1
		move.l	Coord3dstart(a0),a3


		move.w	(a1),a2
		move.w	Relxpos(a1),.posx
		move.w	Relypos(a1),.posy
		move.w	Relzpos(a1),.posz

		move.w	2(a1),.matr12
		move.w	4(a1),.matr13		;a3
		move.w	1*6(a1),a4
		move.w	1*6+2(a1),d3
		move.w	1*6+4(a1),d4
		move.w	2*6(a1),a5
		move.w	2*6+2(a1),a6
		move.w	2*6+4(a1),a7

		move.l	Coordstart(a0),a1
		move.l	Coord2dstart(a0),a0

; a0,a1 (d5,d5,d7) + 3  | 8 ledige
.gjenta:
		movem.w	(a1)+,d5-d7		; sette ut x,y,z
		cmp.w	#$7fff,d5
		beq.w	.neste

;		asr.w	#2,d5
;		asr.w	#2,d6
;		asr.w	#2,d7
		addq.l	#2,a1

		move.w	d5,d0
		move.l	a2,d2
		muls.w	d2,d0		;a2 	(a3)
		move.w	d7,d1
		move.w	.matr12(pc),d2
		muls.w	d6,d2
		add.l	d2,d0
		move.w	.matr13(pc),d2
		muls.w	d2,d1		;a3	4(a3)
		add.l	d1,d0		;x

		move.w	d5,d1
		move.l	a4,d2		;1*8(a3)
		muls.w	d2,d1		;a4
		move.w	d6,d2
		muls.w	d3,d2		;d3 1*8+2(a3)
		add.l	d2,d1
		move.w	d7,d2
		muls.w	d4,d2		;d4 1*8+4(a3)
		add.l	d2,d1		;y

 		move.l	a5,d2
		muls.w	d2,d5		;a5 2*8+(a3)
 		move.l	a6,d2
		muls.w	d2,d6		;a6 2*8+2(a3)
 		move.l	a7,d2
		muls.w	d2,d7		;a7 2*8+4(a3)
		add.l	d7,d6
		add.l	d6,d5		;z

		moveq.l	#14,d2
		lsr.l	d2,d0
		lsr.l	d2,d1
		lsr.l	d2,d5

		sub.w	.posx(pc),d0			;camerapos x,y,z
		sub.w	.posy(pc),d1
		sub.w	.posz(pc),d5

;32/16
; x=x3*dist / z3+dist
; y=y3*dist / z3+dist
;
; dz = dist/z
; x = x3*dz
.dist=192
	;	add.w	xvar,d0
	;	add.w	yvar,d1
	;	add.w	zvar,d5

		move.w	d0,(a3)+	;lagre xcoord
		move.w	d1,(a3)+	;lagre ycoord


		move.w	d5,(a0)+	;lagre zcoord
		beq.w	.ok1


 		move.l	#2^11*.dist,d2
		divu.w	d5,d2		;dist/z
		
		muls.w	d2,d0
		muls.w	d2,d1


		moveq.l	#11,d5
		asr.l	d5,d0
		asr.l	d5,d1

.ok1
		add.w	#E_bredde/2,d0
		move.w	d0,(a0)+
		add.w	#E_hoyde/2,d1
		move.w	d1,(a0)+



;		cmp.w	#320,d0
;		bgt.b	.nope
;		cmp.w	#0,d0
;		blt.b	.nope
;
;		cmp.w	#e_hoyde,d1
;		bgt.b	.nope
;		cmp.w	#0,d1
;		blt.b	.nope
;
;	move.l	a3,.temp
;	move.w	d1,d5
;	muls.w	#320*2,d5
;	ext.l	d0
;	add.l	d0,d5
;	add.l	d0,d5
;	lea	kladd,a3
;	and.l	#$3ffff,d5
;	move.w	#$ffff,(a3,d5.l)
;	movem.l	.temp,a3
;.nope


		addq.l	#2,a0

		bra.w	.gjenta
.neste		

		bra.w	.oloop

.exit
		move.l	.stack,a7

		rts
.posx:		dc.w	0
.posy:		dc.w	0
.posz:		dc.w	0
.matr12:	dc.w	0
.matr13:	dc.w	0
.objpeker:	dc.l	0
.stack:		dc.l	0
.temp		dc.l	0

Avrnorm2txtliste=0
AvrnormZ2gouraudliste=1		;0 when Avrnorm2txtliste=1


ROTATENORMALS:

		move.l	a7,.stack

		lea	visualobjectlist,a6

.oloop:
		move.l	(a6)+,d7
		beq.w	.exit
		move.l	d7,a0

		move.l	Normstart(a0),d5
		beq.b	.oloop			;ingen face normaler
		move.l	d5,a1
		move.l	Coord2dstart(a0),a2
		move.l	Matrixstart(a0),a3

		move.w	2*6(a3),d0
		move.w	2*6+2(a3),d1
		move.w	2*6+4(a3),d2


		addq.l	#6,a2
		move.l	Antpunkter(a0),a0		;antall punkter
.gjenta:
		movem.w	(a1)+,d5-d7		; sette ut x,y,z
		addq.l	#2,a1

		muls.w	d0,d5
		muls.w	d1,d6
		muls.w	d2,d7
		add.l	d7,d6
		add.l	d6,d5		;z
		lsr.l	#8,d5
		lsr.l	#7,d5

	;	move.w	-6(a2),d3	;z
	;	lsr.w	#6,d3
	;	sub.w	d3,d5

		cmp.w	#62,d5
		ble.b	.o
		move.w	#62,d5
.o
		cmp.w	#2,d5
		bge.b	.o2
		moveq	#2,d5
.o2
		
		move.w	d5,(a2)+

		addq.l	#6,a2

		subq.l	#1,a0
		tst.l	a0
		bne.w	.gjenta

		bra.w	.oloop

.exit

		move.l	.stack,a7

		rts

.stack:	dc.l	0

ROTATEFACENORMALS:


		lea	visualobjectlist,a6
.oloop:
		move.l	(a6)+,d7 
		beq.w	.exit
		move.l	d7,a0
		move.l	Facenormstart(a0),d5
		bne.b	.ja
		move.l	Antfaces(a0),d5
		lsl.l	#1,d5
		add.l	d5,a2
		bra.b	.oloop			;ingen face normaler
.ja
		move.l	d5,a1
		move.l	Matrixstart(a0),a3
		move.l	Faceoptstart(a0),a2
		add.l	#4*3,a2

		move.w	2*6(a3),d0
		move.w	2*6+2(a3),d1
		move.w	2*6+4(a3),d2


		move.l	Antfaces(a0),a0		;antall punkter
.gjenta:

		movem.w	(a1)+,d5-d7		; sette ut x,y,z
		addq.l	#2,a1

		muls.w	d0,d5
		muls.w	d1,d6
		muls.w	d2,d7
		add.l	d7,d6
		add.l	d6,d5		;z
		lsr.l	#8,d5
		lsr.l	#7,d5

;		move.w	-6(a2),d3	;z
;		lsr.w	#6,d3
;		sub.w	d3,d5

		cmp.w	#61,d5
		ble.b	.o
		move.w	#61,d5
.o
		cmp.w	#2,d5
		bge.b	.o2
		moveq	#2,d5
.o2
		
		move.w	(a2),d6
		and.w	#$c000,d6
		or.w	d5,d6
		move.w	d6,(a2)

		add.l	#16,a2
		subq.l	#1,a0
		tst.l	a0
		bne.w	.gjenta

		bra.w	.oloop

.exit
		rts


**************************************************************

clear:
	lea kladd,a0
	moveq.l	#0,d2
	
	move.l	#E_hoyde-1,d1
.yloop
	move.l	#E_bredde*2/4-1,d0
.loop
	move.l	d2,(a0)+
	dbf	d0,.loop
;	add.l	#512-320,a0
	dbf	d1,.yloop


	rts



; Txture mapp pass 1

; linjeformat (a1)
; Txturemap
;-------------------
; LONG	LAVESTE x,y
; WORD  Dx (<<8) (linje delta int.x)
; WORD	Dy (<<8) (linje delta int.y)
; WORD  DeltaY
; WORD  Txturedeltax
; WORD  Txturedeltay
; WORD  Gourauddelta
; WORD  Høyestegouraud
; LONG	txturestart x,y
; = 22 bytes
; Gouraud
;--------------------
; LONG	LAVESTE x,y
; WORD  Dx (<<8) (linje delta int.x)
; WORD	Dy (<<8) (linje delta int.y)
; WORD  DeltaY
; WORD  0
; WORD  0
; WORD  Gourauddelta
; WORD  Høyestegouraud
; LONG	0
; = 22 bytes
; Plainshade
;--------------------
; LONG	LAVESTE x,y
; WORD  Dx (<<8) (linje delta int.x)
; WORD	Dy (<<8) (linje delta int.y)
; WORD  DeltaY
; WORD  0
; WORD  0
; WORD  0
; WORD  0
; LONG	0
; = 22 bytes



LAGLINJE:
	move.l	a7,.stack
	move.l	#visualobjectlist,.objpeker
	
.oloop
	move.l	.stack,a7
	move.l	.objpeker(pc),a3
	move.l	(a3)+,d0
	beq.w	.exit
	move.l	a3,.objpeker
	move.l	d0,a0
	
	move.l	Coord2dstart(a0),a2	;	lea	coord2d,a2
	move.l	Flagstart(a0),a3
	move.l	Linjer2start(a0),a6
	move.l	Linjerstart(a0),a0

	bra	.start
.do
	lea	(a6,d3.w*8),a1		;finn linje adresse

************** env *****************************
;	move.l	(a0,d3.w*4),d4
;	move.l  (a2,d4.w*8),d1		;x1,y1	
;	move.l	4(a2,d4.w*8),d3		;txt x1,txt y1
;	swap	d4
;	move.l  (a2,d4.w*8),d2		;x2,y2	
;	move.l	4(a2,d4.w*8),d4		;txt x2,txt y2

 ************** txture *****************************

	lea	(a0,d3.w*4),a4
;	lea	(a0,d3.w*8),a4
	move.l	(a4)+,d0

	move.l	d0,d6
	swap	d6
	move.l  2(a2,d0.w*8),d1		;x1,y1	
	move.w	6(a2,d0.w*8),a5		;gouraud2
	move.l  2(a2,d6.w*8),d2		;x2,y2	
	move.w	6(a2,d6.w*8),d7		;gouraud1
	ext.l	d7
	move.l	(a4)+,d4		;txturecoords
	move.l	(a4)+,d3

	move.l	d2,d0
	sub.w	d1,d0			;DeltaY
	bne.b	.jo
.null	move.l	#$7fff7fff,(a1)
	move.w	d0,8(a1)		;0
	bra.w	.start
.jo
	bgt.b	.over2			;---------------
	neg.w	d0
	exg.l	d1,d2
	exg.l	d3,d4			;Bytt om txtcoord peker. 
	exg.l	a5,d7
.over2					;
					;d2=dx d0=dy ; y2>y1
	move.l	d0,a7
	move.w	#E_hoyde,d6

;------------- yclip nede -------------------------
	cmp.w	d6,d1
	blt.b	.o
	moveq.l	#0,d0
	bra.b	.null			;linje utenfor skjerm.
.o
	cmp.w	d6,d2
	blt.b	.o2
	sub.w	d1,d6
	move.w	d6,a7
.o2

;---------------------------------------------------

	move.l	d1,(a1)+		;laveste x og y  \ y1 < y2
	move.l	d3,d6
	lsl.l	#8,d6

	move.w	d1,d2
	sub.l	d1,d2
	clr.w	d2
	asr.l	#8,d2

	ext.l	d0
	divs.w	d0,d2
	bvc.b	.word
	divs.l	d0,d2
	bra.b	.over
.word	ext.l	d2
.over
	move.l	d2,(a1)+		;dx/dy
	
	sub.l	d3,d4			;delta txtx,txty
	and.l	d3,d3
	beq.b	.gouraud		;ingen txture gouraud only.

	move.l	d4,d5
	move.w	a7,(a1)+		;dy             |
	asr.l	#8,d5
	clr.b	d5
	divs.w	d0,d5
	move.w	d5,(a1)+

	ext.l	d4
	lsl.l	#8,d4
	divs.w	d0,d4
	move.w	d4,(a1)+

	sub.w	a5,d7
	ext.l	d7
	lsl.l	#8,d7
	move.l	a5,d3
	lsl.l	#8,d3
	divs.w	d0,d7

	neg.w	d7
	move.w	d7,(a1)+

	neg.w	d3
	move.w	d3,(a1)+		; høyste gouraud

	and.l	#$ff00ff00,d6		;txturestart x,y
	move.l	d6,(a1)+

;----------- yclip oppe ------------
	neg.w	d1
	ble.b	.start

	sub.w	d1,-14(a1)		;trekk fra i deltay
	bgt.b	.inne
	move.l	#$7fff7fff,-22(a1)
	bra.b	.start		

.inne
	muls.w	d1,d2
	moveq.l	#0,d0	
	asr.l	#8,d2
	addx.w	d0,d2		;avrund til nærmeste
	add.w	d2,-22(a1)		;ny start
	clr.w	-20(a1)

	muls.w	d1,d5			;txture
	muls.w	d1,d4			;txture
	muls.w	d1,d7			;gouraud
	add.w	d4,-(a1)
	add.w	d5,-(a1)
	add.w	d7,-(a1)

.start	
	move.w	(a3)+,d3		;hent ut linjepeker
	bpl.w	.do

	bra	.oloop

.gouraud:
	move.w	a7,(a1)+		;dy             |

	clr.l	(a1)+
	sub.w	a5,d7
	ext.l	d7
	lsl.l	#8,d7
	move.l	a5,d3
	lsl.l	#8,d3
	divs.w	d0,d7

	move.w	d7,(a1)+
	move.w	d3,(a1)+		; høyste gouraud
	clr.l	(a1)

;----------- yclip oppe ------------
	neg.w	d1
	ble.b	.start

	sub.w	d1,-10(a1)		;trekk fra i deltay
	bgt.b	.inne2
	move.l	#$7fff7fff,-18(a1)
	bra.b	.start		

.inne2
	muls.w	d1,d2
	moveq.l	#0,d0	
	asr.l	#8,d2
	addx.w	d0,d2		;avrund til nærmeste
	add.w	d2,-18(a1)		;ny start
	clr.w	-16(a1)

	muls.w	d1,d7			;gouraud
	add.w	d7,-(a1)

	bra.b	.start

.exit:
	move.l	.stack,a7

	rts

.stack:		dc.l	0
.objpeker:	dc.l	0
; $3af
; $3a2
; $38f

RENDER: 
	
	moveq.l	#0,d1		;viktig
	
;	lea	txture,a6
	move.l	a7,.stack
	move.l 	#visuallist,.visual
	move.l	#facepekere,.linjefaces

	bra.w	.start
	cnop	0,8
.poly
	subq.l	#4,a7
	addq.l	#4,a4		;(a5)+
 	move.l	a4,(a7)+	;.facen

	move.l	(a3,d7.w*4),a3

	move.l	(a3)+,a2
	move.l	(a3)+,a1
	move.l	(a3)+,a5
	move.l	(a3)+,d0	;flags,txture #

	move.l	(a2)+,d3  ; to start-punkter
	move.l	(a1)+,d4	;
	move.l	(a5)+,d5	;

;------- black-box ------------------- :)
	cmp.w	d3,d4
	beq.b	.oo
	blt.b	.o
	exg.l	a1,a2
	exg.l	d3,d4
.o	exg.l	d5,d3  ; to start-punkter
	exg.l	a2,a5
.oo
;-------------------------------------- r
	move.l	(a2)+,a3	;xstigningstall til polyfiller
	move.l	(a1)+,a4	;xstigningstall til polyfiller

	move.l	a5,(a7)+

	cmp.l	a3,a4
	bgt.s	.oooo
	beq.w	.start
	exg	a2,a1
	exg 	a3,a4			;a3 < a4
	exg	d3,d4
.oooo
					;a3<a4      tillhørende d0 og d1
	move.w	(a2)+,d2	;dy
	move.w	(a1)+,d6	;dy
	
;------------------- flat top fix ---------------------------
	move.w	d4,d3
	sub.l	d4,d3
	ble.w	.q2
	exg.l	a3,a4
	exg.l	a2,a1
	bra.b	.ooo
.q2
	add.l	d3,d4
	
	neg.l	d3
	addq.w	#1,d3
	cmp.w	d2,d6
	bgt.b	.ooo
	move.w	d6,d2
	addq.w	#1,d3
.ooo
;------------------------------------------------------------
	move.l	d0,d0
	bpl.w	.gouraud		;ingen txture ->gouraud/plainshade

	bra.w	RENDER_TXTGOUR

.gouraud:

	lea	gouraudshades,a6
	lea	(a6,d0.w*8),a6

	btst.l	#30,d0
	beq.w	.plainshade

	move.l	a4,d6
	sub.l	a3,d6
	addq.l	#4,a2		; skip txture
	move.w	(a2)+,a7	;gouraud stigning
	addq.l	#4,a1		;skip
	move.w	(a1)+,d1	;gouraud stigning
	ext.l	d1
	move.w	(a2),a5	;Øverste gouraud verdi

	sub.l	a2,a2

	move.l	d4,d7
	move.w	d3,d7		;flag til neste loop
	swap	d7
	muls.w	#E_bredde*2,d4
	add.w	d7,a0
	add.w	d7,a0
	sub.w	#E_bredde-1,d7		;til x-clip

	add.l	d4,a0		;alt er relativt

;----- Gouraud deltaer ----------------------
	sub.l	a7,d1
	lsl.l	#8,d1
	divs.w	d6,d1
	move.l	a3,d6
	move.w	d1,a3

;------ fix til addx loop etc--------------------
	lsr.l	#8,d3			;light box multi shift :)

	neg.w	d7
	swap	d7
.entil
	swap	d7
.ytre
	move.l	a2,d0
	move.l	d3,d1
	asr.l	#8,d0
	asr.l	#8,d1
 
	move.w	d0,a1
 	move.l	a5,d4			;gouraud start int

	cmp.w	d7,d1
	blt.w	.xcliph
	move.w	d7,d1
.xcliph

	move.w	d0,d5
	neg.w	d5
	add.w	d7,d5
	moveq.l	#0,d0

	sub.w	#E_bredde-1,d5
	ble.b	.xclipv
	add.w	d5,a1

	move.l	a3,d0
	muls.l	d5,d0
	add.l	d0,d4		;gouraud
.xclipv
	sub.w	a1,d1
	bmi.b	.ikke

	add.l	a1,a1
	add.l	a0,a1		;skjerm start

	and.l	#$ffff,d4
	and.l	#$ffff,d5
	
	ror.l	#8,d4
	move.l	a3,d5
	ror.l	#8,d5

	move.l	d4,d0
	add.w	d5,d0		;fix addx carrybug

.indre	move.w	(a6,d4.w*2),(a1)+
	addx.l	d5,d4
	dbf	d1,.indre

.ikke	
	add.l	d6,a2			;polint start x
	add.l	a4,d3			;polint delta
	add.l	a7,a5			;gouraud

	lea	E_bredde*2(a0),a0
	subq.w	#1,d2
	bgt.b	.ytre

.over
	move.l	.hvilkenlinje(pc),a1
	swap	d7
	
;-------- black box -------------- ;)
.black
	subq.w	#1,d7			;sub til .tolooper
	bne.b	.neste

	move.l	(a1)+,d6	;a3	;ny xstigntalle til polyfiller
	move.w	(a1)+,d2		;deltay
	move.w	4(a1),a7		;gouraud

	bra.w	.entil
.neste:
	move.l	(a1)+,a4		;ny stign.
	move.w	(a1)+,d2		;deltay

	subq.w	#1,d7
	beq.w	.entil

;----------------------------------
.start:
	lea	.kladdp(pc),a7
;	move.l	(a7)+,a0		;kladd
;	move.l	(a7)+,a1		;linjer2
;	move.l	(a7)+,a3		;facepeker
;	move.l	(a7),a4		;visuallist

	movem.l	(a7)+,a0/a1/a3/a4

	move.w	(a4),d7
	bpl.w	.poly		; laglinje\.ut2
.ut
	move.l .stack,a7

	rts

	cnop	0,4

.stack	  	dc.l	0
.kladdp		dc.l	kladd
.linjer2p	dc.l	0
.linjefaces	dc.l	facepekere		;linjefaces
.visual	  	dc.l	visuallist
.hvilkenlinje:	dc.l	0
.kladdint:	dc.l	0
		dc.l	0
		dc.l	0
		dc.l	0

.plainshade:
*****************************************************

	swap	d0
	and.w	#$ff,d0
	move.w	-8(a6,d0.w*2),d6		;i shadetable

	sub.l	a2,a2

	move.l	d4,d7
	move.w	d3,d7		;flag til neste loop
	swap	d7
	muls.w	#E_bredde*2,d4
	add.w	d7,a0
	add.w	d7,a0
	sub.w	#E_bredde-1,d7		;til x-clip

	add.l	d4,a0		;alt er relativt

;------ fix til addx loop etc--------------------
	lsr.l	#8,d3			;light box multi shift :)

	neg.w	d7
	swap	d7
.pentil
	swap	d7
.pytre
	move.l	a2,d0
	move.l	d3,d1
	asr.l	#8,d0
	asr.l	#8,d1
 
	move.w	d0,a1

	cmp.w	d7,d1
	blt.w	.pxcliph
	move.w	d7,d1
.pxcliph

	move.w	d0,d5
	neg.w	d5
	add.w	d7,d5

	sub.w	#E_bredde-1,d5
	ble.b	.pxclipv
	add.w	d5,a1

.pxclipv
	sub.w	a1,d1
	bmi.b	.pikke

	add.l	a1,a1
	add.l	a0,a1		;skjerm start

.pindre
	move.w	d6,(a1)+

	dbf	d1,.pindre
.pikke	
	add.l	a3,a2			;polint start x
	add.l	a4,d3			;polint delta

	lea	E_bredde*2(a0),a0
	subq.w	#1,d2
	bgt.b	.pytre

.pover
	move.l	.hvilkenlinje(pc),a1
	swap	d7
	
;-------- black box -------------- ;)
.pblack
	subq.w	#1,d7			;sub til .tolooper
	bne.b	.pneste

	move.l	(a1)+,a3	;a3	;ny xstigntalle til polyfiller
	move.w	(a1)+,d2		;deltay

	bra.w	.pentil
.pneste:
	move.l	(a1)+,a4		;ny stign.
	move.w	(a1)+,d2		;deltay

	subq.w	#1,d7
	beq.w	.pentil

	bra	RENDER\.start

*****************************************************


RENDER_TXTGOUR:
	lea	txturepekere,a6
	move.l	(a6,d0.w*4),a6

;	move.l	RENDER\.stack,a7
;	illegal

	btst.l	#30,d0			;smooth?
	beq.w	.render_txtplain

	move.l	a3,(a7)+	
	move.l	a4,(a7)+	

	move.l	a4,d6
	sub.l	a3,d6
	move.l	(a2)+,a5	;txt vektor
	move.w	(a2)+,a3	;gouraud stigning
   	move.l	(a1)+,d0	;txt vektor
	move.w	(a1)+,d1	;gouraud stigning
	ext.l	d1

	move.l	a5,(a7)+
	move.l	a3,(a7)+

	sub.l	a5,d0	;x stig txt vektor (x.w,y.w)

	move.w	(a2)+,a5	;Øverste gouraud verdi
	move.l	(a2),a7		;Øverste txt coords
	sub.l	a2,a2

	move.l	d4,d7
;	ext.l	d4
	move.w	d3,d7		;flag til neste loop
	swap	d7
	muls.w	#E_bredde*2,d4
;	lsl.l	#8,d4
;	add.l	d4,d4
	add.w	d7,a0
	add.w	d7,a0
	sub.w	#E_bredde-2,d7		;til x-clip

	add.l	d4,a0		;alt er relativt

;----- txture deltaer ----------------------

	move.l	d0,d4	;x		;	clr.w	d4
	asr.l	#8,d4
	divs.w	d6,d4

	ext.l	d0
	lsl.l	#8,d0
	divs.w	d6,d0

;----- Gouraud deltaer ----------------------
	sub.l	a3,d1
	swap	d1
	clr.w	d1
;	lsl.l	#8,d1
	divs.l	d6,d1
;	bvc.b	.nei
	asr.l	#8,d1

	move.l	d1,a3

;------ fix til addx loop etc--------------------
	move.l	d4,d6

	moveq.l	#-1,d4	;teller
	ror.l	#8,d6
	clr.w	d4
	move.b	d6,d4
	lsr.l	#8,d3			;light box multi shift :)
	move.w	d0,d6
;--
	neg.w	d7
	swap	d7
.entil
	swap	d7
.ytre
	move.l	a2,d0
	move.l	d3,d1
	asr.l	#8,d0
	asr.l	#8,d1
 
	move.w	d0,a1
 	move.l	a5,a4			;gouraud start int

	cmp.w	d7,d1
	blt.w	.xcliph
	move.w	d7,d1
.xcliph

	move.w	d0,d5
	neg.w	d5
	add.w	d7,d5
	moveq.l	#0,d0

	sub.w	#E_bredde-1,d5
	ble.b	.xclipv
	ext.l	d5
	add.l	d5,a1

	move.l	a3,d0
	muls.l	d5,d0
	add.l	d0,a4		;gouraud
				;xx00YYyy
	move.l	d6,d0		;xx00YYyy
	swap	d0		;YYyyxx00
	move.b	d4,d0		;YYyyxxXX
	rol.w	#8,d0		;YYyyXXxx
	swap	d0		;XXxxYYyy

	muls.l	d5,d0

.xclipv

	sub.w	a1,d1
	bmi.b	.ikke

	add.l	a1,a1
	add.l	a0,a1		;skjerm start

	add.l	a7,d0		;XXxxYYyy
	move.w	d0,d5
	rol.l	#8,d0		;xxYYyyXX
	swap	d1		;loopkkkk
	move.w	d0,d1		;loopkkXX
	and.w	#$ff,d1		;viktig!!

	move.w	d5,d0		;xxYYYYyy

 	moveq.l	#0,d5	
	swap	d2
				;xx00YYyy
.indre				;loop00XX
	move.w	d0,d5
	move.w	a4,d2
	move.b	d1,d5
	add.l	a3,a4		;	neg.w	d7;	move.b	#$f,d7
	move.b	(a6,d5.l),d2
	add.l	d6,d0
	move.w	(a6,d2.w*2),(a1)+
	addx.l	d4,d1
	bcs.b	.indre
	swap	d2
.ikke	
	lea	RENDER\.kladdint(pc),a1

	add.l	(a1)+,a2			;polint start x
	add.l	(a1)+,d3			;polint delta
	add.l	(a1)+,a7			;Txturestart x,Y int start
	add.l	(a1)+,a5

	lea	E_bredde*2(a0),a0
	subq.w	#1,d2
	bgt.w	.ytre

.over
	move.l	RENDER\.hvilkenlinje(pc),a1
	lea	RENDER\.kladdint+4(pc),a4

	swap	d7
	
;-------- black box -------------- ;)
.black
	subq.w	#1,d7			;sub til .tolooper
	bne.b	.neste

	move.l	(a1)+,-(a4)	;a3	;ny xstigntalle til polyfiller
	move.w	(a1)+,d2		;deltay
	addq.l	#8,a4
	move.l	(a1)+,(a4)+		;nytxtinter
	clr.w	(a4)+
	move.w	(a1)+,(a4)		;gouraud

	bra.w	.entil
.neste:
	move.l	(a1)+,(a4)		;ny xstigntalle til polyfiller
	move.w	(a1)+,d2		;deltay

	subq.w	#1,d7
	beq.w	.entil

;----------------------------------
	bra	RENDER\.start
*************** Txture plainshade *********************
.render_txtplain:
	lsr.l	#8,d0
	neg.w	d0
;	or.w	#$8000,d0
;	neg.w	d0
;	and.l	#$3fffffff,d0
;	neg.l	d0
;	asr.l	#8,d0
	move.l	d0,a5
	
	move.l	a3,(a7)+	
	move.l	a4,(a7)+	

	move.l	a4,d6
	sub.l	a3,d6

	move.l	(a2)+,a4	;txt vektor
   	move.l	(a1)+,d0	;txt vektor

	move.l	a4,(a7)+
	move.l	a3,(a7)+

	sub.l	a4,d0	;x stig txt vektor (x.w,y.w)

	move.l	4(a2),a7		;Øverste txt coords
	sub.l	a2,a2

	move.l	d4,d7
	move.w	d3,d7		;flag til neste loop
	swap	d7
	muls.w	#E_bredde*2,d4
	add.w	d7,a0
	add.w	d7,a0
	sub.w	#E_bredde-2,d7		;til x-clip

	add.l	d4,a0		;alt er relativt

;----- txture deltaer ----------------------

	move.l	d0,d4	;x		;	clr.w	d4
	asr.l	#8,d4
	divs.w	d6,d4

	ext.l	d0
	lsl.l	#8,d0
	divs.w	d6,d0

;------ fix til addx loop etc--------------------
	move.l	d4,d6

	moveq.l	#-1,d4	;teller
	ror.l	#8,d6
	clr.w	d4
	move.b	d6,d4
	lsr.l	#8,d3			;light box multi shift :)
	move.w	d0,d6
;--
	neg.w	d7
	swap	d7
.pentil
	swap	d7
.pytre
	move.l	a2,d0
	move.l	d3,d1
	asr.l	#8,d0
	asr.l	#8,d1
 
	move.w	d0,a1

	cmp.w	d7,d1
	blt.w	.pxcliph
	move.w	d7,d1
.pxcliph

	move.w	d0,d5
	neg.w	d5
	add.w	d7,d5
	moveq.l	#0,d0

	sub.w	#E_bredde-1,d5
	ble.b	.pxclipv
	ext.l	d5
	add.l	d5,a1

	move.l	a3,d0
	muls.l	d5,d0
				;xx00YYyy
	move.l	d6,d0		;xx00YYyy
	swap	d0		;YYyyxx00
	move.b	d4,d0		;YYyyxxXX
	rol.w	#8,d0		;YYyyXXxx
	swap	d0		;XXxxYYyy

	muls.l	d5,d0

.pxclipv
	sub.w	a1,d1
	bmi.b	.pikke

	add.l	a1,a1
	add.l	a0,a1		;skjerm start

	add.l	a7,d0		;XXxxYYyy
	move.w	d0,d5
	rol.l	#8,d0		;xxYYyyXX
	swap	d1		;loopkkkk
	move.w	d0,d1		;loopkkXX
	and.w	#$ff,d1		;viktig!!

	move.w	d5,d0		;xxYYYYyy

 	moveq.l	#0,d5	
	swap	d2
				;xx00YYyy
;	move.w	a4,d2

	move.w	a5,d2	
.pindre				;loop00XX
	move.w	d0,d5
	move.b	d1,d5
	move.b	(a6,d5.l),d2
	add.l	d6,d0
	move.w	(a6,d2.w*2),(a1)+
	addx.l	d4,d1
	bcs.b	.pindre
	swap	d2
.pikke	
	lea	RENDER\.kladdint(pc),a1

	add.l	(a1)+,a2			;polint start x
	add.l	(a1)+,d3			;polint delta
	add.l	(a1)+,a7			;Txturestart x,Y int start

	lea	E_bredde*2(a0),a0
	subq.w	#1,d2
	bgt.w	.pytre

.pover
	move.l	RENDER\.hvilkenlinje(pc),a1
	lea	RENDER\.kladdint+4(pc),a4

	swap	d7
	
;-------- black box -------------- ;)
.pblack
	subq.w	#1,d7			;sub til .tolooper
	bne.b	.pneste

	move.l	(a1)+,-(a4)	;a3	;ny xstigntalle til polyfiller
	move.w	(a1)+,d2		;deltay
	addq.l	#8,a4
	move.l	(a1)+,(a4)+		;nytxtinter
	clr.w	(a4)+

	bra.w	.pentil
.pneste:
	move.l	(a1)+,(a4)		;ny xstigntalle til polyfiller
	move.w	(a1)+,d2		;deltay

	subq.w	#1,d7
	beq.w	.pentil

	bra	RENDER\.start

.ut

*******************************************************
Tabell:	Include "Include:Trigotabell2.I"

SKRIVSTATS:
*************************************************
;		regn ut litt
		lea	.array,a0
		lea	speedtable,a1

                lea     scene,a0
                move.l  Objlistptr(a0),a0      ;peker på star av hoved obj liste.
                addq.l  #4,a0                   ;skip camera.
	;	move.l	objekter,a2

		move.l	4+4(a2),(a0)+
		move.l	4(a2),(a0)+
		move.l	(a1)+,d0			
		move.l	(a1)+,d1		
		move.l	(a1)+,d2	
		move.l	(a1)+,d3
		move.l	(a1)+,d4
		move.l	(a1)+,d5

		moveq.l	#0,d6
		add.l	d0,d6
		add.l	d1,d6
		add.l	d2,d6
		add.l	d3,d6
		add.l	d4,d6
		add.l	d5,d6

		move.l	d0,(a0)+
		muls.l	#100,d0
		divs.l	d6,d0
		move.l	d0,(a0)+

		move.l	d1,(a0)+
		muls.l	#100,d1
		divs.l	d6,d1
		move.l	d1,(a0)+

		move.l	d2,(a0)+
		muls.l	#100,d2
		divs.l	d6,d2
		move.l	d2,(a0)+

		move.l	d3,(a0)+
		muls.l	#100,d3
		divs.l	d6,d3
		move.l	d3,(a0)+

		move.l	d4,(a0)+
		muls.l	#100,d4
		divs.l	d6,d4
		move.l	d4,(a0)+

		move.l	d5,(a0)+
		muls.l	#100,d5
		divs.l	d6,d5
		move.l	d5,(a0)+

		move.l	d6,(a0)+
		muls.l	#100,d6
		divs.l	#312,d6
		move.l	d6,(a0)+
		move.l	#50*100*100,d7
		divs.l	d6,d7
		move.l	d7,(a0)+

;c2pspeed:	dc.l	0
;Rotation:	dc.l	0
;Backface:	dc.l	0
;qsorts:	dc.l	0
;txtmappass1:	dc.l	0
;txtmappass2:	dc.l	0

*************************************************
		move.l	4,a6
		lea	.DOSNAME,a1
		clr.l	d0
		jsr	_LVOOpenLibrary(a6)
		move.l	d0,.DOSBASE
		lea	.TEXT,a0			; Text som skal formateres

		lea	.array,a1		; Array of longwords
		lea	.PUTCHAR(PC),a2		; Rutine for å prosessere text
		lea	.DESTBUFFER,a3		; Destinasjons buffer for alt
		jsr	_LVORawDoFmt(a6)

		move.l	.DOSBASE,a6
		jsr	_LVOOutput(a6)
		move.l	d0,d1
		move.l	#.DESTBUFFER,d2
		move.l	#.E-.DESTBUFFER,d3
		jsr	_LVOWrite(a6)

		move.l	4,a6
		move.l	.DOSBASE,a1
		jsr	_LVOCloseLibrary(a6)
		rts

.PUTCHAR		move.b	d0,(a3)+	; rutine som kalles for kopiering
		rts			; av bufferet.

.HANDLE		dc.l	0
.DOSBASE		dc.l	0
.DOSNAME		dc.b	"dos.library",0

.TEXT

		dc.b	"3d engine by SP (Skjeggspir),Brekke ",$0A
		dc.b    "and Zelow. Os coding by NTR         ",$0A
		dc.b	"------------------------------------",$0a
		dc.b	"       Statistics for this test     ",$0A
		dc.b	"------------------------------------",$0A,$0A
		dc.b	"Faces:  %8lu",$a
		dc.b	"Coords: %8lu",$a
		dc.b	"                  Rasters    Percent",$0A
		dc.b	"               ---------------------",$0A
		dc.b	"C2P           = %8lu    %3lu ",$0A
		dc.b	"Rotation      = %8lu    %3lu ",$0A
		dc.b	"Backface/clip = %8lu    %3lu ",$0A
		dc.b	"Qsort         = %8lu    %3lu ",$0A
		dc.b	"Txtmappass1   = %8lu    %3lu ",$0A
		dc.b	"Txtmappass2   = %8lu    %3lu ",$0A
		dc.b	"               ---------------------",$0A
		dc.b	"Total         = %8lu = %3lu / 100 vbl ",$0A
		dc.b	"Around        =       %8lu / 100 Fps ",$0A 
		dc.b	"    (blitter delay not counted)",$0A
		dc.b	"------------------------------------",$0a
		dc.b	"Contraz - August 97",$0a
		dc.b	$00
		even
		
.array		ds.l	2*12+8+12	; et long per % kommando i stringen

.DESTBUFFER	ds.b	1000	; Konvertert med alle tallene
.E


FILE
.LOAD		move.l	4,.exebase
		move.l	.exebase(PC),a6
		lea	.dosname(PC),a1
		clr.l	d0
		jsr	-552(a6)
		move.l	d0,.dosbase
		beq.w	.out
		lea	.reqname(PC),a1
		clr.l	d0
		jsr	-552(a6)
		move.l	d0,.reqbase
		beq.w	.dosclose
		move.l	#108,d0
		move.l	#$10000,d1
		jsr	-198(a6)
		move.l	d0,.filename
		beq.w	.reqclose
		move.l	.reqbase(PC),a6
		clr.l	d0
		sub.l	a0,a0
		jsr	-30(a6)
		move.l	d0,.request
		beq.w	.reqclose
		lea	.tags(PC),a0
		move.l	d0,a1
		move.l	.filename(PC),a2
		lea	.title(PC),a3
		jsr	-54(a6)
		tst.l	d0
		beq.w	.reqclose
		move.l	.dosbase(PC),a6
		move.l	.request(PC),a0
		move.l	16(a0),d1
		move.l	.filename(PC),d2
		move.l	#256,d3
		jsr	-882(a6)
		tst.l	d0
		beq.w	.reqclose
		move.l	.request(PC),a0
		move.l	16(a0),d1
		move.l	#1005,d2
		jsr	-30(a6)
		move.l	d0,.handle
		beq.s	.reqclose
		move.l	d0,d1
		clr.l	d2
		moveq	#1,d3
		jsr	-66(a6)
		move.l	.handle(PC),d1
		move.l	d0,d2
		moveq	#-1,d3
		jsr	-66(a6)
		move.l	d0,.length
		move.l	.exebase(PC),a6
		move.l	#$10000,d1
		jsr	-198(a6)
		move.l	d0,.pointer
		beq.s	.fileclose
		move.l	.dosbase(PC),a6
		move.l	.handle(PC),d1
		move.l	d0,d2
		move.l	.length(PC),d3
		jsr	-42(a6)
.fileclose	move.l	.dosbase(PC),a6
		move.l	.handle(PC),d1
		jsr	-36(a6)
.freereq	move.l	.reqbase(PC),a6
		move.l	.request(PC),a1
		jsr	-36(a6)
.freebuffer	move.l	.exebase(PC),a6
		move.l	#108,d0
		move.l	.filename(PC),a1
		jsr	-210(a6)
.reqclose	move.l	.exebase(PC),a6
		move.l	.reqbase(PC),a1
		jsr	-414(a6)
.dosclose	move.l	.dosbase(PC),a1
		jsr	-414(a6)
;		moveq	#-1,d0
		move.l	.pointer,d0
		move.l	d0,a0
		cmp.l	#'ctz2',(a0)
		beq.b	.o
		moveq.l	#0,d0
.o

.out		rts
.FREE		move.l	.exebase(PC),a6
		move.l	.length(PC),d0
		move.l	.pointer(PC),a1
		jmp	-210(a6)
.dosbase	dc.l	0
.reqbase	dc.l	0
.request	dc.l	0
.exebase	dc.l	0
.handle		dc.l	0
.length		dc.l	0
.pointer	dc.l	0
.filename	dc.l	0
.tags		dc.l	$8000000B,"_"
		dc.l	$80000003,$0
		dc.l	$80000028,$4
		dc.l	$8000002A,.textok
		dc.l	0,0
.dosname	dc.b	"dos.library",0
.reqname	dc.b	"reqtools.library",0
.title		dc.b	"Choose an ctz2-object file",0
.textok		dc.b	"_Load",0



********************************************
VBL_INTERRUPT:
	jsr	KEYBOARDKONTROL
	move.l	framecounter,d0
	addq.l	#1,d0
	move.l	d0,framecounter

	cmp.l	frames,d0
	blt.b	.o
	clr.l	framecounter
.o

	IFNE 	Music
	movem.l d0-d7/a0-a6,-(sp)
	move    #$20,$dff09c
	move.l  mrate50,d0
	add.l   d0,playpos
	move.l  buffSizeMaskFF(pc),d0
	and.l   d0,playpos
	bsr     play
	movem.l (sp)+,d0-d7/a0-a6
	ENDIF

	nop
	rts

******************** COPPER ***********

	include	include:accede/accede.i

	section	copper,data_c

copper:	
;		dc.w	$0962,$0020		; dma
scroll		dc.w	$102,$0000
		dc.w	$104,$0000
		dc.w	$106,%0000000000100000
bplc0:		dc.w	$100,$0211		; bplcon0
;		dc.w	$100,$0200		; bplcon0


diwsta:		dc.w	$8E,0
diwsto:		dc.w	$90,0
ddfsta:		dc.w	$92,$38
ddfsto: 	dc.w	$94,0
doubleya:	dc.w	$1fc,%11	; 64bit fetch på
doubleyb:	dc.w	$108,0
		dc.w	$10a,0
		dc.w	$1dc,32			;32=pal, 0=ntsc
planar:		dc.w	$0e0,0
		dc.w	$0e2,0
		dc.w	$0e4,0
		dc.w	$0e6,0
		dc.w	$0e8,0
		dc.w	$0ea,0
		dc.w	$0ec,0
		dc.w	$0ee,0
		dc.w	$0f0,0
		dc.w	$0f2,0
		dc.w	$0f4,0
		dc.w	$0f6,0
		dc.w	$0f8,0
		dc.w	$0fa,0
		dc.w	$0fc,0
		dc.w	$0fe,0

		dc.w $ffff,$fffe
		dc.w $ffff,$fffe

makshoyde=E_hoyde+10
maksbredde=1280

	section screen,bss_c
	IFNE	_Cpu3blit
chipbuffa:	ds.b	makshoyde*maksbredde+8*8
	ENDIF
screen:		ds.b	makshoyde*maksbredde+8*8
	section screen2,bss_c

screen2:	ds.b	makshoyde*maksbredde+8*8
screen3:	ds.b	makshoyde*maksbredde+8*8

	section	data,data_f

;	incbin "data:shadetable"
;	incbin	data:shdtbl3.pre
shadetable:
;	incbin	data:shdtbl.pre
;	ds.w	128*256
;txture:
;	ds.b	256*256

;	incbin data:1998/trollmann.chk
;	incbin data:jungle.chk
;	incbin data:dracula.raw
;	incbin data:sol3.chk
;	incbin	data:shdtbl.pre
;	incbin data:dame.chk
;	incbin data:ray3.chk
;	incbin data:bumplys.chk
;	incbin data:kluss.chk
	even

	blk.l	$100,0
cmap:
;	incbin "data:gress.cmap"
;	incbin "data:gress.cmap"
;	incbin "data:gress.cmap"
;	incbin "data:gress.cmap"
	blk.b	1024,0
	blk.b	1024,0
	blk.b	1024,0
	blk.b	1024,0

speedtable:

c2pspeed:	dc.l	0
rotation:	dc.l	0
backface:	dc.l	0
qsorts:		dc.l	0
txtmappass1:	dc.l	0
txtmappass2:	dc.l	0


zrotert:
		dc.w	0	;512	;	dc.w	558
	;	dc.w  32
	;	dc.w 2048/3+4
	;	dc.w	2048/4+(2048/4)/2+4	;2048/4		; 2048-(256+10)
yrotert:	dc.w	0	;512
xrotert:	dc.w 	1024	;+10	;2048/3+24
		cnop	0,4
variabler:
xvar:	dc.l	0		;$FFC80000 	
yvar:	dc.l	0	;$00840000	;100,0	
zvar:	dc.w	$1f0,0	;$FE100000	

antvisualfaces:	dc.l	0
framecounter:	dc.l	0
frames:		dc.l	0
camptr:		dc.l	0

;cammatrix:
	dc.w	1<<14,0,0
	dc.w	0,1<<14,0
	dc.w	0,0,1<<14
	dc.w	0,0,0	;objekt rotasjon
	dc.w	0,0,0	;objekt rotasjonsposisjon
	dc.w	0,0,800	;objekt posisjon
	dc.w	0,0,0	;kamera rel. verden posisjon
	dc.w	0,0,0	;pivot point

	cnop	0,4

xmatrix:	dc.w	1<<14,0,0		;|   1    0    0|
		dc.w	0,0,0		;|   0  cos -sin|
		dc.w	0,0,0		;|   0  sin  cos|

ymatrix:	dc.w	0,0,0		;| cos    0  sin|
		dc.w	0,1<<14,0		;|   0    1    0|
		dc.w	0,0,0		;|-sin    0  cos|

zmatrix:	dc.w	0,0,0		;| cos -sin    0|
		dc.w	0,0,0		;| sin  cos    0|
		dc.w	0,0,1<<14		;|   0    0    1|


kladdmatrix:	dc.w	0,0,0		;|   1    0    0|
		dc.w	0,0,0		;|   0  cos -sin|
		dc.w	0,0,0		;|   0  sin  cos|

kake2:	dc.l	0

					; * trengs ikke
clipobject:
		dc.l	0		; Uobj peker
		dc.l	0		; LONG  Clipobjflags
		dc.l	0		; LONG	Coord3dstart  *
		dc.l	0		; LONG	Matrixstart   *
		dc.l	0		; LONG	Faceoptstart
		dc.l	0		; LONG	Linjer2start
		dc.l	0		; LONG	Flagstart
		dc.l	0		; LONG	Coord2dstart	
		dc.l	'ctz8'		; LONG	Header        *
		dc.l	0		; LONG	Antpunkter    *
		dc.l	0		; LONG	Antfaces      *
		dc.l	0		; LONG	Antlinjer     *
		dc.l	0		; LONG	Boundboxstart * 
		dc.l	0		; LONG	Coordstart    *
		dc.l	0		; LONG	Facestart     *
		dc.l	0		; LONG	Normstart     *
		dc.l	0		; LONG	Facenormstart *
		dc.l	0		; LONG	Linjefacestart*
		dc.l	0		; LONG	Linjerstart
		dc.l	0		; LONG	Fargerstart   *
		dc.l	0		; LONG	Navnstart     *


scenen:
;		incbin demo:lwscene/verden.ctzs
;		incbin demo:lwscene/box.ctzs
;		incbin demo:lwscene/box2.ctzs
;		incbin demo:lwscene/amiga1200+.ctzs
		incbin demo:lwscene/amiga1200.ctzs
;		incbin demo:lwscene/WARRIOR-----.ctzs
w
antallfaces:	dc.l	0		; for hele scenen
antallpunkter:	dc.l	0
antalllinjer:	dc.l	0


	section	blahb,data_f
	IFNE	Music
module	
	incbin	"DH5:Music/srm&xm/to_spira.s3m"
	;incbin  "DH1:Music/srm&xm/to_anuma.s3m"
	ENDIF

	section	tmp2,bss_f

	ds.b	1280*E_hoyde
	ds.b	1280*E_hoyde
	ds.b	1280*E_hoyde
kladd:
	ds.b	1280*E_hoyde
	ds.b	1280*E_hoyde
	ds.b	1280*E_hoyde

	section	tmp2,bss_f

visuallist:	ds.l	Maksantallfaces	
sorttable2:	ds.l	Maksantallfaces	
linjepekere:	ds.w	Maksantalllinjer
linjepekere2:	ds.w	Maksantalllinjer
linjesjekk:	ds.w	Maksantalllinjer
facepekere:	ds.l	Maksantallfaces
surfaces:	ds.l	Maksantallfaces*4
surfaces2:	ds.l	Maksantallfaces*4
facenormals:	ds.w	Maksantallfaces

txturepekere:	ds.l	100	;maks 100 txtures
kladdcmap:	ds.l	1024
objpekerarray:	ds.l	1000	;maks 1000 objekter

	section	merdata,bss_f
visualobjectlist:	ds.l	1000

shadetable2:
	ds.l	256*256
gouraudshades:
	ds.w	256*256
	ds.w	256*256
	ds.w	256*256
;	ds.w	256*256

	printt "Texturepass1: "
	printv ((LAGLINJE\.exit-LAGLINJE\.do))

	printt "Texturepass2: "
	printv ((RENDER_TXTGOUR\.ut-RENDER\.poly))

	printt "backface: "
	printv ((INSIDEOUT\.ut-INSIDEOUT\.loop))
