From fcec127d2d93c9413e3f6a5317550ebc0d06663f Mon Sep 17 00:00:00 2001 From: Erik Charlebois Date: Sat, 27 Mar 2010 14:31:24 -0700 Subject: [PATCH] Debug rendering vocabulary --- extra/game/debug/authors.txt | 1 + extra/game/debug/debug.factor | 212 ++++++++++++++++++++++++++++ extra/game/debug/summary.txt | 1 + extra/game/debug/tags.txt | 1 + extra/game/debug/tests/tests.factor | 68 +++++++++ 5 files changed, 283 insertions(+) create mode 100644 extra/game/debug/authors.txt create mode 100644 extra/game/debug/debug.factor create mode 100644 extra/game/debug/summary.txt create mode 100644 extra/game/debug/tags.txt create mode 100644 extra/game/debug/tests/tests.factor diff --git a/extra/game/debug/authors.txt b/extra/game/debug/authors.txt new file mode 100644 index 0000000000..6f03a12101 --- /dev/null +++ b/extra/game/debug/authors.txt @@ -0,0 +1 @@ +Erik Charlebois diff --git a/extra/game/debug/debug.factor b/extra/game/debug/debug.factor new file mode 100644 index 0000000000..a4f4895812 --- /dev/null +++ b/extra/game/debug/debug.factor @@ -0,0 +1,212 @@ +! Copyright (C) 2010 Erik Charlebois +! See http://factorcode.org/license.txt for BSD license. +USING: accessors alien.c-types arrays circular colors colors.constants +columns destructors fonts gpu.buffers gpu.render gpu.shaders gpu.state +gpu.textures images kernel literals locals make math math.constants +math.functions math.vectors sequences specialized-arrays typed ui.text fry ; +FROM: alien.c-types => float ; +SPECIALIZED-ARRAYS: float uint ; +IN: game.debug + +image ( string color -- image ) + debug-text-font clone swap >>foreground swap string>image drop ; + +:: image>texture ( image -- texture ) + image [ component-order>> ] [ component-type>> ] bi + debug-text-texture-parameters &dispose + [ 0 image allocate-texture-image ] keep ; + +:: screen-quad ( image pt dim -- float-array ) + pt dim v/ 2.0 v*n 1.0 v-n + dup image dim>> dim v/ 2.0 v*n v+ + [ first2 ] bi@ :> ( x0 y0 x1 y1 ) + image upside-down?>> + [ { x0 y0 0 0 x1 y0 1 0 x1 y1 1 1 x0 y1 0 1 } ] + [ { x0 y0 0 1 x1 y0 1 1 x1 y1 1 0 x0 y1 0 0 } ] + if >float-array ; + +: debug-text-uniform-variables ( string color -- image uniforms ) + text>image dup image>texture + float-array{ 0.0 0.0 0.0 } + debug-text-uniforms boa swap ; + +: debug-text-vertex-array ( image pt dim -- vertex-array ) + screen-quad stream-upload draw-usage vertex-buffer byte-array>buffer &dispose + debug-text-program &dispose ; + +: debug-text-index-buffer ( -- index-buffer ) + uint-array{ 0 1 2 2 3 0 } stream-upload draw-usage index-buffer + byte-array>buffer &dispose 0 6 uint-indexes ; + +: debug-text-render ( uniforms vertex-array index-buffer -- ) + [ + { + { "primitive-mode" [ 3drop triangles-mode ] } + { "uniforms" [ 2drop ] } + { "vertex-array" [ drop nip ] } + { "indexes" [ 2nip ] } + } 3 render + ] with-destructors ; + +: debug-shapes-vertex-array ( sequence -- vertex-array ) + stream-upload draw-usage vertex-buffer byte-array>buffer &dispose + debug-shapes-program &dispose &dispose ; + +: draw-debug-primitives ( mode primitives mvp-matrix -- ) + f origin-upper-left 1.0 set-gpu-state + { + { "primitive-mode" [ 2drop ] } + { "uniforms" [ 2nip debug-shapes-uniforms boa ] } + { "vertex-array" [ drop nip debug-shapes-vertex-array ] } + { "indexes" [ drop nip length 0 swap ] } + } 3 render ; + +CONSTANT: box-vertices + { { { 1 1 1 } { 1 1 -1 } } + { { 1 1 1 } { 1 -1 1 } } + { { 1 1 1 } { -1 1 1 } } + { { -1 -1 -1 } { -1 -1 1 } } + { { -1 -1 -1 } { -1 1 -1 } } + { { -1 -1 -1 } { 1 -1 -1 } } + { { -1 -1 1 } { -1 1 1 } } + { { -1 -1 1 } { 1 -1 1 } } + { { -1 1 -1 } { -1 1 1 } } + { { -1 1 -1 } { 1 1 -1 } } + { { 1 -1 -1 } { 1 -1 1 } } + { { 1 -1 -1 } { 1 1 -1 } } } + +CONSTANT: cylinder-vertices + $[ 12 iota [ 2pi 12 / * [ cos ] [ drop 0.0 ] [ sin ] tri 3array ] map ] + +:: scale-cylinder-vertices ( radius half-height verts -- bot-verts top-verts ) + verts + [ [ radius v*n { 0 half-height 0 } v- ] map ] + [ [ radius v*n { 0 half-height 0 } v+ ] map ] bi ; +PRIVATE> + +: debug-point ( pt color -- ) + [ first3 [ , ] tri@ ] + [ [ red>> , ] [ green>> , ] [ blue>> , ] tri ] + bi* ; inline + +: debug-line ( from to color -- ) + dup swapd [ debug-point ] 2bi@ ; inline + +: debug-axes ( pt mat -- ) + [ 0 normalize over v+ COLOR: red debug-line ] + [ 1 normalize over v+ COLOR: green debug-line ] + [ 2 normalize over v+ COLOR: blue debug-line ] + 2tri ; inline + +:: debug-box ( pt half-widths color -- ) + box-vertices [ + first2 [ half-widths v* pt v+ ] bi@ color debug-line + ] each ; inline + +:: debug-circle ( points color -- ) + points dup [ 1 swap change-circular-start ] keep + [ color debug-line ] 2each ; inline + +:: debug-cylinder ( pt half-height radius color -- ) + radius half-height cylinder-vertices scale-cylinder-vertices + [ [ color debug-circle ] bi@ ] + [ color '[ _ debug-line ] 2each ] 2bi ; inline + +TYPED: draw-debug-lines ( lines: float-array mvp-matrix -- ) + [ lines-mode -rot draw-debug-primitives ] with-destructors ; inline + +TYPED: draw-debug-points ( points: float-array mvp-matrix -- ) + [ points-mode -rot draw-debug-primitives ] with-destructors ; inline + +TYPED: draw-text ( string color: rgba pt dim -- ) + [ + [ debug-text-uniform-variables ] 2dip + debug-text-vertex-array + debug-text-index-buffer + debug-text-render + ] with-destructors ; inline diff --git a/extra/game/debug/summary.txt b/extra/game/debug/summary.txt new file mode 100644 index 0000000000..1f772ef24b --- /dev/null +++ b/extra/game/debug/summary.txt @@ -0,0 +1 @@ +Simple shape rendering for visual debugging. diff --git a/extra/game/debug/tags.txt b/extra/game/debug/tags.txt new file mode 100644 index 0000000000..84d4140a70 --- /dev/null +++ b/extra/game/debug/tags.txt @@ -0,0 +1 @@ +games diff --git a/extra/game/debug/tests/tests.factor b/extra/game/debug/tests/tests.factor new file mode 100644 index 0000000000..049aa2b492 --- /dev/null +++ b/extra/game/debug/tests/tests.factor @@ -0,0 +1,68 @@ +! Copyright (C) 2010 Erik Charlebois +! See http://factorcode.org/license.txt for BSD license. +USING: accessors colors.constants game.loop game.worlds gpu +gpu.framebuffers gpu.util.wasd game.debug kernel literals locals +make math math.constants math.matrices math.parser sequences +alien.c-types specialized-arrays ui.gadgets.worlds ui.pixel-formats ; +FROM: alien.c-types => float ; +SPECIALIZED-ARRAY: float +IN: game.debug.tests + +:: clear-screen ( color -- ) + system-framebuffer { + { default-attachment color } + } clear-framebuffer ; + +: deg>rad ( d -- r ) + 180 / pi * ; + +:: draw-debug-tests ( world -- ) + world [ wasd-p-matrix ] [ wasd-mv-matrix ] bi m. :> mvp-matrix + { 0 0 0 } clear-screen + + [ + { 0 0 0 } { 1 0 0 } COLOR: red debug-line + { 0 0 0 } { 0 1 0 } COLOR: green debug-line + { 0 0 0 } { 0 0 1 } COLOR: blue debug-line + { -1.2 0 0 } { 0 1 0 } 0 deg>rad rotation-matrix3 debug-axes + { 3 5 -2 } { 3 2 1 } COLOR: white debug-box + { 0 9 0 } 8 2 COLOR: blue debug-cylinder + ] float-array{ } make + mvp-matrix draw-debug-lines + + [ + { 0 4.0 0 } COLOR: red debug-point + { 0 4.1 0 } COLOR: green debug-point + { 0 4.2 0 } COLOR: blue debug-point + ] float-array{ } make + mvp-matrix draw-debug-points + + "Frame: " world frame-number>> number>string append + COLOR: purple { 5 5 } world dim>> draw-text + world [ 1 + ] change-frame-number drop ; + +TUPLE: tests-world < wasd-world frame-number ; +M: tests-world draw-world* draw-debug-tests ; +M: tests-world wasd-movement-speed drop 1/16. ; +M: tests-world wasd-near-plane drop 1/32. ; +M: tests-world wasd-far-plane drop 1024.0 ; +M: tests-world begin-game-world + init-gpu + 0 >>frame-number + { 0.0 0.0 2.0 } 0 0 set-wasd-view drop ; + +GAME: run-tests { + { world-class tests-world } + { title "game.debug.tests" } + { pixel-format-attributes { + windowed + double-buffered + T{ depth-bits { value 24 } } + } } + { grab-input? t } + { use-game-input? t } + { pref-dim { 1024 768 } } + { tick-interval-micros $[ 60 fps ] } + } ; + +MAIN: run-tests