# DirectX/10.0/Direct3D/Loading Maya

This tutorial will cover how to import static 3D models from Maya 2011. Note that this tutorial will be focused on Maya but it also applies to pretty much any other 3D modeling software package with some slight changes.
In the previous tutorials we have already created our own model format and rendered 3D models using that format.
The goal now is to convert Maya 2011 models into our format and render them. I won't go into how to model 3D objects in Maya as there are hundreds of tutorials on the net already dedicated to that, we will instead start at the point where you have a textured and triangulated 3D model ready for export.
For the Maya export format we will use the .OBJ format as it is easily readable and good for beginners to start with.
To export your model in the .obj format you must first enable the .OBJ exporter in Maya. Click "Window", then "Settings/Preferences", then "Plug-in Manager". Scroll down to objExport.mll and select both "Loaded" and "Auto load". Now to export your model in this format click on "File", then "Export All". Now at the bottom select "Files of type:" and scroll down and select "OBJexport". Give it a file name and hit "Export All" and it will export it to a text file with a .obj extension. To look at the file you can right click and select Open With and choose WordPad to read the file.
You will then see something that looks like the following:

## Cube.obj

This particular .OBJ model file represents a 3D cube. It has 8 vertices, 24 texture coordinates and normal vectors, and 6 sides made up of 12 faces in total. When examining the file you can ignore every line unless it starts with a "V", "VT", "VN", or "F".
The extra information in the file will not be needed for converting .obj to our file format. Lets look at what each of the important lines means:
1. The "V" lines are for the vertices. The cube is made up of 8 vertices for the eight corners of the cube. Each is listed in X, Y, Z float format.
2. The "VT" lines are for the texture coordinates. The cube is has 24 texture coordinates and most of them are duplicated since it records them for every vertex in every triangle in the cube model. They are listed in TU, TV float format.
3. The "VN" lines are for the normal vectors. The cube is has 24 normal vectors and most of them are duplicated again since it records them for every vertex in every triangle in the cube model. They are listed in NX, NY, NZ float format.
4. The "F" lines are for each triangle (face) in the cube model.
The values listed are indexes into the vertices, texture coordinates, and normal vectors. The format of each face is:
So a line that says "f 3/13/5 4/14/6 5/15/7" then translates to "Vertex3/Texture13/Normal5 Vertex4/Texture14/Normal6 Vertex5/Texture15/Normal7".
The order the data is listed in the .obj file is very important. For example the first vertex in the file corresponds to Vertex1 in the face list. This is the same for texture coordinates and normals as well.
Looking at the face lines in the .obj file notice that the three index groups per line make an individual triangle. And in the case of this cube model the 12 total faces make up the 6 sides of the cube that has 2 triangles per side.

## Right hand to Left hand conversion

By default Maya 2011 is a right handed coordinate system and exports the .obj file data in right hand coordinates. To convert that data into a left handed system which DirectX 11 is by default you have to do the following:
1. Invert the Z coordinate vertices. In the code you will see it do this: vertices[vertexIndex].z = vertices[vertexIndex].z * -1.0f;
2. Invert the TV texture coordinate. In the code you will see it do this: texcoords[texcoordIndex].y = 1.0f - texcoords[texcoordIndex].y;
3. Invert the NZ normal vertex. In the code you will see it do this: normals[normalIndex].z = normals[normalIndex].z * -1.0f;
4. Convert the drawing order from counter clockwise to clockwise.
In the code I simply read in the indexes in reverse order instead of re-organizing it after the fact:
With those four steps complete the model data will be ready for DirectX 11 to render correctly.

## Main.cpp

The program to convert the Maya 2011 .obj files into our DirectX 11 format is fairly simple and is a single program file called main.cpp. It opens a command prompt and asks for the name of the .obj file to convert. Once the user types in the name it will attempt to open the file and read in the data counts and build the structures required to read the data into. After that it reads the data into those structures and converts it to a left hand system. Once that is done it then writes the data out to a model.txt file. That file can then be renamed and used for rendering in DirectX 11 using the 3D model render project from the previous tutorial.

## Summary

We can now convert Maya 2011 .obj files into our simple model format.

## To Do Exercises

1. Recompile the program and run it with the supplied .obj model file.
2. Create (or have someone else make for you) a Maya 2011 model and export it in .obj format and run this program to convert it.
3. Convert this code to read in and export a different model format that you might prefer.