Skip to main content

Post-processing tools 2023 R2

Test010.cpp

Last update: 17.04.2023

Different types of primitives.

This example demonstrates the different types of primitives that are available. At the moment WebGL is limited to primitives no larger than triangles (3 vertices per primitive).

Most applications generally stick to POINTS, LINES, or TRIANGLES.

/*
* 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>
#include "GLTFWriter.h"
#include "test.h"
using namespace ANSYS::Nexus;
namespace {
void PositionAppend(std::vector<float> &dest,
const std::vector<float> &src,
unsigned int srcOffset,
unsigned int srcCount,
float offsetX,
float offsetY,
float offsetZ)
{
unsigned int destOffset = (unsigned int)dest.size();
dest.resize(destOffset + 3 * srcCount);
for (unsigned int i = 0; i < srcCount; ++i) {
dest[destOffset + 3 * i + 0] = src[3 * (srcOffset + i) + 0] + offsetX;
dest[destOffset + 3 * i + 1] = src[3 * (srcOffset + i) + 1] + offsetY;
dest[destOffset + 3 * i + 2] = src[3 * (srcOffset + i) + 2] + offsetZ;
}
}
};
// primitiveTypes
TESTFUNC(PrimitiveTypes)
{
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.0F, GLTFWriter::Scene::BT_TB, 0.42F, 0.55F, 0.871F, 1, 1, 1);
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);
}
float x = 0;
float y = 0;
float z = 0;
std::vector<float> locations;
locations.push_back(x + 0.0F); locations.push_back(y + 0.0F); locations.push_back(z - 0.0F);
locations.push_back(x + 1.0F); locations.push_back(y + 0.0F); locations.push_back(z - 0.0F);
locations.push_back(x + 1.0F); locations.push_back(y + 1.0F); locations.push_back(z - 0.0F);
locations.push_back(x + 0.0F); locations.push_back(y + 1.0F); locations.push_back(z - 0.0F);
locations.push_back(x + 0.0F); locations.push_back(y + 0.0F); locations.push_back(z - 1.0F);
locations.push_back(x + 1.0F); locations.push_back(y + 0.0F); locations.push_back(z - 1.0F);
locations.push_back(x + 1.0F); locations.push_back(y + 1.0F); locations.push_back(z - 1.0F);
locations.push_back(x + 0.0F); locations.push_back(y + 1.0F); locations.push_back(z - 1.0F);
// POINTS
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Points");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 1.0, 0.0, 0.0, 1.0)) || // color name is used to auto-generate a solid colored object
!material->AppendValue(GLTFWriter::Value::Create(gltf, "pointSize", 50.0))) {
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 8, x, y, z);
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_POINTS, material);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex)) {
throw std::runtime_error("Can't create primitive");
}
}
y += 2;
// INDEXED POINTS
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Indexed Points (Does not make sense)");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.5, 0.0, 0.0, 1.0)) || // color name is used to auto-generate a solid colored object
!material->AppendValue(GLTFWriter::Value::Create(gltf, "pointSize", 50.0))) {
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 8, x, y, z);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// INDICES
std::vector<unsigned short> indices;
for (unsigned int i = 0; i < numVertices; ++i)
indices.push_back(i);
// INDEX
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, (unsigned int)indices.size(), &indices[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_POINTS, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex)) {
throw std::runtime_error("Can't create primitive");
}
}
x += 2;
y -= 2;
// LINES
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Lines");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.0, 1.0, 0.0, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 2, x, y, z);
PositionAppend(vertices, locations, 1, 2, x, y, z);
PositionAppend(vertices, locations, 2, 2, x, y, z);
PositionAppend(vertices, locations, 3, 1, x, y, z); PositionAppend(vertices, locations, 0, 1, x, y, z);
PositionAppend(vertices, locations, 4, 2, x, y, z);
PositionAppend(vertices, locations, 5, 2, x, y, z);
PositionAppend(vertices, locations, 6, 2, x, y, z);
PositionAppend(vertices, locations, 7, 1, x, y, z); PositionAppend(vertices, locations, 4, 1, x, y, z);
PositionAppend(vertices, locations, 0, 1, x, y, z); PositionAppend(vertices, locations, 4, 1, x, y, z);
PositionAppend(vertices, locations, 1, 1, x, y, z); PositionAppend(vertices, locations, 5, 1, x, y, z);
PositionAppend(vertices, locations, 2, 1, x, y, z); PositionAppend(vertices, locations, 6, 1, x, y, z);
PositionAppend(vertices, locations, 3, 1, x, y, z); PositionAppend(vertices, locations, 7, 1, x, y, z);
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_LINES, material);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex)) {
throw std::runtime_error("Can't create primitive");
}
}
y += 2;
// INDEXED LINES
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Indexed Lines");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.0, 0.5, 0.0, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 8, x, y, z);
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// INDICES
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1);
indices.push_back(1); indices.push_back(2);
indices.push_back(2); indices.push_back(3);
indices.push_back(3); indices.push_back(0);
indices.push_back(4); indices.push_back(5);
indices.push_back(5); indices.push_back(6);
indices.push_back(6); indices.push_back(7);
indices.push_back(7); indices.push_back(4);
indices.push_back(0); indices.push_back(4);
indices.push_back(1); indices.push_back(5);
indices.push_back(2); indices.push_back(6);
indices.push_back(3); indices.push_back(7);
// INDEX
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, (unsigned int)indices.size(), &indices[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_LINES, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex)) {
throw std::runtime_error("Can't create primitive");
}
}
x += 2;
y -= 2;
// LINE LOOP
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Line Loop");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.0, 0.0, 1.0, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 3, x, y, z);
PositionAppend(vertices, locations, 6, 1, x, y, z);
PositionAppend(vertices, locations, 5, 1, x, y, z);
PositionAppend(vertices, locations, 4, 1, x, y, z);
PositionAppend(vertices, locations, 7, 1, x, y, z);
PositionAppend(vertices, locations, 3, 1, x, y, z);
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_LINE_LOOP, material);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex)) {
throw std::runtime_error("Can't create primitive");
}
}
y += 2;
// INDEXED LINE LOOP
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Indexed Line Loop");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.0, 0.0, 0.5, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 8, x, y, z);
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// INDICES
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(2);
indices.push_back(6);
indices.push_back(5);
indices.push_back(4);
indices.push_back(7);
indices.push_back(3);
// INDEX
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, (unsigned int)indices.size(), &indices[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_LINE_LOOP, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex)) {
throw std::runtime_error("Can't create primitive");
}
}
x += 2;
y -= 2;
// LINE STRIP
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Line Strip");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.75, 0.75, 0.0, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 4, x, y, z);
PositionAppend(vertices, locations, 0, 1, x, y, z);
PositionAppend(vertices, locations, 4, 4, x, y, z);
PositionAppend(vertices, locations, 4, 1, x, y, z);
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_LINE_LOOP, material);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex)) {
throw std::runtime_error("Can't create primitive");
}
}
y += 2;
// INDEXED LINE STRIP
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Indexed Line Strip");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.25, 0.25, 0.0, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 8, x, y, z);
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// INDICES
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(2); indices.push_back(3);
indices.push_back(0);
indices.push_back(4); indices.push_back(5); indices.push_back(6); indices.push_back(7);
indices.push_back(4);
// INDEX
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, (unsigned int)indices.size(), &indices[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_LINE_LOOP, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex)) {
throw std::runtime_error("Can't create primitive");
}
}
x += 2;
y -= 2;
// TRIANGLES
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Triangles");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.0, 1.0, 1.0, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 3, x, y, z);
PositionAppend(vertices, locations, 0, 1, x, y, z); PositionAppend(vertices, locations, 2, 2, x, y, z);
PositionAppend(vertices, locations, 1, 1, x, y, z); PositionAppend(vertices, locations, 5, 2, x, y, z);
PositionAppend(vertices, locations, 1, 1, x, y, z); PositionAppend(vertices, locations, 6, 1, x, y, z); PositionAppend(vertices, locations, 2, 1, x, y, z);
PositionAppend(vertices, locations, 5, 1, x, y, z); PositionAppend(vertices, locations, 4, 1, x, y, z); PositionAppend(vertices, locations, 7, 1, x, y, z);
PositionAppend(vertices, locations, 5, 1, x, y, z); PositionAppend(vertices, locations, 7, 1, x, y, z); PositionAppend(vertices, locations, 6, 1, x, y, z);
PositionAppend(vertices, locations, 4, 1, x, y, z); PositionAppend(vertices, locations, 0, 1, x, y, z); PositionAppend(vertices, locations, 3, 1, x, y, z);
PositionAppend(vertices, locations, 4, 1, x, y, z); PositionAppend(vertices, locations, 3, 1, x, y, z); PositionAppend(vertices, locations, 7, 1, x, y, z);
PositionAppend(vertices, locations, 4, 2, x, y, z); PositionAppend(vertices, locations, 1, 1, x, y, z);
PositionAppend(vertices, locations, 4, 1, x, y, z); PositionAppend(vertices, locations, 1, 1, x, y, z); PositionAppend(vertices, locations, 0, 1, x, y, z);
PositionAppend(vertices, locations, 3, 1, x, y, z); PositionAppend(vertices, locations, 2, 1, x, y, z); PositionAppend(vertices, locations, 6, 1, x, y, z);
PositionAppend(vertices, locations, 3, 1, x, y, z); PositionAppend(vertices, locations, 6, 2, x, y, z);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// NORMALS
std::vector<float> vertexNormals(3 * numVertices);
{
std::vector<unsigned short> indices;
for (unsigned int i = 0; i < numVertices; ++i)
indices.push_back(i);
const unsigned int numTriangles = (unsigned int)indices.size() / 3;
GLTFWriter::Utils::ComputeVertexNormals3(numVertices, &vertices[0], numTriangles, 3, &indices[0], &vertexNormals[0]);
}
// NORMAL ATTRIBUTE
GLTFWriter::Attribute *normal = GLTFWriter::Attribute::CreateNormal(gltf, numVertices, &vertexNormals[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLES, material);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(normal)) {
throw std::runtime_error("Can't create primitive");
}
}
y += 2;
// INDEXED TRIANGLES
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Indexed Triangles");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.0, 0.5, 0.5, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 8, x, y, z);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, numVertices, &vertices[0]);
// INDICES
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(2);
indices.push_back(0); indices.push_back(2); indices.push_back(3);
indices.push_back(1); indices.push_back(5); indices.push_back(6);
indices.push_back(1); indices.push_back(6); indices.push_back(2);
indices.push_back(5); indices.push_back(4); indices.push_back(7);
indices.push_back(5); indices.push_back(7); indices.push_back(6);
indices.push_back(4); indices.push_back(0); indices.push_back(3);
indices.push_back(4); indices.push_back(3); indices.push_back(7);
indices.push_back(0); indices.push_back(4); indices.push_back(5);
indices.push_back(0); indices.push_back(5); indices.push_back(1);
indices.push_back(2); indices.push_back(6); indices.push_back(7);
indices.push_back(2); indices.push_back(7); indices.push_back(3);
const unsigned int numTriangles = (unsigned int)indices.size() / 3;
// INDEX
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, (unsigned int)indices.size(), &indices[0]);
// NORMALS
std::vector<float> vertexNormals(3 * numVertices);
GLTFWriter::Utils::ComputeVertexNormals3(numVertices, &vertices[0], numTriangles, 3, &indices[0], &vertexNormals[0]);
// NORMAL ATTRIBUTE
GLTFWriter::Attribute *normal = GLTFWriter::Attribute::CreateNormal(gltf, numVertices, &vertexNormals[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLES, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(normal)) {
throw std::runtime_error("Can't create primitive");
}
}
x += 2;
y -= 2;
// TRIANGLE STRIP
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Triangle Strip");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 1.0, 0.0, 1.0, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 2, x, y, z); PositionAppend(vertices, locations, 3, 1, x, y, z);
PositionAppend(vertices, locations, 2, 1, x, y, z);
PositionAppend(vertices, locations, 7, 1, x, y, z);
PositionAppend(vertices, locations, 6, 1, x, y, z);
PositionAppend(vertices, locations, 4, 1, x, y, z);
PositionAppend(vertices, locations, 5, 1, x, y, z);
PositionAppend(vertices, locations, 0, 1, x, y, z);
PositionAppend(vertices, locations, 1, 1, x, y, z);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// NORMALS
std::vector<float> vertexNormals(3 * numVertices);
{
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(2);
indices.push_back(2); indices.push_back(1); indices.push_back(3);
indices.push_back(2); indices.push_back(3); indices.push_back(4);
indices.push_back(4); indices.push_back(3); indices.push_back(5);
indices.push_back(4); indices.push_back(5); indices.push_back(6);
indices.push_back(6); indices.push_back(5); indices.push_back(7);
indices.push_back(6); indices.push_back(7); indices.push_back(8);
indices.push_back(8); indices.push_back(7); indices.push_back(9);
const unsigned int numTriangles = (unsigned int)indices.size() / 3;
GLTFWriter::Utils::ComputeVertexNormals3(numVertices, &vertices[0], numTriangles, 3, &indices[0], &vertexNormals[0]);
}
// NORMAL ATTRIBUTE
GLTFWriter::Attribute *normal = GLTFWriter::Attribute::CreateNormal(gltf, numVertices, &vertexNormals[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLE_STRIP, material);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(normal)) {
throw std::runtime_error("Can't create primitive");
}
}
y += 2;
// INDEXED TRIANGLE STRIP
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Indexed Triangle Strip");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.5, 0.0, 0.5, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 8, x, y, z);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, numVertices, &vertices[0]);
// INDICES
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(3);
indices.push_back(2);
indices.push_back(7);
indices.push_back(6);
indices.push_back(4);
indices.push_back(5);
indices.push_back(0);
indices.push_back(1);
// INDEX
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, (unsigned int)indices.size(), &indices[0]);
// NORMALS
std::vector<float> vertexNormals(3 * numVertices);
{
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(3);
indices.push_back(3); indices.push_back(1); indices.push_back(2);
indices.push_back(3); indices.push_back(2); indices.push_back(7);
indices.push_back(7); indices.push_back(2); indices.push_back(6);
indices.push_back(7); indices.push_back(6); indices.push_back(4);
indices.push_back(4); indices.push_back(6); indices.push_back(5);
indices.push_back(4); indices.push_back(5); indices.push_back(0);
indices.push_back(0); indices.push_back(5); indices.push_back(1);
const unsigned int numTriangles = (unsigned int)indices.size() / 3;
GLTFWriter::Utils::ComputeVertexNormals3(numVertices, &vertices[0], numTriangles, 3, &indices[0], &vertexNormals[0]);
}
// NORMAL ATTRIBUTE
GLTFWriter::Attribute *normal = GLTFWriter::Attribute::CreateNormal(gltf, numVertices, &vertexNormals[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLE_STRIP, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(normal)) {
throw std::runtime_error("Can't create primitive");
}
}
x += 2;
y -= 2;
// TRIANGLE FAN
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Triangle Fan");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.75, 0.0, 0.75, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 4, x, y, z);
PositionAppend(vertices, locations, 7, 1, x, y, z);
PositionAppend(vertices, locations, 4, 2, x, y, z);
PositionAppend(vertices, locations, 1, 1, x, y, z);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// NORMALS
std::vector<float> vertexNormals(3 * numVertices);
{
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(2);
indices.push_back(0); indices.push_back(2); indices.push_back(3);
indices.push_back(0); indices.push_back(3); indices.push_back(4);
indices.push_back(0); indices.push_back(4); indices.push_back(5);
indices.push_back(0); indices.push_back(5); indices.push_back(6);
indices.push_back(0); indices.push_back(6); indices.push_back(7);
const unsigned int numTriangles = (unsigned int)indices.size() / 3;
GLTFWriter::Utils::ComputeVertexNormals3(numVertices, &vertices[0], numTriangles, 3, &indices[0], &vertexNormals[0]);
}
// NORMAL ATTRIBUTE
GLTFWriter::Attribute *normal = GLTFWriter::Attribute::CreateNormal(gltf, numVertices, &vertexNormals[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLE_FAN, material);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(normal)) {
throw std::runtime_error("Can't create primitive");
}
}
y += 2;
// INDEXED TRIANGLE FAN
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "Indexed Triangle Fan");
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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
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, "color", 0.25, 0.0, 0.25, 1.0))) { // color name is used to auto-generate a solid colored object
throw std::runtime_error("Can't create material");
}
// VERTICES
std::vector<float> vertices;
PositionAppend(vertices, locations, 0, 8, x, y, z);
const unsigned int numVertices = (unsigned int)vertices.size() / 3;
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, numVertices, &vertices[0]);
// INDICES
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(2);
indices.push_back(3);
indices.push_back(7);
indices.push_back(4);
indices.push_back(5);
indices.push_back(1);
// INDEX
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, (unsigned int)indices.size(), &indices[0]);
// NORMALS
std::vector<float> vertexNormals(3 * numVertices);
{
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(2);
indices.push_back(0); indices.push_back(2); indices.push_back(3);
indices.push_back(0); indices.push_back(3); indices.push_back(7);
indices.push_back(0); indices.push_back(7); indices.push_back(4);
indices.push_back(0); indices.push_back(4); indices.push_back(5);
indices.push_back(0); indices.push_back(5); indices.push_back(1);
const unsigned int numTriangles = (unsigned int)indices.size() / 3;
GLTFWriter::Utils::ComputeVertexNormals3(numVertices, &vertices[0], numTriangles, 3, &indices[0], &vertexNormals[0]);
}
// NORMAL ATTRIBUTE
GLTFWriter::Attribute *normal = GLTFWriter::Attribute::CreateNormal(gltf, numVertices, &vertexNormals[0]);
// PRIMITIVE
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create(gltf, GLTFWriter::Primitive::PT_TRIANGLE_FAN, material, index);
if (!primitive ||
!mesh->AppendPrimitive(primitive) ||
!primitive->AppendAttribute(vertex) ||
!primitive->AppendAttribute(normal)) {
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 * CreatePosition(GLTF *gltf, unsigned int count, const float *data, Buffer *buffer=0)
static Attribute * CreateNormal(GLTF *gltf, unsigned int count, const float *data, Buffer *buffer=0)
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 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 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 * CreateLight(GLTF *gltf)
@ PT_POINTS
Treats each vertex as a single point.
Definition: GLTFMesh.h:35
@ PT_LINES
Treats each pair of vertices as an independent line segment.
Definition: GLTFMesh.h:36
@ PT_TRIANGLES
Treats each triplet of vertices as an independent triangle.
Definition: GLTFMesh.h:39
@ PT_TRIANGLE_FAN
Draws a connected group of triangles. One triangle is defined for each vertex presented after the fir...
Definition: GLTFMesh.h:41
@ PT_LINE_LOOP
Draws a connected group of line segments from the first vertex to the last, then back to the first.
Definition: GLTFMesh.h:37
@ PT_TRIANGLE_STRIP
Draws a connected group of triangles. One triangle is defined for each vertex presented after the fir...
Definition: GLTFMesh.h:40
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 Value * Create(GLTF *gltf, const char *name, const Value *value=0)
void ComputeVertexNormals3(const unsigned int numVertices, const float *vertices, const unsigned int numElements, const unsigned int elementSize, const unsigned short *indices, float *normals)