stephenbakalian
stephenbakalian

Reputation: 1159

How to import a .obj file in DirectX 11

Currently trying to get my 3D modeled ship.obj file imported into my DirectX11 game. And I have two questions about this.

First, are there any simple libraries (with good documentation) to import the .obj in DirectX 11 for beginners?

Second, how does one import and render their model?

Upvotes: 3

Views: 9404

Answers (2)

Chuck Walbourn
Chuck Walbourn

Reputation: 41067

With VS 2012 and VS 2013, the content pipeline can export Wavefront OBJ files as a CMO. The DirectXMesh library includes a command-line tool Meshconvert that will load Wavefront OBJ files and export them as CMOs or SDKMESH files.

DirectX Tool Kit's Model class can load and render from CMOs or SDKMESH files.

DirectX Tool Kit: http://go.microsoft.com/fwlink/?LinkId=248929

DirectXMesh: http://go.microsoft.com/fwlink/?LinkID=324981

There is also a WaveFrontReader class available in the DirectXMesh Utilities folder you can use for an example of parsing Wavefront OBJ files into a VB and IB.

Upvotes: 4

Poriferous
Poriferous

Reputation: 1582

An obj file usually has the following format:

v <numbers>
etc..

vt <numbers>
etc..

vn <numbers>
etc..

f <numbers>
etc..

Where v would be position data; vt texture normals; vn position normals; face would be the indices thereof. So, you have to read each value and place them in the correct container (I recommend vectors due to their ease of use) depending on the first letter. You don't have to put all the values of the indices into a vector since the indices are only numbers that point to a certain vertex of a certain type. Typically, and assuredly, an 'f' line should have values in this format:

f v/vt/vn v/vt/vn v/vt/vn assuming a model is triangulated. This represents three position vertices per triangle which otherwise makes up the entire model. If your model isn't triangulated, then you would have 4 groups of values, each pointing to a value within each of those groups.

At which point you need to order your position vertices, normals and texture normals thereof according to what index is being pointed to in the line 'f'. Some pseudo-code:

for (int i = 0; i < indices; i++)
{
    orderedPositions.push_back(unOrderedPositions[positionIndex[indices] * # of indices + k]);
    // Same applies to normals and textures
}

// Where k is the group number of the face from 1 to n depending on how many groups of faces there are per line.

Hope this helps!

Upvotes: 0

Related Questions