Factored collada implementation a bit more and fixed some bugs
parent
0770b940b0
commit
676673fc7e
|
@ -8,8 +8,9 @@ io io.encodings.ascii io.files io.files.temp kernel locals math
|
||||||
math.matrices math.vectors.simd math.parser math.vectors
|
math.matrices math.vectors.simd math.parser math.vectors
|
||||||
method-chains namespaces sequences splitting threads ui ui.gadgets
|
method-chains namespaces sequences splitting threads ui ui.gadgets
|
||||||
ui.gadgets.worlds ui.pixel-formats specialized-arrays
|
ui.gadgets.worlds ui.pixel-formats specialized-arrays
|
||||||
specialized-vectors literals collada fry xml xml.traversal sequences.deep
|
specialized-vectors literals game.models.collada fry xml xml.traversal sequences.deep
|
||||||
|
|
||||||
|
math.bitwise
|
||||||
opengl.gl
|
opengl.gl
|
||||||
prettyprint ;
|
prettyprint ;
|
||||||
FROM: alien.c-types => float ;
|
FROM: alien.c-types => float ;
|
||||||
|
@ -23,18 +24,26 @@ uniform vec3 light_position;
|
||||||
|
|
||||||
attribute vec3 POSITION;
|
attribute vec3 POSITION;
|
||||||
attribute vec3 NORMAL;
|
attribute vec3 NORMAL;
|
||||||
|
attribute vec2 TEXCOORD;
|
||||||
|
|
||||||
|
varying vec2 texit;
|
||||||
|
varying vec3 norm;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec4 position = mv_matrix * vec4(POSITION, 1.0);
|
vec4 position = mv_matrix * vec4(POSITION, 1.0);
|
||||||
gl_Position = p_matrix * position;
|
gl_Position = p_matrix * position;
|
||||||
|
texit = TEXCOORD;
|
||||||
|
norm = NORMAL;
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
GLSL-SHADER: collada-fragment-shader fragment-shader
|
GLSL-SHADER: collada-fragment-shader fragment-shader
|
||||||
|
varying vec2 texit;
|
||||||
|
varying vec3 norm;
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_FragColor = vec4(1, 1, 0, 1);
|
gl_FragColor = vec4(texit, 0, 1) + vec4(norm, 1);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -78,8 +87,9 @@ TUPLE: collada-world < wasd-world
|
||||||
{ collada collada-state } ;
|
{ collada collada-state } ;
|
||||||
|
|
||||||
VERTEX-FORMAT: collada-vertex
|
VERTEX-FORMAT: collada-vertex
|
||||||
{ "POSITION" float-components 3 f }
|
{ "POSITION" float-components 3 f }
|
||||||
{ "NORMAL" float-components 3 f } ;
|
{ "NORMAL" float-components 3 f }
|
||||||
|
{ "TEXCOORD" float-components 2 f } ;
|
||||||
|
|
||||||
VERTEX-FORMAT: debug-vertex
|
VERTEX-FORMAT: debug-vertex
|
||||||
{ "POSITION" float-components 3 f }
|
{ "POSITION" float-components 3 f }
|
||||||
|
@ -112,8 +122,8 @@ VERTEX-FORMAT: debug-vertex
|
||||||
|
|
||||||
: <collada-state> ( -- collada-state )
|
: <collada-state> ( -- collada-state )
|
||||||
collada-state new
|
collada-state new
|
||||||
#! "C:/Users/erikc/Downloads/mech.dae"
|
"C:/Users/erikc/Downloads/test2.dae"
|
||||||
"/Users/erikc/Documents/mech.dae"
|
#! "/Users/erikc/Documents/mech.dae"
|
||||||
file>xml "mesh" deep-tags-named [ mesh>models ] map flatten >>models ;
|
file>xml "mesh" deep-tags-named [ mesh>models ] map flatten >>models ;
|
||||||
|
|
||||||
M: collada-world begin-game-world
|
M: collada-world begin-game-world
|
||||||
|
@ -152,10 +162,15 @@ M: collada-world begin-game-world
|
||||||
{ 0 0 0 } { 0 0 1 } { 0 0 1 } } draw-lines ;
|
{ 0 0 0 } { 0 0 1 } { 0 0 1 } } draw-lines ;
|
||||||
|
|
||||||
: draw-collada ( world -- )
|
: draw-collada ( world -- )
|
||||||
GL_COLOR_BUFFER_BIT glClear
|
0 0 0 0 glClearColor
|
||||||
|
1 glClearDepth
|
||||||
|
HEX: ffffffff glClearStencil
|
||||||
|
{ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT GL_STENCIL_BUFFER_BIT } flags glClear
|
||||||
|
|
||||||
[
|
[
|
||||||
triangle-lines dup t <triangle-state> set-gpu-state
|
#! triangle-lines dup t <triangle-state> set-gpu-state
|
||||||
|
face-ccw cull-back <triangle-cull-state> set-gpu-state
|
||||||
|
cmp-less <depth-state> set-gpu-state
|
||||||
[ collada>> vertex-arrays>> ]
|
[ collada>> vertex-arrays>> ]
|
||||||
[ collada>> index-vectors>> ]
|
[ collada>> index-vectors>> ]
|
||||||
[ <collada-uniforms> ]
|
[ <collada-uniforms> ]
|
||||||
|
@ -170,6 +185,7 @@ M: collada-world begin-game-world
|
||||||
] curry 2each
|
] curry 2each
|
||||||
]
|
]
|
||||||
[
|
[
|
||||||
|
cmp-always <depth-state> set-gpu-state
|
||||||
draw-axes
|
draw-axes
|
||||||
]
|
]
|
||||||
bi ;
|
bi ;
|
||||||
|
@ -177,7 +193,7 @@ M: collada-world begin-game-world
|
||||||
M: collada-world draw-world*
|
M: collada-world draw-world*
|
||||||
draw-collada ;
|
draw-collada ;
|
||||||
|
|
||||||
M: collada-world wasd-movement-speed drop 1/16. ;
|
M: collada-world wasd-movement-speed drop 1/4. ;
|
||||||
M: collada-world wasd-near-plane drop 1/32. ;
|
M: collada-world wasd-near-plane drop 1/32. ;
|
||||||
M: collada-world wasd-far-plane drop 1024.0 ;
|
M: collada-world wasd-far-plane drop 1024.0 ;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Erik Charlebois
|
|
@ -2,13 +2,13 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: help.markup help.crossref help.stylesheet help.topics help.syntax
|
USING: help.markup help.crossref help.stylesheet help.topics help.syntax
|
||||||
definitions io prettyprint summary arrays math sequences vocabs strings
|
definitions io prettyprint summary arrays math sequences vocabs strings
|
||||||
see xml.data hashtables ;
|
see xml.data hashtables assocs ;
|
||||||
IN: collada
|
IN: game.models.collada
|
||||||
|
|
||||||
ABOUT: "collada"
|
ABOUT: "game.models.collada"
|
||||||
|
|
||||||
ARTICLE: "collada" "Conversion of COLLADA assets"
|
ARTICLE: "game.models.collada" "Conversion of COLLADA assets"
|
||||||
"The " { $vocab-link "collada" } " vocabulary implements words for converting COLLADA assets to data suitable for use with OpenGL. See the COLLADA documentation at " { $url "http://collada.org" } "." ;
|
"The " { $vocab-link "game.models.collada" } " vocabulary implements words for converting COLLADA assets to data suitable for use with OpenGL. See the COLLADA documentation at " { $url "http://collada.org" } "." ;
|
||||||
|
|
||||||
HELP: model
|
HELP: model
|
||||||
{ $class-description "Tuple of a packed attribute buffer, index buffer and vertex format suitable for a single OpenGL draw call." } ;
|
{ $class-description "Tuple of a packed attribute buffer, index buffer and vertex format suitable for a single OpenGL draw call." } ;
|
||||||
|
@ -22,11 +22,12 @@ HELP: up-axis
|
||||||
HELP: unit-ratio
|
HELP: unit-ratio
|
||||||
{ $description "Scaling ratio for the coordinates of the tags being read." } ;
|
{ $description "Scaling ratio for the coordinates of the tags being read." } ;
|
||||||
|
|
||||||
HELP: missing-attr
|
HELP: indexed-seq
|
||||||
{ $description "An error thrown when an attribute is missing from a tag." } ;
|
{ $class-description "A sequence described by a sequence of unique elements and a sequence of indices. The sequence can only be appended to. An associative map is used as a reverse lookup table when appending." } ;
|
||||||
|
|
||||||
HELP: missing-child
|
HELP: <indexed-seq>
|
||||||
{ $description "An error thrown when a child is missing from a tag." } ;
|
{ $values { "dseq-exemplar" sequence } { "iseq-examplar" sequence } { "rassoc-examplar" assoc } }
|
||||||
|
{ $class-description "Construct an " { $link indexed-seq } " using the given examplars for the underlying data structures." } ;
|
||||||
|
|
||||||
HELP: string>numbers ( string -- number-seq )
|
HELP: string>numbers ( string -- number-seq )
|
||||||
{ $values { "string" string } { "number-seq" sequence } }
|
{ $values { "string" string } { "number-seq" sequence } }
|
||||||
|
@ -37,7 +38,7 @@ HELP: y-up { $class-description "Right-handed 3D coordinate system where Y is up
|
||||||
HELP: z-up { $class-description "Right-handed 3D coordinate system where Z is up." } ;
|
HELP: z-up { $class-description "Right-handed 3D coordinate system where Z is up." } ;
|
||||||
|
|
||||||
HELP: >y-up-axis!
|
HELP: >y-up-axis!
|
||||||
{ $values { "sequence" sequence } { "from-axis" rh-up } { "sequence" sequence } }
|
{ $values { "seq" sequence } { "from-axis" rh-up } { "seq" sequence } }
|
||||||
{ $description "Destructively swizzles the first three elements of the input sequence to a right-handed 3D coordinate system where Y is up and returns the modified sequence." } ;
|
{ $description "Destructively swizzles the first three elements of the input sequence to a right-handed 3D coordinate system where Y is up and returns the modified sequence." } ;
|
||||||
|
|
||||||
HELP: source>seq
|
HELP: source>seq
|
||||||
|
@ -57,11 +58,11 @@ HELP: mesh>vertices
|
||||||
{ $description "Convert the mesh tag's vertices element to a pair for further lookup in " { $link collect-sources } ". " } ;
|
{ $description "Convert the mesh tag's vertices element to a pair for further lookup in " { $link collect-sources } ". " } ;
|
||||||
|
|
||||||
HELP: collect-sources
|
HELP: collect-sources
|
||||||
{ $values { "sources" hashtable } { "vertices" pair } { "inputs" tag sequence } { "soures" sequence } }
|
{ $values { "sources" hashtable } { "vertices" pair } { "inputs" tag sequence } { "sources" sequence } }
|
||||||
{ $description "Look up the sources for these " { $emphasis "input" } " elements and return a sequence of " { $link source } " tuples." } ;
|
{ $description "Look up the sources for these " { $emphasis "input" } " elements and return a sequence of " { $link source } " tuples." } ;
|
||||||
|
|
||||||
HELP: group-indices
|
HELP: group-indices
|
||||||
{ $values { "index-stride" number } { "triangle-count" number } { indices "sequence" } { "grouped-indices" sequence } }
|
{ $values { "index-stride" number } { "triangle-count" number } { "indices" sequence } { "grouped-indices" sequence } }
|
||||||
{ $description "Groups the index sequence by triangle and then groups each triangle's indices by vertex." } ;
|
{ $description "Groups the index sequence by triangle and then groups each triangle's indices by vertex." } ;
|
||||||
|
|
||||||
HELP: triangles>numbers
|
HELP: triangles>numbers
|
||||||
|
@ -76,6 +77,10 @@ HELP: <model>
|
||||||
{ $values { "attribute-buffer" sequence } { "index-buffer" sequence } { "sources" sequence } { "model" model } }
|
{ $values { "attribute-buffer" sequence } { "index-buffer" sequence } { "sources" sequence } { "model" model } }
|
||||||
{ $description "Converts the inputs to a form suitable for use with " { $vocab-link "gpu" } " and constructs a " { $link model } "." } ;
|
{ $description "Converts the inputs to a form suitable for use with " { $vocab-link "gpu" } " and constructs a " { $link model } "." } ;
|
||||||
|
|
||||||
|
HELP: pack-attributes
|
||||||
|
{ $values { "source-indices" sequence } { "sources" sequence } { "attributes" sequence } }
|
||||||
|
{ $description "Packs the attributes for a single vertex into a sequence from a set of source data streams." } ;
|
||||||
|
|
||||||
HELP: soa>aos
|
HELP: soa>aos
|
||||||
{ $values { "triangles-indices" sequence } { "sources" sequence } { "attribute-buffer" sequence } { "index-buffer" sequence } }
|
{ $values { "triangles-indices" sequence } { "sources" sequence } { "attribute-buffer" sequence } { "index-buffer" sequence } }
|
||||||
{ $description "Swizzles the input sources from a structure of arrays form to an array of structures form and generates a new index buffer." } ;
|
{ $description "Swizzles the input sources from a structure of arrays form to an array of structures form and generates a new index buffer." } ;
|
||||||
|
@ -85,7 +90,7 @@ HELP: triangles>model
|
||||||
{ $description "Creates a " { $link model } " tuple from the given triangles tag, source set and vertices pair." } ;
|
{ $description "Creates a " { $link model } " tuple from the given triangles tag, source set and vertices pair." } ;
|
||||||
|
|
||||||
HELP: mesh>triangles
|
HELP: mesh>triangles
|
||||||
{ $values { "souces" sequence } { "vertices" pair } { "mesh-tag" tag } { "models" sequence } }
|
{ $values { "sources" sequence } { "vertices" pair } { "mesh-tag" tag } { "models" sequence } }
|
||||||
{ $description "Creates a sequence of models from the triangles in the mesh tag." } ;
|
{ $description "Creates a sequence of models from the triangles in the mesh tag." } ;
|
||||||
|
|
||||||
HELP: mesh>models
|
HELP: mesh>models
|
|
@ -5,8 +5,8 @@ locals math math.parser sequences sequences.deep
|
||||||
specialized-arrays.instances.alien.c-types.float
|
specialized-arrays.instances.alien.c-types.float
|
||||||
specialized-arrays.instances.alien.c-types.uint splitting xml
|
specialized-arrays.instances.alien.c-types.uint splitting xml
|
||||||
xml.data xml.traversal math.order
|
xml.data xml.traversal math.order
|
||||||
namespaces combinators images gpu.shaders io ;
|
namespaces combinators images gpu.shaders io make ;
|
||||||
IN: collada
|
IN: game.models.collada
|
||||||
|
|
||||||
TUPLE: model attribute-buffer index-buffer vertex-format ;
|
TUPLE: model attribute-buffer index-buffer vertex-format ;
|
||||||
TUPLE: source semantic offset data ;
|
TUPLE: source semantic offset data ;
|
||||||
|
@ -16,8 +16,48 @@ SYMBOLS: up-axis unit-ratio ;
|
||||||
ERROR: missing-attr tag attr ;
|
ERROR: missing-attr tag attr ;
|
||||||
ERROR: missing-child tag child-name ;
|
ERROR: missing-child tag child-name ;
|
||||||
|
|
||||||
|
TUPLE: indexed-seq dseq iseq rassoc ;
|
||||||
|
INSTANCE: indexed-seq sequence
|
||||||
|
|
||||||
|
M: indexed-seq length
|
||||||
|
iseq>> length ; inline
|
||||||
|
|
||||||
|
M: indexed-seq nth
|
||||||
|
[ iseq>> nth ] keep dseq>> nth ; inline
|
||||||
|
|
||||||
|
M:: indexed-seq set-nth ( elt n seq -- )
|
||||||
|
seq dseq>> :> dseq
|
||||||
|
seq iseq>> :> iseq
|
||||||
|
seq rassoc>> :> rassoc
|
||||||
|
seq length n = not [ seq immutable ] when
|
||||||
|
elt rassoc at
|
||||||
|
[
|
||||||
|
iseq push
|
||||||
|
]
|
||||||
|
[
|
||||||
|
dseq length
|
||||||
|
[ elt rassoc set-at ]
|
||||||
|
[ iseq push ] bi
|
||||||
|
elt dseq push
|
||||||
|
] if* ; inline
|
||||||
|
|
||||||
|
: <indexed-seq> ( dseq-examplar iseq-exampler rassoc-examplar -- indexed-seq )
|
||||||
|
indexed-seq new
|
||||||
|
swap clone >>rassoc
|
||||||
|
swap clone >>iseq
|
||||||
|
swap clone >>dseq ;
|
||||||
|
|
||||||
|
M: indexed-seq new-resizable
|
||||||
|
[ dseq>> ] [ iseq>> ] [ rassoc>> ] tri <indexed-seq>
|
||||||
|
dup -rot
|
||||||
|
[ [ dseq>> new-resizable ] keep (>>dseq) ]
|
||||||
|
[ [ iseq>> new-resizable ] keep (>>iseq) ]
|
||||||
|
[ [ rassoc>> clone nip ] keep (>>rassoc) ]
|
||||||
|
2tri ;
|
||||||
|
|
||||||
|
|
||||||
: string>numbers ( string -- number-seq )
|
: string>numbers ( string -- number-seq )
|
||||||
" \t\n" split [ string>number ] map ;
|
" \t\n" split [ "" = ] trim [ string>number ] map ;
|
||||||
|
|
||||||
: x/ ( tag child-name -- child-tag )
|
: x/ ( tag child-name -- child-tag )
|
||||||
[ tag-named ]
|
[ tag-named ]
|
||||||
|
@ -69,8 +109,8 @@ M: z-up >y-up-axis!
|
||||||
rot
|
rot
|
||||||
[ "float_array" x/ xt string>numbers [ * ] with map ]
|
[ "float_array" x/ xt string>numbers [ * ] with map ]
|
||||||
[ nip "technique_common" x/ "accessor" x/ "stride" x@ string>number ] 2bi
|
[ nip "technique_common" x/ "accessor" x/ "stride" x@ string>number ] 2bi
|
||||||
<groups>
|
group
|
||||||
[ swap >y-up-axis! ] with map ;
|
[ swap over length 2 > [ >y-up-axis! ] [ drop ] if ] with map ;
|
||||||
|
|
||||||
: source>pair ( source-tag -- pair )
|
: source>pair ( source-tag -- pair )
|
||||||
[ "id" x@ ]
|
[ "id" x@ ]
|
||||||
|
@ -108,7 +148,7 @@ M: z-up >y-up-axis!
|
||||||
] map flatten ;
|
] map flatten ;
|
||||||
|
|
||||||
: group-indices ( index-stride triangle-count indices -- grouped-indices )
|
: group-indices ( index-stride triangle-count indices -- grouped-indices )
|
||||||
dup length rot / <groups> swap [ <groups> ] curry map ;
|
dup length rot / group swap [ group ] curry map ;
|
||||||
|
|
||||||
: triangles>numbers ( triangles-tag -- number-seq )
|
: triangles>numbers ( triangles-tag -- number-seq )
|
||||||
"p" x/ children>string " \t\n" split [ string>number ] map ;
|
"p" x/ children>string " \t\n" split [ string>number ] map ;
|
||||||
|
@ -130,32 +170,21 @@ M: z-up >y-up-axis!
|
||||||
] map
|
] map
|
||||||
] tri* model boa ;
|
] tri* model boa ;
|
||||||
|
|
||||||
:: soa>aos ( triangles-indices sources -- attribute-buffer index-buffer )
|
: pack-attributes ( source-indices sources -- attributes )
|
||||||
V{ } clone :> attribute-buffer
|
[
|
||||||
V{ } clone :> index-buffer
|
|
||||||
H{ } clone :> inverse-attribute-buffer
|
|
||||||
triangles-indices [
|
|
||||||
[
|
[
|
||||||
[| triangle-index triangle-offset |
|
[
|
||||||
triangle-index triangle-offset sources
|
[ data>> ] [ offset>> ] bi
|
||||||
[| index offset source |
|
rot = [ nth ] [ 2drop f ] if
|
||||||
source offset>> offset = [
|
] with with map sift flatten ,
|
||||||
index source data>> nth
|
] curry each-index
|
||||||
] [ f ] if
|
] V{ } make flatten ;
|
||||||
] with with map sift flatten :> attributes
|
|
||||||
|
:: soa>aos ( triangles-indices sources -- attribute-buffer index-buffer )
|
||||||
attributes inverse-attribute-buffer at [
|
[ triangles-indices [ [
|
||||||
index-buffer push
|
sources pack-attributes ,
|
||||||
] [
|
] each ] each ]
|
||||||
attribute-buffer length
|
V{ } V{ } H{ } <indexed-seq> make [ dseq>> ] [ iseq>> ] bi ;
|
||||||
[ attributes inverse-attribute-buffer set-at ]
|
|
||||||
[ index-buffer push ] bi
|
|
||||||
attributes attribute-buffer push
|
|
||||||
] if*
|
|
||||||
] each-index
|
|
||||||
] each
|
|
||||||
] each
|
|
||||||
attribute-buffer index-buffer ;
|
|
||||||
|
|
||||||
: triangles>model ( sources vertices triangles-tag -- model )
|
: triangles>model ( sources vertices triangles-tag -- model )
|
||||||
[ "input" tags-named collect-sources ] keep swap
|
[ "input" tags-named collect-sources ] keep swap
|
||||||
|
@ -174,7 +203,7 @@ M: z-up >y-up-axis!
|
||||||
|
|
||||||
: mesh>models ( mesh-tag -- models )
|
: mesh>models ( mesh-tag -- models )
|
||||||
[
|
[
|
||||||
{ { up-axis z-up } { unit-ratio 0.5 } } [
|
{ { up-axis y-up } { unit-ratio 0.5 } } [
|
||||||
mesh>sources
|
mesh>sources
|
||||||
] bind
|
] bind
|
||||||
]
|
]
|
|
@ -0,0 +1 @@
|
||||||
|
Conversion of COLLADA geometry assets to OpenGL vertex and index buffers
|
Loading…
Reference in New Issue