-
Notifications
You must be signed in to change notification settings - Fork 45
Models
This article describes how models stored in OBJ files can be used to initialize vertex buffers with vertex data.
You first need to load the vertex data encoded in the file format to be loaded into memory. This can be accomplished with the Mesh class as shown below.
GL::Mesh object( "object.obj" );The constructor takes a path to an OBJ file and loads the data in the file into structures in memory.
The VertexBuffer class has an extra constructor that takes a Mesh instance. However, as you know, OpenGL allows you to define your very own vertex format. This was solved in OOGL by having the VertexBuffer constructor take a formatting function.
GL::VertexBuffer vbo( object, GL::BufferUsage::StaticDraw, [] ( const GL::Vertex& v, GL::VertexDataBuffer& data )
{
data.Vec3( v.Pos );
data.Vec3( v.Normal );
} );The anonymous function that is passed receives vertices from the Mesh instance with position, normal and texture coordinate data. The code in the function body then takes this information to output the data for a single vertex in the format desired by the user using the VertexDataBuffer helper class.
In this case the programmer was only interested in the position and normal data. Someone developing a 2D game would simply discard the Z coordinate. By providing a callback function like this, OOGL allows you to do additional operations like normalizing the input normals and calculating additional vertex data from the input.