Skip to main content

Post-processing tools 2023 R2

Test005.cpp

Last update: 17.04.2023

Textured 2D Quad.

This example shows how to create a simple 2D textured quad.

/*
* 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 Textured Quad
TESTFUNC(Textured2DQuad)
{
return;
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);
lightNode->AppendLight(light2);
}
// 3D 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");
}
// TECHNIQUE
GLTFWriter::Technique *technique = GLTFWriter::Technique::Create(gltf);
if (!technique ||
// TECHNIQUE STATES
!technique->AppendState(GLTFWriter::State::Create(gltf, GLTFWriter::State::ST_BLENDENABLE, 1))) {
throw std::runtime_error("Can't create technique");
}
// MATERIAL
GLTFWriter::Material *material = GLTFWriter::Material::Create(gltf, technique);
// VERTICES
std::vector<float> vertices;
const float width = 1;
const float height = 1;
const float depth = 1;
const float shiftX = 0;
const float shiftY = 0;
const float shiftZ = 0;
vertices.push_back(-width / 2 + shiftX); vertices.push_back(-height / 2 + shiftY); vertices.push_back(-depth / 2 + shiftZ);
vertices.push_back( width / 2 + shiftX); vertices.push_back(-height / 2 + shiftY); vertices.push_back(-depth / 2 + shiftZ);
vertices.push_back( width / 2 + shiftX); vertices.push_back( height / 2 + shiftY); vertices.push_back(-depth / 2 + shiftZ);
vertices.push_back(-width / 2 + shiftX); vertices.push_back( height / 2 + shiftY); vertices.push_back(-depth / 2 + shiftZ);
vertices.push_back(-width / 2 + shiftX); vertices.push_back(-height / 2 + shiftY); vertices.push_back( depth / 2 + shiftZ);
vertices.push_back( width / 2 + shiftX); vertices.push_back(-height / 2 + shiftY); vertices.push_back( depth / 2 + shiftZ);
vertices.push_back( width / 2 + shiftX); vertices.push_back( height / 2 + shiftY); vertices.push_back( depth / 2 + shiftZ);
vertices.push_back(-width / 2 + shiftX); vertices.push_back( height / 2 + shiftY); vertices.push_back( depth / 2 + shiftZ);
// POSITION ATTRIBUTE
GLTFWriter::Attribute *vertex = GLTFWriter::Attribute::CreatePosition(gltf, (unsigned int)vertices.size() / 3, &vertices[0]);
// COLORS
std::vector<float> colors;
colors.push_back(0); colors.push_back(0); colors.push_back(0); colors.push_back(1);
colors.push_back(0); colors.push_back(0); colors.push_back(1); colors.push_back(0.75);
colors.push_back(0); colors.push_back(1); colors.push_back(0); colors.push_back(0.5);
colors.push_back(0); colors.push_back(1); colors.push_back(1); colors.push_back(0.25);
colors.push_back(1); colors.push_back(0); colors.push_back(1); colors.push_back(0.5);
colors.push_back(1); colors.push_back(0); colors.push_back(0); colors.push_back(0.25);
colors.push_back(1); colors.push_back(1); colors.push_back(1); colors.push_back(1);
colors.push_back(1); colors.push_back(1); colors.push_back(0); colors.push_back(0.75);
// COLOR ATTRIBUTE
GLTFWriter::Attribute *color = GLTFWriter::Attribute::CreateColor(gltf, GLTFWriter::Attribute::AT_FLOAT_VEC4, (unsigned int)colors.size() / 4, &colors[0]);
// INDICES
std::vector<unsigned short> indices;
indices.push_back(0); indices.push_back(2); indices.push_back(1);
indices.push_back(0); indices.push_back(3); indices.push_back(2);
indices.push_back(0); indices.push_back(4); indices.push_back(7);
indices.push_back(0); indices.push_back(7); indices.push_back(3);
indices.push_back(4); indices.push_back(5); indices.push_back(6);
indices.push_back(4); indices.push_back(6); indices.push_back(7);
indices.push_back(5); indices.push_back(1); indices.push_back(2);
indices.push_back(5); indices.push_back(2); indices.push_back(6);
indices.push_back(0); indices.push_back(1); indices.push_back(5);
indices.push_back(0); indices.push_back(5); indices.push_back(4);
indices.push_back(2); indices.push_back(3); indices.push_back(7);
indices.push_back(2); indices.push_back(7); indices.push_back(6);
// INDICES
GLTFWriter::Index *index = GLTFWriter::Index::Create(gltf, (unsigned int)indices.size(), &indices[0]);
// NORMALS
std::vector<float> normals(vertices.size());
GLTFWriter::Utils::ComputeVertexNormals3((unsigned int)vertices.size() / 3, &vertices[0], (unsigned int)indices.size() / 3, 3, &indices[0], &normals[0]);
// NORMAL ATTRIBUTE
GLTFWriter::Attribute *normal = GLTFWriter::Attribute::CreateNormal(gltf, (unsigned int)vertices.size() / 3, &normals[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) ||
!primitive->AppendAttribute(color)) {
throw std::runtime_error("Can't create primitive");
}
}
// 2D MESH NODE
{
// NODE
GLTFWriter::Node *node = GLTFWriter::Node::CreateMesh(gltf, "2D Textured Quad");
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");
}
// TEXTURE
{
std::vector<unsigned char> imageData;
const unsigned width = 150;
const unsigned height = 250;
imageData.resize(width * height * 4, 0);
# define SETRGB(data, width, x, y, r, g, b, a) { \
data[4 * ((x) + (y) * (width)) + 0] = r; \
data[4 * ((x) + (y) * (width)) + 1] = g; \
data[4 * ((x) + (y) * (width)) + 2] = b; \
data[4 * ((x) + (y) * (width)) + 3] = a; \
}
// fill in top left corner
for (unsigned int i = 0; i < 10; ++i)
for (unsigned int j = 0; j < 10; ++j)
SETRGB(imageData, width, i + 2, j + 2, 255, 255, 255, 255);
// PRIMITIVE
for (unsigned int i = 0; i < width; ++i) {
SETRGB(imageData, width, i, 0, 255, 0, 0, 255);
SETRGB(imageData, width, i, 1, 255, 0, 0, 255);
SETRGB(imageData, width, i, height - 1, 255, 0, 0, 255);
SETRGB(imageData, width, i, height - 2, 255, 0, 0, 255);
}
for (unsigned int i = 0; i < height; ++i) {
SETRGB(imageData, width, 0, i, 255, 0, 0, 255);
SETRGB(imageData, width, 1, i, 255, 0, 0, 255);
SETRGB(imageData, width, width - 1, i, 255, 0, 0, 255);
SETRGB(imageData, width, width - 2, i, 255, 0, 0, 255);
}
GLTFWriter::Texture *texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
GLTFWriter::Primitive *primitive = GLTFWriter::Primitive::Create2DTexture(gltf, texture, 0.15F, 0.25F, GLTFWriter::Primitive::PXA_LEFT, GLTFWriter::Primitive::PYA_TOP);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < width; ++i) {
SETRGB(imageData, width, i, 0, 0, 255, 0, 255);
SETRGB(imageData, width, i, 1, 0, 255, 0, 255);
SETRGB(imageData, width, i, height - 1, 0, 255, 0, 255);
SETRGB(imageData, width, i, height - 2, 0, 255, 0, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < width; ++i) {
SETRGB(imageData, width, i, 0, 0, 0, 255, 255);
SETRGB(imageData, width, i, 1, 0, 0, 255, 255);
SETRGB(imageData, width, i, height - 1, 0, 0, 255, 255);
SETRGB(imageData, width, i, height - 2, 0, 0, 255, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < height; ++i) {
SETRGB(imageData, width, 0, i, 0, 255, 0, 255);
SETRGB(imageData, width, 1, i, 0, 255, 0, 255);
SETRGB(imageData, width, width - 1, i, 0, 255, 0, 255);
SETRGB(imageData, width, width - 2, i, 0, 255, 0, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < width; ++i) {
SETRGB(imageData, width, i, 0, 0, 255, 0, 255);
SETRGB(imageData, width, i, 1, 0, 255, 0, 255);
SETRGB(imageData, width, i, height - 1, 0, 255, 0, 255);
SETRGB(imageData, width, i, height - 2, 0, 255, 0, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < width; ++i) {
SETRGB(imageData, width, i, 0, 255, 0, 0, 255);
SETRGB(imageData, width, i, 1, 255, 0, 0, 255);
SETRGB(imageData, width, i, height - 1, 255, 0, 0, 255);
SETRGB(imageData, width, i, height - 2, 255, 0, 0, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < height; ++i) {
SETRGB(imageData, width, 0, i, 0, 0, 255, 255);
SETRGB(imageData, width, 1, i, 0, 0, 255, 255);
SETRGB(imageData, width, width - 1, i, 0, 0, 255, 255);
SETRGB(imageData, width, width - 2, i, 0, 0, 255, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < width; ++i) {
SETRGB(imageData, width, i, 0, 0, 255, 0, 255);
SETRGB(imageData, width, i, 1, 0, 255, 0, 255);
SETRGB(imageData, width, i, height - 1, 0, 255, 0, 255);
SETRGB(imageData, width, i, height - 2, 0, 255, 0, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < width; ++i) {
SETRGB(imageData, width, i, 0, 0, 0, 255, 255);
SETRGB(imageData, width, i, 1, 0, 0, 255, 255);
SETRGB(imageData, width, i, height - 1, 0, 0, 255, 255);
SETRGB(imageData, width, i, height - 2, 0, 0, 255, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
mesh->AppendPrimitive(primitive);
for (unsigned int i = 0; i < width; ++i) {
SETRGB(imageData, width, i, 0, 255, 255, 255, 255);
SETRGB(imageData, width, i, 1, 255, 255, 255, 255);
SETRGB(imageData, width, i, height - 1, 255, 255, 255, 255);
SETRGB(imageData, width, i, height - 2, 255, 255, 255, 255);
}
for (unsigned int i = 0; i < height; ++i) {
SETRGB(imageData, width, 0, i, 255, 255, 255, 255);
SETRGB(imageData, width, 1, i, 255, 255, 255, 255);
SETRGB(imageData, width, width - 1, i, 255, 255, 255, 255);
SETRGB(imageData, width, width - 2, i, 255, 255, 255, 255);
}
texture = GLTFWriter::Texture::Create(gltf, GLTFWriter::Texture::TF_RGBA, width, height, &imageData[0]);
primitive = GLTFWriter::Primitive::Create2DTexture(gltf, texture, 0.15F, 0.25F, 0.2F, 0.4F);
mesh->AppendPrimitive(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 Attribute * CreateColor(GLTF *gltf, AttributeType type, unsigned int count, const float *data, Buffer *buffer=0)
static void Destroy(GLTF *gltf)
@ OT_GLTF
GLTF for ANSYS Viewer.
Definition: GLTFGLTF.h:57
@ OT_AVZ
AVZ file for ANSYS Viewer.
Definition: GLTFGLTF.h:56
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_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)
@ PYA_BOTTOM
Anchor at bottom edge.
Definition: GLTFMesh.h:55
@ PYA_CENTER
Center in vertical direction.
Definition: GLTFMesh.h:56
@ PYA_TOP
Anchor at top edge.
Definition: GLTFMesh.h:57
@ PXA_RIGHT
Anchor at right edge.
Definition: GLTFMesh.h:49
@ PXA_CENTER
Center in horizontal direction.
Definition: GLTFMesh.h:48
@ PXA_LEFT
Anchor at left edge.
Definition: GLTFMesh.h:47
static Primitive * Create2DTexture(GLTF *gltf, Texture *texture, float width, float height, PrimitiveXAttachment xAttach=PXA_LEFT, PrimitiveYAttachment yAttach=PYA_CENTER)
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
static State * Create(GLTF *gltf, StateType type, double v1, double v2=0, double v3=0, double v4=0)
@ ST_BLENDENABLE
Enables blending of fragment colors. Possible values are:
@ 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_RGBA
4 unsigned chars per pixel.
Definition: GLTFTexture.h:35
void ComputeVertexNormals3(const unsigned int numVertices, const float *vertices, const unsigned int numElements, const unsigned int elementSize, const unsigned short *indices, float *normals)