Skip to main content

Post-processing tools 2023 R2

Test013.cpp

Last update: 17.04.2023

Legends

This example demonstrates the use of legends.

Several legends are created demonstrating different options to the legend object.

/*
* 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 <cmath>
#ifdef _WIN32
#include <codecvt>
#endif
#include "GLTFWriter.h"
#include "test.h"
using namespace ANSYS::Nexus;
namespace {
#ifdef _WIN32
std::string
ConvertToUtf8String(const std::wstring& wstr)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.to_bytes(wstr);
}
#endif
};
// Legends
TESTFUNC(Legends)
{
GLTFWriter::GLTF *gltf = GLTFWriter::GLTF::Create("MyApp", "1.0", functionName.c_str(), type);
if (!gltf)
throw std::runtime_error("Can't create GLTF");
// SCENE
#ifdef _WIN32
GLTFWriter::Scene *scene = GLTFWriter::Scene::Create(gltf, ConvertToUtf8String(L"ԎҿșϯϨҁęɳę").c_str(), "", 0, GLTFWriter::Scene::BT_TB, 0.42F, 0.55F, 0.871F, 1, 1, 1);
#else
GLTFWriter::Scene *scene = GLTFWriter::Scene::Create(gltf, "Testscene", "m", 1.0F, GLTFWriter::Scene::BT_TB, 0.42F, 0.55F, 0.871F, 1, 1, 1);
#endif
if (!scene) {
throw std::runtime_error("Can't create scene");
}
// CAMERA
{
// CAMERA
GLTFWriter::Camera *camera = GLTFWriter::Camera::CreateOrthographic(gltf);
// CAMERA NODE
std::vector<double> mat(16);
mat[0] = 0;
mat[1] = 1;
mat[2] = 0;
mat[3] = 0;
mat[4] = 0;
mat[5] = 0;
mat[6] = 1;
mat[7] = 0;
mat[8] = 1;
mat[9] = 0;
mat[10] = 0;
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);
}
// banded texture
{
float mn = 1.5;
float mx = 2.5;
float range = mx - mn;
// TEXTURE
std::vector<unsigned char> colors;
colors.push_back(0); colors.push_back(0); colors.push_back(255); // blue
colors.push_back(0); colors.push_back(255); colors.push_back(255); // cyan
colors.push_back(0); colors.push_back(255); colors.push_back(0); // green
colors.push_back(255); colors.push_back(255); colors.push_back(0); // yellow
colors.push_back(255); colors.push_back(0); colors.push_back(0); // red
GLTFWriter::Texture *bandedTexture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGB, (unsigned int)colors.size() / 3, &colors[0], false);
// generate vertices, and texture coordinates for a ball
std::vector<float> vertices;
std::vector<unsigned int> lines;
std::vector<unsigned int> triangles;
GLTFWriter::Test::MakeSphere(0, -1, 0, 0.5, 64, vertices, lines, triangles);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
const unsigned int numLines = (unsigned int)lines.size() / 2;
const unsigned int numTriangles = (unsigned int)triangles.size() / 3;
std::vector<float> textureCoords(numVertices);
for (unsigned int i = 0; i < numVertices; ++i)
textureCoords[i] = (vertices[3 * i + 2] + 0.5F) * range + mn; // coordinates go from mn to mx
// NODE
{
// LEGEND
// make this banded colors
std::vector<float> valueLocation;
//valueLocation.push_back(1, mx);
GLTFWriter::Legend *legend = GLTFWriter::Legend::Create(gltf, GLTFWriter::Legend::LO_VERTICAL, 0.75, bandedTexture, mn, mx, GLTFWriter::Legend::LA_X_YCENTER,
#ifdef _WIN32
ConvertToUtf8String(L"Z Ƥṏᴤΐṱϊӧת\nBanded").c_str(),
#else
"Z Position\nBanded",
#endif
"m", "This is an example\nbanded legend\nX, Y Center", 6, 0,
true, false, 0.05F, 0, 0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F, 0.25F);
// BUFFER - combine attributes into a single buffer
GLTFWriter::Buffer *buffer = GLTFWriter::Buffer::Create(gltf);
if (!buffer) {
throw std::runtime_error("Can't create buffer");
}
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Banded");
if (!node || !scene->AppendMesh(node)) {
throw std::runtime_error("Can't create mesh node");
}
// LEGEND NODE
#ifdef _WIN32
GLTFWriter::Node *legendNode = GLTFWriter::Node::CreateLegend(gltf, legend, ConvertToUtf8String(L"Ḷḙḡḗṋḍ").c_str());
#else
GLTFWriter::Node *legendNode = GLTFWriter::Node::CreateLegend(gltf, legend, "Legend");
#endif
node->AppendChild(legendNode);
// MESH NODE
GLTFWriter::Node *meshNode = GLTFWriter::Node::CreateMesh(gltf, "Mesh");
if (!meshNode || !node->AppendChild(meshNode)) {
throw std::runtime_error("Can't create mesh node");
}
// MESH
GLTFWriter::Mesh *mesh = GLTFWriter::Mesh::Create(gltf);
if (!mesh || !meshNode->AppendMesh(mesh)) {
throw std::runtime_error("Can't create mesh");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
// TECHNIQUE STATES
throw std::runtime_error("Can't create technique");
}
// MATERIAL
GLTFWriter::Material *material = GLTFWriter::Material::Create(gltf, technique);
if (!material ||
// MATERIAL UNIFORM VALUES
!material->AppendValue(GLTFWriter::Value::Create(gltf, "texture0", bandedTexture->GetID()))) { // texture0 name is auto-generated from Attribute TEXCOORD0
throw std::runtime_error("Can't create material");
}
// repack unsigned int indices into multiple sets of unsigned short indices
GLTFWriter::Utils::Repack *repack = GLTFWriter::Utils::Repack::Create(numTriangles, 3, &triangles[0]);
if (!repack) {
throw std::runtime_error("Can't create repack");
}
for (unsigned int i = 0; i < repack->NumPacks(); ++i) {
const float *packedData;
unsigned int numAttributes = repack->NumAttributes(i);
// POSITION ATTRIBUTE
packedData = repack->ConstructRepackAttribute(&triangles[0], 3, &vertices[0], i);
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::Create(gltf, "POSITION", GLTFWriter::Attribute::AT_FLOAT_VEC3, numAttributes, packedData, buffer);
// TEXCOORD ATTRIBUTE
packedData = repack->ConstructRepackAttribute(&triangles[0], 1, &textureCoords[0], i);
GLTFWriter::Attribute *texture = GLTFWriter::Attribute::Create(gltf, "TEXCOORD0", GLTFWriter::Attribute::AT_FLOAT, numAttributes, packedData, buffer);
// INDICES
unsigned int numIndices = 3U * repack->NumElements(i);
const unsigned short *packedIndex = repack->ConstructRepackIndex(&triangles[0], i);
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, numIndices, packedIndex, buffer);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLES, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(texture)) {
throw std::runtime_error("Can't create primitive");
}
}
}
}
// gradient texture
{
float mn = -0.5;
float mx = 0.5;
float range = mx - mn;
// TEXTURE
std::vector<unsigned char> colors;
colors.push_back(0); colors.push_back(0); colors.push_back(255); // blue
colors.push_back(0); colors.push_back(255); colors.push_back(255); // cyan
colors.push_back(0); colors.push_back(255); colors.push_back(0); // green
colors.push_back(255); colors.push_back(255); colors.push_back(0); // yellow
colors.push_back(255); colors.push_back(0); colors.push_back(0); // red
GLTFWriter::Texture *smoothTexture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGB, (unsigned int)colors.size() / 3, &colors[0]);
// generate vertices, and texture coordinates for a ball
std::vector<float> vertices;
std::vector<unsigned int> lines;
std::vector<unsigned int> triangles;
GLTFWriter::Test::MakeSphere(0, 1, 0, 0.5, 64, vertices, lines, triangles);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
const unsigned int numLines = (unsigned int)lines.size() / 2;
const unsigned int numTriangles = (unsigned int)triangles.size() / 3;
std::vector<float> textureCoords(numVertices);
for (unsigned int i = 0; i < numVertices; ++i)
textureCoords[i] = (vertices[3 * i + 2] + 0.5F) * range + mn; // coordinates go from mn to mx
// NODE
{
// LEGEND
// make this gradient colors
std::vector<float> valueLocation;
//valueLocation.push_back(1, mx);
GLTFWriter::Legend *legend = GLTFWriter::Legend::Create(gltf, GLTFWriter::Legend::LO_VERTICAL, 0.333F, smoothTexture, mn, mx, GLTFWriter::Legend::LA_XRIGHT_YCENTER,
#ifdef _WIN32
ConvertToUtf8String(L"Z Ƥṏᴤΐṱϊӧת\nSmooth").c_str(),
#else
"Z Position\nSmooth",
#endif
"m", "This is an example\ngradient legend\nX Right, Y Center", 0, 0,
false, false);
// BUFFER - combine attributes into a single buffer
GLTFWriter::Buffer *buffer = GLTFWriter::Buffer::Create(gltf);
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Linear");
if (!node || !scene->AppendMesh(node)) {
throw std::runtime_error("Can't create mesh node");
}
// LEGEND NODE
#ifdef _WIN32
GLTFWriter::Node *legendNode = GLTFWriter::Node::CreateLegend(gltf, legend, ConvertToUtf8String(L"Ḷḙḡḗṋḍ").c_str());
#else
GLTFWriter::Node *legendNode = GLTFWriter::Node::CreateLegend(gltf, legend, "Legend");
#endif
node->AppendChild(legendNode);
// MESH NODE
GLTFWriter::Node *meshNode = GLTFWriter::Node::CreateMesh(gltf, "Mesh");
if (!meshNode || !node->AppendChild(meshNode)) {
throw std::runtime_error("Can't create mesh node");
}
// MESH
GLTFWriter::Mesh *mesh = GLTFWriter::Mesh::Create(gltf);
if (!mesh || !meshNode->AppendMesh(mesh)) {
throw std::runtime_error("Can't create mesh");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
// TECHNIQUE STATES
throw std::runtime_error("Can't create technique");
}
// MATERIAL
GLTFWriter::Material *material = GLTFWriter::Material::Create(gltf, technique);
if (!material ||
// MATERIAL UNIFORM VALUES
!material->AppendValue(GLTFWriter::Value::Create(gltf, "texture0", smoothTexture->GetID()))) { // texture0 name is auto-generated from Attribute TEXCOORD0
throw std::runtime_error("Can't create material");
}
// repack unsigned int indices into multiple sets of unsigned short indices
GLTFWriter::Utils::Repack *repack = GLTFWriter::Utils::Repack::Create(numTriangles, 3, &triangles[0]);
if (!repack) {
throw std::runtime_error("Can't create repack");
}
for (unsigned int i = 0; i < repack->NumPacks(); ++i) {
const float *packedData;
unsigned int numAttributes = repack->NumAttributes(i);
// POSITION ATTRIBUTE
packedData = repack->ConstructRepackAttribute(&triangles[0], 3, &vertices[0], i);
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::Create(gltf, "POSITION", GLTFWriter::Attribute::AT_FLOAT_VEC3, numAttributes, packedData, buffer);
// TEXCOORD ATTRIBUTE
packedData = repack->ConstructRepackAttribute(&triangles[0], 1, &textureCoords[0], i);
GLTFWriter::Attribute *texture = GLTFWriter::Attribute::Create(gltf, "TEXCOORD0", GLTFWriter::Attribute::AT_FLOAT, numAttributes, packedData, buffer);
// INDICES
unsigned int numIndices = 3U * repack->NumElements(i);
const unsigned short *packedIndex = repack->ConstructRepackIndex(&triangles[0], i);
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, numIndices, packedIndex, buffer);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLES, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(texture)) {
throw std::runtime_error("Can't create primitive");
}
}
}
}
// banded log texture
{
float mn = 10;
float mx = 100;
float range = mx - mn;
// TEXTURE
std::vector<unsigned char> colors;
colors.push_back(0); colors.push_back(0); colors.push_back(255); // blue
colors.push_back(0); colors.push_back(255); colors.push_back(255); // cyan
colors.push_back(0); colors.push_back(255); colors.push_back(0); // green
colors.push_back(255); colors.push_back(255); colors.push_back(0); // yellow
colors.push_back(255); colors.push_back(0); colors.push_back(0); // red
std::vector<float> bandLocations; // fractions from 0 to 1
bandLocations.push_back(0);
bandLocations.push_back(0.064988132F);
bandLocations.push_back(0.167987381F);
bandLocations.push_back(0.33123019F);
bandLocations.push_back(0.589952605F);
GLTFWriter::Texture *bandedTexture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGB, (unsigned int)colors.size() / 3, &colors[0], false, (unsigned int)bandLocations.size(), &bandLocations[0]);
// generate vertices, and texture coordinates for a ball
std::vector<float> vertices;
std::vector<unsigned int> lines;
std::vector<unsigned int> triangles;
GLTFWriter::Test::MakeSphere(0, 0, -1, 0.5, 64, vertices, lines, triangles);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
const unsigned int numLines = (unsigned int)lines.size() / 2;
const unsigned int numTriangles = (unsigned int)triangles.size() / 3;
std::vector<float> textureCoords(numVertices);
for (unsigned int i = 0; i < numVertices; ++i)
textureCoords[i] = (vertices[3 * i + 1] + 0.5F) * range + mn; // coordinates go from mn to mx
// NODE
{
// LEGEND
// make this logarithmic with banded colors
std::vector<float> valueLocations; // pairs of numbers, both of which are fractions from 0 to 1
valueLocations.push_back(0); valueLocations.push_back(0);
valueLocations.push_back((15.84893192461114F - mn) / range); valueLocations.push_back(0.2F);
valueLocations.push_back((25.1188643150958F - mn) / range); valueLocations.push_back(0.4F);
valueLocations.push_back((39.8107170553498F - mn) / range); valueLocations.push_back(0.6F);
valueLocations.push_back((63.0957344480194F - mn) / range); valueLocations.push_back(0.8F);
valueLocations.push_back(1); valueLocations.push_back(1);
GLTFWriter::Legend *legend = GLTFWriter::Legend::Create(gltf, GLTFWriter::Legend::LO_HORIZONTAL, 0.75, bandedTexture, mn, mx, GLTFWriter::Legend::LA_XCENTER_YBOTTOM,
#ifdef _WIN32
ConvertToUtf8String(L"Y Ƥṏᴤΐṱϊӧת\nBanded").c_str(),
#else
"Y Position\nBanded",
#endif
"m", "User specified\nbanded log legend\nX Center, Y Bottom", 6, &valueLocations[0],
false, false, 0, 0, 0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F, 0.25F);
// BUFFER - combine attributes into a single buffer
GLTFWriter::Buffer *buffer = GLTFWriter::Buffer::Create(gltf);
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Banded Log User Specified");
if (!node || !scene->AppendMesh(node)) {
throw std::runtime_error("Can't create mesh node");
}
// LEGEND NODE
#ifdef _WIN32
GLTFWriter::Node *legendNode = GLTFWriter::Node::CreateLegend(gltf, legend, ConvertToUtf8String(L"Ḷḙḡḗṋḍ").c_str());
#else
GLTFWriter::Node *legendNode = GLTFWriter::Node::CreateLegend(gltf, legend, "Legend");
#endif
node->AppendChild(legendNode);
// MESH NODE
GLTFWriter::Node *meshNode = GLTFWriter::Node::CreateMesh(gltf, "Mesh");
if (!meshNode || !node->AppendChild(meshNode)) {
throw std::runtime_error("Can't create mesh node");
}
// MESH
GLTFWriter::Mesh *mesh = GLTFWriter::Mesh::Create(gltf);
if (!mesh || !meshNode->AppendMesh(mesh)) {
throw std::runtime_error("Can't create mesh");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
// TECHNIQUE STATES
throw std::runtime_error("Can't create technique");
}
// MATERIAL
GLTFWriter::Material *material = GLTFWriter::Material::Create(gltf, technique);
if (!material ||
// MATERIAL UNIFORM VALUES
!material->AppendValue(GLTFWriter::Value::Create(gltf, "texture0", bandedTexture->GetID()))) { // texture0 name is auto-generated from Attribute TEXCOORD0
throw std::runtime_error("Can't create material");
}
// repack unsigned int indices into multiple sets of unsigned short indices
GLTFWriter::Utils::Repack *repack = GLTFWriter::Utils::Repack::Create(numTriangles, 3, &triangles[0]);
if (!repack) {
throw std::runtime_error("Can't create repack");
}
for (unsigned int i = 0; i < repack->NumPacks(); ++i) {
const float *packedData;
unsigned int numAttributes = repack->NumAttributes(i);
// POSITION ATTRIBUTE
packedData = repack->ConstructRepackAttribute(&triangles[0], 3, &vertices[0], i);
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::Create(gltf, "POSITION", GLTFWriter::Attribute::AT_FLOAT_VEC3, numAttributes, packedData, buffer);
// TEXCOORD ATTRIBUTE
packedData = repack->ConstructRepackAttribute(&triangles[0], 1, &textureCoords[0], i);
GLTFWriter::Attribute *texture = GLTFWriter::Attribute::Create(gltf, "TEXCOORD0", GLTFWriter::Attribute::AT_FLOAT, numAttributes, packedData, buffer);
// INDICES
unsigned int numIndices = 3U * repack->NumElements(i);
const unsigned short *packedIndex = repack->ConstructRepackIndex(&triangles[0], i);
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, numIndices, packedIndex, buffer);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLES, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(texture)) {
throw std::runtime_error("Can't create primitive");
}
}
}
}
// gradient log texture
// we need to fudge the texture, because auto generated shaders do not support logging of data
{
float mn = 10;
float mx = 100;
float range = mx - mn;
// TEXTURE
std::vector<unsigned char> colors;
colors.push_back(0); colors.push_back(0); colors.push_back(255); // blue
colors.push_back(0); colors.push_back(255); colors.push_back(255); // cyan
colors.push_back(0); colors.push_back(255); colors.push_back(0); // green
colors.push_back(255); colors.push_back(255); colors.push_back(0); // yellow
colors.push_back(255); colors.push_back(0); colors.push_back(0); // red
// create a gradient texture that is really made up of bands
const unsigned int numBands = 256;
std::vector<float> bandLocations; // fractions from 0 to 1
for (unsigned int i = 0; i < numBands; ++i)
bandLocations.push_back((float)(pow(10.0, (double)i / numBands) * 10.0 - mn) / range);
GLTFWriter::Texture *smoothTexture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGB, (unsigned int)colors.size() / 3, &colors[0], false, (unsigned int)bandLocations.size(), &bandLocations[0]);
// generate vertices, and texture coordinates for a ball
std::vector<float> vertices;
std::vector<unsigned int> lines;
std::vector<unsigned int> triangles;
GLTFWriter::Test::MakeSphere(0, 0, 1, 0.5, 64, vertices, lines, triangles);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
const unsigned int numLines = (unsigned int)lines.size() / 2;
const unsigned int numTriangles = (unsigned int)triangles.size() / 3;
std::vector<float> textureCoords(numVertices);
for (unsigned int i = 0; i < numVertices; ++i)
textureCoords[i] = (vertices[3 * i + 1] + 0.5F) * range + mn; // coordinates go from mn to mx
// NODE
{
// LEGEND
GLTFWriter::Legend *legend = GLTFWriter::Legend::Create(gltf, GLTFWriter::Legend::LO_HORIZONTAL, 0.333F, smoothTexture, mn, mx, GLTFWriter::Legend::LA_XCENTER_YTOP,
#ifdef _WIN32
ConvertToUtf8String(L"Y Ƥṏᴤΐṱϊӧת\nGradient").c_str(),
#else
"Y Position\nGradient",
#endif
"m", "This is an example\ngradient log legend\nX Center, Y Top", 0, 0,
true, true, 0, 0, 0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F, 0.25F);
// BUFFER - combine attributes into a single buffer
GLTFWriter::Buffer *buffer = GLTFWriter::Buffer::Create(gltf);
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Gradient Log");
if (!node || !scene->AppendMesh(node)) {
throw std::runtime_error("Can't create mesh node");
}
// LEGEND NODE
#ifdef _WIN32
GLTFWriter::Node *legendNode = GLTFWriter::Node::CreateLegend(gltf, legend, ConvertToUtf8String(L"Ḷḙḡḗṋḍ").c_str());
#else
GLTFWriter::Node *legendNode = GLTFWriter::Node::CreateLegend(gltf, legend, "Legend");
#endif
node->AppendChild(legendNode);
// MESH NODE
GLTFWriter::Node *meshNode = GLTFWriter::Node::CreateMesh(gltf, "Mesh");
if (!meshNode || !node->AppendChild(meshNode)) {
throw std::runtime_error("Can't create mesh node");
}
// MESH
GLTFWriter::Mesh *mesh = GLTFWriter::Mesh::Create(gltf);
if (!mesh || !meshNode->AppendMesh(mesh)) {
throw std::runtime_error("Can't create mesh");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
// TECHNIQUE STATES
throw std::runtime_error("Can't create technique");
}
// MATERIAL
GLTFWriter::Material *material = GLTFWriter::Material::Create(gltf, technique);
if (!material ||
// MATERIAL UNIFORM VALUES
!material->AppendValue(GLTFWriter::Value::Create(gltf, "texture0", smoothTexture->GetID()))) { // texture0 name is auto-generated from Attribute TEXCOORD0
throw std::runtime_error("Can't create material");
}
// repack unsigned int indices into multiple sets of unsigned short indices
GLTFWriter::Utils::Repack *repack = GLTFWriter::Utils::Repack::Create(numTriangles, 3, &triangles[0]);
if (!repack) {
throw std::runtime_error("Can't create repack");
}
for (unsigned int i = 0; i < repack->NumPacks(); ++i) {
const float *packedData;
unsigned int numAttributes = repack->NumAttributes(i);
// POSITION ATTRIBUTE
packedData = repack->ConstructRepackAttribute(&triangles[0], 3, &vertices[0], i);
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::Create(gltf, "POSITION", GLTFWriter::Attribute::AT_FLOAT_VEC3, numAttributes, packedData, buffer);
// TEXCOORD ATTRIBUTE
packedData = repack->ConstructRepackAttribute(&triangles[0], 1, &textureCoords[0], i);
GLTFWriter::Attribute *texture = GLTFWriter::Attribute::Create(gltf, "TEXCOORD0", GLTFWriter::Attribute::AT_FLOAT, numAttributes, packedData, buffer);
texture->SetLogarithmic(true);
// INDICES
unsigned int numIndices = 3U * repack->NumElements(i);
const unsigned short *packedIndex = repack->ConstructRepackIndex(&triangles[0], i);
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, numIndices, packedIndex, buffer);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLES, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(texture)) {
throw std::runtime_error("Can't create primitive");
}
}
}
}
if (!gltf->Write()) {
throw std::runtime_error("Error creating file");
}
GLTFWriter::GLTF::GLTFError error = gltf->GetError();
throw std::runtime_error("Error creating file");
}
static Attribute * Create(GLTF *gltf, const char *name, AttributeType type, unsigned int count, const int *data, Buffer *buffer=0)
static Buffer * Create(GLTF *gltf)
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 Index * Create(GLTF *gltf, unsigned int count, const unsigned short *indices, Buffer *buffer=0)
static Legend * Create(GLTF *gltf, LegendOrientation orientation, float size, Texture *texture, float mn, float mx, LegendAttachment attachment=LA_XLEFT_YCENTER, const char *title=0, const char *units=0, const char *other=0, unsigned int numTicks=0, const float *valueLocations=0, bool border=false, bool logarithmic=false, float x=0, float y=0, float foregroundR=0, float foregroundG=0, float foregroundB=0, float backgroundR=1, float backgroundG=1, float backgroundB=1, float backgroundA=0)
@ LA_XCENTER_YBOTTOM
Attach to bottom and position at horizontal center.
Definition: GLTFLegend.h:51
@ LA_XCENTER_YTOP
Attach to top and position at horizontal center.
Definition: GLTFLegend.h:52
@ LA_XRIGHT_YCENTER
Attach to right and position at vertical center.
Definition: GLTFLegend.h:49
@ LA_X_YCENTER
Use x location and position at vertical center.
Definition: GLTFLegend.h:41
static Material * Create(GLTF *gltf, Technique *technique)
static Mesh * Create(GLTF *gltf)
static Node * CreateMesh(GLTF *gltf, const char *name=0, bool visible=true, const double *matrix=0)
static Node * CreateLegend(GLTF *gltf, Legend *legend, const char *name=0, bool visible=true)
static Node * CreateCamera(GLTF *gltf, Camera *camera, const char *name=0, const double *matrix=0)
@ PT_TRIANGLES
Treats each triplet of vertices as an independent triangle.
Definition: GLTFMesh.h:39
static Primitive * Create(GLTF *gltf, PrimitiveType type, Material *material, Index *indices=0, bool isPickable=true)
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_TB
Make gradient from color 1 top to color 2 bottom.
Definition: GLTFScene.h:36
static State * Create(GLTF *gltf, StateType type, double v1, double v2=0, double v3=0, double v4=0)
@ ST_DEPTHTESTENABLE
Enables testing of depth buffer. Possible values are:
static Technique * Create(GLTF *gltf, Program *program=0, bool is2D=false)
static Texture * Create(GLTF *gltf, TextureFormat format, TextureTarget target, Sampler *sampler, Image *image)
@ TF_RGB
3 unsigned chars per pixel.
Definition: GLTFTexture.h:34
static Repack * Create(unsigned int elementCount, unsigned int elementSize, const unsigned int *indices)
static void Destroy(Repack *packer)
static Value * Create(GLTF *gltf, const char *name, const Value *value=0)