Overview
In this tutorial, you’ll learn how to:- Initialize the Filament engine
- Create vertex and index buffers
- Define a simple unlit material
- Set up a camera and scene
- Render and animate geometry
Complete Source Code
The complete example is available insamples/hellotriangle.cpp. Here’s the step-by-step breakdown:
Define the Application Structure
First, define a structure to hold all the Filament objects we’ll need:This structure keeps track of our vertex buffer, index buffer, material, camera, and the renderable entity.
Define Vertex Data
Create a vertex structure and define the triangle vertices with positions and colors:The vertices form an equilateral triangle with different colors at each vertex. Colors are stored as RGBA in packed 32-bit format.
Create Vertex Buffer
Build a vertex buffer that describes the layout of vertex data:Key points:
vertexCount(3): We have 3 verticesattribute(): Defines position (2 floats at offset 0) and color (4 bytes at offset 8)normalized(VertexAttribute::COLOR): Normalizes color from 0-255 to 0.0-1.0- Stride is 12 bytes (8 bytes for position + 4 bytes for color)
Create Index Buffer
Create an index buffer for the triangle:The index buffer defines the order in which vertices are connected to form triangles.
Create Material
Load the baked color material from precompiled resources:This material is unlit and uses vertex colors. The material package was compiled using the
matc tool.Create Renderable Entity
Build a renderable entity that combines geometry, material, and rendering properties:Important details:
Builder(1): We have 1 primitive (one set of geometry)boundingBox(): Defines spatial bounds for cullinggeometry(): Associates vertex/index buffers with the primitiveculling(false): Disables backface culling
Set Up Camera
Create and configure an orthographic camera:In the animation loop, update the camera projection:
Add Animation
Animate the triangle by rotating it around the Z-axis:The
now parameter is time in seconds, creating a smooth rotation.Key Concepts
Vertex Attributes
Filament supports various vertex attributes:POSITION: Vertex position in object spaceCOLOR: Per-vertex color (can be normalized)UV0/UV1: Texture coordinatesTANGENTS: Tangent frame for normal mapping
Coordinate System
Filament uses a right-handed coordinate system:- X-axis points right
- Y-axis points up
- Z-axis points toward the viewer
Feature Levels
This example usesFEATURE_LEVEL_0, the most basic feature level that works on all platforms including WebGL 1.0.
Building and Running
Next Steps
- Hello PBR - Learn about physically-based materials and lighting
- glTF Viewer - Load and display complex 3D models
- Custom Materials - Create your own material definitions