Skip to content

Commit

Permalink
Port handle_input and face_normal to assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
Overv committed Jun 10, 2013
1 parent 13bb3fc commit 2a71812
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 173 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Build flat binary

bin/mineassemble.bin: bin src/link.ld bin/init.o bin/interrupts.o bin/vga.o bin/main.o bin/reference.o bin/textures.o bin/cmath.o bin/splash.o bin/world.o bin/player.o bin/graphics.o bin/globals.o
ld -m elf_i386 -T src/link.ld -o bin/mineassemble.bin bin/init.o bin/interrupts.o bin/vga.o bin/main.o bin/reference.o bin/textures.o bin/cmath.o bin/splash.o bin/world.o bin/player.o bin/graphics.o bin/globals.o
bin/mineassemble.bin: bin src/link.ld bin/init.o bin/interrupts.o bin/vga.o bin/main.o bin/reference.o bin/textures.o bin/cmath.o bin/splash.o bin/world.o bin/player.o bin/input.o bin/graphics.o bin/globals.o
ld -m elf_i386 -T src/link.ld -o bin/mineassemble.bin bin/init.o bin/interrupts.o bin/vga.o bin/main.o bin/reference.o bin/textures.o bin/cmath.o bin/splash.o bin/world.o bin/player.o bin/input.o bin/graphics.o bin/globals.o

bin/reference.o: src/reference.c
gcc -m32 -c -g -o bin/reference.o src/reference.c -std=c99 -ffreestanding -Ofast
Expand Down
29 changes: 28 additions & 1 deletion src/constants.asm
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,32 @@
; Internal constants
;

; Block types
%define BLOCK_AIR 0
%define BLOCK_DIRT 1
%define BLOCK_DIRT 1

; Block faces
%define FACE_LEFT 0
%define FACE_RIGHT 1
%define FACE_BOTTOM 2
%define FACE_TOP 3
%define FACE_BACK 4
%define FACE_FRONT 5

; Key scancodes
%define KEY_ESC 0x01

%define KEY_Q 0x10
%define KEY_E 0x12

%define KEY_W 0x11
%define KEY_A 0x1E
%define KEY_S 0x1F
%define KEY_D 0x20

%define KEY_SPACE 0x39

%define KEY_UP 0x48
%define KEY_LEFT 0x4B
%define KEY_RIGHT 0x4D
%define KEY_DOWN 0x50
4 changes: 2 additions & 2 deletions src/globals.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

global vga

global zero, half_pi, nhalf_pi
global zero, half_pi, neg_half_pi

global hFov

Expand All @@ -23,7 +23,7 @@ section .data
; Helpful constants
zero dd 0.0
half_pi dd 1.57
nhalf_pi dd -1.57
neg_half_pi dd -1.57

; Horizontal field-of-view
hFov dd HOR_FOV
Expand Down
298 changes: 173 additions & 125 deletions src/graphics.asm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

[bits 32]

global ray_dir, face_normal

extern zero
extern hFov
extern yawC, yawS, pitchC, pitchS

Expand All @@ -15,134 +18,179 @@ section .data
height dd 200
halfHeight dd 100
aspect dd 1.6
naspect dd -1.6
neg_aspect dd -1.6
two dd 2.0
half dd 0.5
doubleCircle dd 720.0

section .text

global rayDir

; Takes screen space x and y and returns ray direction
; as (x, y, z) floats
rayDir:
push ebp
mov ebp, esp

; Allocate local variables (vFov, fov, clipX, clipY, length)
sub esp, 20

; Load struct address (x, y, z)
mov eax, dword [ebp + 8]

; Calculate vertical fov and fov constant from horizontal fov

; vFov = 2.0f * atanf(tanf(hFov / 720.0f * M_PI) * 320.0f / 200.0f);
; RPN: 2.0 hFov 720.0 / pi * tan width * height / atan mul
fld dword [two]
fld dword [hFov]
fld dword [doubleCircle]
fdiv
fldpi
fmul
fptan
fstp dword [eax] ; Dump 1 the tan instruction pushes for some reason
fild dword [width]
fmul
fild dword [height]
fdiv
fld1 ; 1 required for atan (takes two parameters like atan2 in C)
fpatan
fmul
fst dword [ebp - 4] ; vFov

; fov = tanf(0.5f * vFov);
; RPN: 0.5 vFov * tan
fld dword [half]
fmul
fptan
fstp dword [eax]
fstp dword [ebp - 8] ; fov

; clip X = x / 160.0f - 1.0f
; RPN: x 160.0 / 1.0 -
fild dword [ebp + 12] ; x parameter
fild dword [halfWidth]
fdiv
fld1
fsub
fstp dword [ebp - 12] ; clipX

; clip Y = 1.0f - y / 100.0f
; RPN: 1.0 y 100.0 / -
fld1
fild dword [ebp + 16] ; y parameter
fild dword [halfHeight]
fdiv
fsub
fstp dword [ebp - 16] ; clipY

; X dir = 1.6f * fov * yawC * clipX + fov * yawS * pitchS * clipY - pitchC * yawS
; RPN: 1.6 fov yawC clipX * * * fov yawS pitchS clipY * * * + pitchC yawS * -
fld dword [aspect]
fld dword [ebp - 8] ; fov
fld dword [yawC]
fld dword [ebp - 12] ; clipX
fmul
fmul
fmul
fld dword [ebp - 8] ; fov
fld dword [yawS]
fld dword [pitchS]
fld dword [ebp - 16] ; clipY
fmul
fmul
fmul
fadd
fld dword [pitchC]
fld dword [yawS]
fmul
fsub
fstp dword [eax] ; X dir

; Y dir = fov * pitchC * clipY + pitchS
; RPN: fov pitchC clipY * * pitchS +
fld dword [ebp - 8] ; fov
fld dword [pitchC]
fld dword [ebp - 16] ; clipY
fmul
fmul
fld dword [pitchS]
fadd
fstp dword [eax + 4] ; Y dir

; Z dir = -1.6f * fov * yawS * clipX + fov * yawC * pitchS * clipY - pitchC * yawC
; RPN: naspect fov yawS clipX * * * fov yawC pitchS clipY * * * + pitchC yawC * -
fld dword [naspect]
fld dword [ebp - 8] ; fov
fld dword [yawS]
fld dword [ebp - 12] ; clipX
fmul
fmul
fmul
fld dword [ebp - 8] ; fov
fld dword [yawC]
fld dword [pitchS]
fld dword [ebp - 16] ; clipY
fmul
fmul
fmul
fadd
fld dword [pitchC]
fld dword [yawC]
fmul
fsub
fstp dword [eax + 8] ; Z dir

; Resulting direction is not normalized, but that doesn't matter
; for the raytracing algorithm

mov esp, ebp
pop ebp
ret 4
; Takes screen space x and y and returns ray direction
; as (x, y, z) floats
ray_dir:
push ebp
mov ebp, esp

; Allocate local variables (vFov, fov, clipX, clipY, length)
sub esp, 20

; Load struct address (x, y, z)
mov eax, dword [ebp + 8]

; Calculate vertical fov and fov constant from horizontal fov

; vFov = 2.0f * atanf(tanf(hFov / 720.0f * M_PI) * 320.0f / 200.0f);
; RPN: 2.0 hFov 720.0 / pi * tan width * height / atan mul
fld dword [two]
fld dword [hFov]
fld dword [doubleCircle]
fdiv
fldpi
fmul
fptan
fstp dword [eax] ; Dump 1 the tan instruction pushes for some reason
fild dword [width]
fmul
fild dword [height]
fdiv
fld1 ; 1 required for atan (takes two parameters like atan2 in C)
fpatan
fmul
fst dword [ebp - 4] ; vFov

; fov = tanf(0.5f * vFov);
; RPN: 0.5 vFov * tan
fld dword [half]
fmul
fptan
fstp dword [eax]
fstp dword [ebp - 8] ; fov

; clip X = x / 160.0f - 1.0f
; RPN: x 160.0 / 1.0 -
fild dword [ebp + 12] ; x parameter
fild dword [halfWidth]
fdiv
fld1
fsub
fstp dword [ebp - 12] ; clipX

; clip Y = 1.0f - y / 100.0f
; RPN: 1.0 y 100.0 / -
fld1
fild dword [ebp + 16] ; y parameter
fild dword [halfHeight]
fdiv
fsub
fstp dword [ebp - 16] ; clipY

; X dir = 1.6f * fov * yawC * clipX + fov * yawS * pitchS * clipY - pitchC * yawS
; RPN: 1.6 fov yawC clipX * * * fov yawS pitchS clipY * * * + pitchC yawS * -
fld dword [aspect]
fld dword [ebp - 8] ; fov
fld dword [yawC]
fld dword [ebp - 12] ; clipX
fmul
fmul
fmul
fld dword [ebp - 8] ; fov
fld dword [yawS]
fld dword [pitchS]
fld dword [ebp - 16] ; clipY
fmul
fmul
fmul
fadd
fld dword [pitchC]
fld dword [yawS]
fmul
fsub
fstp dword [eax] ; X dir

; Y dir = fov * pitchC * clipY + pitchS
; RPN: fov pitchC clipY * * pitchS +
fld dword [ebp - 8] ; fov
fld dword [pitchC]
fld dword [ebp - 16] ; clipY
fmul
fmul
fld dword [pitchS]
fadd
fstp dword [eax + 4] ; Y dir

; Z dir = -1.6f * fov * yawS * clipX + fov * yawC * pitchS * clipY - pitchC * yawC
; RPN: neg_aspect fov yawS clipX * * * fov yawC pitchS clipY * * * + pitchC yawC * -
fld dword [neg_aspect]
fld dword [ebp - 8] ; fov
fld dword [yawS]
fld dword [ebp - 12] ; clipX
fmul
fmul
fmul
fld dword [ebp - 8] ; fov
fld dword [yawC]
fld dword [pitchS]
fld dword [ebp - 16] ; clipY
fmul
fmul
fmul
fadd
fld dword [pitchC]
fld dword [yawC]
fmul
fsub
fstp dword [eax + 8] ; Z dir

; Resulting direction is not normalized, but that doesn't matter
; for the raytracing algorithm

mov esp, ebp
pop ebp
ret 4

; Returns normal of given block face (face, *nx, *ny, *nz)
face_normal:
; Initialize nx, ny and nz to zero (no direction)
mov eax, [esp + 8] ; nx
mov dword [eax], 0
mov eax, [esp + 12] ; ny
mov dword [eax], 0
mov eax, [esp + 16] ; nz
mov dword [eax], 0

; Jump to face
mov eax, [esp + 4]
jmp [.face_tbl + eax * 4]

; Face jump table
.face_tbl:
dd .face_left
dd .face_right
dd .face_bottom
dd .face_top
dd .face_back
dd .face_front
.face_left:
mov eax, [esp + 8] ; nx = -1
mov dword [eax], -1
ret
.face_right:
mov eax, [esp + 8] ; nx = 1
mov dword [eax], 1
ret
.face_bottom:
mov eax, [esp + 12] ; ny = -1
mov dword [eax], -1
ret
.face_top
mov eax, [esp + 12] ; ny = 1
mov dword [eax], 1
ret
.face_back:
mov eax, [esp + 16] ; nz = -1
mov dword [eax], -1
ret
.face_front:
mov eax, [esp + 16] ; nz = 1
mov dword [eax], 1
ret
Loading

0 comments on commit 2a71812

Please sign in to comment.