Simple Mesh Loaders

Published June 27, 2014
Advertisement
Over the years, I have relied on the trusty old Milkshape3D file format for getting my 3D meshes into my engine. When I first started out in 3D programming, I didn't have a lot of cash to pick up one of the heavy duty modeling tools, so I shelled out the $20 for Milkshape and used that for most of my model loading needs. It came with a simple SDK that I used to understand the format, and then I wrote a file loader for it which worked just fine (despite my lack of experience writing such things...).

Later on, a PLY loader was written by Jack Hoxley (jollyjeffers for those of you who have been around here a while) while we were working on Practical Rendering and Computation with Direct3D 11. Other than these two formats, all other geometry loaded into the engine was procedurally created or just brute force created in code. I had been thinking of integrating AssImp for quite a while, and finally sat down to try it out.

While I have lots of respect for the authors of AssImp, and I think it is a great project that meets a big need, I decided not to incorporate it into Hieroglyph 3. In general, I don't like adding dependencies to the library unless they are absolutely needed. AssImp seemed potentially worth the hassle, so I spent a day or two reading its docs and trying to get a feel for how the API worked and what I would need to do to get it up and running. By the time I was done, I felt relatively confident that I could get something up and running quickly.

So I tried to integrate the building of AssImp into my solution and add it as one of the primary projects in the build chain. I messed with the project files for about 45 minutes, and finally decided that it wasn't meant to be - if I can't add a project into a solution seamlessly in the first few tries, then something isn't working. Either their build system is different, or I'm not understanding something, or whatever - I just didn't want to add a bunch of complexity to the engine just to add more file format capability.

Instead, I decided I would simply write some basic file loaders for the formats that I wanted to work with. To start out with, I implemented the STL loader, which was actually exceedingly easy to do. In fact, here is the complete code for the loader://--------------------------------------------------------------------------------// This file is a portion of the Hieroglyph 3 Rendering Engine. It is distributed// under the MIT License, available in the root of this distribution and // at the following URL://// http://www.opensource.org/licenses/mit-license.php//// Copyright (c) Jason Zink //--------------------------------------------------------------------------------// This is a simple loader for STL binary files. The usage concept is that the// face data gets loaded into a vector, and the application can then use the face// data as it sees fit. This simplifies the loading of the files, while not // making decisions for the developer about how to use the data.//// Our face representation eliminates the unused AttributeByteCount to allow each// face to align to 4 byte boundaries. More information about the STL file format // can be found on the wikipedia page:// http://en.wikipedia.org/wiki/STL_%28file_format%29.//--------------------------------------------------------------------------------#ifndef MeshSTL_h#define MeshSTL_h//--------------------------------------------------------------------------------#include #include #include "Vector3f.h"//--------------------------------------------------------------------------------namespace Glyph3 { namespace STL {templatevoid read( std::ifstream& s, T& item ){ s.read( reinterpret_cast(&item), sizeof(item) );}//--------------------------------------------------------------------------------class MeshSTL{public: MeshSTL( const std::wstring& filename ) : faces() { unsigned int faceCount = 0; // Open the file for input, in binary mode, and put the marker at the end. // This let's us grab the file size by reading the 'get' marker location. // If the file doesn't open, simply return without loading. std::ifstream stlFile( filename, std::ios::in | std::ios::ate | std::ios::binary ); if ( !stlFile.is_open() ) { return; } unsigned int fileSize = static_cast( stlFile.tellg() ); // Skip the header of the STL file, and read in the number of faces. We // then ensure that the file is actually large enough to handle that many // faces before we proceed. stlFile.seekg( 80 ); read( stlFile, faceCount ); if ( fileSize < 84 + faceCount * FILE_FACE_SIZE ) { return; } // Now we read the face data in, and add it to our vector of faces. We // provided an ifstream constructor for our face to allow constructing // the vector elements in place. Before starting the loop, we reserve // enough space in the vector to ensure we don't need to reallocate while // loading (and skip all of the unneeded copying...). faces.reserve( faceCount ); for ( unsigned int i = 0; i < faceCount; ++i ) { faces.emplace_back( stlFile ); } }public: struct Face { Face( std::ifstream& s ) { read( s, normal ); // Read normal vector read( s, v0 ); // Read vertex 0 read( s, v1 ); // Read vertex 1 read( s, v2 ); // Read vertex 2 s.seekg( 2, std::ios_base::cur ); // Skip 2 bytes for unused data } Vector3f normal; Vector3f v0; Vector3f v1; Vector3f v2; }; static const unsigned int FILE_FACE_SIZE = sizeof(Vector3f)*4 + sizeof(unsigned short); std::vector faces;};} }//--------------------------------------------------------------------------------#endif // MeshSTL_h//--------------------------------------------------------------------------------
That's the whole thing - in a single header file. The general idea is to load the file contents into memory, and then let the developer decide how to use that data to generate the needed vertices and indices. I don't necessarily know exactly what vertex layout and all that in advance, so having flexibility is pretty important in Hieroglyph 3. Once I wrote this (which I would be happy to get criticism on by the way!) I decided that I could also write an OBJ loader, along with the corresponding MTL file loader to go with it. I am quite honestly so happy that I went this path instead of using another third party library - now I just need to add a single header file, and I have access to a new format.
4 likes 2 comments

Comments

TheChubu

No tex coords in STL?

June 27, 2014 09:54 AM
Jason Z

Nope - there is a way to add color to the vertices, but generally it is a geometry only file format (vertex position + face normal).

June 27, 2014 11:04 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement