Importing a Blender Model into OpenGL

August 31st, 2009
By: Cory Mathews

Recently I was looking for a simple way to export a basic model from blender to use in my opengl code written with c++. After a few searches I came up with nothing and decided to look at all the different ways I could export a blender file. The first that was easy enough to recognize the pattern right away was the the Raw Faces (.raw) format.

I quickly created a very basic (and very bad) model of a cup to see how it looked when I exported the cup in the .raw format. You can export a blender model using this format by first selecting the object you want to export (For whatever reason only the selected object(s) will be exported.) Then go to File -> Export -> Raw Faces(.raw). This is what I got after exporting. There shouldn’t be line wraps, however depending on your monitor you may see them. You can download the file in .raw format here.

0.011633 -0.011633 0.011633 -0.001836 -0.016245 0.011633 -0.001512 -0.011633 -0.011633 0.008956 -0.008956 -0.011633
-0.001836 -0.016245 0.011633 -0.011633 -0.011633 0.011633 -0.008956 -0.008956 -0.011633 -0.001512 -0.011633 -0.011633
0.008956 0.008956 -0.011633 -0.001512 0.011633 -0.011633 -0.001836 0.016993 0.011633 0.011633 0.011633 0.011633
-0.001512 0.011633 -0.011633 -0.008956 0.008956 -0.011633 -0.011633 0.011633 0.011633 -0.001836 0.016993 0.011633
0.011633 -0.001745 -0.011633 0.008956 0.008956 -0.011633 0.011633 0.011633 0.011633 0.016943 -0.002119 0.011633
0.016943 -0.002119 0.011633 0.011633 -0.011633 0.011633 0.008956 -0.008956 -0.011633 0.011633 -0.001745 -0.011633
-0.011633 -0.011633 0.011633 -0.016295 -0.002119 0.011633 -0.011633 -0.001745 -0.011633 -0.008956 -0.008956 -0.011633
-0.016295 -0.002119 0.011633 -0.011633 0.011633 0.011633 -0.008956 0.008956 -0.011633 -0.011633 -0.001745 -0.011633
-0.001512 -0.011633 -0.011633 -0.001512 -0.001745 -0.011633 0.011633 -0.001745 -0.011633 0.008956 -0.008956 -0.011633
0.008956 0.008956 -0.011633 0.011633 -0.001745 -0.011633 -0.001512 -0.001745 -0.011633 -0.001512 0.011633 -0.011633
-0.008956 -0.008956 -0.011633 -0.011633 -0.001745 -0.011633 -0.001512 -0.001745 -0.011633 -0.001512 -0.011633 -0.011633
-0.011633 -0.001745 -0.011633 -0.008956 0.008956 -0.011633 -0.001512 0.011633 -0.011633 -0.001512 -0.001745 -0.011633
-0.011633 0.011633 0.011633 -0.016295 -0.002119 0.011633 -0.013645 -0.001861 0.011633 -0.009766 0.009583 0.011633
-0.016295 -0.002119 0.011633 -0.011633 -0.011633 0.011633 -0.009766 -0.009779 0.011633 -0.013645 -0.001861 0.011633
0.011633 -0.011633 0.011633 0.016943 -0.002119 0.011633 0.014015 -0.001861 0.011633 0.009596 -0.009779 0.011633
0.016943 -0.002119 0.011633 0.011633 0.011633 0.011633 0.009596 0.009583 0.011633 0.014015 -0.001861 0.011633
-0.011633 -0.011633 0.011633 -0.001836 -0.016245 0.011633 -0.001613 -0.013616 0.011633 -0.009766 -0.009779 0.011633
-0.001836 -0.016245 0.011633 0.011633 -0.011633 0.011633 0.009596 -0.009779 0.011633 -0.001613 -0.013616 0.011633
-0.001836 0.016993 0.011633 -0.011633 0.011633 0.011633 -0.009766 0.009583 0.011633 -0.001613 0.014044 0.011633
0.011633 0.011633 0.011633 -0.001836 0.016993 0.011633 -0.001613 0.014044 0.011633 0.009596 0.009583 0.011633
0.009596 0.009583 0.011633 -0.001613 0.014044 0.011633 -0.001311 0.010066 -0.007815 0.006850 0.006819 -0.007815
-0.001613 0.014044 0.011633 -0.009766 0.009583 0.011633 -0.007247 0.006819 -0.007815 -0.001311 0.010066 -0.007815
-0.001613 -0.013616 0.011633 0.009596 -0.009779 0.011633 0.006850 -0.007278 -0.007815 -0.001311 -0.010072 -0.007815
-0.009766 -0.009779 0.011633 -0.001613 -0.013616 0.011633 -0.001311 -0.010072 -0.007815 -0.007247 -0.007278 -0.007815
0.014015 -0.001861 0.011633 0.009596 0.009583 0.011633 0.006850 0.006819 -0.007815 0.010067 -0.001513 -0.007815
0.009596 -0.009779 0.011633 0.014015 -0.001861 0.011633 0.010067 -0.001513 -0.007815 0.006850 -0.007278 -0.007815
-0.013645 -0.001861 0.011633 -0.009766 -0.009779 0.011633 -0.007247 -0.007278 -0.007815 -0.010071 -0.001513 -0.007815
-0.009766 0.009583 0.011633 -0.013645 -0.001861 0.011633 -0.010071 -0.001513 -0.007815 -0.007247 0.006819 -0.007815
-0.001311 0.010066 -0.007815 -0.001115 -0.001287 -0.007815 0.010067 -0.001513 -0.007815 0.006850 0.006819 -0.007815
-0.001115 -0.001287 -0.007815 -0.001311 -0.010072 -0.007815 0.006850 -0.007278 -0.007815 0.010067 -0.001513 -0.007815
-0.007247 0.006819 -0.007815 -0.010071 -0.001513 -0.007815 -0.001115 -0.001287 -0.007815 -0.001311 0.010066 -0.007815
-0.010071 -0.001513 -0.007815 -0.007247 -0.007278 -0.007815 -0.001311 -0.010072 -0.007815 -0.001115 -0.001287 -0.007815

Basically, if you haven’t already noticed, it is 4 sets of x,y,z points. You will not get any of the materials when you export the object only the polygons.

Now that we have our object we will need to be able to display it using opengl. First we must read in and store the points.

CupVectors[3][4][33];

ifstream cupFile;
cupFile.open(“cup.raw”);

for(int z=0;z<=32;z++)
for(int y=0;y<=3;y++)
for(int x=0;x<=2;x++)
cupFile >> CupVectors[x][y][z];

cupFile.close();

Pretty simple code. Store the points, open the file, loop through the points, store the points, close the file.

Now that the cup vertex points are stored we are able to easily display them in opengl with another small loop that will create the triangles and save them in our display list.