top of page

Using index buffers and creating a material file format using Lua

  • itzvnodm
  • Sep 23, 2014
  • 2 min read
Purpose:

The assignment was to

  • Understand the importance of and use the index buffer to draw a rectangle with minimum number of vertices.

  • Create human readable material file that stores the path to VertexShader and FragmentShader using Lua tables as data description.

Program output:
Output.PNG
Coordinates values for vertices:

The above window spans in X axis from -1 to +1 and in Y axis from -1 to +1. The rectangles coordinates are

  • -0.5, -0.5 (Red)

  • -0.5, 0.5 (Green)

  • 0.5, 0.5 (Blue)

  • 0.5, -0.5 (White)

IndexBuffer:

In the previous assignment, to render a triangle, I had to input three vertices to the GPU. In order to draw anything on to window, GPU draws them as a set of triangles which are in turn set of three vertices per triangle. If we are to render a rectangle, GPU needs vertices for two triangles (6 vertices). Although the only minimum vertices required are 4 (one for each edge of a rectangle).

Rectangle.PNG

Although storing 6 vertices instead of 4 doesn’t seem to be a huge waste of memory, but consider rendering a cube.

Cube.jpg

Instead of 8 vertices (96 bytes considering each vertex of size 12 bytes- 3 floats) we have to input 36 vertices (432 bytes), leading to the waste of 336 bytes. In order to optimize this DirectX has the concept of IndexBuffer.

IndexBuffer allows us to reuse individual vertices. We can add the unique vertices only to vertex buffer and expand the list of triangles using the index buffer. This integer values ​​in IndexBuffer are used, which simply specify the index of each vertex in the vertex buffer.

PIX Output:

The below screenshot shows the details of 12th frame that was captured. In Events window, PIX shows all the DirectX calls in that frame. DrawIndexedPrimitive call draws the input vertices on the screen from index buffer with mapped vertices from the vertex shader.

PixOutput.PNG

The details tab->Mesh->PreVS, shows the indices in index buffer, the mapped input vertex values from vertex buffer and any other user data (Color in our case).

PreVS.PNG

The PreVs tab shows the different indices used to specify the vertices for two triangles.

Rectangle - Copy.PNG

Indices of first Triangle: 0, 1, 3

Indices of second Triangle: 1, 2 3

Creating a human readable material file:

A material file is a Lua script which looks like below. It stores the path for vertex shader and fragment shader which are .hlsl files.

Material.PNG

return is Top level table. VertexShader and FragmentShader are tables with one entry each specifying their respective paths. The program pretty much looks for tables named VertexShader and FragmentShader and loads the shaders with those paths. I chose tables for them anticipating if I ever had to store the shaders based on the name and check if the shader was already loaded. One can edit the path of the shaders with a different name and the program loads the new value.

Extra: Separated out the engine.

Time Spent:

Rendering a rectangle using IndexBuffer: 1hr

Understanding Lua tables: 3.5hr

Modifying material class to load from table: 2hr

Extra: 0.5hr

Writeup: 2hr

Total: 9hr


 
 
 

Comments


Recent Posts
Archive
Search By Tags
bottom of page