2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* collada.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
2017-01-01 22:01:57 +01:00
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
2017-04-08 00:11:42 +02:00
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
# ifdef TOOLS_ENABLED
# include "collada.h"
2017-01-16 08:04:19 +01:00
2017-08-27 21:07:15 +02:00
# include <stdio.h>
2014-02-10 02:10:30 +01:00
//#define DEBUG_DEFAULT_ANIMATION
//#define DEBUG_COLLADA
# ifdef DEBUG_COLLADA
# define COLLADA_PRINT(m_what) print_line(m_what)
# else
# define COLLADA_PRINT(m_what)
# endif
# define COLLADA_IMPORT_SCALE_SCENE
/* HELPERS */
2017-03-05 16:44:50 +01:00
String Collada : : Effect : : get_texture_path ( const String & p_source , Collada & state ) const {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String image = p_source ;
ERR_FAIL_COND_V ( ! state . state . image_map . has ( image ) , " " ) ;
2014-02-10 02:10:30 +01:00
return state . state . image_map [ image ] . path ;
}
Transform Collada : : get_root_transform ( ) const {
Transform unit_scale_transform ;
# ifndef COLLADA_IMPORT_SCALE_SCENE
2017-03-05 16:44:50 +01:00
unit_scale_transform . scale ( Vector3 ( state . unit_scale , state . unit_scale , state . unit_scale ) ) ;
2014-02-10 02:10:30 +01:00
# endif
return unit_scale_transform ;
}
void Collada : : Vertex : : fix_unit_scale ( Collada & state ) {
# ifdef COLLADA_IMPORT_SCALE_SCENE
2017-03-05 16:44:50 +01:00
vertex * = state . state . unit_scale ;
2014-02-10 02:10:30 +01:00
# endif
}
2017-03-05 16:44:50 +01:00
static String _uri_to_id ( const String & p_uri ) {
2014-02-10 02:10:30 +01:00
if ( p_uri . begins_with ( " # " ) )
2017-03-05 16:44:50 +01:00
return p_uri . substr ( 1 , p_uri . size ( ) - 1 ) ;
2014-02-10 02:10:30 +01:00
else
return p_uri ;
}
/** HELPER FUNCTIONS **/
2017-03-05 16:44:50 +01:00
Transform Collada : : fix_transform ( const Transform & p_transform ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Transform tr = p_transform ;
2014-02-10 02:10:30 +01:00
# ifndef NO_UP_AXIS_SWAP
2017-03-05 16:44:50 +01:00
if ( state . up_axis ! = Vector3 : : AXIS_Y ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < 3 ; i + + )
SWAP ( tr . basis [ 1 ] [ i ] , tr . basis [ state . up_axis ] [ i ] ) ;
for ( int i = 0 ; i < 3 ; i + + )
SWAP ( tr . basis [ i ] [ 1 ] , tr . basis [ i ] [ state . up_axis ] ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
SWAP ( tr . origin [ 1 ] , tr . origin [ state . up_axis ] ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
tr . basis [ state . up_axis ] [ 0 ] = - tr . basis [ state . up_axis ] [ 0 ] ;
tr . basis [ state . up_axis ] [ 1 ] = - tr . basis [ state . up_axis ] [ 1 ] ;
tr . basis [ 0 ] [ state . up_axis ] = - tr . basis [ 0 ] [ state . up_axis ] ;
tr . basis [ 1 ] [ state . up_axis ] = - tr . basis [ 1 ] [ state . up_axis ] ;
tr . origin [ state . up_axis ] = - tr . origin [ state . up_axis ] ;
2014-02-10 02:10:30 +01:00
}
# endif
2017-01-14 12:26:56 +01:00
//tr.scale(Vector3(state.unit_scale.unit_scale.unit_scale));
2014-02-10 02:10:30 +01:00
return tr ;
//return state.matrix_fix * p_transform;
}
2017-03-05 16:44:50 +01:00
static Transform _read_transform_from_array ( const Vector < float > & array , int ofs = 0 ) {
2014-02-10 02:10:30 +01:00
Transform tr ;
// i wonder why collada matrices are transposed, given that's opposed to opengl..
2017-03-05 16:44:50 +01:00
tr . basis . elements [ 0 ] [ 0 ] = array [ 0 + ofs ] ;
tr . basis . elements [ 0 ] [ 1 ] = array [ 1 + ofs ] ;
tr . basis . elements [ 0 ] [ 2 ] = array [ 2 + ofs ] ;
tr . basis . elements [ 1 ] [ 0 ] = array [ 4 + ofs ] ;
tr . basis . elements [ 1 ] [ 1 ] = array [ 5 + ofs ] ;
tr . basis . elements [ 1 ] [ 2 ] = array [ 6 + ofs ] ;
tr . basis . elements [ 2 ] [ 0 ] = array [ 8 + ofs ] ;
tr . basis . elements [ 2 ] [ 1 ] = array [ 9 + ofs ] ;
tr . basis . elements [ 2 ] [ 2 ] = array [ 10 + ofs ] ;
tr . origin . x = array [ 3 + ofs ] ;
tr . origin . y = array [ 7 + ofs ] ;
tr . origin . z = array [ 11 + ofs ] ;
2014-02-10 02:10:30 +01:00
return tr ;
}
/* STRUCTURES */
Transform Collada : : Node : : compute_transform ( Collada & state ) const {
Transform xform ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < xform_list . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
Transform xform_step ;
2017-03-05 16:44:50 +01:00
const XForm & xf = xform_list [ i ] ;
switch ( xf . op ) {
2014-02-10 02:10:30 +01:00
case XForm : : OP_ROTATE : {
2017-03-05 16:44:50 +01:00
if ( xf . data . size ( ) > = 4 ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
xform_step . rotate ( Vector3 ( xf . data [ 0 ] , xf . data [ 1 ] , xf . data [ 2 ] ) , Math : : deg2rad ( xf . data [ 3 ] ) ) ;
2014-02-10 02:10:30 +01:00
}
} break ;
case XForm : : OP_SCALE : {
2017-03-05 16:44:50 +01:00
if ( xf . data . size ( ) > = 3 ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
xform_step . scale ( Vector3 ( xf . data [ 0 ] , xf . data [ 1 ] , xf . data [ 2 ] ) ) ;
2014-02-10 02:10:30 +01:00
}
} break ;
case XForm : : OP_TRANSLATE : {
2017-03-05 16:44:50 +01:00
if ( xf . data . size ( ) > = 3 ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
xform_step . origin = Vector3 ( xf . data [ 0 ] , xf . data [ 1 ] , xf . data [ 2 ] ) ;
2014-02-10 02:10:30 +01:00
}
} break ;
case XForm : : OP_MATRIX : {
2017-03-05 16:44:50 +01:00
if ( xf . data . size ( ) > = 16 ) {
xform_step = _read_transform_from_array ( xf . data , 0 ) ;
2014-02-10 02:10:30 +01:00
}
} break ;
default : { }
}
xform = xform * xform_step ;
}
# ifdef COLLADA_IMPORT_SCALE_SCENE
2017-03-05 16:44:50 +01:00
xform . origin * = state . state . unit_scale ;
2014-02-10 02:10:30 +01:00
# endif
return xform ;
}
Transform Collada : : Node : : get_transform ( ) const {
return default_transform ;
}
Transform Collada : : Node : : get_global_transform ( ) const {
if ( parent )
return parent - > get_global_transform ( ) * default_transform ;
else
return default_transform ;
}
Vector < float > Collada : : AnimationTrack : : get_value_at_time ( float p_time ) {
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( keys . size ( ) = = 0 , Vector < float > ( ) ) ;
int i = 0 ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( i = 0 ; i < keys . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( keys [ i ] . time > p_time )
2014-02-10 02:10:30 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
if ( i = = 0 )
2014-02-10 02:10:30 +01:00
return keys [ 0 ] . data ;
2017-03-05 16:44:50 +01:00
if ( i = = keys . size ( ) )
return keys [ keys . size ( ) - 1 ] . data ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
switch ( keys [ i ] . interp_type ) {
2014-02-10 02:10:30 +01:00
case INTERP_BEZIER : //wait for bezier
case INTERP_LINEAR : {
2017-03-05 16:44:50 +01:00
float c = ( p_time - keys [ i - 1 ] . time ) / ( keys [ i ] . time - keys [ i - 1 ] . time ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( keys [ i ] . data . size ( ) = = 16 ) {
2014-02-10 02:10:30 +01:00
//interpolate a matrix
2017-03-05 16:44:50 +01:00
Transform src = _read_transform_from_array ( keys [ i - 1 ] . data ) ;
2014-02-10 02:10:30 +01:00
Transform dst = _read_transform_from_array ( keys [ i ] . data ) ;
2017-03-05 16:44:50 +01:00
Transform interp = c < 0.001 ? src : src . interpolate_with ( dst , c ) ;
2014-02-10 02:10:30 +01:00
Vector < float > ret ;
ret . resize ( 16 ) ;
Transform tr ;
// i wonder why collada matrices are transposed, given that's opposed to opengl..
2017-03-05 16:44:50 +01:00
ret [ 0 ] = interp . basis . elements [ 0 ] [ 0 ] ;
ret [ 1 ] = interp . basis . elements [ 0 ] [ 1 ] ;
ret [ 2 ] = interp . basis . elements [ 0 ] [ 2 ] ;
ret [ 4 ] = interp . basis . elements [ 1 ] [ 0 ] ;
ret [ 5 ] = interp . basis . elements [ 1 ] [ 1 ] ;
ret [ 6 ] = interp . basis . elements [ 1 ] [ 2 ] ;
ret [ 8 ] = interp . basis . elements [ 2 ] [ 0 ] ;
ret [ 9 ] = interp . basis . elements [ 2 ] [ 1 ] ;
ret [ 10 ] = interp . basis . elements [ 2 ] [ 2 ] ;
ret [ 3 ] = interp . origin . x ;
ret [ 7 ] = interp . origin . y ;
ret [ 11 ] = interp . origin . z ;
ret [ 12 ] = 0 ;
ret [ 13 ] = 0 ;
ret [ 14 ] = 0 ;
ret [ 15 ] = 1 ;
2014-02-10 02:10:30 +01:00
return ret ;
} else {
Vector < float > dest ;
dest . resize ( keys [ i ] . data . size ( ) ) ;
2017-03-05 16:44:50 +01:00
for ( int j = 0 ; j < dest . size ( ) ; j + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
dest [ j ] = keys [ i ] . data [ j ] * c + keys [ i - 1 ] . data [ j ] * ( 1.0 - c ) ;
2014-02-10 02:10:30 +01:00
}
return dest ;
//interpolate one by one
}
} break ;
}
ERR_FAIL_V ( Vector < float > ( ) ) ;
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_asset ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
String name = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( name = = " up_axis " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
if ( parser . get_node_data ( ) = = " X_UP " )
state . up_axis = Vector3 : : AXIS_X ;
if ( parser . get_node_data ( ) = = " Y_UP " )
state . up_axis = Vector3 : : AXIS_Y ;
if ( parser . get_node_data ( ) = = " Z_UP " )
state . up_axis = Vector3 : : AXIS_Z ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " up axis: " + parser . get_node_data ( ) ) ;
} else if ( name = = " unit " ) {
2014-02-10 02:10:30 +01:00
state . unit_scale = parser . get_attribute_value ( " meter " ) . to_double ( ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " unit scale: " + rtos ( state . unit_scale ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " asset " )
2014-02-10 02:10:30 +01:00
break ; //end of <asset>
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_image ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_SCENE ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
Image image ;
2017-03-05 16:44:50 +01:00
if ( state . version < State : : Version ( 1 , 4 , 0 ) ) {
2014-02-10 02:10:30 +01:00
/* <1.4 */
String path = parser . get_attribute_value ( " source " ) . strip_edges ( ) ;
2017-03-05 16:44:50 +01:00
if ( path . find ( " :// " ) = = - 1 & & path . is_rel_path ( ) ) {
2014-02-10 02:10:30 +01:00
// path is relative to file being loaded, so convert to a resource path
2017-07-19 22:00:46 +02:00
image . path = ProjectSettings : : get_singleton ( ) - > localize_path ( state . local_path . get_base_dir ( ) + " / " + path . percent_decode ( ) ) ;
2014-02-10 02:10:30 +01:00
}
} else {
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
String name = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( name = = " init_from " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
2014-09-15 16:33:30 +02:00
String path = parser . get_node_data ( ) . strip_edges ( ) . percent_decode ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( path . find ( " :// " ) = = - 1 & & path . is_rel_path ( ) ) {
2014-02-10 02:10:30 +01:00
// path is relative to file being loaded, so convert to a resource path
2017-07-19 22:00:46 +02:00
path = ProjectSettings : : get_singleton ( ) - > localize_path ( state . local_path . get_base_dir ( ) + " / " + path ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( path . find ( " file:/// " ) = = 0 ) {
path = path . replace_first ( " file:/// " , " " ) ;
2017-07-19 22:00:46 +02:00
path = ProjectSettings : : get_singleton ( ) - > localize_path ( path ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
image . path = path ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( name = = " data " ) {
2014-02-10 02:10:30 +01:00
ERR_PRINT ( " COLLADA Embedded image data not supported! " ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " extra " & & ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " image " )
2014-02-10 02:10:30 +01:00
break ; //end of <asset>
}
}
2017-03-05 16:44:50 +01:00
state . image_map [ id ] = image ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_material ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_SCENE ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
Material material ;
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
2014-06-28 14:50:20 +02:00
if ( parser . has_attribute ( " name " ) )
2017-03-05 16:44:50 +01:00
material . name = parser . get_attribute_value ( " name " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( state . version < State : : Version ( 1 , 4 , 0 ) ) {
2014-02-10 02:10:30 +01:00
/* <1.4 */
ERR_PRINT ( " Collada Materials < 1.4 are not supported (yet) " ) ;
} else {
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT & & parser . get_node_name ( ) = = " instance_effect " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
material . instance_effect = _uri_to_id ( parser . get_attribute_value ( " url " ) ) ;
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " material " )
2014-02-10 02:10:30 +01:00
break ; //end of <asset>
}
}
2017-03-05 16:44:50 +01:00
state . material_map [ id ] = material ;
2014-02-10 02:10:30 +01:00
}
//! reads floats from inside of xml element until end of xml element
2017-03-05 16:44:50 +01:00
Vector < float > Collada : : _read_float_array ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) )
return Vector < float > ( ) ;
Vector < String > splitters ;
splitters . push_back ( " " ) ;
splitters . push_back ( " \n " ) ;
splitters . push_back ( " \r " ) ;
splitters . push_back ( " \t " ) ;
Vector < float > array ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
// TODO: check for comments inside the element
// and ignore them.
if ( parser . get_node_type ( ) = = XMLParser : : NODE_TEXT ) {
// parse float data
String str = parser . get_node_data ( ) ;
2017-03-05 16:44:50 +01:00
array = str . split_floats_mk ( splitters , false ) ;
2014-02-10 02:10:30 +01:00
//array=str.split_floats(" ",false);
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END )
2014-02-10 02:10:30 +01:00
break ; // end parsing text
}
return array ;
}
2017-03-05 16:44:50 +01:00
Vector < String > Collada : : _read_string_array ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) )
return Vector < String > ( ) ;
Vector < String > array ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
// TODO: check for comments inside the element
// and ignore them.
if ( parser . get_node_type ( ) = = XMLParser : : NODE_TEXT ) {
// parse String data
String str = parser . get_node_data ( ) ;
2017-03-05 16:44:50 +01:00
array = str . split_spaces ( ) ;
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END )
2014-02-10 02:10:30 +01:00
break ; // end parsing text
}
return array ;
}
2017-03-05 16:44:50 +01:00
Transform Collada : : _read_transform ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) )
return Transform ( ) ;
2016-11-29 23:55:12 +01:00
Vector < String > array ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
// TODO: check for comments inside the element
// and ignore them.
if ( parser . get_node_type ( ) = = XMLParser : : NODE_TEXT ) {
// parse float data
String str = parser . get_node_data ( ) ;
2017-03-05 16:44:50 +01:00
array = str . split_spaces ( ) ;
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END )
2014-02-10 02:10:30 +01:00
break ; // end parsing text
}
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( array . size ( ) ! = 16 , Transform ( ) ) ;
2016-11-29 23:55:12 +01:00
Vector < float > farr ;
farr . resize ( 16 ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < 16 ; i + + ) {
farr [ i ] = array [ i ] . to_double ( ) ;
2016-11-29 23:55:12 +01:00
}
return _read_transform_from_array ( farr ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
String Collada : : _read_empty_draw_type ( XMLParser & parser ) {
2016-06-05 20:59:21 +02:00
String empty_draw_type = " " ;
2017-03-05 16:44:50 +01:00
2016-06-05 20:59:21 +02:00
if ( parser . is_empty ( ) )
return empty_draw_type ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2016-06-05 20:59:21 +02:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_TEXT ) {
empty_draw_type = parser . get_node_data ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END )
2016-06-05 20:59:21 +02:00
break ; // end parsing text
}
return empty_draw_type ;
}
2017-03-05 16:44:50 +01:00
Variant Collada : : _parse_param ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) )
return Variant ( ) ;
String from = parser . get_node_name ( ) ;
Variant data ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
if ( parser . get_node_name ( ) = = " float " ) {
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_TEXT ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
data = parser . get_node_data ( ) . to_double ( ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( parser . get_node_name ( ) = = " float2 " ) {
Vector < float > v2 = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
if ( v2 . size ( ) > = 2 ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
data = Vector2 ( v2 [ 0 ] , v2 [ 1 ] ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( parser . get_node_name ( ) = = " float3 " ) {
Vector < float > v3 = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
if ( v3 . size ( ) > = 3 ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
data = Vector3 ( v3 [ 0 ] , v3 [ 1 ] , v3 [ 2 ] ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( parser . get_node_name ( ) = = " float4 " ) {
Vector < float > v4 = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
if ( v4 . size ( ) > = 4 ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
data = Color ( v4 [ 0 ] , v4 [ 1 ] , v4 [ 2 ] , v4 [ 3 ] ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( parser . get_node_name ( ) = = " sampler2D " ) {
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " source " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
if ( parser . get_node_type ( ) = = XMLParser : : NODE_TEXT ) {
2017-03-05 16:44:50 +01:00
data = parser . get_node_data ( ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " sampler2D " )
2014-02-10 02:10:30 +01:00
break ;
}
} else if ( parser . get_node_name ( ) = = " surface " ) {
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " init_from " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
if ( parser . get_node_type ( ) = = XMLParser : : NODE_TEXT ) {
2017-03-05 16:44:50 +01:00
data = parser . get_node_data ( ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " surface " )
2014-02-10 02:10:30 +01:00
break ;
}
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = from )
break ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " newparam ending " + parser . get_node_name ( ) ) ;
2014-02-10 02:10:30 +01:00
return data ;
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_effect_material ( XMLParser & parser , Effect & effect , String & id ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_SCENE ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
// first come the tags we descend, but ignore the top-levels
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " node name: " + parser . get_node_name ( ) ) ;
2014-02-10 02:10:30 +01:00
if ( ! parser . is_empty ( ) & & ( parser . get_node_name ( ) = = " profile_COMMON " | | parser . get_node_name ( ) = = " technique " | | parser . get_node_name ( ) = = " extra " ) ) {
2017-03-05 16:44:50 +01:00
_parse_effect_material ( parser , effect , id ) ; // try again
2014-02-10 02:10:30 +01:00
} else if ( parser . get_node_name ( ) = = " newparam " ) {
String name = parser . get_attribute_value ( " sid " ) ;
Variant value = _parse_param ( parser ) ;
2017-03-05 16:44:50 +01:00
effect . params [ name ] = value ;
COLLADA_PRINT ( " param: " + name + " value: " + String ( value ) ) ;
2014-02-10 02:10:30 +01:00
} else if ( parser . get_node_name ( ) = = " constant " | |
2017-03-05 16:44:50 +01:00
parser . get_node_name ( ) = = " lambert " | |
parser . get_node_name ( ) = = " phong " | |
parser . get_node_name ( ) = = " blinn " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " shade model: " + parser . get_node_name ( ) ) ;
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
String what = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( what = = " emission " | |
what = = " diffuse " | |
what = = " specular " | |
what = = " reflective " ) {
2014-02-10 02:10:30 +01:00
// color or texture types
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " color " ) {
2014-02-10 02:10:30 +01:00
Vector < float > colorarr = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " colorarr size: " + rtos ( colorarr . size ( ) ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( colorarr . size ( ) > = 3 ) {
2014-02-10 02:10:30 +01:00
// alpha strangely not allright? maybe it needs to be multiplied by value as a channel intensity
2017-03-05 16:44:50 +01:00
Color color ( colorarr [ 0 ] , colorarr [ 1 ] , colorarr [ 2 ] , 1.0 ) ;
if ( what = = " diffuse " )
effect . diffuse . color = color ;
if ( what = = " specular " )
effect . specular . color = color ;
if ( what = = " emission " )
effect . emission . color = color ;
COLLADA_PRINT ( what + " color: " + color ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_name ( ) = = " texture " ) {
2014-02-10 02:10:30 +01:00
String sampler = parser . get_attribute_value ( " texture " ) ;
if ( ! effect . params . has ( sampler ) ) {
2017-03-05 16:44:50 +01:00
ERR_PRINT ( String ( " Couldn't find sampler: " + sampler + " in material: " + id ) . utf8 ( ) . get_data ( ) ) ;
2014-02-10 02:10:30 +01:00
} else {
String surface = effect . params [ sampler ] ;
if ( ! effect . params . has ( surface ) ) {
2017-03-05 16:44:50 +01:00
ERR_PRINT ( String ( " Couldn't find surface: " + surface + " in material: " + id ) . utf8 ( ) . get_data ( ) ) ;
2014-02-10 02:10:30 +01:00
} else {
String uri = effect . params [ surface ] ;
2017-03-05 16:44:50 +01:00
if ( what = = " diffuse " ) {
effect . diffuse . texture = uri ;
} else if ( what = = " specular " ) {
effect . specular . texture = uri ;
} else if ( what = = " emission " ) {
effect . emission . texture = uri ;
} else if ( what = = " bump " ) {
if ( parser . has_attribute ( " bumptype " ) & & parser . get_attribute_value ( " bumptype " ) ! = " NORMALMAP " ) {
2014-02-10 02:10:30 +01:00
WARN_PRINT ( " 'bump' texture type is not NORMALMAP, only NORMALMAP is supported. " )
}
2017-03-05 16:44:50 +01:00
effect . bump . texture = uri ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( what + " texture: " + uri ) ;
2014-02-10 02:10:30 +01:00
}
}
} else if ( ! parser . is_empty ( ) )
parser . skip_section ( ) ;
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = what )
break ;
}
2017-03-05 16:44:50 +01:00
} else if ( what = = " shininess " ) {
effect . shininess = _parse_param ( parser ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & ( parser . get_node_name ( ) = = " constant " | |
parser . get_node_name ( ) = = " lambert " | |
parser . get_node_name ( ) = = " phong " | |
parser . get_node_name ( ) = = " blinn " ) )
2014-02-10 02:10:30 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_name ( ) = = " double_sided " | | parser . get_node_name ( ) = = " show_double_sided " ) { // colladamax / google earth
2014-02-10 02:10:30 +01:00
// 3DS Max / Google Earth double sided extension
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
effect . found_double_sided = true ;
effect . double_sided = parser . get_node_data ( ) . to_int ( ) ;
COLLADA_PRINT ( " double sided: " + itos ( parser . get_node_data ( ) . to_int ( ) ) ) ;
} else if ( parser . get_node_name ( ) = = " unshaded " ) {
2016-06-07 22:07:48 +02:00
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
effect . unshaded = parser . get_node_data ( ) . to_int ( ) ;
} else if ( parser . get_node_name ( ) = = " bump " ) {
2014-02-10 02:10:30 +01:00
// color or texture types
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " texture " ) {
2014-02-10 02:10:30 +01:00
String sampler = parser . get_attribute_value ( " texture " ) ;
if ( ! effect . params . has ( sampler ) ) {
2017-03-05 16:44:50 +01:00
ERR_PRINT ( String ( " Couldn't find sampler: " + sampler + " in material: " + id ) . utf8 ( ) . get_data ( ) ) ;
2014-02-10 02:10:30 +01:00
} else {
String surface = effect . params [ sampler ] ;
if ( ! effect . params . has ( surface ) ) {
2017-03-05 16:44:50 +01:00
ERR_PRINT ( String ( " Couldn't find surface: " + surface + " in material: " + id ) . utf8 ( ) . get_data ( ) ) ;
2014-02-10 02:10:30 +01:00
} else {
String uri = effect . params [ surface ] ;
2017-03-05 16:44:50 +01:00
if ( parser . has_attribute ( " bumptype " ) & & parser . get_attribute_value ( " bumptype " ) ! = " NORMALMAP " ) {
2014-02-10 02:10:30 +01:00
WARN_PRINT ( " 'bump' texture type is not NORMALMAP, only NORMALMAP is supported. " )
}
2017-03-05 16:44:50 +01:00
effect . bump . texture = uri ;
COLLADA_PRINT ( " bump: " + uri ) ;
2014-02-10 02:10:30 +01:00
}
}
} else if ( ! parser . is_empty ( ) )
parser . skip_section ( ) ;
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " bump " )
break ;
}
} else if ( ! parser . is_empty ( ) )
parser . skip_section ( ) ;
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & &
2017-03-05 16:44:50 +01:00
( parser . get_node_name ( ) = = " effect " | |
parser . get_node_name ( ) = = " profile_COMMON " | |
parser . get_node_name ( ) = = " technique " | |
parser . get_node_name ( ) = = " extra " ) )
2014-02-10 02:10:30 +01:00
break ;
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_effect ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_SCENE ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
Effect effect ;
2014-06-28 14:50:20 +02:00
if ( parser . has_attribute ( " name " ) )
2017-03-05 16:44:50 +01:00
effect . name = parser . get_attribute_value ( " name " ) ;
_parse_effect_material ( parser , effect , id ) ;
2014-06-28 14:50:20 +02:00
2017-03-05 16:44:50 +01:00
state . effect_map [ id ] = effect ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " Effect ID: " + id ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_camera ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_SCENE ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . camera_data_map [ id ] = CameraData ( ) ;
CameraData & camera = state . camera_data_map [ id ] ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
String name = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( name = = " perspective " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
camera . mode = CameraData : : MODE_PERSPECTIVE ;
} else if ( name = = " orthographic " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
camera . mode = CameraData : : MODE_ORTHOGONAL ;
} else if ( name = = " xfov " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
camera . perspective . x_fov = parser . get_node_data ( ) . to_double ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " yfov " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
camera . perspective . y_fov = parser . get_node_data ( ) . to_double ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " xmag " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
camera . orthogonal . x_mag = parser . get_node_data ( ) . to_double ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " ymag " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
camera . orthogonal . y_mag = parser . get_node_data ( ) . to_double ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " aspect_ratio " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
camera . aspect = parser . get_node_data ( ) . to_double ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " znear " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
camera . z_near = parser . get_node_data ( ) . to_double ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " zfar " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
camera . z_far = parser . get_node_data ( ) . to_double ( ) ;
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " camera " )
2014-02-10 02:10:30 +01:00
break ; //end of <asset>
}
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " Camera ID: " + id ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_light ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_SCENE ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . light_data_map [ id ] = LightData ( ) ;
LightData & light = state . light_data_map [ id ] ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
String name = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( name = = " ambient " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
light . mode = LightData : : MODE_AMBIENT ;
} else if ( name = = " directional " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
light . mode = LightData : : MODE_DIRECTIONAL ;
} else if ( name = = " point " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
light . mode = LightData : : MODE_OMNI ;
} else if ( name = = " spot " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
light . mode = LightData : : MODE_SPOT ;
} else if ( name = = " color " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
Vector < float > colorarr = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " colorarr size: " + rtos ( colorarr . size ( ) ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( colorarr . size ( ) > = 4 ) {
2014-02-10 02:10:30 +01:00
// alpha strangely not allright? maybe it needs to be multiplied by value as a channel intensity
2017-03-05 16:44:50 +01:00
Color color ( colorarr [ 0 ] , colorarr [ 1 ] , colorarr [ 2 ] , 1.0 ) ;
light . color = color ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( name = = " constant_attenuation " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
light . constant_att = parser . get_node_data ( ) . to_double ( ) ;
} else if ( name = = " linear_attenuation " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
light . linear_att = parser . get_node_data ( ) . to_double ( ) ;
} else if ( name = = " quadratic_attenuation " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
light . quad_att = parser . get_node_data ( ) . to_double ( ) ;
} else if ( name = = " falloff_angle " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
light . spot_angle = parser . get_node_data ( ) . to_double ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( name = = " falloff_exponent " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
light . spot_exp = parser . get_node_data ( ) . to_double ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " light " )
2014-02-10 02:10:30 +01:00
break ; //end of <asset>
}
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " Light ID: " + id ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_curve_geometry ( XMLParser & parser , String p_id , String p_name ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_SCENE ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
//load everything into a pre dictionary
2017-03-05 16:44:50 +01:00
state . curve_data_map [ p_id ] = CurveData ( ) ;
2014-02-10 02:10:30 +01:00
CurveData & curvedata = state . curve_data_map [ p_id ] ;
2017-03-05 16:44:50 +01:00
curvedata . name = p_name ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " curve name: " + p_name ) ;
2014-02-10 02:10:30 +01:00
String current_source ;
// handles geometry node and the curve childs in this loop
// read sources with arrays and accessor for each curve
if ( parser . is_empty ( ) ) {
return ;
}
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String section = parser . get_node_name ( ) ;
2014-02-10 02:10:30 +01:00
if ( section = = " source " ) {
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
curvedata . sources [ id ] = CurveData : : Source ( ) ;
current_source = id ;
COLLADA_PRINT ( " source data: " + id ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( section = = " float_array " | | section = = " array " ) {
2014-02-10 02:10:30 +01:00
// create a new array and read it.
if ( curvedata . sources . has ( current_source ) ) {
curvedata . sources [ current_source ] . array = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " section: " + current_source + " read " + itos ( curvedata . sources [ current_source ] . array . size ( ) ) + " values. " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( section = = " Name_array " ) {
2014-02-10 02:10:30 +01:00
// create a new array and read it.
if ( curvedata . sources . has ( current_source ) ) {
curvedata . sources [ current_source ] . sarray = _read_string_array ( parser ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " section: " + current_source + " read " + itos ( curvedata . sources [ current_source ] . array . size ( ) ) + " values. " ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( section = = " technique_common " ) {
//skip it
} else if ( section = = " accessor " ) { // child of source (below a technique tag)
if ( curvedata . sources . has ( current_source ) ) {
2017-03-05 16:44:50 +01:00
curvedata . sources [ current_source ] . stride = parser . get_attribute_value ( " stride " ) . to_int ( ) ;
COLLADA_PRINT ( " section: " + current_source + " stride " + itos ( curvedata . sources [ current_source ] . stride ) ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( section = = " control_vertices " ) {
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " input " ) {
2014-02-10 02:10:30 +01:00
String semantic = parser . get_attribute_value ( " semantic " ) ;
2017-03-05 16:44:50 +01:00
String source = _uri_to_id ( parser . get_attribute_value ( " source " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
curvedata . control_vertices [ semantic ] = source ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( section + " input semantic: " + semantic + " source: " + source ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = section )
2014-02-10 02:10:30 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
} else if ( ! parser . is_empty ( ) ) {
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " spline " )
break ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_mesh_geometry ( XMLParser & parser , String p_id , String p_name ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_SCENE ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
//load everything into a pre dictionary
2017-03-05 16:44:50 +01:00
state . mesh_data_map [ p_id ] = MeshData ( ) ;
2014-02-10 02:10:30 +01:00
MeshData & meshdata = state . mesh_data_map [ p_id ] ;
2017-03-05 16:44:50 +01:00
meshdata . name = p_name ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " mesh name: " + p_name ) ;
2014-02-10 02:10:30 +01:00
String current_source ;
// handles geometry node and the mesh childs in this loop
// read sources with arrays and accessor for each mesh
if ( parser . is_empty ( ) ) {
return ;
}
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String section = parser . get_node_name ( ) ;
2014-02-10 02:10:30 +01:00
if ( section = = " source " ) {
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
meshdata . sources [ id ] = MeshData : : Source ( ) ;
current_source = id ;
COLLADA_PRINT ( " source data: " + id ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( section = = " float_array " | | section = = " array " ) {
2014-02-10 02:10:30 +01:00
// create a new array and read it.
if ( meshdata . sources . has ( current_source ) ) {
meshdata . sources [ current_source ] . array = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " section: " + current_source + " read " + itos ( meshdata . sources [ current_source ] . array . size ( ) ) + " values. " ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( section = = " technique_common " ) {
//skip it
} else if ( section = = " accessor " ) { // child of source (below a technique tag)
if ( meshdata . sources . has ( current_source ) ) {
2017-03-05 16:44:50 +01:00
meshdata . sources [ current_source ] . stride = parser . get_attribute_value ( " stride " ) . to_int ( ) ;
COLLADA_PRINT ( " section: " + current_source + " stride " + itos ( meshdata . sources [ current_source ] . stride ) ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( section = = " vertices " ) {
MeshData : : Vertices vert ;
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " input " ) {
2014-02-10 02:10:30 +01:00
String semantic = parser . get_attribute_value ( " semantic " ) ;
2017-03-05 16:44:50 +01:00
String source = _uri_to_id ( parser . get_attribute_value ( " source " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
vert . sources [ semantic ] = source ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( section + " input semantic: " + semantic + " source: " + source ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = section )
2014-02-10 02:10:30 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
meshdata . vertices [ id ] = vert ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( section = = " triangles " | | section = = " polylist " | | section = = " polygons " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
bool polygons = ( section = = " polygons " ) ;
2014-02-10 02:10:30 +01:00
if ( polygons ) {
WARN_PRINT ( " Primitive type \" polygons \" is not well supported (concave shapes may fail). To ensure that the geometry is properly imported, please re-export using \" triangles \" or \" polylist \" . " ) ;
}
MeshData : : Primitives prim ;
if ( parser . has_attribute ( " material " ) )
2017-03-05 16:44:50 +01:00
prim . material = parser . get_attribute_value ( " material " ) ;
prim . count = parser . get_attribute_value ( " count " ) . to_int ( ) ;
prim . vertex_size = 0 ;
int last_ref = 0 ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " input " ) {
2014-02-10 02:10:30 +01:00
String semantic = parser . get_attribute_value ( " semantic " ) ;
2017-03-05 16:44:50 +01:00
String source = _uri_to_id ( parser . get_attribute_value ( " source " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( semantic = = " TEXCOORD " ) {
2014-02-10 02:10:30 +01:00
/*
if ( parser . has_attribute ( " set " ) ) // a texcoord
semantic + = parser . get_attribute_value ( " set " ) ;
else
semantic = " TEXCOORD0 " ; */
2017-03-05 16:44:50 +01:00
semantic = " TEXCOORD " + itos ( last_ref + + ) ;
2014-02-10 02:10:30 +01:00
}
int offset = parser . get_attribute_value ( " offset " ) . to_int ( ) ;
MeshData : : Primitives : : SourceRef sref ;
2017-03-05 16:44:50 +01:00
sref . source = source ;
sref . offset = offset ;
prim . sources [ semantic ] = sref ;
prim . vertex_size = MAX ( prim . vertex_size , offset + 1 ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( section + " input semantic: " + semantic + " source: " + source + " offset: " + itos ( offset ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_name ( ) = = " p " ) { //indices
2014-02-10 02:10:30 +01:00
Vector < float > values = _read_float_array ( parser ) ;
if ( polygons ) {
2017-03-05 16:44:50 +01:00
prim . polygons . push_back ( values . size ( ) / prim . vertex_size ) ;
2014-02-10 02:10:30 +01:00
int from = prim . indices . size ( ) ;
2017-03-05 16:44:50 +01:00
prim . indices . resize ( from + values . size ( ) ) ;
for ( int i = 0 ; i < values . size ( ) ; i + + )
prim . indices [ from + i ] = values [ i ] ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( prim . vertex_size > 0 ) {
prim . indices = values ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " read " + itos ( values . size ( ) ) + " index values " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_name ( ) = = " vcount " ) { // primitive
2014-02-10 02:10:30 +01:00
Vector < float > values = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
prim . polygons = values ;
COLLADA_PRINT ( " read " + itos ( values . size ( ) ) + " polygon values " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = section )
2014-02-10 02:10:30 +01:00
break ;
}
meshdata . primitives . push_back ( prim ) ;
} else if ( parser . get_node_name ( ) = = " double_sided " ) {
parser . read ( ) ;
2017-03-05 16:44:50 +01:00
meshdata . found_double_sided = true ;
meshdata . double_sided = parser . get_node_data ( ) . to_int ( ) ;
2014-02-10 02:10:30 +01:00
} else if ( parser . get_node_name ( ) = = " polygons " ) {
ERR_PRINT ( " Primitive type \" polygons \" not supported, re-export using \" polylist \" or \" triangles \" . " ) ;
2017-03-05 16:44:50 +01:00
} else if ( ! parser . is_empty ( ) ) {
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " mesh " )
break ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_skin_controller ( XMLParser & parser , String p_id ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . skin_controller_data_map [ p_id ] = SkinControllerData ( ) ;
2014-02-10 02:10:30 +01:00
SkinControllerData & skindata = state . skin_controller_data_map [ p_id ] ;
2017-03-05 16:44:50 +01:00
skindata . base = _uri_to_id ( parser . get_attribute_value ( " source " ) ) ;
2014-02-10 02:10:30 +01:00
String current_source ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String section = parser . get_node_name ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( section = = " bind_shape_matrix " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
skindata . bind_shape = _read_transform ( parser ) ;
2014-02-10 02:10:30 +01:00
# ifdef COLLADA_IMPORT_SCALE_SCENE
skindata . bind_shape . origin * = state . unit_scale ;
# endif
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " skeleton bind shape transform: " + skindata . bind_shape ) ;
2014-02-10 02:10:30 +01:00
} else if ( section = = " source " ) {
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
skindata . sources [ id ] = SkinControllerData : : Source ( ) ;
current_source = id ;
COLLADA_PRINT ( " source data: " + id ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( section = = " float_array " | | section = = " array " ) {
2014-02-10 02:10:30 +01:00
// create a new array and read it.
if ( skindata . sources . has ( current_source ) ) {
skindata . sources [ current_source ] . array = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " section: " + current_source + " read " + itos ( skindata . sources [ current_source ] . array . size ( ) ) + " values. " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( section = = " Name_array " | | section = = " IDREF_array " ) {
2014-02-10 02:10:30 +01:00
// create a new array and read it.
2017-03-05 16:44:50 +01:00
if ( section = = " IDREF_array " )
skindata . use_idrefs = true ;
2014-02-10 02:10:30 +01:00
if ( skindata . sources . has ( current_source ) ) {
skindata . sources [ current_source ] . sarray = _read_string_array ( parser ) ;
2017-03-05 16:44:50 +01:00
if ( section = = " IDREF_array " ) {
2014-02-10 02:10:30 +01:00
Vector < String > sa = skindata . sources [ current_source ] . sarray ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < sa . size ( ) ; i + + )
2014-02-10 02:10:30 +01:00
state . idref_joints . insert ( sa [ i ] ) ;
}
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " section: " + current_source + " read " + itos ( skindata . sources [ current_source ] . array . size ( ) ) + " values. " ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( section = = " technique_common " ) {
//skip it
} else if ( section = = " accessor " ) { // child of source (below a technique tag)
if ( skindata . sources . has ( current_source ) ) {
2017-03-05 16:44:50 +01:00
int stride = 1 ;
2014-02-10 02:10:30 +01:00
if ( parser . has_attribute ( " stride " ) )
2017-03-05 16:44:50 +01:00
stride = parser . get_attribute_value ( " stride " ) . to_int ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
skindata . sources [ current_source ] . stride = stride ;
COLLADA_PRINT ( " section: " + current_source + " stride " + itos ( skindata . sources [ current_source ] . stride ) ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( section = = " joints " ) {
SkinControllerData : : Joints joint ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " input " ) {
2014-02-10 02:10:30 +01:00
String semantic = parser . get_attribute_value ( " semantic " ) ;
2017-03-05 16:44:50 +01:00
String source = _uri_to_id ( parser . get_attribute_value ( " source " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
joint . sources [ semantic ] = source ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( section + " input semantic: " + semantic + " source: " + source ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = section )
2014-02-10 02:10:30 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
skindata . joints = joint ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( section = = " vertex_weights " ) {
2014-02-10 02:10:30 +01:00
SkinControllerData : : Weights weights ;
2017-03-05 16:44:50 +01:00
weights . count = parser . get_attribute_value ( " count " ) . to_int ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " input " ) {
2014-02-10 02:10:30 +01:00
String semantic = parser . get_attribute_value ( " semantic " ) ;
2017-03-05 16:44:50 +01:00
String source = _uri_to_id ( parser . get_attribute_value ( " source " ) ) ;
2014-02-10 02:10:30 +01:00
int offset = parser . get_attribute_value ( " offset " ) . to_int ( ) ;
SkinControllerData : : Weights : : SourceRef sref ;
2017-03-05 16:44:50 +01:00
sref . source = source ;
sref . offset = offset ;
weights . sources [ semantic ] = sref ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( section + " input semantic: " + semantic + " source: " + source + " offset: " + itos ( offset ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_name ( ) = = " v " ) { //indices
2014-02-10 02:10:30 +01:00
Vector < float > values = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
weights . indices = values ;
COLLADA_PRINT ( " read " + itos ( values . size ( ) ) + " index values " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_name ( ) = = " vcount " ) { // weightsitive
2014-02-10 02:10:30 +01:00
Vector < float > values = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
weights . sets = values ;
COLLADA_PRINT ( " read " + itos ( values . size ( ) ) + " polygon values " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = section )
2014-02-10 02:10:30 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
skindata . weights = weights ;
2017-01-14 12:26:56 +01:00
}
/*
else if ( ! parser . is_empty ( ) )
parser . skip_section ( ) ;
*/
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " skin " )
break ;
2014-02-10 02:10:30 +01:00
}
/* STORE REST MATRICES */
Vector < Transform > rests ;
ERR_FAIL_COND ( ! skindata . joints . sources . has ( " JOINT " ) ) ;
ERR_FAIL_COND ( ! skindata . joints . sources . has ( " INV_BIND_MATRIX " ) ) ;
String joint_arr = skindata . joints . sources [ " JOINT " ] ;
String ibm = skindata . joints . sources [ " INV_BIND_MATRIX " ] ;
ERR_FAIL_COND ( ! skindata . sources . has ( joint_arr ) ) ;
ERR_FAIL_COND ( ! skindata . sources . has ( ibm ) ) ;
SkinControllerData : : Source & joint_source = skindata . sources [ joint_arr ] ;
SkinControllerData : : Source & ibm_source = skindata . sources [ ibm ] ;
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND ( joint_source . sarray . size ( ) ! = ibm_source . array . size ( ) / 16 ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < joint_source . sarray . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
String name = joint_source . sarray [ i ] ;
2017-03-05 16:44:50 +01:00
Transform xform = _read_transform_from_array ( ibm_source . array , i * 16 ) ; //<- this is a mistake, it must be applied to vertices
2014-02-10 02:10:30 +01:00
xform . affine_invert ( ) ; // inverse for rest, because it's an inverse
# ifdef COLLADA_IMPORT_SCALE_SCENE
2017-03-05 16:44:50 +01:00
xform . origin * = state . unit_scale ;
2014-02-10 02:10:30 +01:00
# endif
2017-03-05 16:44:50 +01:00
skindata . bone_rest_map [ name ] = xform ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_morph_controller ( XMLParser & parser , String p_id ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . morph_controller_data_map [ p_id ] = MorphControllerData ( ) ;
2014-02-10 02:10:30 +01:00
MorphControllerData & morphdata = state . morph_controller_data_map [ p_id ] ;
2017-03-05 16:44:50 +01:00
morphdata . mesh = _uri_to_id ( parser . get_attribute_value ( " source " ) ) ;
morphdata . mode = parser . get_attribute_value ( " method " ) ;
2014-02-10 02:10:30 +01:00
String current_source ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String section = parser . get_node_name ( ) ;
2014-02-10 02:10:30 +01:00
if ( section = = " source " ) {
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
morphdata . sources [ id ] = MorphControllerData : : Source ( ) ;
current_source = id ;
COLLADA_PRINT ( " source data: " + id ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( section = = " float_array " | | section = = " array " ) {
2014-02-10 02:10:30 +01:00
// create a new array and read it.
if ( morphdata . sources . has ( current_source ) ) {
morphdata . sources [ current_source ] . array = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " section: " + current_source + " read " + itos ( morphdata . sources [ current_source ] . array . size ( ) ) + " values. " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( section = = " Name_array " | | section = = " IDREF_array " ) {
2014-02-10 02:10:30 +01:00
// create a new array and read it.
2017-01-14 12:26:56 +01:00
/*
if ( section = = " IDREF_array " )
morphdata . use_idrefs = true ;
*/
2014-02-10 02:10:30 +01:00
if ( morphdata . sources . has ( current_source ) ) {
morphdata . sources [ current_source ] . sarray = _read_string_array ( parser ) ;
/*
if ( section = = " IDREF_array " ) {
Vector < String > sa = morphdata . sources [ current_source ] . sarray ;
for ( int i = 0 ; i < sa . size ( ) ; i + + )
state . idref_joints . insert ( sa [ i ] ) ;
} */
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " section: " + current_source + " read " + itos ( morphdata . sources [ current_source ] . array . size ( ) ) + " values. " ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( section = = " technique_common " ) {
//skip it
} else if ( section = = " accessor " ) { // child of source (below a technique tag)
if ( morphdata . sources . has ( current_source ) ) {
2017-03-05 16:44:50 +01:00
int stride = 1 ;
2014-02-10 02:10:30 +01:00
if ( parser . has_attribute ( " stride " ) )
2017-03-05 16:44:50 +01:00
stride = parser . get_attribute_value ( " stride " ) . to_int ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
morphdata . sources [ current_source ] . stride = stride ;
COLLADA_PRINT ( " section: " + current_source + " stride " + itos ( morphdata . sources [ current_source ] . stride ) ) ;
2014-02-10 02:10:30 +01:00
}
} else if ( section = = " targets " ) {
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " input " ) {
2014-02-10 02:10:30 +01:00
String semantic = parser . get_attribute_value ( " semantic " ) ;
2017-03-05 16:44:50 +01:00
String source = _uri_to_id ( parser . get_attribute_value ( " source " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
morphdata . targets [ semantic ] = source ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( section + " input semantic: " + semantic + " source: " + source ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = section )
2014-02-10 02:10:30 +01:00
break ;
}
}
2017-01-14 12:26:56 +01:00
/*
else if ( ! parser . is_empty ( ) )
parser . skip_section ( ) ;
*/
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " morph " )
break ;
2014-02-10 02:10:30 +01:00
}
if ( morphdata . targets . has ( " MORPH_WEIGHT " ) ) {
2017-03-05 16:44:50 +01:00
state . morph_name_map [ morphdata . targets [ " MORPH_WEIGHT " ] ] = p_id ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_controller ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) ) {
return ;
}
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String section = parser . get_node_name ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( section = = " skin " ) {
_parse_skin_controller ( parser , id ) ;
2014-02-10 02:10:30 +01:00
} else if ( section = = " morph " ) {
2017-03-05 16:44:50 +01:00
_parse_morph_controller ( parser , id ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " controller " )
break ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
Collada : : Node * Collada : : _parse_visual_instance_geometry ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
String type = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
NodeGeometry * geom = memnew ( NodeGeometry ) ;
geom - > controller = type = = " instance_controller " ;
2014-02-10 02:10:30 +01:00
geom - > source = _uri_to_id ( parser . get_attribute_value_safe ( " url " ) ) ;
if ( parser . is_empty ( ) ) //nothing else to parse...
return geom ;
// try to find also many materials and skeletons!
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " instance_material " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String symbol = parser . get_attribute_value ( " symbol " ) ;
String target = _uri_to_id ( parser . get_attribute_value ( " target " ) ) ;
2014-02-10 02:10:30 +01:00
NodeGeometry : : Material mat ;
2017-03-05 16:44:50 +01:00
mat . target = target ;
geom - > material_map [ symbol ] = mat ;
COLLADA_PRINT ( " uses material: ' " + target + " ' on primitive' " + symbol + " ' " ) ;
} else if ( parser . get_node_name ( ) = = " skeleton " ) {
2014-02-10 02:10:30 +01:00
parser . read ( ) ;
String uri = _uri_to_id ( parser . get_node_data ( ) ) ;
2017-03-05 16:44:50 +01:00
if ( uri ! = " " ) {
2014-02-10 02:10:30 +01:00
geom - > skeletons . push_back ( uri ) ;
}
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = type )
2014-02-10 02:10:30 +01:00
break ;
}
if ( geom - > controller ) {
if ( geom - > skeletons . empty ( ) ) {
//XSI style
if ( state . skin_controller_data_map . has ( geom - > source ) ) {
SkinControllerData * skin = & state . skin_controller_data_map [ geom - > source ] ;
//case where skeletons reference bones with IDREF (XSI)
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! skin - > joints . sources . has ( " JOINT " ) , geom ) ;
2014-02-10 02:10:30 +01:00
String joint_arr = skin - > joints . sources [ " JOINT " ] ;
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! skin - > sources . has ( joint_arr ) , geom ) ;
2014-02-10 02:10:30 +01:00
Collada : : SkinControllerData : : Source & joint_source = skin - > sources [ joint_arr ] ;
2017-03-05 16:44:50 +01:00
geom - > skeletons = joint_source . sarray ; //quite crazy, but should work.
2014-02-10 02:10:30 +01:00
}
}
}
return geom ;
}
2017-03-05 16:44:50 +01:00
Collada : : Node * Collada : : _parse_visual_instance_camera ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
String type = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
NodeCamera * cam = memnew ( NodeCamera ) ;
cam - > camera = _uri_to_id ( parser . get_attribute_value_safe ( " url " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( state . up_axis = = Vector3 : : AXIS_Z ) //collada weirdness
cam - > post_transform . basis . rotate ( Vector3 ( 1 , 0 , 0 ) , - Math_PI * 0.5 ) ;
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) ) //nothing else to parse...
return cam ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " instance_camera " )
2014-02-10 02:10:30 +01:00
break ;
}
return cam ;
}
2017-03-05 16:44:50 +01:00
Collada : : Node * Collada : : _parse_visual_instance_light ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
String type = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
NodeLight * cam = memnew ( NodeLight ) ;
cam - > light = _uri_to_id ( parser . get_attribute_value_safe ( " url " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( state . up_axis = = Vector3 : : AXIS_Z ) //collada weirdness
cam - > post_transform . basis . rotate ( Vector3 ( 1 , 0 , 0 ) , - Math_PI * 0.5 ) ;
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) ) //nothing else to parse...
return cam ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " instance_light " )
2014-02-10 02:10:30 +01:00
break ;
}
return cam ;
}
2017-03-05 16:44:50 +01:00
Collada : : Node * Collada : : _parse_visual_node_instance_data ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
String instance_type = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( instance_type = = " instance_geometry " | | instance_type = = " instance_controller " ) {
2014-02-10 02:10:30 +01:00
return _parse_visual_instance_geometry ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( instance_type = = " instance_camera " ) {
2014-02-10 02:10:30 +01:00
return _parse_visual_instance_camera ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( instance_type = = " instance_light " ) {
2014-02-10 02:10:30 +01:00
return _parse_visual_instance_light ( parser ) ;
}
if ( parser . is_empty ( ) ) //nothing else to parse...
return NULL ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = instance_type )
2014-02-10 02:10:30 +01:00
break ;
}
return NULL ;
}
2017-03-05 16:44:50 +01:00
Collada : : Node * Collada : : _parse_visual_scene_node ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
String name ;
2016-03-09 00:00:52 +01:00
String id = parser . get_attribute_value_safe ( " id " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
bool found_name = false ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( id = = " " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
id = " %NODEID% " + itos ( Math : : rand ( ) ) ;
2014-02-10 02:10:30 +01:00
} else {
2017-03-05 16:44:50 +01:00
found_name = true ;
2014-02-10 02:10:30 +01:00
}
Vector < Node : : XForm > xform_list ;
2017-03-05 16:44:50 +01:00
Vector < Node * > children ;
String empty_draw_type = " " ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Node * node = NULL ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
name = parser . has_attribute ( " name " ) ? parser . get_attribute_value_safe ( " name " ) : parser . get_attribute_value_safe ( " id " ) ;
if ( name = = " " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
name = id ;
2014-02-10 02:10:30 +01:00
} else {
2017-03-05 16:44:50 +01:00
found_name = true ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
if ( ( parser . has_attribute ( " type " ) & & parser . get_attribute_value ( " type " ) = = " JOINT " ) | | state . idref_joints . has ( name ) ) {
2014-02-10 02:10:30 +01:00
// handle a bone
2017-03-05 16:44:50 +01:00
NodeJoint * joint = memnew ( NodeJoint ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . has_attribute ( " sid " ) ) { //bones may not have sid
joint - > sid = parser . get_attribute_value ( " sid " ) ;
2017-01-14 12:26:56 +01:00
//state.bone_map[joint->sid]=joint;
2015-12-12 18:55:16 +01:00
} else if ( state . idref_joints . has ( name ) ) {
2017-03-05 16:44:50 +01:00
joint - > sid = name ; //kind of a cheat but..
2015-12-12 18:55:16 +01:00
} else if ( parser . has_attribute ( " name " ) ) {
2017-03-05 16:44:50 +01:00
joint - > sid = parser . get_attribute_value_safe ( " name " ) ;
2015-12-12 18:55:16 +01:00
}
2017-03-05 16:44:50 +01:00
if ( joint - > sid ! = " " ) {
state . sid_to_node_map [ joint - > sid ] = id ;
2014-09-15 16:33:30 +02:00
}
2017-03-05 16:44:50 +01:00
node = joint ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
String section = parser . get_node_name ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( section = = " translate " ) {
2014-02-10 02:10:30 +01:00
Node : : XForm xf ;
if ( parser . has_attribute ( " sid " ) ) {
2017-03-05 16:44:50 +01:00
xf . id = parser . get_attribute_value ( " sid " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
xf . op = Node : : XForm : : OP_TRANSLATE ;
2014-02-10 02:10:30 +01:00
Vector < float > xlt = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
xf . data = xlt ;
2014-02-10 02:10:30 +01:00
xform_list . push_back ( xf ) ;
2017-03-05 16:44:50 +01:00
} else if ( section = = " rotate " ) {
2014-02-10 02:10:30 +01:00
Node : : XForm xf ;
if ( parser . has_attribute ( " sid " ) ) {
2017-03-05 16:44:50 +01:00
xf . id = parser . get_attribute_value ( " sid " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
xf . op = Node : : XForm : : OP_ROTATE ;
2014-02-10 02:10:30 +01:00
Vector < float > rot = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
xf . data = rot ;
2014-02-10 02:10:30 +01:00
xform_list . push_back ( xf ) ;
2017-03-05 16:44:50 +01:00
} else if ( section = = " scale " ) {
2014-02-10 02:10:30 +01:00
Node : : XForm xf ;
if ( parser . has_attribute ( " sid " ) ) {
2017-03-05 16:44:50 +01:00
xf . id = parser . get_attribute_value ( " sid " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
xf . op = Node : : XForm : : OP_SCALE ;
2014-02-10 02:10:30 +01:00
Vector < float > scale = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
xf . data = scale ;
2014-02-10 02:10:30 +01:00
xform_list . push_back ( xf ) ;
2017-03-05 16:44:50 +01:00
} else if ( section = = " matrix " ) {
2014-02-10 02:10:30 +01:00
Node : : XForm xf ;
if ( parser . has_attribute ( " sid " ) ) {
2017-03-05 16:44:50 +01:00
xf . id = parser . get_attribute_value ( " sid " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
xf . op = Node : : XForm : : OP_MATRIX ;
2014-02-10 02:10:30 +01:00
Vector < float > matrix = _read_float_array ( parser ) ;
2017-03-05 16:44:50 +01:00
xf . data = matrix ;
2014-02-10 02:10:30 +01:00
String mtx ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < matrix . size ( ) ; i + + )
mtx + = " " + rtos ( matrix [ i ] ) ;
2014-02-10 02:10:30 +01:00
xform_list . push_back ( xf ) ;
2017-03-05 16:44:50 +01:00
} else if ( section = = " visibility " ) {
Node : : XForm xf ;
if ( parser . has_attribute ( " sid " ) ) {
xf . id = parser . get_attribute_value ( " sid " ) ;
}
xf . op = Node : : XForm : : OP_VISIBILITY ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Vector < float > visible = _read_float_array ( parser ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
xf . data = visible ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
xform_list . push_back ( xf ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( section = = " empty_draw_type " ) {
2016-06-05 20:59:21 +02:00
empty_draw_type = _read_empty_draw_type ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( section = = " technique " | | section = = " extra " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( section ! = " node " ) {
2014-02-10 02:10:30 +01:00
//usually what defines the type of node
if ( section . begins_with ( " instance_ " ) ) {
if ( ! node ) {
node = _parse_visual_node_instance_data ( parser ) ;
} else {
ERR_PRINT ( " Multiple instance_* not supported. " ) ;
}
}
2017-03-05 16:44:50 +01:00
} else if ( section = = " node " ) {
2014-02-10 02:10:30 +01:00
/* Found a child node!! what to do..*/
Node * child = _parse_visual_scene_node ( parser ) ;
children . push_back ( child ) ;
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " node " )
break ;
2014-02-10 02:10:30 +01:00
}
if ( ! node ) {
2017-03-05 16:44:50 +01:00
node = memnew ( Node ) ; //generic node, nothing of relevance found
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
node - > noname = ! found_name ;
node - > xform_list = xform_list ;
node - > children = children ;
for ( int i = 0 ; i < children . size ( ) ; i + + ) {
node - > children [ i ] - > parent = node ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
node - > name = name ;
node - > id = id ;
node - > empty_draw_type = empty_draw_type ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( node - > children . size ( ) = = 1 ) {
2014-02-10 02:10:30 +01:00
if ( node - > children [ 0 ] - > noname & & ! node - > noname ) {
2017-03-05 16:44:50 +01:00
node - > children [ 0 ] - > name = node - > name ;
node - > name = node - > name + " -base " ;
2014-02-10 02:10:30 +01:00
}
}
node - > default_transform = node - > compute_transform ( * this ) ;
2017-03-05 16:44:50 +01:00
state . scene_map [ id ] = node ;
2014-02-10 02:10:30 +01:00
return node ;
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_visual_scene ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String id = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) ) {
return ;
}
2017-03-05 16:44:50 +01:00
state . visual_scene_map [ id ] = VisualScene ( ) ;
2014-02-10 02:10:30 +01:00
VisualScene & vscene = state . visual_scene_map [ id ] ;
if ( parser . has_attribute ( " name " ) )
2017-03-05 16:44:50 +01:00
vscene . name = parser . get_attribute_value ( " name " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2017-03-05 16:44:50 +01:00
String section = parser . get_node_name ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( section = = " node " ) {
2014-02-10 02:10:30 +01:00
vscene . root_nodes . push_back ( _parse_visual_scene_node ( parser ) ) ;
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " visual_scene " )
break ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " Scene ID: " + id ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_animation ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_ANIMATION ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
Map < String , Vector < float > > float_sources ;
Map < String , Vector < String > > string_sources ;
Map < String , int > source_strides ;
Map < String , Map < String , String > > samplers ;
Map < String , Vector < String > > source_param_names ;
Map < String , Vector < String > > source_param_types ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String id = " " ;
2014-02-10 02:10:30 +01:00
if ( parser . has_attribute ( " id " ) )
2017-03-05 16:44:50 +01:00
id = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
String current_source ;
String current_sampler ;
Vector < String > channel_sources ;
Vector < String > channel_targets ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
String name = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( name = = " source " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
current_source = parser . get_attribute_value ( " id " ) ;
source_param_names [ current_source ] = Vector < String > ( ) ;
source_param_types [ current_source ] = Vector < String > ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( name = = " float_array " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( current_source ! = " " ) {
float_sources [ current_source ] = _read_float_array ( parser ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( name = = " Name_array " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( current_source ! = " " ) {
string_sources [ current_source ] = _read_string_array ( parser ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( name = = " accessor " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( current_source ! = " " & & parser . has_attribute ( " stride " ) ) {
source_strides [ current_source ] = parser . get_attribute_value ( " stride " ) . to_int ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( name = = " sampler " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
current_sampler = parser . get_attribute_value ( " id " ) ;
samplers [ current_sampler ] = Map < String , String > ( ) ;
} else if ( name = = " param " ) {
2014-02-10 02:10:30 +01:00
if ( parser . has_attribute ( " name " ) )
source_param_names [ current_source ] . push_back ( parser . get_attribute_value ( " name " ) ) ;
else
source_param_names [ current_source ] . push_back ( " " ) ;
if ( parser . has_attribute ( " type " ) )
source_param_types [ current_source ] . push_back ( parser . get_attribute_value ( " type " ) ) ;
else
source_param_types [ current_source ] . push_back ( " " ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " input " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( current_sampler ! = " " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
samplers [ current_sampler ] [ parser . get_attribute_value ( " semantic " ) ] = parser . get_attribute_value ( " source " ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( name = = " channel " ) {
2014-02-10 02:10:30 +01:00
channel_sources . push_back ( parser . get_attribute_value ( " source " ) ) ;
channel_targets . push_back ( parser . get_attribute_value ( " target " ) ) ;
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " animation " )
2014-02-10 02:10:30 +01:00
break ; //end of <asset>
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < channel_sources . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String source = _uri_to_id ( channel_sources [ i ] ) ;
String target = channel_targets [ i ] ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! samplers . has ( source ) ) ;
2017-03-05 16:44:50 +01:00
Map < String , String > & sampler = samplers [ source ] ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! sampler . has ( " INPUT " ) ) ; //no input semantic? wtf?
2017-03-05 16:44:50 +01:00
String input_id = _uri_to_id ( sampler [ " INPUT " ] ) ;
COLLADA_PRINT ( " input id is " + input_id ) ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! float_sources . has ( input_id ) ) ;
ERR_CONTINUE ( ! sampler . has ( " OUTPUT " ) ) ;
2017-03-05 16:44:50 +01:00
String output_id = _uri_to_id ( sampler [ " OUTPUT " ] ) ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! float_sources . has ( output_id ) ) ;
ERR_CONTINUE ( ! source_param_names . has ( output_id ) ) ;
Vector < String > & names = source_param_names [ output_id ] ;
2017-03-05 16:44:50 +01:00
for ( int l = 0 ; l < names . size ( ) ; l + + ) {
2014-02-10 02:10:30 +01:00
String name = names [ l ] ;
2017-03-05 16:44:50 +01:00
Vector < float > & time_keys = float_sources [ input_id ] ;
2014-02-10 02:10:30 +01:00
int key_count = time_keys . size ( ) ;
AnimationTrack track ; //begin crating track
2017-03-05 16:44:50 +01:00
track . id = id ;
2014-02-10 02:10:30 +01:00
track . keys . resize ( key_count ) ;
2017-03-05 16:44:50 +01:00
for ( int j = 0 ; j < key_count ; j + + ) {
track . keys [ j ] . time = time_keys [ j ] ;
state . animation_length = MAX ( state . animation_length , time_keys [ j ] ) ;
2014-02-10 02:10:30 +01:00
}
//now read actual values
2017-03-05 16:44:50 +01:00
int stride = 1 ;
2014-02-10 02:10:30 +01:00
if ( source_strides . has ( output_id ) )
2017-03-05 16:44:50 +01:00
stride = source_strides [ output_id ] ;
int output_len = stride / names . size ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
ERR_CONTINUE ( output_len = = 0 ) ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! float_sources . has ( output_id ) ) ;
Vector < float > & output = float_sources [ output_id ] ;
ERR_EXPLAIN ( " Wrong number of keys in output " ) ;
2017-03-05 16:44:50 +01:00
ERR_CONTINUE ( ( output . size ( ) / stride ) ! = key_count ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int j = 0 ; j < key_count ; j + + ) {
2014-02-10 02:10:30 +01:00
track . keys [ j ] . data . resize ( output_len ) ;
2017-03-05 16:44:50 +01:00
for ( int k = 0 ; k < output_len ; k + + )
2017-09-21 01:59:19 +02:00
track . keys [ j ] . data [ k ] = output [ l + j * stride + k ] ; //super weird but should work:
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
if ( sampler . has ( " INTERPOLATION " ) ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String interp_id = _uri_to_id ( sampler [ " INTERPOLATION " ] ) ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! string_sources . has ( interp_id ) ) ;
2017-03-05 16:44:50 +01:00
Vector < String > & interps = string_sources [ interp_id ] ;
ERR_CONTINUE ( interps . size ( ) ! = key_count ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int j = 0 ; j < key_count ; j + + ) {
if ( interps [ j ] = = " BEZIER " )
track . keys [ j ] . interp_type = AnimationTrack : : INTERP_BEZIER ;
2014-02-10 02:10:30 +01:00
else
2017-03-05 16:44:50 +01:00
track . keys [ j ] . interp_type = AnimationTrack : : INTERP_LINEAR ;
2014-02-10 02:10:30 +01:00
}
}
if ( sampler . has ( " IN_TANGENT " ) & & sampler . has ( " OUT_TANGENT " ) ) {
//bezier control points..
2017-03-05 16:44:50 +01:00
String intangent_id = _uri_to_id ( sampler [ " IN_TANGENT " ] ) ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! float_sources . has ( intangent_id ) ) ;
2017-03-05 16:44:50 +01:00
Vector < float > & intangents = float_sources [ intangent_id ] ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
ERR_CONTINUE ( intangents . size ( ) ! = key_count * 2 * names . size ( ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
String outangent_id = _uri_to_id ( sampler [ " OUT_TANGENT " ] ) ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! float_sources . has ( outangent_id ) ) ;
2017-03-05 16:44:50 +01:00
Vector < float > & outangents = float_sources [ outangent_id ] ;
ERR_CONTINUE ( outangents . size ( ) ! = key_count * 2 * names . size ( ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int j = 0 ; j < key_count ; j + + ) {
track . keys [ j ] . in_tangent = Vector2 ( intangents [ j * 2 * names . size ( ) + 0 + l * 2 ] , intangents [ j * 2 * names . size ( ) + 1 + l * 2 ] ) ;
track . keys [ j ] . out_tangent = Vector2 ( outangents [ j * 2 * names . size ( ) + 0 + l * 2 ] , outangents [ j * 2 * names . size ( ) + 1 + l * 2 ] ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
if ( target . find ( " / " ) ! = - 1 ) { //transform component
track . target = target . get_slicec ( ' / ' , 0 ) ;
track . param = target . get_slicec ( ' / ' , 1 ) ;
if ( track . param . find ( " . " ) ! = - 1 )
track . component = track . param . get_slice ( " . " , 1 ) . to_upper ( ) ;
track . param = track . param . get_slice ( " . " , 0 ) ;
if ( names . size ( ) > 1 & & track . component = = " " ) {
2014-02-10 02:10:30 +01:00
//this is a guess because the collada spec is ambiguous here...
//i suppose if you have many names (outputs) you can't use a component and i should abide to that.
2017-03-05 16:44:50 +01:00
track . component = name ;
2014-02-10 02:10:30 +01:00
}
} else {
2017-03-05 16:44:50 +01:00
track . target = target ;
2014-02-10 02:10:30 +01:00
}
state . animation_tracks . push_back ( track ) ;
if ( ! state . referenced_tracks . has ( target ) )
2017-03-05 16:44:50 +01:00
state . referenced_tracks [ target ] = Vector < int > ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . referenced_tracks [ target ] . push_back ( state . animation_tracks . size ( ) - 1 ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( id ! = " " ) {
2014-02-10 02:10:30 +01:00
if ( ! state . by_id_tracks . has ( id ) )
2017-03-05 16:44:50 +01:00
state . by_id_tracks [ id ] = Vector < int > ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . by_id_tracks [ id ] . push_back ( state . animation_tracks . size ( ) - 1 ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " loaded animation with " + itos ( key_count ) + " keys " ) ;
2014-02-10 02:10:30 +01:00
}
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_animation_clip ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( state . import_flags & IMPORT_FLAG_ANIMATION ) ) {
if ( ! parser . is_empty ( ) )
2014-02-10 02:10:30 +01:00
parser . skip_section ( ) ;
return ;
}
AnimationClip clip ;
if ( parser . has_attribute ( " name " ) )
2017-03-05 16:44:50 +01:00
clip . name = parser . get_attribute_value ( " name " ) ;
2014-10-28 02:54:32 +01:00
else if ( parser . has_attribute ( " id " ) )
2017-03-05 16:44:50 +01:00
clip . name = parser . get_attribute_value ( " id " ) ;
2014-02-10 02:10:30 +01:00
if ( parser . has_attribute ( " start " ) )
2017-03-05 16:44:50 +01:00
clip . begin = parser . get_attribute_value ( " start " ) . to_double ( ) ;
2014-02-10 02:10:30 +01:00
if ( parser . has_attribute ( " end " ) )
2017-03-05 16:44:50 +01:00
clip . end = parser . get_attribute_value ( " end " ) . to_double ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
String name = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( name = = " instance_animation " ) {
2014-02-10 02:10:30 +01:00
String url = _uri_to_id ( parser . get_attribute_value ( " url " ) ) ;
clip . tracks . push_back ( url ) ;
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " animation_clip " )
2014-02-10 02:10:30 +01:00
break ; //end of <asset>
}
state . animation_clips . push_back ( clip ) ;
}
2017-10-21 19:31:23 +02:00
2017-03-05 16:44:50 +01:00
void Collada : : _parse_scene ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) ) {
return ;
}
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
String name = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
if ( name = = " instance_visual_scene " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . root_visual_scene = _uri_to_id ( parser . get_attribute_value ( " url " ) ) ;
} else if ( name = = " instance_physics_scene " ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . root_physics_scene = _uri_to_id ( parser . get_attribute_value ( " url " ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " scene " )
2014-02-10 02:10:30 +01:00
break ; //end of <asset>
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _parse_library ( XMLParser & parser ) {
2014-02-10 02:10:30 +01:00
if ( parser . is_empty ( ) ) {
return ;
}
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
String name = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " library name is: " + name ) ;
if ( name = = " image " ) {
2014-02-10 02:10:30 +01:00
_parse_image ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " material " ) {
2014-02-10 02:10:30 +01:00
_parse_material ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " effect " ) {
2014-02-10 02:10:30 +01:00
_parse_effect ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " camera " ) {
2014-02-10 02:10:30 +01:00
_parse_camera ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " light " ) {
2014-02-10 02:10:30 +01:00
_parse_light ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " geometry " ) {
2014-02-10 02:10:30 +01:00
String id = parser . get_attribute_value ( " id " ) ;
String name = parser . get_attribute_value_safe ( " name " ) ;
2017-03-05 16:44:50 +01:00
while ( parser . read ( ) = = OK ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( parser . get_node_name ( ) = = " mesh " ) {
state . mesh_name_map [ id ] = ( name ! = " " ) ? name : id ;
_parse_mesh_geometry ( parser , id , name ) ;
} else if ( parser . get_node_name ( ) = = " spline " ) {
state . mesh_name_map [ id ] = ( name ! = " " ) ? name : id ;
_parse_curve_geometry ( parser , id , name ) ;
2014-02-10 02:10:30 +01:00
} else if ( ! parser . is_empty ( ) )
parser . skip_section ( ) ;
2017-03-05 16:44:50 +01:00
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) = = " geometry " )
2014-02-10 02:10:30 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
} else if ( name = = " controller " ) {
2014-02-10 02:10:30 +01:00
_parse_controller ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " animation " ) {
2014-02-10 02:10:30 +01:00
_parse_animation ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " animation_clip " ) {
2014-02-10 02:10:30 +01:00
_parse_animation_clip ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( name = = " visual_scene " ) {
2014-02-10 02:10:30 +01:00
COLLADA_PRINT ( " visual scene " ) ;
_parse_visual_scene ( parser ) ;
} else if ( ! parser . is_empty ( ) )
parser . skip_section ( ) ;
} else if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT_END & & parser . get_node_name ( ) . begins_with ( " library_ " ) )
break ; //end of <asset>
}
}
void Collada : : _joint_set_owner ( Collada : : Node * p_node , NodeSkeleton * p_owner ) {
2017-03-05 16:44:50 +01:00
if ( p_node - > type = = Node : : TYPE_JOINT ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
NodeJoint * nj = static_cast < NodeJoint * > ( p_node ) ;
nj - > owner = p_owner ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < nj - > children . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
_joint_set_owner ( nj - > children [ i ] , p_owner ) ;
2014-02-10 02:10:30 +01:00
}
}
}
2017-03-05 16:44:50 +01:00
void Collada : : _create_skeletons ( Collada : : Node * * p_node , NodeSkeleton * p_skeleton ) {
2014-02-10 02:10:30 +01:00
Node * node = * p_node ;
2017-03-05 16:44:50 +01:00
if ( node - > type = = Node : : TYPE_JOINT ) {
2014-02-10 02:10:30 +01:00
2014-12-02 18:02:41 +01:00
if ( ! p_skeleton ) {
// ohohohoohoo it's a joint node, time to work!
2017-03-05 16:44:50 +01:00
NodeSkeleton * sk = memnew ( NodeSkeleton ) ;
* p_node = sk ;
2014-12-02 18:02:41 +01:00
sk - > children . push_back ( node ) ;
2017-03-05 16:44:50 +01:00
sk - > parent = node - > parent ;
node - > parent = sk ;
p_skeleton = sk ;
2014-12-02 18:02:41 +01:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
NodeJoint * nj = static_cast < NodeJoint * > ( node ) ;
nj - > owner = p_skeleton ;
2014-02-10 02:10:30 +01:00
} else {
2017-03-05 16:44:50 +01:00
p_skeleton = NULL ;
2014-12-02 18:02:41 +01:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < node - > children . size ( ) ; i + + ) {
_create_skeletons ( & node - > children [ i ] , p_skeleton ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
bool Collada : : _remove_node ( Node * p_parent , Node * p_node ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_parent - > children . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( p_parent - > children [ i ] = = p_node ) {
2014-02-10 02:10:30 +01:00
p_parent - > children . remove ( i ) ;
return true ;
}
2017-03-05 16:44:50 +01:00
if ( _remove_node ( p_parent - > children [ i ] , p_node ) )
2014-02-10 02:10:30 +01:00
return true ;
}
return false ;
}
2017-03-05 16:44:50 +01:00
void Collada : : _remove_node ( VisualScene * p_vscene , Node * p_node ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_vscene - > root_nodes . size ( ) ; i + + ) {
if ( p_vscene - > root_nodes [ i ] = = p_node ) {
2014-02-10 02:10:30 +01:00
p_vscene - > root_nodes . remove ( i ) ;
return ;
}
2017-03-05 16:44:50 +01:00
if ( _remove_node ( p_vscene - > root_nodes [ i ] , p_node ) )
2014-02-10 02:10:30 +01:00
return ;
}
ERR_PRINT ( " ERROR: Not found node to remove? " ) ;
}
2017-03-05 16:44:50 +01:00
void Collada : : _merge_skeletons ( VisualScene * p_vscene , Node * p_node ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( p_node - > type = = Node : : TYPE_GEOMETRY ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
NodeGeometry * gnode = static_cast < NodeGeometry * > ( p_node ) ;
2014-02-10 02:10:30 +01:00
if ( gnode - > controller ) {
// recount skeletons used
2017-03-05 16:44:50 +01:00
Set < NodeSkeleton * > skeletons ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < gnode - > skeletons . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
String nodeid = gnode - > skeletons [ i ] ;
2017-03-05 16:44:50 +01:00
ERR_CONTINUE ( ! state . scene_map . has ( nodeid ) ) ; //weird, it should have it...
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
NodeJoint * nj = SAFE_CAST < NodeJoint * > ( state . scene_map [ nodeid ] ) ;
2017-08-08 13:14:51 +02:00
ERR_CONTINUE ( ! nj ) ; //broken collada
2017-03-05 16:44:50 +01:00
ERR_CONTINUE ( ! nj - > owner ) ; //weird, node should have a skeleton owner
2014-02-10 02:10:30 +01:00
skeletons . insert ( nj - > owner ) ;
}
2017-03-05 16:44:50 +01:00
if ( skeletons . size ( ) > 1 ) {
2014-02-10 02:10:30 +01:00
//do the merger!!
2017-03-05 16:44:50 +01:00
Set < NodeSkeleton * > : : Element * E = skeletons . front ( ) ;
2014-02-10 02:10:30 +01:00
NodeSkeleton * base = E - > get ( ) ;
2017-03-05 16:44:50 +01:00
for ( E = E - > next ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
NodeSkeleton * merged = E - > get ( ) ;
2017-03-05 16:44:50 +01:00
_remove_node ( p_vscene , merged ) ;
for ( int i = 0 ; i < merged - > children . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
_joint_set_owner ( merged - > children [ i ] , base ) ;
base - > children . push_back ( merged - > children [ i ] ) ;
merged - > children [ i ] - > parent = base ;
2014-02-10 02:10:30 +01:00
}
merged - > children . clear ( ) ; //take children from it
2017-03-05 16:44:50 +01:00
memdelete ( merged ) ;
2014-02-10 02:10:30 +01:00
}
}
}
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_node - > children . size ( ) ; i + + ) {
_merge_skeletons ( p_vscene , p_node - > children [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
2014-09-15 16:33:30 +02:00
}
void Collada : : _merge_skeletons2 ( VisualScene * p_vscene ) {
2017-03-05 16:44:50 +01:00
for ( Map < String , SkinControllerData > : : Element * E = state . skin_controller_data_map . front ( ) ; E ; E = E - > next ( ) ) {
2014-09-15 16:33:30 +02:00
2017-03-05 16:44:50 +01:00
SkinControllerData & cd = E - > get ( ) ;
2014-09-15 16:33:30 +02:00
2017-03-05 16:44:50 +01:00
NodeSkeleton * skeleton = NULL ;
2014-09-15 16:33:30 +02:00
2017-03-05 16:44:50 +01:00
for ( Map < String , Transform > : : Element * F = cd . bone_rest_map . front ( ) ; F ; F = F - > next ( ) ) {
2014-09-15 16:33:30 +02:00
String name ;
if ( ! state . sid_to_node_map . has ( F - > key ( ) ) ) {
continue ;
}
name = state . sid_to_node_map [ F - > key ( ) ] ;
2017-03-05 16:44:50 +01:00
ERR_CONTINUE ( ! state . scene_map . has ( name ) ) ;
2014-09-15 16:33:30 +02:00
2017-03-05 16:44:50 +01:00
Node * node = state . scene_map [ name ] ;
ERR_CONTINUE ( node - > type ! = Node : : TYPE_JOINT ) ;
if ( node - > type ! = Node : : TYPE_JOINT )
2014-09-15 16:33:30 +02:00
continue ;
2017-03-05 16:44:50 +01:00
NodeSkeleton * sk = NULL ;
2014-09-15 16:33:30 +02:00
2017-03-05 16:44:50 +01:00
while ( node & & ! sk ) {
2014-09-15 16:33:30 +02:00
2017-03-05 16:44:50 +01:00
if ( node - > type = = Node : : TYPE_SKELETON ) {
sk = static_cast < NodeSkeleton * > ( node ) ;
2014-09-15 16:33:30 +02:00
}
2017-03-05 16:44:50 +01:00
node = node - > parent ;
2014-09-15 16:33:30 +02:00
}
2017-03-05 16:44:50 +01:00
ERR_CONTINUE ( ! sk ) ;
2014-09-15 16:33:30 +02:00
if ( ! sk )
continue ; //bleh
if ( ! skeleton ) {
2017-03-05 16:44:50 +01:00
skeleton = sk ;
2014-09-15 16:33:30 +02:00
continue ;
}
2017-03-05 16:44:50 +01:00
if ( skeleton ! = sk ) {
2014-09-15 16:33:30 +02:00
//whoa.. wtf, merge.
2017-03-05 16:44:50 +01:00
_remove_node ( p_vscene , sk ) ;
for ( int i = 0 ; i < sk - > children . size ( ) ; i + + ) {
2014-09-15 16:33:30 +02:00
2017-03-05 16:44:50 +01:00
_joint_set_owner ( sk - > children [ i ] , skeleton ) ;
skeleton - > children . push_back ( sk - > children [ i ] ) ;
sk - > children [ i ] - > parent = skeleton ;
2014-09-15 16:33:30 +02:00
}
sk - > children . clear ( ) ; //take children from it
2017-03-05 16:44:50 +01:00
memdelete ( sk ) ;
2014-09-15 16:33:30 +02:00
}
}
}
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
bool Collada : : _optimize_skeletons ( VisualScene * p_vscene , Node * p_node ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Node * node = p_node ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( node - > type = = Node : : TYPE_SKELETON & & node - > parent & & node - > parent - > type = = Node : : TYPE_NODE & & node - > parent - > children . size ( ) = = 1 ) {
2014-02-10 02:10:30 +01:00
//replace parent by this...
Node * parent = node - > parent ;
//i wonder if this is allright.. i think it is since created skeleton (first joint) is already animated by bone..
2017-03-05 16:44:50 +01:00
node - > id = parent - > id ;
node - > name = parent - > name ;
node - > xform_list = parent - > xform_list ;
node - > default_transform = parent - > default_transform ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
state . scene_map [ node - > id ] = node ;
node - > parent = parent - > parent ;
2014-02-10 02:10:30 +01:00
if ( parent - > parent ) {
Node * gp = parent - > parent ;
2017-03-05 16:44:50 +01:00
bool found = false ;
for ( int i = 0 ; i < gp - > children . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( gp - > children [ i ] = = parent ) {
gp - > children [ i ] = node ;
found = true ;
2014-02-10 02:10:30 +01:00
break ;
}
}
if ( ! found ) {
ERR_PRINT ( " BUG " ) ;
}
} else {
2017-03-05 16:44:50 +01:00
bool found = false ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_vscene - > root_nodes . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( p_vscene - > root_nodes [ i ] = = parent ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
p_vscene - > root_nodes [ i ] = node ;
found = true ;
2014-02-10 02:10:30 +01:00
break ;
}
}
if ( ! found ) {
ERR_PRINT ( " BUG " ) ;
}
}
parent - > children . clear ( ) ;
memdelete ( parent ) ;
return true ;
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < node - > children . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( _optimize_skeletons ( p_vscene , node - > children [ i ] ) )
2014-02-10 02:10:30 +01:00
return false ; //stop processing, go up
}
return false ;
}
2017-03-05 16:44:50 +01:00
bool Collada : : _move_geometry_to_skeletons ( VisualScene * p_vscene , Node * p_node , List < Node * > * p_mgeom ) {
2014-02-10 02:10:30 +01:00
// bind shape matrix escala los huesos y los hace gigantes, asi la matriz despues achica
// al modelo?
// solucion: aplicarle la bind shape matrix a los VERTICES, y si el objeto viene con escala, se la dejo me parece!
2017-03-05 16:44:50 +01:00
if ( p_node - > type = = Node : : TYPE_GEOMETRY ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
NodeGeometry * ng = static_cast < NodeGeometry * > ( p_node ) ;
2014-02-10 02:10:30 +01:00
if ( ng - > ignore_anim )
return false ; //already made child of skeleton and processeg
if ( ng - > controller & & ng - > skeletons . size ( ) ) {
String nodeid = ng - > skeletons [ 0 ] ;
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! state . scene_map . has ( nodeid ) , false ) ; //weird, it should have it...
NodeJoint * nj = SAFE_CAST < NodeJoint * > ( state . scene_map [ nodeid ] ) ;
ERR_FAIL_COND_V ( ! nj , false ) ;
ERR_FAIL_COND_V ( ! nj - > owner , false ) ; //weird, node should have a skeleton owner
2014-02-10 02:10:30 +01:00
NodeSkeleton * sk = nj - > owner ;
2017-03-05 16:44:50 +01:00
Node * p = sk - > parent ;
bool node_is_parent_of_skeleton = false ;
2014-02-10 02:10:30 +01:00
while ( p ) {
2017-03-05 16:44:50 +01:00
if ( p = = p_node ) {
node_is_parent_of_skeleton = true ;
2014-02-10 02:10:30 +01:00
break ;
}
2017-03-05 16:44:50 +01:00
p = p - > parent ; // try again
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( node_is_parent_of_skeleton , false ) ;
2014-02-10 02:10:30 +01:00
//this should be correct
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! state . skin_controller_data_map . has ( ng - > source ) , false ) ;
SkinControllerData & skin = state . skin_controller_data_map [ ng - > source ] ;
2014-02-10 02:10:30 +01:00
Transform skel_inv = sk - > get_global_transform ( ) . affine_inverse ( ) ;
2017-03-05 16:44:50 +01:00
p_node - > default_transform = skel_inv * ( skin . bind_shape /* p_node->get_global_transform()*/ ) ; // i honestly have no idea what to do with a previous model xform.. most exporters ignore it
2014-02-10 02:10:30 +01:00
//make rests relative to the skeleton (they seem to be always relative to world)
2017-03-05 16:44:50 +01:00
for ( Map < String , Transform > : : Element * E = skin . bone_rest_map . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
E - > get ( ) = skel_inv * E - > get ( ) ; //make the bone rest local to the skeleton
2017-03-05 16:44:50 +01:00
state . bone_rest_map [ E - > key ( ) ] = E - > get ( ) ; // make it remember where the bone is globally, now that it's relative
2014-02-10 02:10:30 +01:00
}
//but most exporters seem to work only if i do this..
//p_node->default_transform = p_node->get_global_transform();
//p_node->default_transform=Transform(); //this seems to be correct, because bind shape makes the object local to the skeleton
2017-03-05 16:44:50 +01:00
p_node - > ignore_anim = true ; // collada may animate this later, if it does, then this is not supported (redo your original asset and don't animate the base mesh)
p_node - > parent = sk ;
2017-03-24 21:45:31 +01:00
//sk->children.push_back(0,p_node); //avoid INFINITE loop
2014-02-10 02:10:30 +01:00
p_mgeom - > push_back ( p_node ) ;
return true ;
}
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_node - > children . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( _move_geometry_to_skeletons ( p_vscene , p_node - > children [ i ] , p_mgeom ) ) {
2014-02-10 02:10:30 +01:00
p_node - > children . remove ( i ) ;
i - - ;
}
}
return false ;
}
2017-03-05 16:44:50 +01:00
void Collada : : _find_morph_nodes ( VisualScene * p_vscene , Node * p_node ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( p_node - > type = = Node : : TYPE_GEOMETRY ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
NodeGeometry * nj = static_cast < NodeGeometry * > ( p_node ) ;
2014-02-10 02:10:30 +01:00
if ( nj - > controller ) {
String base = nj - > source ;
2017-03-05 16:44:50 +01:00
while ( base ! = " " & & ! state . mesh_data_map . has ( base ) ) {
2014-02-10 02:10:30 +01:00
if ( state . skin_controller_data_map . has ( base ) ) {
SkinControllerData & sk = state . skin_controller_data_map [ base ] ;
2017-03-05 16:44:50 +01:00
base = sk . base ;
2014-02-10 02:10:30 +01:00
} else if ( state . morph_controller_data_map . has ( base ) ) {
2017-03-05 16:44:50 +01:00
state . morph_ownership_map [ base ] = nj - > id ;
2014-02-10 02:10:30 +01:00
break ;
} else {
ERR_EXPLAIN ( " Invalid scene " ) ;
ERR_FAIL ( ) ;
}
}
}
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_node - > children . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
_find_morph_nodes ( p_vscene , p_node - > children [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
}
void Collada : : _optimize ( ) {
2017-03-05 16:44:50 +01:00
for ( Map < String , VisualScene > : : Element * E = state . visual_scene_map . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
VisualScene & vs = E - > get ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < vs . root_nodes . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
_create_skeletons ( & vs . root_nodes [ i ] ) ;
}
2017-08-27 21:07:15 +02:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < vs . root_nodes . size ( ) ; i + + ) {
_merge_skeletons ( & vs , vs . root_nodes [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
2014-09-15 16:33:30 +02:00
_merge_skeletons2 ( & vs ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < vs . root_nodes . size ( ) ; i + + ) {
_optimize_skeletons ( & vs , vs . root_nodes [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < vs . root_nodes . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
List < Node * > mgeom ;
if ( _move_geometry_to_skeletons ( & vs , vs . root_nodes [ i ] , & mgeom ) ) {
2014-02-10 02:10:30 +01:00
vs . root_nodes . remove ( i ) ;
i - - ;
}
2017-03-05 16:44:50 +01:00
while ( ! mgeom . empty ( ) ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Node * n = mgeom . front ( ) - > get ( ) ;
2014-02-10 02:10:30 +01:00
n - > parent - > children . push_back ( n ) ;
mgeom . pop_front ( ) ;
}
}
2017-08-27 21:07:15 +02:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < vs . root_nodes . size ( ) ; i + + ) {
_find_morph_nodes ( & vs , vs . root_nodes [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
}
}
int Collada : : get_uv_channel ( String p_name ) {
if ( ! channel_map . has ( p_name ) ) {
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( channel_map . size ( ) = = 2 , 0 ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
channel_map [ p_name ] = channel_map . size ( ) ;
2014-02-10 02:10:30 +01:00
}
return channel_map [ p_name ] ;
}
2017-03-05 16:44:50 +01:00
Error Collada : : load ( const String & p_path , int p_flags ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Ref < XMLParser > parserr = memnew ( XMLParser ) ;
2014-02-10 02:10:30 +01:00
XMLParser & parser = * parserr . ptr ( ) ;
Error err = parser . open ( p_path ) ;
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( err , err ) ;
2014-02-10 02:10:30 +01:00
2017-07-19 22:00:46 +02:00
state . local_path = ProjectSettings : : get_singleton ( ) - > localize_path ( p_path ) ;
2017-03-05 16:44:50 +01:00
state . import_flags = p_flags ;
2014-02-10 02:10:30 +01:00
/* Skip headers */
2017-03-05 16:44:50 +01:00
err = OK ;
while ( ( err = parser . read ( ) ) = = OK ) {
2014-02-10 02:10:30 +01:00
if ( parser . get_node_type ( ) = = XMLParser : : NODE_ELEMENT ) {
if ( parser . get_node_name ( ) = = " COLLADA " ) {
break ;
} else if ( ! parser . is_empty ( ) )
2017-03-05 16:44:50 +01:00
parser . skip_section ( ) ; // unknown section, likely headers
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( err ! = OK , ERR_FILE_CORRUPT ) ;
2014-02-10 02:10:30 +01:00
/* Start loading Collada */
{
//version
String version = parser . get_attribute_value ( " version " ) ;
2017-03-05 16:44:50 +01:00
state . version . major = version . get_slice ( " . " , 0 ) . to_int ( ) ;
state . version . minor = version . get_slice ( " . " , 1 ) . to_int ( ) ;
state . version . rev = version . get_slice ( " . " , 2 ) . to_int ( ) ;
COLLADA_PRINT ( " Collada VERSION: " + version ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
while ( ( err = parser . read ( ) ) = = OK ) {
2014-02-10 02:10:30 +01:00
/* Read all the main sections.. */
if ( parser . get_node_type ( ) ! = XMLParser : : NODE_ELEMENT )
continue ; //no idea what this may be, but skipping anyway
String section = parser . get_node_name ( ) ;
2017-03-05 16:44:50 +01:00
COLLADA_PRINT ( " section: " + section ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( section = = " asset " ) {
2014-02-10 02:10:30 +01:00
_parse_asset ( parser ) ;
} else if ( section . begins_with ( " library_ " ) ) {
_parse_library ( parser ) ;
2017-03-05 16:44:50 +01:00
} else if ( section = = " scene " ) {
2014-02-10 02:10:30 +01:00
_parse_scene ( parser ) ;
} else if ( ! parser . is_empty ( ) ) {
parser . skip_section ( ) ; // unknown section, likely headers
}
}
_optimize ( ) ;
return OK ;
}
Collada : : Collada ( ) {
}
# endif