<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Limegarden.net &#187; Wavefront</title>
	<atom:link href="http://limegarden.net/tag/wavefront/feed/" rel="self" type="application/rss+xml" />
	<link>http://limegarden.net</link>
	<description>Personal site of Wouter Lindenhof</description>
	<lastBuildDate>Mon, 06 Sep 2010 21:15:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Wavefront Obj Mesh Loader</title>
		<link>http://limegarden.net/2010/03/02/wavefront-obj-mesh-loader/</link>
		<comments>http://limegarden.net/2010/03/02/wavefront-obj-mesh-loader/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 02:06:47 +0000</pubDate>
		<dc:creator>Wouter Lindenhof</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[code snippit]]></category>
		<category><![CDATA[model loading]]></category>
		<category><![CDATA[Wavefront]]></category>

		<guid isPermaLink="false">http://limegarden.net/2010/03/02/wavefront-obj-mesh-loader/</guid>
		<description><![CDATA[UPDATED 2010-03-02 11:07: There was a minor bug in the code which caused to tokens recognition to file. You won't encounter it in an obj file, but I fixed it for the good order. I can remember the first time I wrote my Obj Mesh loader. It took hours. Today I needed also an obj [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATED 2010-03-02 11:07:</strong> There was a minor bug in the code which caused to tokens recognition to file. You won't encounter it in an obj file, but I fixed it for the good order.</p>
<p>I can remember the first time I wrote my Obj Mesh loader. It took hours. Today I needed also an obj mesh loader and this time it took mere minutes (under 15 minutes at least), so I have decided to share it. Keep in mind you should most likely separate it in header and source files.</p>
<pre class="brush: cpp; ">

/**
 * The MIT License
 *
 * Copyright (c) 2010 Wouter Lindenhof (http://limegarden.net)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the &quot;Software&quot;), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;

#define TOKEN_VERTEX_POS &quot;v&quot;
#define TOKEN_VERTEX_NOR &quot;vn&quot;
#define TOKEN_VERTEX_TEX &quot;vt&quot;
#define TOKEN_FACE &quot;f&quot;

struct Vector2f{
    float x, y;
};
struct Vector3f{
    float x, y, z;
};

struct ObjMeshVertex{
    Vector3f pos;
    Vector2f texcoord;
    Vector3f normal;
};

/* This is a triangle, that we can render */
struct ObjMeshFace{
    ObjMeshVertex vertices[3];
};

/* This contains a list of triangles */
struct ObjMesh{
    std::vector&lt;ObjMeshFace&gt; faces;
};

/* Internal structure */
struct _ObjMeshFaceIndex{
    int pos_index[3];
    int tex_index[3];
    int nor_index[3];
};

/* Call this function to load a model, only loads trianglized meshes */
ObjMesh LoadObjMesh(std::string filename){
    ObjMesh myMesh;

    std::vector&lt;Vector3f&gt;           positions;
    std::vector&lt;Vector2f&gt;           texcoords;
    std::vector&lt;Vector3f&gt;           normals;
    std::vector&lt;_ObjMeshFaceIndex&gt;  faces;
    /**
     * Load file, parse it
     * Lines beginning with:
     * &#039;#&#039;  are comments can be ignored
     * &#039;v&#039;  are vertices positions (3 floats that can be positive or negative)
     * &#039;vt&#039; are vertices texcoords (2 floats that can be positive or negative)
     * &#039;vn&#039; are vertices normals   (3 floats that can be positive or negative)
     * &#039;f&#039;  are faces, 3 values that contain 3 values which are separated by / and &lt;space&gt;
     */

    char char_buffer[256];
    std::ifstream filestream;
    filestream.open(filename.c_str());
    while(filestream.eof() == false &amp;&amp; filestream.bad() == false){
        memset(char_buffer, 0, 256);
        filestream.getline(char_buffer, 256);
        /* FIXED:
         * Where strlen stood was first 256, however this would cause a problem
         * when you only have a token
         */
        std::stringstream str_stream(std::string(char_buffer, strlen(char_buffer)));
        std::string type_str;
        str_stream &gt;&gt; type_str;
        if(type_str == TOKEN_VERTEX_POS){
            Vector3f pos;
            str_stream &gt;&gt; pos.x &gt;&gt; pos.y &gt;&gt; pos.z;
            positions.push_back(pos);
        }else if(type_str == TOKEN_VERTEX_TEX){
            Vector2f tex;
            str_stream &gt;&gt; tex.x &gt;&gt; tex.y;
            texcoords.push_back(tex);
        }else if(type_str == TOKEN_VERTEX_NOR){
            Vector3f nor;
            str_stream &gt;&gt; nor.x &gt;&gt; nor.y &gt;&gt; nor.z;
            normals.push_back(nor);
        }else if(type_str == TOKEN_FACE){
            _ObjMeshFaceIndex face_index;
            char interupt;
            for(int i = 0; i &lt; 3; ++i){
                str_stream &gt;&gt;  face_index.pos_index[i] &gt;&gt; interupt
                           &gt;&gt; face_index.tex_index[i]  &gt;&gt; interupt
                           &gt;&gt; face_index.nor_index[i];
            }
            faces.push_back(face_index);
        }
    }
    filestream.close();

    for(size_t i = 0; i &lt; faces.size(); ++i){
        ObjMeshFace face;
        for(size_t j = 0; j &lt; 3; ++j){
            face.vertices[j].pos        = positions[faces[i].pos_index[j] - 1];
            face.vertices[j].texcoord   = texcoords[faces[i].tex_index[j] - 1];
            face.vertices[j].normal     = normals[faces[i].nor_index[j] - 1];
        }
        myMesh.faces.push_back(face);
    }

    return myMesh;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://limegarden.net/2010/03/02/wavefront-obj-mesh-loader/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
