Skip to main content

Post-processing tools 2023 R2

Test006.cpp

Last update: 17.04.2023

Animation.

This example shows how to add animation to a scene. Animations involve creating samplers that describe values of properties at defined times. The ANSYSViewer interpolates the property to generate values at times that fall between the defined times. The samplers are added to the animation as channels. The channels defined which property to animate.

Only some properties can be animated:

  • node - matrix
  • node - visible
  • material - Any value

In this example the matrix property and visible property of nodes are changed along with the color value of a material.

/*
* Copyright 2018-2021 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
*
* Restricted Rights Legend
*
* Use, duplication, or disclosure of this
* software and its documentation by the
* Government is subject to restrictions as
* set forth in subdivision [(b)(3)(ii)] of
* the Rights in Technical Data and Computer
* Software clause at 52.227-7013.
*/
#include <vector>
#include "GLTFWriter.h"
#include "test.h"
using namespace ANSYS::Nexus;
// Simple creation of animated lighted solid color box
TESTFUNC(Animation)
{
GLTFWriter::GLTF *gltf = GLTFWriter::GLTF::Create("MyApp", "1.0", functionName.c_str(), type);
if (!gltf)
throw std::runtime_error("Can't create GLTF");
// SCENE
GLTFWriter::Scene *scene = GLTFWriter::Scene::Create(gltf, "TestScene", "m", 1.0, GLTFWriter::Scene::BT_SOLID, 0.5, 0.5, 0.5);
if (!scene) {
throw std::runtime_error("Can't create scene");
}
GLTFWriter::Animation *animation = GLTFWriter::Animation::Create(gltf, "My Animation");
if (!animation) {
throw std::runtime_error("Can't create animation");
}
// create a reusable time
GLTFWriter::Attribute *time1 = 0;
{
float _time[3];
unsigned int i = 0;
_time[i++] = 0; // time 0
_time[i++] = 9; // time 1
_time[i++] = 18; // time 2
}
// ANIMATION SAMPLER1 for sampling matrix
GLTFWriter::AnimationSampler *sampler1 = 0;
{
float _mat[3 * 16];
unsigned int i = 0;
// time 0
GLTFWriter::Test::Matrix4 m1; // identity
for (unsigned int j = 0; j < 16; ++j)
_mat[i++] = (float)m1[j];
// time 1
GLTFWriter::Test::Matrix4 m2; // identity
GLTFWriter::Test::Matrix4 t2;
t2.LoadTranslate(GLTFWriter::Test::Vector3(0, 0, 0));
m2 *= t2;
GLTFWriter::Test::Matrix4 r2;
r2.LoadRotation(GLTFWriter::Test::Vector3(-1, 0, 0), GLTFWriter::Test::Vector3(-1, 0, 1), DegreesToRadians(-45));
m2 *= r2;
GLTFWriter::Test::Matrix4 s2;
s2.LoadScale(GLTFWriter::Test::Vector3(1, 1, 1));
m2 *= s2;
for (unsigned int j = 0; j < 16; ++j)
_mat[i++] = (float)m2[j];
// time 2
GLTFWriter::Test::Matrix4 m3; // identity
GLTFWriter::Test::Matrix4 t3;
t3.LoadTranslate(GLTFWriter::Test::Vector3(0, 0, 0));
m3 *= t3;
GLTFWriter::Test::Matrix4 r3;
r3.LoadRotation(GLTFWriter::Test::Vector3(-1, 0, 0), GLTFWriter::Test::Vector3(-1, 0, 1), DegreesToRadians(-90));
m3 *= r3;
GLTFWriter::Test::Matrix4 s3;
s3.LoadScale(GLTFWriter::Test::Vector3(1, 1, 1));
m3 *= s3;
for (unsigned int j = 0; j < 16; ++j)
_mat[i++] = (float)m3[j];
GLTFWriter::Attribute *matrix = GLTFWriter::Attribute::Create(gltf, "MATRIX", GLTFWriter::Attribute::AT_FLOAT_MAT4, 3, _mat);
sampler1 = GLTFWriter::AnimationSampler::Create(gltf, time1, matrix);
}
// ANIMATION SAMPLER2 for sampling matrix
GLTFWriter::AnimationSampler *sampler2 = 0;
{
float _mat[3 * 16];
unsigned int i = 0;
// time 0
GLTFWriter::Test::Matrix4 m0; // identity
for (unsigned int j = 0; j < 16; ++j)
_mat[i++] = (float)m0[j];
// time 1
GLTFWriter::Test::Matrix4 m1; // identity
GLTFWriter::Test::Matrix4 t1;
t1.LoadTranslate(GLTFWriter::Test::Vector3(0, 0, 0));
m1 *= t1;
GLTFWriter::Test::Matrix4 r1;
r1.LoadRotation(GLTFWriter::Test::Vector3(0, 0, 0), GLTFWriter::Test::Vector3(0, 0, 1), DegreesToRadians(45));
m1 *= r1;
GLTFWriter::Test::Matrix4 s1;
s1.LoadScale(GLTFWriter::Test::Vector3(3, 3, 3));
m1 *= s1;
for (unsigned int j = 0; j < 16; ++j)
_mat[i++] = (float)m1[j];
// time 2
GLTFWriter::Test::Matrix4 m2; // identity
GLTFWriter::Test::Matrix4 t2;
t2.LoadTranslate(GLTFWriter::Test::Vector3(0, 0, 0));
m2 *= t2;
GLTFWriter::Test::Matrix4 r2;
r2.LoadRotation(GLTFWriter::Test::Vector3(0, 0, 0), GLTFWriter::Test::Vector3(0, 0, 1), DegreesToRadians(90));
m2 *= r2;
GLTFWriter::Test::Matrix4 s2;
s2.LoadScale(GLTFWriter::Test::Vector3(1, 1, 1));
m2 *= s2;
for (unsigned int j = 0; j < 16; ++j)
_mat[i++] = (float)m2[j];
GLTFWriter::Attribute *matrix = GLTFWriter::Attribute::Create(gltf, "MATRIX", GLTFWriter::Attribute::AT_FLOAT_MAT4, 3, _mat);
sampler2 = GLTFWriter::AnimationSampler::Create(gltf, time1, matrix);
}
// ANIMATION SAMPLER3 for sampling color
GLTFWriter::AnimationSampler *sampler3 = 0;
{
float _time[3];
unsigned int i = 0;
_time[i++] = 4; // time 0
_time[i++] = 9; // time 1
_time[i++] = 14; // time 2
GLTFWriter::Attribute *time = GLTFWriter::Attribute::Create(gltf, "TIME", GLTFWriter::Attribute::AT_FLOAT, 3, _time);
float _col[3 * 4];
i = 0;
// time 0
_col[i++] = 1;
_col[i++] = 0;
_col[i++] = 0;
_col[i++] = 0.5;
// time 1
_col[i++] = 1;
_col[i++] = 1;
_col[i++] = 0;
_col[i++] = 0.75;
// time 2
_col[i++] = 0;
_col[i++] = 1;
_col[i++] = 0;
_col[i++] = 1;
GLTFWriter::Attribute *color = GLTFWriter::Attribute::Create(gltf, "COLOR", GLTFWriter::Attribute::AT_FLOAT_VEC4, 3, _col);
sampler3 = GLTFWriter::AnimationSampler::Create(gltf, time, color);
}
// ANIMATION SAMPLER4 for sampling visibility
GLTFWriter::AnimationSampler *sampler4 = 0;
{
float _time[2];
unsigned int i = 0;
_time[i++] = 3; // time 0
_time[i++] = 15; // time 1
GLTFWriter::Attribute *time = GLTFWriter::Attribute::Create(gltf, "TIME", GLTFWriter::Attribute::AT_FLOAT, 2, _time);
int _vis[2];
i = 0;
// time 0
_vis[i++] = 1;
// time 1
_vis[i++] = 0;
GLTFWriter::Attribute *visibility = GLTFWriter::Attribute::Create(gltf, "VISIBILITY", GLTFWriter::Attribute::AT_INT, 2, _vis);
sampler4 = GLTFWriter::AnimationSampler::Create(gltf, time, visibility);
}
// LIGHTS
{
// LIGHT NODE
GLTFWriter::Node *lightNode = GLTFWriter::Node::CreateLight(gltf);
if (!lightNode || !scene->SetLight(lightNode)) {
throw std::runtime_error("Can't create light");
}
// LIGHTS
GLTFWriter::Light *light1 = GLTFWriter::Light::CreateAmbient(gltf);
lightNode->AppendLight(light1);
GLTFWriter::Light *light2 = GLTFWriter::Light::CreateDirectional(gltf, 1, 1, 1, -1, -1, -3);
lightNode->AppendLight(light2);
}
// CAMERA
{
// CAMERA
GLTFWriter::Camera *camera = GLTFWriter::Camera::CreateOrthographic(gltf);
// CAMERA NODE
std::vector<double> mat(16);
mat[0] = 1;
mat[1] = 0;
mat[2] = 0;
mat[3] = 0;
mat[4] = 0;
mat[5] = 1;
mat[6] = 0;
mat[7] = 0;
mat[8] = 0;
mat[9] = 0;
mat[10] = 1;
mat[11] = 0;
mat[12] = 0;
mat[13] = 0;
mat[14] = 0;
mat[15] = 1;
GLTFWriter::Node *cameraNode = GLTFWriter::Node::CreateCamera(gltf, camera, "TestCamera", &mat[0]);
scene->SetCamera(cameraNode);
}
// MESH NODE
{
// NODE
std::vector<double> mat(16);
mat[0] = 1;
mat[1] = 0;
mat[2] = 0;
mat[3] = 0;
mat[4] = 0;
mat[5] = 1;
mat[6] = 0;
mat[7] = 0;
mat[8] = 0;
mat[9] = 0;
mat[10] = 1;
mat[11] = 0;
mat[12] = 0;
mat[13] = 0;
mat[14] = 0;
mat[15] = 1;
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "3D Box 1 with an extremely really long meaningless name", false, &mat[0]);
if (!node || !scene->AppendMesh(node)) {
throw std::runtime_error("Can't create mesh node");
}
// MESH
GLTFWriter::Mesh *mesh = GLTFWriter::Mesh::Create(gltf);
if (!mesh || !node->AppendMesh(mesh)) {
throw std::runtime_error("Can't create mesh");
}
// PRIMITIVE
std::pair<GLTFWriter::Primitive *, GLTFWriter::Material *> pm = GLTFWriter::Test::CreateSolidColoredBoxPrimitive(gltf, -1, 0, 0, 1, 1, 1, 1, 0, 0, 0.5);
mesh->AppendPrimitive(pm.first);
animation->AppendChannel(sampler1, node, "matrix");
animation->AppendChannel(sampler3, pm.second, "color");
animation->AppendChannel(sampler4, node, "visible");
}
// MESH NODE
{
GLTFWriter::Test::Matrix4 mat; // identity
mat.LoadTranslate(GLTFWriter::Test::Vector3(1, 0, 0));
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "3D Box 2", true, &(mat.Get())[0]);
if (!node || !scene->AppendMesh(node)) {
throw std::runtime_error("Can't create mesh node");
}
{
// NODE
std::vector<double> mat(16);
mat[0] = 1;
mat[1] = 0;
mat[2] = 0;
mat[3] = 0;
mat[4] = 0;
mat[5] = 1;
mat[6] = 0;
mat[7] = 0;
mat[8] = 0;
mat[9] = 0;
mat[10] = 1;
mat[11] = 0;
mat[12] = 0;
mat[13] = 0;
mat[14] = 0;
mat[15] = 1;
GLTFWriter::Node *n = GLTFWriter::Node::CreateMesh(gltf, "", true, &mat[0]);
if (!n || !node->AppendChild(n)) {
throw std::runtime_error("Can't create mesh node");
}
// MESH
GLTFWriter::Mesh *mesh = GLTFWriter::Mesh::Create(gltf);
if (!mesh || !n->AppendMesh(mesh)) {
throw std::runtime_error("Can't create mesh node");
}
// PRIMITIVE
std::pair<GLTFWriter::Primitive *, GLTFWriter::Material *> pm = GLTFWriter::Test::CreateSolidColoredBoxPrimitive(gltf, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0.5);
mesh->AppendPrimitive(pm.first);
animation->AppendChannel(sampler2, n, "matrix");
}
}
// MESH NODE
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "3D Box 3");
if (!node || !scene->AppendMesh(node)) {
throw std::runtime_error("Can't create mesh node");
}
// MESH
GLTFWriter::Mesh *mesh = GLTFWriter::Mesh::Create(gltf);
if (!mesh || !node->AppendMesh(mesh)) {
throw std::runtime_error("Can't create mesh");
}
// PRIMITIVE
std::pair<GLTFWriter::Primitive *, GLTFWriter::Material *> pm = GLTFWriter::Test::CreateSolidColoredBoxPrimitive(gltf, 0, 0, 0, 3, 1, 2, 1, 0.5, 0, 0.25);
mesh->AppendPrimitive(pm.first);
}
if (!gltf->Write()) {
throw std::runtime_error("Error creating file");
}
GLTFWriter::GLTF::GLTFError error = gltf->GetError();
throw std::runtime_error("Error creating file");
}
static AnimationSampler * Create(GLTF *gltf, Attribute *input, Attribute *output, AnimationSamplerType type=AST_LINEAR)
static Animation * Create(GLTF *gltf, const char *name=0)
static Attribute * Create(GLTF *gltf, const char *name, AttributeType type, unsigned int count, const int *data, Buffer *buffer=0)
static Camera * CreateOrthographic(GLTF *gltf, const char *name=0, float xmag=1, float ymag=1, float zfar=1000, float znear=-1000)
static void Destroy(GLTF *gltf)
static GLTF * Create(const char *application, const char *applicationVersion, const char *filePath, OutputType fileType=OT_AVZ)
static Light * CreateAmbient(GLTF *gltf, float colR=0.3, float colG=0.3, float colB=0.3)
static Light * CreateDirectional(GLTF *gltf, float colR=1, float colG=1, float colB=1, float dirX=0, float dirY=0, float dirZ=-1, float specColR=1, float specColG=1, float specColB=1, float shininess=100)
static Mesh * Create(GLTF *gltf)
static Node * CreateMesh(GLTF *gltf, const char *name=0, bool visible=true, const double *matrix=0)
static Node * CreateLight(GLTF *gltf)
static Node * CreateCamera(GLTF *gltf, Camera *camera, const char *name=0, const double *matrix=0)
static Scene * Create(GLTF *gltf, const char *name=0, const char *units=0, float scale=1, Scene::BackgroundType backgroundType=Scene::BT_NONE, float r1=0, float g1=0, float b1=0, float r2=0, float g2=0, float b2=0)
@ BT_SOLID
Make solid color from color 1.
Definition: GLTFScene.h:35