Vulkan on Android 4 — Model Rendering Part 5

Jin-Long Wu
2 min readNov 7, 2018

--

Photo by Nicolas Thomas on Unsplash

Having loading textures, it’s time to load models. We’ll load an earth model with Assimp and upload its data to the GPU side.

Model

The Model class contains these members:

  • aiScene is a custom data type from Assimp which is the root of the model loaded.
  • VertexComponent(s) is an enumeration listing various types of a vertex attributes.
  • VertexLayout(s) specifies how types of vertex attributes are laid out and their byte offsets(offsets) from the first element of components.
  • Dimension indicates the size of the model.
  • Material(s) are materials used by the model and they contains texture names used by the material, while _materialIndices is the material indices referencing the aiMaterial array in aiScene.
  • MVP includes the model, view, and projection matrix of the model.

ReaFile loads a model at the given path and filename, and with optional ModelInfo that we can scale subsequent vertex data. Assimp::Importer loads a model with a flag specifying post processes being executed after loading is finished. If reading file succeeds, we iterate the aiMesh(es) in aiScene with the following actions:

  1. Check whether a particular type of vertex attribute exists and record its existence to components and its offset to offsets.
  2. Iterate all the vertex data to fill up our vertexBuffer, and also, we calculate the size of the model at the same time.
  3. After iterating all the vertices, we know the boundaries of the model so we are able to calculate _dimension.size.
  4. Iterate all the index data to fill up our indexBuffer.
  5. Record the material indices the current aiMesh uses to _materialIndices.
  6. Iterate all the aiMaterial(s) and record their texture names used for various type of texture(diffuse, specular, normal, etc) to _materials.

ModelResource holds the buffer of vertex and indices, the indices count, Mesh(es) recording the vertex/index count and its position in the vertex/index buffer for each Mesh. And also, operations on the buffers cooperate with Device like Texture.

UploadToGPU uploads data from Model to vertices and indices. The method can be divided into three parts:

  1. For each Mesh in Model, we calculate the vertices count with the stride of the current VertexLayout for the current Mesh. And vertexBase and indexBase are accumulated vertexCount and indexCount so far while we iterate the Mesh(es).
  2. As we did on uploading pixels to an image, we need a staging buffer for temporary storage. And copy the data of vertexBuffer and indexBuffer to the corresponding staging buffers.
  3. Record the command that copies the data of the staging buffers to vertices and indices, and submit it to the queue.

--

--