Sunday, January 18, 2015

OpenGL + Assimp !!!

Assimp had a proven track record with me. Earlier, I integrated the same with DirectX. This time I decided to give it a go with OpenGL. In my ultimate triumph to master OpenGl, so far I am successful in taking care of lots of basic stuff. The terminologies, understanding of OpenGL as a state machine, VBOs, VAOs etc etc. Eventually, I started with coding a small framework with all the things I learned. 

So far, with whatever I learned with respect to vbo & vao was converted into a standalone GLCube class. This class is responsible for drawing a single cube with specified color at a specified world location. Later on, it turned out this to be a great visual debugging tool. 

Here is the screenshot of three cubes R,G,B colored located at (-5,0,0), (0,0,0) & (5,0,0) in world space.  

And of course the wireframe showing the mesh topology.

Once this was done, it was a quick realization that I had to turn my attention towards loading custom model formats if I need to have better lit scene & better looking models than the normal cubes. Without wasting a minute, i started looking at Assimp integration with OpenGL. I quickly realized that it will be bit different than the DirectX integration due to the way opengl behaves, as a state machine. After googling a lot on this I came across wonderfully written tutorial & a complete website for Opengl learning. I personally, highly recommend going through as much as possible through this website for easy to understand OpenGL tutorials. Here is the link : 

LearnOpenGL : Assimp Loading

Once you go through these tutorials assimp is a piece of cake. The core idea remains the same for both DirectX & OpenGL, grab the data into vertex buffers & index buffers, fetch all the materials assigned, do the texture loading yourself, use a viable data structure for storage & you are done! Key is also to understand how Assimp's node structure & mesh data structures are co-related. Author in the mentioned link is spot on with the explanation, so I will leave it to that page itself. Here is the screenshot of deer model grabbed free from the internet, mesh format : collada.  

Before I proceed with further management of code, I realized that if loaded model has no texture in it, i can apply a default texture over it just so that user(me only) will come to know about missing texture information on the object. This was easy to handle during texture loading stage itself using the condition : 

if( material->GetTextureCount(aiTextureType_DIFFUSE) == 0 ) 

Here is the screenshot for torus without any texture information in it, but with default texture applied anyways during rendering : 

Alright! So now I can load any format mesh that assimp supports in my framework! But what if I need to load many more of these models? I need to have a way to place them in world space location. This led to the class called GameObject. This class acts as a container for Assimp Model, Shader used & its world space transformations! 

Here is the minimalistic GameObject class. It has Model object, Shader object & transformations. Other extra params such as id & name are for the book keeping purpose which will used later on in the framework for overall management of all the GameObjects in the scene. But that is for the later.

Loading any mesh in the scene is very easy with few lines of code mainly creating GameObject instance, assigning it a position, rotation or scale as per need & then calling Render() on it. Here is the final output of our earlier 3 cubes along with newly loaded collada & obj format meshes at the same world location. 

So far framework has simple things such as Fly through camera, Input handlers for Keyborad & Mouse using GLFW, & bunch of helper classes. 

Next will be my favorite area : Lighting & Shading! 

Stay tuned. 

No comments: