Working collada viewer, temporary vocab

db4
erikc 2010-01-29 15:48:50 -08:00
parent ef20b40093
commit e44973e2c7
3 changed files with 288 additions and 0 deletions

View File

@ -0,0 +1,7 @@
USING: help.markup help.crossref help.stylesheet help.topics help.syntax
definitions io prettyprint summary arrays math sequences vocabs strings
see ;
IN: collada
ABOUT: "collada"

View File

@ -0,0 +1,148 @@
! Copyright (C) 2010 Erik Charlebois
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs grouping hashtables kernel
locals math math.parser sequences sequences.deep
specialized-arrays.instances.alien.c-types.float
specialized-arrays.instances.alien.c-types.uint splitting xml
xml.data xml.traversal math.order
combinators
images
gpu.shaders
io prettyprint
;
IN: collada
TUPLE: model attribute-buffer index-buffer vertex-format ;
TUPLE: source semantic offset data ;
:: collect-sources ( sources vertices inputs -- sources )
inputs
[| input |
input "source" attr rest vertices first =
[
vertices second [| vertex |
vertex first
input "offset" attr string>number
vertex second rest sources at source boa
] map
]
[
input [ "semantic" attr ]
[ "offset" attr string>number ]
[ "source" attr rest sources at ] tri source boa
] if
] map flatten ;
: string>numbers ( string -- number-seq )
" \t\n" split [ string>number ] map ; inline
: x/ ( x x -- x ) tag-named ; inline
: x@ ( x x -- x ) attr ; inline
: xt ( x -- x ) children>string ; inline
: map-tags-named ( tag string quot -- seq )
[ tags-named ] dip map ; inline
SINGLETONS: x-up y-up z-up ;
GENERIC: up-axis-swizzle! ( from-axis seq -- seq )
M: x-up up-axis-swizzle!
drop dup
[
[ 0 swap nth neg ]
[ 1 swap nth ]
[ 2 swap nth ] tri
swap -rot
] [
[ 2 swap set-nth ]
[ 1 swap set-nth ]
[ 0 swap set-nth ] tri
] bi ;
M: y-up up-axis-swizzle! drop ;
M: z-up up-axis-swizzle!
drop dup
[
[ 0 swap nth ]
[ 1 swap nth ]
[ 2 swap nth neg ] tri
swap
] [
[ 2 swap set-nth ]
[ 1 swap set-nth ]
[ 0 swap set-nth ] tri
] bi ;
: source>array ( source-tag up-axis scale -- array )
rot
[ "float_array" x/ xt string>numbers [ * ] with map ]
[ nip "technique_common" x/ "accessor" x/ "stride" x@ string>number ] 2bi
<groups>
[ swap up-axis-swizzle! ] with map ;
:: collada-mesh>model ( mesh-tag -- models )
mesh-tag "source" [
[ "id" x@ ]
[
[ "float_array" x/ xt string>numbers ]
[ "technique_common" x/ "accessor" x/ "stride" x@ string>number ] bi <groups>
] bi 2array
] map-tags-named >hashtable :> sources
mesh-tag "vertices" tag-named
[ "id" attr ]
[
"input" tags-named [
[ "semantic" attr ] [ "source" attr ] bi 2array
] map
]
bi 2array :> vertices
mesh-tag "triangles" tags-named
[| triangle |
triangle "count" attr string>number :> count
sources vertices triangle "input" tags-named collect-sources :> flattened-sources
triangle "p" tag-named children>string " \t\n" split [ string>number ] map :> indices
flattened-sources [ offset>> ] [ max ] map-reduce :> max-offset
indices dup length count / <groups> [ max-offset 1 + <groups> ] map :> triangles-indices
V{ } clone :> index-buffer
V{ } clone :> attribute-buffer
V{ } clone :> vertex-format
H{ } clone :> inverse-attribute-buffer
triangles-indices [
[
[| triangle-index triangle-offset |
triangle-index triangle-offset flattened-sources
[| index offset source |
source offset>> offset = [
index source data>> nth
] [ f ] if
] with with map sift flatten :> blah
blah inverse-attribute-buffer at [
index-buffer push
] [
attribute-buffer length
[ blah inverse-attribute-buffer set-at ]
[ index-buffer push ] bi
blah attribute-buffer push
] if*
] each-index
] each
] each
attribute-buffer flatten >float-array
index-buffer flatten >uint-array
flattened-sources [
{
[ semantic>> ]
[ drop float-components ]
[ data>> first length ]
[ drop f ]
} cleave vertex-attribute boa
] map
model boa
] map
;

View File

@ -0,0 +1,133 @@
! Copyright (C) 2010 Erik Charlebois
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types arrays classes.struct combinators
combinators.short-circuit game.loop game.worlds gpu gpu.buffers
gpu.util.wasd gpu.framebuffers gpu.render gpu.shaders gpu.state
gpu.textures gpu.util grouping http.client images images.loader
io io.encodings.ascii io.files io.files.temp kernel locals math
math.matrices math.vectors.simd math.parser math.vectors
method-chains namespaces sequences splitting threads ui ui.gadgets
ui.gadgets.worlds ui.pixel-formats specialized-arrays
specialized-vectors literals collada fry xml xml.traversal sequences.deep
opengl.gl
prettyprint ;
FROM: alien.c-types => float ;
SPECIALIZED-ARRAY: float
SPECIALIZED-VECTOR: uint
IN: collada.viewer
GLSL-SHADER: collada-vertex-shader vertex-shader
uniform mat4 mv_matrix, p_matrix;
uniform vec3 light_position;
attribute vec3 POSITION;
void main()
{
vec4 position = mv_matrix * vec4(POSITION, 1.0);
gl_Position = p_matrix * position;
}
;
GLSL-SHADER: collada-fragment-shader fragment-shader
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
;
GLSL-PROGRAM: collada-program
collada-vertex-shader collada-fragment-shader ;
UNIFORM-TUPLE: collada-uniforms < mvp-uniforms
{ "light-position" vec3-uniform f } ;
TUPLE: collada-state
models
vertex-arrays
index-vectors ;
TUPLE: collada-world < wasd-world
{ collada collada-state } ;
VERTEX-FORMAT: collada-vertex
{ "POSITION" float-components 3 f }
{ f float-components 3 f } ;
:: mymax ( x y -- x ) x third y third > [ x ] [ y ] if ;
: <collada-buffers> ( models -- buffers )
! drop
! float-array{ -0.5 0 0 0 0 0 0 1 0 0 0 0 0.5 0 0 0 0 0 }
! uint-array{ 0 1 2 }
! f model boa 1array
[
[ attribute-buffer>> underlying>> static-upload draw-usage vertex-buffer byte-array>buffer ]
[ index-buffer>> underlying>> static-upload draw-usage index-buffer byte-array>buffer ]
[ index-buffer>> length ] tri 3array
] map ;
: fill-collada-state ( collada-state -- )
dup models>> <collada-buffers>
[
[
first collada-program <program-instance> collada-vertex buffer>vertex-array
] map >>vertex-arrays drop
]
[
[
[ second ] [ third ] bi
'[ _ 0 <buffer-ptr> _ uint-indexes <index-elements> ] call
] map >>index-vectors drop
] 2bi ;
: <collada-state> ( -- collada-state )
collada-state new
"C:/Users/erikc/Downloads/mech.dae" file>xml "mesh" deep-tags-named [ collada-mesh>model ] map flatten >>models ;
M: collada-world begin-game-world
init-gpu
{ 0.0 0.0 2.0 } 0 0 set-wasd-view
<collada-state> [ fill-collada-state drop ] [ >>collada drop ] 2bi ;
: <collada-uniforms> ( world -- uniforms )
[ wasd-mv-matrix ] [ wasd-p-matrix ] bi
{ -10000.0 10000.0 10000.0 } ! light position
collada-uniforms boa ;
: draw-collada ( world -- )
GL_COLOR_BUFFER_BIT glClear
triangle-lines dup t <triangle-state> set-gpu-state
[ collada>> vertex-arrays>> ]
[ collada>> index-vectors>> ]
[ <collada-uniforms> ]
tri
[
{
{ "primitive-mode" [ 3drop triangles-mode ] }
{ "uniforms" [ swap drop swap drop ] }
{ "vertex-array" [ drop drop ] }
{ "indexes" [ drop swap drop ] }
} 3<render-set> render
] curry 2each ;
M: collada-world draw-world*
draw-collada ;
M: collada-world wasd-movement-speed drop 1/16. ;
M: collada-world wasd-near-plane drop 1/32. ;
M: collada-world wasd-far-plane drop 1024.0 ;
GAME: collada-game {
{ world-class collada-world }
{ title "Collada Viewer" }
{ pixel-format-attributes {
windowed
double-buffered
} }
{ grab-input? t }
{ use-game-input? t }
{ pref-dim { 1024 768 } }
{ tick-interval-micros $[ 60 fps ] }
} ;