• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    316
  • comments
    485
  • views
    321135

Simple Mesh Loaders

Sign in to follow this  
Followers 0
Jason Z

633 views

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
Sign in to follow this  
Followers 0


2 Comments


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

1

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now