2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2010-2022 Blender Authors
|
2023-06-14 23:30:43 +10:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-02-27 20:30:35 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup collada
|
2011-02-27 20:30:35 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-01-26 20:08:04 +01:00
|
|
|
#include "COLLADAFWMatrix.h"
|
|
|
|
|
#include "COLLADAFWRotate.h"
|
|
|
|
|
#include "COLLADAFWScale.h"
|
|
|
|
|
#include "COLLADAFWTranslate.h"
|
2010-10-05 00:05:14 +00:00
|
|
|
|
|
|
|
|
#include "TransformReader.h"
|
|
|
|
|
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_matrix.h"
|
|
|
|
|
#include "BLI_math_rotation.h"
|
|
|
|
|
|
2012-06-20 16:43:48 +00:00
|
|
|
TransformReader::TransformReader(UnitConverter *conv) : unit_converter(conv)
|
|
|
|
|
{
|
|
|
|
|
/* pass */
|
2012-06-12 22:05:33 +00:00
|
|
|
}
|
2010-10-05 00:05:14 +00:00
|
|
|
|
2017-03-21 18:05:10 +01:00
|
|
|
void TransformReader::get_node_mat(float mat[4][4],
|
|
|
|
|
COLLADAFW::Node *node,
|
|
|
|
|
std::map<COLLADAFW::UniqueId, Animation> *animation_map,
|
|
|
|
|
Object *ob)
|
|
|
|
|
{
|
2020-11-06 17:49:09 +01:00
|
|
|
get_node_mat(mat, node, animation_map, ob, nullptr);
|
2017-03-21 18:05:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TransformReader::get_node_mat(float mat[4][4],
|
|
|
|
|
COLLADAFW::Node *node,
|
|
|
|
|
std::map<COLLADAFW::UniqueId, Animation> *animation_map,
|
|
|
|
|
Object *ob,
|
|
|
|
|
float parent_mat[4][4])
|
2010-10-05 00:05:14 +00:00
|
|
|
{
|
|
|
|
|
float cur[4][4];
|
|
|
|
|
float copy[4][4];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-05 00:05:14 +00:00
|
|
|
unit_m4(mat);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-25 17:04:52 +10:00
|
|
|
for (uint i = 0; i < node->getTransformations().getCount(); i++) {
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-05 00:05:14 +00:00
|
|
|
COLLADAFW::Transformation *tm = node->getTransformations()[i];
|
|
|
|
|
COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-02-26 21:58:06 +00:00
|
|
|
switch (type) {
|
|
|
|
|
case COLLADAFW::Transformation::MATRIX:
|
2013-07-29 17:27:27 +00:00
|
|
|
/* When matrix AND Trans/Rot/Scale are defined for a node,
|
|
|
|
|
* then this is considered as redundant information.
|
|
|
|
|
* So if we find a Matrix we use that and return. */
|
2013-02-26 21:58:06 +00:00
|
|
|
dae_matrix_to_mat4(tm, mat);
|
2017-03-21 18:05:10 +01:00
|
|
|
if (parent_mat) {
|
|
|
|
|
mul_m4_m4m4(mat, parent_mat, mat);
|
|
|
|
|
}
|
2013-02-26 21:58:06 +00:00
|
|
|
return;
|
|
|
|
|
case COLLADAFW::Transformation::TRANSLATE:
|
|
|
|
|
dae_translate_to_mat4(tm, cur);
|
|
|
|
|
break;
|
|
|
|
|
case COLLADAFW::Transformation::ROTATE:
|
|
|
|
|
dae_rotate_to_mat4(tm, cur);
|
|
|
|
|
break;
|
|
|
|
|
case COLLADAFW::Transformation::SCALE:
|
|
|
|
|
dae_scale_to_mat4(tm, cur);
|
|
|
|
|
break;
|
|
|
|
|
case COLLADAFW::Transformation::LOOKAT:
|
Partial rewrite of the Collada Module for Blender 2.8
Most important changes are in the Animation exporter and Animation Importer.
There is still some cleaning up to be done. But the Exporter/Importer basically
work within Blender 2.8
Some details:
User Interface:
The interface has been reorganized to look more like the FBX interface.
New options in user interface:
* keep_keyframes:
When sampling the distance between 2 keyframes is defined by
the sampling rate. Furthermore the keyframes defined in the
FCurves are not exported. However when this option is enabled
then also the defined keyframes will be added to the exported fcurves
* keep_smooth_curves:
When sampling we do not use FCurves. So we also have no Curve handles
for smooth exporting. However when this option is enabled, Blender
does its best to recreate the handles for export. This is a very
experimental feature and it is know to break when:
- the exported animated objects have parent inverse matrices
different from the unit matrix
- The exported objects have negative scaling
There may be many other situations when this feature breaks.
This needs to be further tested. It may be removed later or replaced
by something less wonky.
BlenderContext:
is a new class that contains the bridge to Blender. It contains
pointers to the current export/import context plus derived values
of Depsgraph, Scene, Main
Reporting:
I reorganized the output on the Blender Console to become more
informative and more readable
Preservation of Item names:
name attributes are now encoded with XML entities. This makes
sure that i can export/import names exactly defined in the tool.
This affects material names, bone names and object names.
Hierarchy export:
* Object and Bone Hierarchies are now exported correctly
by taking the Blender parent/child hierarchy into account
* Export also not selected intermediate objects
Problem:
When we export an Object Hierarchy, then we must export
all elements of the hierarchy to maintain the transforms. This
is especially important when exporting animated objects, because the
animation curves are exported as relative curves based on the
parent-child hierarchy. If an intermediate animated object is missing
then the exported animation breaks.
Solution:
If the "Selected" Optioon is enabled, then take care
to also export all objects which are not selected and hidden,
but which are parents of selected objects.
Node Based Material Importer (wip):
Added basic support for Materials with diffuse color and
diffuse textures. More properties (opacity, emission) need
changes in the used shader.
Note: Materials are all constructed by using the principled BSDF shader.
Animation Exporter:
* Massive optimization of the Animation Bake tool (Animation Sampler).
Instead of sampling each fcurve separately, i now sample all
exported fcurves simultaneously. So i avoid many (many!)
scene updates during animation export.
* Add support for Continuous Acceleration (Fcurve handles)
This allows us to create smoother FCurves during importing Collada
Animation curves. Possibly this should become an option ionstead of
a fixed import feature.
* Add support for sampling curves (to bake animations)
* The animation sampler now can be used for any animation curve.
Before the sampler only looked at curves which are supported by
Standard Collada 1.4. However the Collada exporter currently
ignores all animation curves which are not covered by the 1.4.1
Collada Standards. There is still some room for improvements
here (work in progres)
Known issues:
* Some exports do currently not work reliably, among those
are the camera animations, material animations and light animations
those animations will be added back next (work in progres)
* Exporting animation curves with keyframes (and tangents)
sometimes results in odd curves (when parent inverse matrix is involved)
This needs to be checked in more depth (probably it can not be solved).
* Export of "all animations in scene" is disabled because the
Collada Importer can not handle this reliably at the
moment (work in progres).
* Support for Animation Clip export
Added one extra level to the exported animations
such that now all scene animations are enclosed:
<Animation name="id_name(ob)_Action">
<Animation>...</Animation>
...
</Animation>
Animation Importer:
* Import of animations for objects with multiple materials
When importing multiple materials for one object,
the imported material animation curves have all been
assigned to the first material in the object.
Error handling (wip):
The Importer was a bit confused as it sometimes ignored fatal
parsing errors and continued to import. I did my best to
unconfuse it, but i believe that this needs to be tested more.
Refactoring:
update : move generation of effect id names into own function
update : adjust importer/exporter for no longer supported HEMI lights
cleanup: Removed no lopnger existing attribute from the exporter presets
cleanup: Removed not needed Context attribute from DocumentExporter
fix : Avoid duplicate deletion of temporary items
cleanup: fixed indentation and white space issues
update : Make BCAnimation class more self contained
cleanup: Renamed classes, updated comments for better reading
cleanup: Moved static class functions to collada_utils
cleanup: Moved typedefs to more intuitive locations
cleanup: indentation and class method declarations
cleanup: Removed no longer needed methods
update : Moved Classes into separate files
cleanup: Added comments
cleanup: take care of name conventions
... : many more small changes, not helpful to list them all
2018-11-23 15:57:45 +01:00
|
|
|
fprintf(stderr, "|! LOOKAT transformations are not supported yet.\n");
|
|
|
|
|
break;
|
2013-02-26 21:58:06 +00:00
|
|
|
case COLLADAFW::Transformation::SKEW:
|
Partial rewrite of the Collada Module for Blender 2.8
Most important changes are in the Animation exporter and Animation Importer.
There is still some cleaning up to be done. But the Exporter/Importer basically
work within Blender 2.8
Some details:
User Interface:
The interface has been reorganized to look more like the FBX interface.
New options in user interface:
* keep_keyframes:
When sampling the distance between 2 keyframes is defined by
the sampling rate. Furthermore the keyframes defined in the
FCurves are not exported. However when this option is enabled
then also the defined keyframes will be added to the exported fcurves
* keep_smooth_curves:
When sampling we do not use FCurves. So we also have no Curve handles
for smooth exporting. However when this option is enabled, Blender
does its best to recreate the handles for export. This is a very
experimental feature and it is know to break when:
- the exported animated objects have parent inverse matrices
different from the unit matrix
- The exported objects have negative scaling
There may be many other situations when this feature breaks.
This needs to be further tested. It may be removed later or replaced
by something less wonky.
BlenderContext:
is a new class that contains the bridge to Blender. It contains
pointers to the current export/import context plus derived values
of Depsgraph, Scene, Main
Reporting:
I reorganized the output on the Blender Console to become more
informative and more readable
Preservation of Item names:
name attributes are now encoded with XML entities. This makes
sure that i can export/import names exactly defined in the tool.
This affects material names, bone names and object names.
Hierarchy export:
* Object and Bone Hierarchies are now exported correctly
by taking the Blender parent/child hierarchy into account
* Export also not selected intermediate objects
Problem:
When we export an Object Hierarchy, then we must export
all elements of the hierarchy to maintain the transforms. This
is especially important when exporting animated objects, because the
animation curves are exported as relative curves based on the
parent-child hierarchy. If an intermediate animated object is missing
then the exported animation breaks.
Solution:
If the "Selected" Optioon is enabled, then take care
to also export all objects which are not selected and hidden,
but which are parents of selected objects.
Node Based Material Importer (wip):
Added basic support for Materials with diffuse color and
diffuse textures. More properties (opacity, emission) need
changes in the used shader.
Note: Materials are all constructed by using the principled BSDF shader.
Animation Exporter:
* Massive optimization of the Animation Bake tool (Animation Sampler).
Instead of sampling each fcurve separately, i now sample all
exported fcurves simultaneously. So i avoid many (many!)
scene updates during animation export.
* Add support for Continuous Acceleration (Fcurve handles)
This allows us to create smoother FCurves during importing Collada
Animation curves. Possibly this should become an option ionstead of
a fixed import feature.
* Add support for sampling curves (to bake animations)
* The animation sampler now can be used for any animation curve.
Before the sampler only looked at curves which are supported by
Standard Collada 1.4. However the Collada exporter currently
ignores all animation curves which are not covered by the 1.4.1
Collada Standards. There is still some room for improvements
here (work in progres)
Known issues:
* Some exports do currently not work reliably, among those
are the camera animations, material animations and light animations
those animations will be added back next (work in progres)
* Exporting animation curves with keyframes (and tangents)
sometimes results in odd curves (when parent inverse matrix is involved)
This needs to be checked in more depth (probably it can not be solved).
* Export of "all animations in scene" is disabled because the
Collada Importer can not handle this reliably at the
moment (work in progres).
* Support for Animation Clip export
Added one extra level to the exported animations
such that now all scene animations are enclosed:
<Animation name="id_name(ob)_Action">
<Animation>...</Animation>
...
</Animation>
Animation Importer:
* Import of animations for objects with multiple materials
When importing multiple materials for one object,
the imported material animation curves have all been
assigned to the first material in the object.
Error handling (wip):
The Importer was a bit confused as it sometimes ignored fatal
parsing errors and continued to import. I did my best to
unconfuse it, but i believe that this needs to be tested more.
Refactoring:
update : move generation of effect id names into own function
update : adjust importer/exporter for no longer supported HEMI lights
cleanup: Removed no lopnger existing attribute from the exporter presets
cleanup: Removed not needed Context attribute from DocumentExporter
fix : Avoid duplicate deletion of temporary items
cleanup: fixed indentation and white space issues
update : Make BCAnimation class more self contained
cleanup: Renamed classes, updated comments for better reading
cleanup: Moved static class functions to collada_utils
cleanup: Moved typedefs to more intuitive locations
cleanup: indentation and class method declarations
cleanup: Removed no longer needed methods
update : Moved Classes into separate files
cleanup: Added comments
cleanup: take care of name conventions
... : many more small changes, not helpful to list them all
2018-11-23 15:57:45 +01:00
|
|
|
fprintf(stderr, "|! SKEW transformations are not supported yet.\n");
|
2013-02-26 21:58:06 +00:00
|
|
|
break;
|
2013-01-21 13:45:49 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-02-26 21:58:06 +00:00
|
|
|
copy_m4_m4(copy, mat);
|
2013-05-26 18:36:25 +00:00
|
|
|
mul_m4_m4m4(mat, copy, cur);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-05 00:05:14 +00:00
|
|
|
if (animation_map) {
|
|
|
|
|
/* AnimationList that drives this Transformation */
|
|
|
|
|
const COLLADAFW::UniqueId &anim_list_id = tm->getAnimationList();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-05 00:05:14 +00:00
|
|
|
/* store this so later we can link animation data with ob */
|
|
|
|
|
Animation anim = {ob, node, tm};
|
|
|
|
|
(*animation_map)[anim_list_id] = anim;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-03-21 18:05:10 +01:00
|
|
|
if (parent_mat) {
|
|
|
|
|
mul_m4_m4m4(mat, parent_mat, mat);
|
|
|
|
|
}
|
2010-10-05 00:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-11 14:29:01 +00:00
|
|
|
void TransformReader::dae_rotate_to_mat4(COLLADAFW::Transformation *tm, float m[4][4])
|
2010-10-05 00:05:14 +00:00
|
|
|
{
|
2012-06-12 22:05:33 +00:00
|
|
|
COLLADAFW::Rotate *ro = (COLLADAFW::Rotate *)tm;
|
2010-10-05 00:05:14 +00:00
|
|
|
COLLADABU::Math::Vector3 &axis = ro->getRotationAxis();
|
2022-09-25 18:33:28 +10:00
|
|
|
const float angle = float(DEG2RAD(ro->getRotationAngle()));
|
|
|
|
|
const float ax[] = {float(axis[0]), float(axis[1]), float(axis[2])};
|
2021-07-20 15:01:03 +10:00
|
|
|
#if 0
|
|
|
|
|
float quat[4];
|
|
|
|
|
axis_angle_to_quat(quat, axis, angle);
|
|
|
|
|
quat_to_mat4(m, quat);
|
|
|
|
|
#endif
|
2010-10-05 00:05:14 +00:00
|
|
|
axis_angle_to_mat4(m, ax, angle);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-11 14:29:01 +00:00
|
|
|
void TransformReader::dae_translate_to_mat4(COLLADAFW::Transformation *tm, float m[4][4])
|
2010-10-05 00:05:14 +00:00
|
|
|
{
|
2012-06-12 22:05:33 +00:00
|
|
|
COLLADAFW::Translate *tra = (COLLADAFW::Translate *)tm;
|
2010-10-05 00:05:14 +00:00
|
|
|
COLLADABU::Math::Vector3 &t = tra->getTranslation();
|
|
|
|
|
|
|
|
|
|
unit_m4(m);
|
|
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
m[3][0] = float(t[0]);
|
|
|
|
|
m[3][1] = float(t[1]);
|
|
|
|
|
m[3][2] = float(t[2]);
|
2010-10-05 00:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-11 14:29:01 +00:00
|
|
|
void TransformReader::dae_scale_to_mat4(COLLADAFW::Transformation *tm, float m[4][4])
|
2010-10-05 00:05:14 +00:00
|
|
|
{
|
2012-06-12 22:05:33 +00:00
|
|
|
COLLADABU::Math::Vector3 &s = ((COLLADAFW::Scale *)tm)->getScale();
|
2022-09-25 18:33:28 +10:00
|
|
|
float size[3] = {float(s[0]), float(s[1]), float(s[2])};
|
2010-10-05 00:05:14 +00:00
|
|
|
size_to_mat4(m, size);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-11 14:29:01 +00:00
|
|
|
void TransformReader::dae_matrix_to_mat4(COLLADAFW::Transformation *tm, float m[4][4])
|
2010-10-05 00:05:14 +00:00
|
|
|
{
|
2020-09-04 12:26:24 +02:00
|
|
|
UnitConverter::dae_matrix_to_mat4_(m, ((COLLADAFW::Matrix *)tm)->getMatrix());
|
2010-10-05 00:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TransformReader::dae_translate_to_v3(COLLADAFW::Transformation *tm, float v[3])
|
|
|
|
|
{
|
2012-06-12 22:05:33 +00:00
|
|
|
dae_vector3_to_v3(((COLLADAFW::Translate *)tm)->getTranslation(), v);
|
2010-10-05 00:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TransformReader::dae_scale_to_v3(COLLADAFW::Transformation *tm, float v[3])
|
|
|
|
|
{
|
2012-06-12 22:05:33 +00:00
|
|
|
dae_vector3_to_v3(((COLLADAFW::Scale *)tm)->getScale(), v);
|
2010-10-05 00:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TransformReader::dae_vector3_to_v3(const COLLADABU::Math::Vector3 &v3, float v[3])
|
|
|
|
|
{
|
|
|
|
|
v[0] = v3.x;
|
|
|
|
|
v[1] = v3.y;
|
|
|
|
|
v[2] = v3.z;
|
2010-10-05 00:49:39 +00:00
|
|
|
}
|