# DirectX/10.0/Direct3D/3D Model Rendering

This tutorial will cover how to render 3D models in DirectX 11 using HLSL.
The code in this tutorial is based on the code from the diffuse lighting tutorial.
We have already been rendering 3D models in the previous tutorials, however they were composed of a single triangle and were fairly uninteresting. Now that the basics have been covered we'll move forward to render a more complex object. In this case the object will be a cube. Before we get into how to render more complex models we will first talk about model formats.
There are many tools available that allow users to create 3D models.
Maya and 3D Studio Max are two of the more popular 3D modeling programs. There are also many other tools with less features but still can do the basics for what we need.
Regardless of which tool you choose to use they will all export their models into numerous different formats. My suggestion is that you create your own model format and write a parser to convert their export format into your own format. The reason for this is that the 3D modeling package that you use may change over time and their model format will also change.
Also you may be using more than one 3D modeling package so you will have multiple different formats to deal with. So if you have your own format and convert their formats to your own then your code will never need to change. You will only need to change your parser program(s) for changing those formats to your own. As well most 3D modeling packages export a ton of junk that is only useful to that modeling program and you don't need any of it in your model format.
The most important part to making your own format is that it covers everything you need it to do and that it is simple for you to use. You can also consider making a couple different formats for different objects as some may have animation data, some may be static, and so forth.
The model format I'm going to present is very basic. It will contain a line for each vertex in the model. Each line will match the vertex format used in the code which will be position vector (x, y, z), texture coordinates (tu, tv), and the normal vector (nx, ny, nz). The format will also have the vertex count at the top so you can read the first line and build the memory structures needed before reading in the data. The format will also require that every three lines makes a triangle, and that the vertices in the model format are presented in clockwise order. Here is the model file for the cube we are going to render:

## Cube.txt

So as you can see there are 36 lines of x, y, z, tu, tv, nx, ny, nz data. Every three lines composes its own triangle giving us 12 triangles that will form a cube. The format is very straight forward and can be read directly into our vertex buffers and rendered without any modifications.
Now one thing to watch out for is that some 3D modeling programs export the data in different orders such as left hand or right hand coordinate systems. Remember that by default DirectX 11 is a left-handed coordinate system by default and so the model data needs to match that. Keep an eye out for those differences and ensure your parsing program can handle converting data into the correct format/order.

## Modelclass.h

For this tutorial all we needed to do was make some minor changes to the ModelClass for it to render 3D models from our text model files.
The fstream library is now included to handle reading from the model text file.
The next change is the addition of a new structure to represent the model format. It is called ModelType. It contains position, texture, and normal vectors the same as our file format does.
The Initialize function will now take as input the character string file name of the model to be loaded.
We also have two new functions to handle loading and unloading the model data from the text file.
The final change is a new private variable called m_model which is going to be an array of the new private structure ModelType. This variable will be used to read in and hold the model data before it is placed in the vertex buffer.

## Modelclass.cpp

The new model structure is set to null in the class constructor.
The Initialize function now takes as input the file name of the model that should be loaded.
In the Initialize function we now call the new LoadModel function first. It will load the model data from the file name we provide into the new m_model array. Once this model array is filled we can then build the vertex and index buffers from it. Since InitializeBuffers now depends on this model data you have to make sure to call the functions in the correct order.
In the Shutdown function we add a call to the ReleaseModel function to delete the m_model array data once we are done.
Take note that we will no longer manually set the vertex and index count here. Once we get to the ModelClass::LoadModel function you will see that we read the vertex and index counts in at that point instead.
Loading the vertex and index arrays has changed a bit. Instead of setting the values manually we loop through all the elements in the new m_model array and copy that data from there into the vertex array. The index array is easy to build as each vertex we load has the same index number as the position in the array it was loaded into.
This is the new LoadModel function which handles loading the model data from the text file into the m_model array variable. It opens the text file and reads in the vertex count first. After reading the vertex count it creates the ModelType array and then reads each line into the array. Both the vertex count and index count are now set in this function.
The ReleaseModel function handles deleting the model data array.

## Graphicsclass.h

The header for the GraphicsClass has not changed since the previous tutorial.

## Graphicsclass.cpp

The model initialization now takes in the filename of the model file it is loading. In this tutorial we will use the cube.txt file so this model loads in a 3D cube object for rendering.
I have changed the diffuse light color to white for this tutorial.

## Summary

With the changes to the ModelClass we can now load in 3D models and render them. The format used here is just for basic static objects with lighting, however it is a good start to understanding how model formats work.

## To Do Exercises

1. Recompile the code and run the program. You should get a rotating cube with the same seafloor.dds texture on it. Press escape to quit once done.
2. Find a decent 3D modeling package (hopefully something free) and create your own simple models and export them. Start looking at the format.
3. Write a simple parser program that takes the model exports and converts it to the format used here. Replace cube.txt with your model and run the program.