Add some code structure for future triangle rendering

This commit is contained in:
hugova 2025-04-28 16:01:06 +02:00
parent a7031b7418
commit c27e79efa6
5 changed files with 139 additions and 1 deletions

View file

@ -1,6 +1,7 @@
;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*- ;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
;;Not values but register position in memory ;;Not values but register position in memory
;; F4 - EB
X_end = $EF X_end = $EF
Y_end = $F0 Y_end = $F0
dx = $F3 dx = $F3

View file

@ -0,0 +1,17 @@
;;The triangles 3 corner-positions, they may be in any given order.
A_X = $EA
A_Y = $E9
B_X = $E8
B_Y = $E7
C_X = $E6
C_Y = $E5
;; Differense in height (after these points have been sorted look in triangle.s for more detail)
AB = $E4
BC = $E3
;; These come from mem.inc
;; Takes up FF - F5
X_pos = $FC
Y_pos = $FB
byte_to_paint = $FE ; Byte with one 1 that corisponds to a pixel.
btp_mem_pos = $F9 ; 16-bit value (uses FA), byte to paint memory position

View file

@ -0,0 +1,80 @@
.proc triangle
;; Subroutine for drawing a filld triangle.
;; It takes in values from A_X, A_Y, B_X, B_Y C_X, C_Y, see triangle.inc
.include "triangle.inc"
;; Algorithm
;; (i) For height n it steps through Bresenham's line algorithm to know where the triangle starts and ends at height n.
;; (ii) It draws all pixel in this given range.
;; (iii) It change if the algorithm has hit a corner and need to change line!
;; Implementation
;; In the implementation the triangle split into 2 souch that (iii) can be redused to: check if the triangle has ended.
;; Each side of the sub-triangle can have 4 posiboul states (look at how line is implemented)
;;Lets first sort the edges such as
;; (i) A is at the highest point
;; (ii) B is at the middle point
;; (iii) C is at the lowest point
;; Lets also calculate the height-differense for the new points:
;; * AB for differense betwean A and B
;; * BC for differense betwean B and C
;;AB
SEC
LDA B_Y
SBC A_Y
STA AB
BCC AB_overflow_end
;; A_Y > B_Y
AB_overflow:
EOR #$ff ; Fix byte underflow
;; swop A and B
LDX B_Y
LDY A_Y
STX A_Y
STY B_Y
LDX B_X
LDY A_X
STX A_X
STY B_X
AB_overflow_end:
;;BC
SEC
LDA C_Y
SBC B_Y
STA BC
BCC BC_overflow_end
;; B_Y > C_Y
BC_overflow:
EOR #$ff ; Fix byte underflow
;; swop A and B
LDX C_Y
LDY B_Y
STX B_Y
STY C_Y
LDX C_X
LDY B_X
STX B_X
STY C_X
;; If this is the case A and B may be in the wrong order. Lets redo stuff from the start
JMP triangle
BC_overflow_end:
draw_upper_triangle:
# This triangle has 10 posibol cases, see table bellow
# ----------------------------------------------------
# | name left_side right_side |
# ----------------------------------------------------
# | RR right-sloaping-steep right-sloaping-steep|
# | rR right-sloaping right-sloaping-steep|
# | lR left-sloaping right-sloaping-steep|
# | LR left-sloaping-steep right-sloaping-steep|
# | rr right-sloaping right-sloaping |
# | lr left-sloaping right-sloaping |
# | Lr left-sloaping-steep right-sloaping |
# | ll left-sloaping left-sloaping |
# | Ll left-sloaping-sleep left-sloaping |
# | LL left-sloaping-sleep left-sloaping-sleep |
# ----------------------------------------------------
RTS
.endproc

View file

@ -0,0 +1,38 @@
.scope triangle_test
;; Test program that tries to draw a triangle
.include "triangle.inc"
LDA #$33
STA A_X
STA A_Y
LDA #$55
STA C_X
STA C_Y
LDA #$77
STA B_X
LDA #$20
STA B_Y
; jmp skipp_edges ; Skip drawing the edges
;;Draw the edges (only for reference)
edges:
X_end = $EF
Y_end = $F0
;X_pos = $FC
;Y_pos = $FB
;;; A -- B
Mov_16 X_pos, Y_pos, A_X, A_Y
Mov_16 X_end, Y_end, B_X, B_Y
jsr line
;;; A -- C
Mov_16 X_pos, Y_pos, A_X, A_Y
Mov_16 X_end, Y_end, C_X, C_Y
jsr line
;;; B -- C
Mov_16 X_pos, Y_pos, C_X, C_Y
Mov_16 X_end, Y_end, B_X, B_Y
jsr line
skip_edges:
;; lets draw the example triangle
jsr triangle
jmp exit
.endscope

View file

@ -6,11 +6,13 @@
.include "routines/memory/mem.inc" .include "routines/memory/mem.inc"
;;Code to run ;;Code to run
.include "STARTUP.s" .include "STARTUP.s"
.include "routines/line/line_test.s" ;.include "routines/line/line_test.s"
;.include "routines/memory/memcpy_test.s" ;.include "routines/memory/memcpy_test.s"
.include "routines/triangle/triangle_test.s"
exit: exit:
jmp exit jmp exit
.include "routines/line/line.s" .include "routines/line/line.s"
.include "routines/triangle/triangle.s"
.include "routines/memory/pixel_draw.s" .include "routines/memory/pixel_draw.s"
.include "routines/memory/memset.s" .include "routines/memory/memset.s"
.include "routines/memory/memcpy.s" .include "routines/memory/memcpy.s"