
Wavefront Obj Mesh Loader
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.
/** * 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 "Software"), 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 "AS IS", 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 <string> #include <vector> #include <sstream> #include <fstream> #define TOKEN_VERTEX_POS "v" #define TOKEN_VERTEX_NOR "vn" #define TOKEN_VERTEX_TEX "vt" #define TOKEN_FACE "f" 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<ObjMeshFace> 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<Vector3f> positions; std::vector<Vector2f> texcoords; std::vector<Vector3f> normals; std::vector<_ObjMeshFaceIndex> faces; /** * Load file, parse it * Lines beginning with: * '#' are comments can be ignored * 'v' are vertices positions (3 floats that can be positive or negative) * 'vt' are vertices texcoords (2 floats that can be positive or negative) * 'vn' are vertices normals (3 floats that can be positive or negative) * 'f' are faces, 3 values that contain 3 values which are separated by / and <space> */ char char_buffer[256]; std::ifstream filestream; filestream.open(filename.c_str()); while(filestream.eof() == false && filestream.bad() == false){ memset(char_buffer, 0, 256); filestream.getline(char_buffer, 256); std::stringstream str_stream(std::string(char_buffer, 256)); std::string type_str; str_stream >> type_str; if(type_str == TOKEN_VERTEX_POS){ Vector3f pos; str_stream >> pos.x >> pos.y >> pos.z; positions.push_back(pos); }else if(type_str == TOKEN_VERTEX_TEX){ Vector2f tex; str_stream >> tex.x >> tex.y; texcoords.push_back(tex); }else if(type_str == TOKEN_VERTEX_NOR){ Vector3f nor; str_stream >> nor.x >> nor.y >> nor.z; normals.push_back(nor); }else if(type_str == TOKEN_FACE){ _ObjMeshFaceIndex face_index; char interupt; for(int i = 0; i < 3; ++i){ str_stream >> face_index.pos_index[i] >> interupt >> face_index.tex_index[i] >> interupt >> face_index.nor_index[i]; } faces.push_back(face_index); } } filestream.close(); for(size_t i = 0; i < faces.size(); ++i){ ObjMeshFace face; for(size_t j = 0; j < 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; }

Procedural story
A little while ago I was meeting a good friend of mine and we had a lot of things to talk about. For one he had just returned from Kenya and he had a lot to show and tell and the things he told me where amazing. But as the day went one, we almost always seem to come back to one subject: Games and developing of games. Funny enough the two of us have almost had the same kind of ideas.
Both of us are developing games in our free time and both have the idea of trying to make as much as possible procedural generated. One of the things we are both interested in is creating the story. While I look for an infinite solution (where the player can play forever and has an interesting storyline) he has a more pragmatic approach.

Nova’s architecture
In November 2009 I posted a blog entry that Nova was the fifth engine that I was going to write and some of the basic ideas that I had at that time. Now its two months later and I have decided to become a bit more public about Nova.
Let me start with saying that the actual act of writing code has only started this week and there is still a lot of work to be done. I did start earlier but every time the engine looked similar to the previous engines I have written which all had a single flaw: Once I needed a certain function I had to hack it in, and I don’t like to hack things. Obviously this has changed since the beginning of this week and I will begin there.

Updated the website
It has taken a bit of time but I have completely updated the layout of the website which was delayed far too long.
Biggest change is that comments are enabled again and I'm using Mollom as a method to prevent spam and I hope it works as advertised.
The second change is that I have decided to start using geshi for my code color formatting (and this blog entry is to test it). If correct the RSS feed should now show the code in full color.
#include <mylib.h> int main(int argc, const char* argv[]) { return 0; // A quick and dirty test }

Looking forward
2010 is going to be an exciting year for me. First of all I’m intended to graduate this summer which brings me to a logical "second of all" which is that I’m going to look for a job in the game industry.
School is not going to be a problem this year, only one more project (my graduation project) and I will have my degree in "International Game Architecture and Design" as a programmer.

Writing using Latex
Writing is something I always enjoyed, but there were always obstacles that I had to work around. In the past I always had to deliver documents but this became more and more troublesome as there was a difference between Microsoft Word and Word Perfect and these days I sometimes encounter someone who has MS Word 2003, which is really outdated by now.

Going Nova!
Nova is the fifth engine that I’m intending to write which is not something I’m proud of. The first four engines were not bad as they were often reincarnations of the previous versions but each and every time I knew it could be done better.

Finishing my school project
As some know I have talked a lot about procedural generation and since last summer one of my ideas has been accepted. And now two months later I have finished my research paper.
I never really liked research papers, in many cases there is too much data to work through and in generally I’m only interested in the solution. However when you have to write your very own research paper of 3000 words your glad to have much data except when it reaches 4500 words then it becomes a pain since you have to spell-check and grammar-check those extra 1500 words. And then to think that I’m still not glad with some explanations and think some part of the document needs to be improved. Currently I’m guessing it’s going to be around the 5250 words.

Donuts! (Procedural Torus in C++)
Recently I got a mail through the contact form of my website about texture mapping on torus (a donut) who said that D3DXCreateTorus didn’t not provide texture coordinates. The reason why DirectX and OpenGL (and GLUT) don’t provide texture coordinates with these function is best seen when you generate a cube. A cube has 8 vertices, however when you add texture coordinates it will increase because otherwise they will share the same texture coordinate. With normals it is no problem (in fact you would prefer that) but if you have a dice (from 1 to 6) than each vertex would use a single texture coordinate for three sides. Just draw an unfolded dice on paper where each edge is connected, but does not cross another edge. It is simply not possible.

Memory leaks
Nobody likes memory leaks and especially when it is far in the project and you need to go through a heap of issues to get it solved. And yes, "heap of issues" was meant as a joke.
However on Windows it is fairly easy. All you need to do is call _CrtDumpMemoryLeaks (include crtdbg.h) at the end of the program and it will report all the allocated memory that has not yet been deallocated. so a simple example:

The last two months
This is going to be a huge entry about the last two months of what has happened.
The last two months have been quite hectic. There was a heap of work that had to be done and I am glad to say that everything is done. In this update I will talk about various things. First my awesome internship at Codeglue, second I have finally written an application in which I could use the acyclic visitor with observer pattern, third I would like to point to a site of an artistic friend of mine who is following the same education as I do. Fourth I have finally started on my challenge of procedural design.

Rewriting examples: Direct3D tutorial 1 with D3DVERTEXELEMENT9
A while ago a friend asked me how to do something with Direct3D (better known as DirectX) about streams. I really enjoyed it since it was sometime I have worked with my favorite 3D API and so I explained the usage of VertexDeceleration by rewriting the very first tutorial of Direct3D of which you can see the result below

Accessing other derivated protected function
Have you ever encountered that you made a function protected and then discovered you needed to access it from a derivation to another derivation?
For example:

The new and improved acyclic visitor with observer pattern (C++)
I have improved my “Acyclic Visitor with Observer” pattern (here is a link to the old one). Now you can create classes that are derived from two Acyclic visitors, something that was not possible with the previous version. On top of that I have made it less complex to use. On top of that they no longer have generic names (like “INotification”). The coolest thing is the registering. Previously you had to create the objects in the user code and also specify the class that should receive it. Now, it's simply done when you define the class.
Anyway just compare the code:
The old code

Freedom of Information
There are two purposes to my blog. One is self-branding, advertising yourself so to speak. I think that it's really useful since the majority of the employers can simply google or bing your name to get a quick impression of you. For fun you should just search your own name and count how many times you are mentioned at good sites and bad sites. The second reason, is to use my website as an source for information.