![]() |
TGX 1.0.3
A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.
|
3D mesh data stucture. More...
#include <Mesh3D.h>
Public Attributes | |
int32_t | id |
Set to 1 (may change in future version). | |
uint16_t | nb_vertices |
Number of vertices in the vertex array. | |
uint16_t | nb_texcoords |
Number of texture coordinates in the texcoord array. | |
uint16_t | nb_normals |
Number of normal vectors in the normal array. | |
uint16_t | nb_faces |
Number of triangular faces in the mesh. | |
uint16_t | len_face |
Number of elements (uint16_t) in the face array. | |
const fVec3 * | vertice |
Vertex array. | |
const fVec2 * | texcoord |
Texture coord array (nullptr if none). | |
const fVec3 * | normal |
Normal vector array (nullptr if none). all vectors must have unit norm. | |
const uint16_t * | face |
Array of triangles (format described above). | |
const Image< color_t > * | texture |
Texture image (or nullptr if none). | |
RGBf | color |
Default color to use when texturing is disabled. | |
float | ambiant_strength |
Object ambiant coefficient (how much it reflects the ambiant light component). Typical value: 0.2f. | |
float | diffuse_strength |
Object diffuse coefficient (how much it reflects the diffuse light component). Typical value: 0.7f. | |
float | specular_strength |
Object ambiant coefficient (how much it reflects the specular light component). Typical value: 0.5f. | |
int | specular_exponent |
Specular exponent. 0 to disable specular lightning. Typical value between 4 and 64. | |
const Mesh3D * | next |
Next object to draw when chaining is enabled. nullptr at end of chain. | |
fBox3 | bounding_box |
Object bounding box. | |
const char * | name |
Mesh name, or nullptr. | |
3D mesh data stucture.
A Mesh3D structure contain all the informations about an object geometry, color, material property and textures. A mesh can be rendered onto an Image
object using the Renderer3D::drawMesh()
method.
The mesh data format is designed to be compact and favors linear access to elements in order to improve cache coherency so that meshes can be stored and rendered directly from "slow" memory such as FLASH on a MCU.
The Python scripts obj_to_h
located in the \tools
directory of the library can be used to create a Mesh3D
object directly from an .obj file. The associated image textures (if any) can be converted using the Python script texture_2_h
.
MESH FORMAT
vertice | Array of all the vertices of the mesh in (x,y,z) fVec3 format. A vertex is refered by its index in this array. Maximum number of vertices per mesh: 32767. |
texcoord | Array of texture coordinate in (u,v) fVec2 format. A texture coord is refered by its index in this array. Set to nullptr (and nb_texcoords=0) if the mesh has no texture. Maximum number of texture coords: 65535 |
normal | Array of normal vectors in (x,y,z) fVec3 format. A normal vector is refered by its index in this array. Set to nullptr if no normal vectors are defined. Normal vectors must have unit norm. Maximum number of normal vectors: 65535 |
face | Array of triangular faces. The array is composed of uint16_t and is divided into chains of triangles. See below for details. |
texture | image texture object associated with the mesh (or nullptr is none). |
next | pointer to the next mesh (or nullptr if none). Chaining mesh enalbe to draw complex models with multiple textures images and more than 32767 vertices... |
Structure of the face
array.
the array is composed of "chains of triangles". Each chain starts with a uint16_t
that specifies its length and is followed by a sequence of elements where each element occupies 1, 2 or 3 uint16_t
. The array takes the following general form:
(int16_t)0
is used as a sentinel to mark the end of the face
array.A "element" represent a vertex of a triangle and occupies 1, 2 or 3 uint16_t
depending on whether the textcoord and normal arrays exist in the mesh. The first uint16_t
of an element is the vertex index (with a direction bit), then is followed by the texture index if a texture array is present and then finally followed by the normal index if a normal array is present.
Schematiccally:
The first 3 elements of a chain represent the 3 vertices that compose the first triangle of the chain. Their DBIT is always 0. Each subsequent element is combined with the current triangle to deduce the next triangle in the following way: suppose the current triangle is [V1, V2, V3]
and the next element is DBIT|V4
:
DBIT = 0
, then the next triangle is [V1, V3, V4]
DBIT = 1
, then the next triangle is [V3, V2, V4]
.Example
We assume here texcoord = nullptr
but normal != nullptr
so that each element is coded on 2 uint16_t
.
Let us consider the following face array composed of 19 uint16_t
:
It should be read as
after decoding, this gives a list of 4 triangles (with normals associated with each vertex index).