Kanzi  3.9.5
Kanzi Engine API
kanzi::Mesh::CreateInfo Struct Reference

Use Mesh::CreateInfo to describe and create meshes. More...

#include <kanzi/core.ui/graphics3d/mesh_create_info.hpp>

Classes

struct  Cluster
 Parameters for Mesh::Cluster creation. More...
 

Public Types

typedef vector< PropertyEntryPropertyContainer
 
typedef pair< AbstractPropertyType, VariantPropertyEntry
 

Public Member Functions

 CreateInfo ()
 Default constructor for mesh create info. More...
 

Public Attributes

Box boundingBox
 Bounding box for mesh. More...
 
vector< Mesh::CreateInfo::Clusterclusters
 Clusters. More...
 
MemoryType memoryType
 Memory type for the texture. More...
 
PropertyContainer properties
 Properties to copy to the mesh. More...
 
Skeleton skeleton
 Mesh skeleton for bone animations. More...
 
bool uploadDataAtCreation
 Indicates whether vertex and index data should be upload at buffer creation. More...
 
size_t vertexCount
 Number of vertices in the mesh. More...
 
vector< bytevertexData
 Vertex buffer data. More...
 
MeshAttributeCollection vertexFormat
 Mesh vertex attributes. More...
 
size_t vertexSize
 Size of one vertex in bytes. This is sum of all mesh vertex attributes sizes. More...
 

Detailed Description

Use Mesh::CreateInfo to describe and create meshes.

Vertex and index listings define meshes. Indices refer to vertices which define the faces of a mesh.

To define a mesh, you need:

  • Vertex format definition. This definition describes the components that define a vertex. This includes information about the vertex position, texture coordinates, normal, and tangent.
  • List of vertices. Each vertex contains components that the format definition describes.
  • List of indices. Indices describe polygons by referring to each vertex by index. When triangles describe a polygon, three indices describe a triangle. You can use several index listings, each of which uses a material.

In Kanzi a mesh can have at most 65536 vertices, a limit set by GLES2. If you need more vertices, create additional meshes.

Examples

To create a cube mesh:

// Define a CreateInfo structure and set it to keep the data after uploading it to the GPU.
Mesh::CreateInfo createInfo;
createInfo.memoryType = GPUResource::GpuAndRam;
// Describe the format for each vertex. In this example, every vertex has a position in 3D space,
// texture coordinates pointing to 2D texture space, and normal and tangent data in 3D space.
// Add to the vertex format description a three-component position.
// Use the standard Kanzi name kzPosition.
MeshVertexAttribute position("kzPosition", VertexAttribute::SemanticPosition, 0, GraphicsDataTypeSFLOAT32, 3, ~0u);
createInfo.vertexFormat.push_back(position);
// Add to the vertex format description a two-component texture coordinate.
// Texture coordinates enable you to use textures on a mesh.
// Use the standard Kanzi name kzTextureCoordinate0.
MeshVertexAttribute textureCoordinate("kzTextureCoordinate0", VertexAttribute::SemanticTextureCoordinate, 0, GraphicsDataTypeSFLOAT32, 2, ~0u);
createInfo.vertexFormat.push_back(textureCoordinate);
// Add to the vertex format description three-component normals.
// Normals enable you to use lighting on a mesh.
// Use the standard Kanzi name kzNormal.
MeshVertexAttribute normal("kzNormal", VertexAttribute::SemanticNormal, 0, GraphicsDataTypeSFLOAT32, 3, ~0u);
createInfo.vertexFormat.push_back(normal);
// Add to the vertex format description three-component tangents.
// Tangents enable you to use normal maps on a mesh.
// Use the standard Kanzi name kzTangent.
MeshVertexAttribute tangent("kzTangent", VertexAttribute::SemanticTangent, 0, GraphicsDataTypeSFLOAT32, 3, ~0u);
createInfo.vertexFormat.push_back(tangent);
// Now that you added all components to the vertex format description, update the offsets, strides, and vertex size.
// Create a cluster. Two triangles define a face. Since a cube has six faces, you must create 36 indices
// (2 * 3 * 6 = 36). In this example the entire mesh uses a single material, which is why you need only
// one cluster.
Mesh::CreateInfo::Cluster cluster(GraphicsPrimitiveTypeTriangles, 36, IndexBufferTypeUInt16, material, materialUrl);
createInfo.clusters.push_back(cluster);
// Create the vertices. Four vertices define one face of a cube. Since cube has six faces, you must
// create 24 vertices (4 * 6 = 24). Here you create vertex faces with normals that point to the same
// direction. This makes the faces of a cube appear flat. You cannot reuse vertex positions because the
// normals for each face are separate.
{
createInfo.vertexCount = 24;
size_t dataSize = createInfo.vertexCount * createInfo.vertexSize;
createInfo.vertexData.resize(dataSize);
// Define the positions of the cube corners.
// This example first defines the back face, then the front face.
const Vector3 cornerPositions[] =
{
Vector3(1.0f, 1.0f, 1.0f),
Vector3(-1.0f, 1.0f, 1.0f),
Vector3(-1.0f, -1.0f, 1.0f),
Vector3(1.0f, -1.0f, 1.0f),
Vector3(1.0f, -1.0f, -1.0f),
Vector3(1.0f, 1.0f, -1.0f),
Vector3(-1.0f, 1.0f, -1.0f),
Vector3(-1.0f, -1.0f, -1.0f)
};
// Define vertex indices for all faces.
const Vector2 UVs[] =
{
Vector2(1.0f, 1.0f),
Vector2(0.0f, 1.0f),
Vector2(0.0f, 0.0f),
Vector2(1.0f, 0.0f)
};
// Define vertex indices for all faces.
const unsigned int faceIndices[][4] =
{
// Front.
{ 0, 1, 2, 3 },
// Right.
{ 5, 0, 3, 4 },
// Back.
{ 6, 5, 4, 7 },
// Left.
{ 1, 6, 7, 2 },
// Top.
{ 5, 6, 1, 0 },
// Bottom.
{ 3, 2, 7, 4 }
};
// Define the normals for each face.
const Vector3 faceNormals[] =
{
Vector3(0.0f, 0.0f, 1.0f),
Vector3(1.0f, 0.0f, 0.0f),
Vector3(0.0f, 0.0f, -1.0f),
Vector3(-1.0f, 0.0f, 0.0f),
Vector3(0.0f, 1.0f, 0.0f),
Vector3(0.0f, -1.0f, 0.0f)
};
// Define tangents for each face.
const Vector3 faceTangent[] =
{
Vector3(1.0f, 0.0f, 0.0f),
Vector3(0.0f, 0.0f, -1.0f),
Vector3(-1.0f, 0.0f, 0.0f),
Vector3(0.0f, 0.0f, 1.0f),
Vector3(1.0f, 0.0f, 0.0f),
Vector3(1.0f, 0.0f, 0.0f)
};
uint16_t vertexIndex = 0;
// To generate the vertex data, loop the data arrays you defined above.
for (unsigned int face = 0; face < 6; ++face)
{
// Add two triangles.
uint16_t* indexData = reinterpret_cast<uint16_t*>(&createInfo.clusters[0].indexData[0]);
unsigned int faceBaseIndex = face * 6;
// The first three vertices of a face define the first triangle...
indexData[faceBaseIndex + 0] = vertexIndex + 0;
indexData[faceBaseIndex + 1] = vertexIndex + 1;
indexData[faceBaseIndex + 2] = vertexIndex + 2;
// ...and the second triangle uses the two last vertices of the face with the first vertex.
// These two triangles create a quad.
indexData[faceBaseIndex + 3] = vertexIndex + 2;
indexData[faceBaseIndex + 4] = vertexIndex + 3;
indexData[faceBaseIndex + 5] = vertexIndex + 0;
// Write vertices for the face.
for (unsigned int faceCorner = 0; faceCorner < 4; ++faceCorner, ++vertexIndex)
{
unsigned int boxCorner = faceIndices[face][faceCorner];
// Write the correct vertex for this corner of each face.
writeVertexAttribute(createInfo, vertexIndex, 0, cornerPositions[boxCorner]);
// UVs for each face are the same.
writeVertexAttribute(createInfo, vertexIndex, 1, UVs[faceCorner]);
// Normal is constant throughout the face.
writeVertexAttribute(createInfo, vertexIndex, 2, faceNormals[face]);
// Tangent is constant throughout the face.
writeVertexAttribute(createInfo, vertexIndex, 3, faceTangent[face]);
}
}
}
// Center the bounding volume in the center of the mesh and set the size of each axis to 1.0f.
createInfo.boundingBox = Box::fromCenterAndSize(Vector3(0.0f, 0.0f, 0.0f), Vector3(1.0f, 1.0f, 1.0f));
// Create a mesh from the data you defined in the CreateInfo.
MeshSharedPtr mesh = Mesh::create(domain, createInfo, "box");
See also
Mesh

Member Typedef Documentation

◆ PropertyEntry

◆ PropertyContainer

Constructor & Destructor Documentation

◆ CreateInfo()

kanzi::Mesh::CreateInfo::CreateInfo ( )
explicit

Default constructor for mesh create info.

Member Data Documentation

◆ memoryType

MemoryType kanzi::Mesh::CreateInfo::memoryType

Memory type for the texture.

If Gpu is not included, texture will not be deployed when created. If Ram is not included, texture will not contain images after it has been deployed. Default value is GpuOnly.

See also
MemoryType

◆ clusters

vector<Mesh::CreateInfo::Cluster> kanzi::Mesh::CreateInfo::clusters

Clusters.

◆ boundingBox

Box kanzi::Mesh::CreateInfo::boundingBox

Bounding box for mesh.

◆ skeleton

Skeleton kanzi::Mesh::CreateInfo::skeleton

Mesh skeleton for bone animations.

◆ vertexFormat

MeshAttributeCollection kanzi::Mesh::CreateInfo::vertexFormat

Mesh vertex attributes.

◆ vertexSize

size_t kanzi::Mesh::CreateInfo::vertexSize

Size of one vertex in bytes. This is sum of all mesh vertex attributes sizes.

◆ vertexCount

size_t kanzi::Mesh::CreateInfo::vertexCount

Number of vertices in the mesh.

◆ vertexData

vector<byte> kanzi::Mesh::CreateInfo::vertexData

Vertex buffer data.

◆ uploadDataAtCreation

bool kanzi::Mesh::CreateInfo::uploadDataAtCreation

Indicates whether vertex and index data should be upload at buffer creation.

◆ properties

PropertyContainer kanzi::Mesh::CreateInfo::properties

Properties to copy to the mesh.

Currently they are not used.


The documentation for this struct was generated from the following file: