Skip to main content

Post-processing tools 2023 R2

Test004.cpp

Last update: 17.04.2023

Solid colored box.

This example shows how to create an object that is colored by a single value.

A perspective camera is added to the scene with a transformation that orients the scene in such a way that the object (box) is in an isometric like orientation. Perspective cameras are not recommended because of strange perspectives when the scene is zoomed out so the objects are small.

The tooltip in the viewer contains no texture coordinate value, since to textures are used.

/*
* 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;
// Lighted solid colored box
TESTFUNC(SolidColor)
{
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");
}
// 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::CreatePerspective(gltf);
// CAMERA NODE
std::vector<double> mat(16);
mat[0] = 0.635085;
mat[1] = 0;
mat[2] = -0.635085;
mat[3] = 0;
mat[4] = -0.366667;
mat[5] = 0.733333;
mat[6] = -0.366667;
mat[7] = 0;
mat[8] = 0.518545;
mat[9] = 0.518545;
mat[10] = 0.518545;
mat[11] = 0;
mat[12] = 2.59272;
mat[13] = 2.59272;
mat[14] = 2.59272;
mat[15] = 1;
GLTFWriter::Node *cameraNode = GLTFWriter::Node::CreateCamera(gltf, camera, "TestCamera", &mat[0]);
scene->SetCamera(cameraNode);
}
// MESH NODE
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "3D Box");
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, 1, 1, 1, 1, 0, 0, 0.5);
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 Camera * CreatePerspective(GLTF *gltf, const char *name=0, float yfov=22.619864948, float aspectRatio=1, float zfar=100000, float znear=0.1)
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