This document is a simple guide on one of the ways in which 3D models can be loaded and displayed using OpenGL.
- Blender (open source) or a similar program that allows to export 3D models in the .obj format. We’ll be using Blender to export the .obj file with the essential information to draw the 3D model, without information on textures, for example.
- i.MX6 Linux BSP image with X11 support – For this document we’ll use the L3.10.31 BSP, which is compiled using Yocto 1.6.You may use a newer BSP. We’ll use the fsl-image-gui. You may use a Qt5 image with X11 from newer BSPs.
- GCC Toolchain -You may either cross compile on a Host or compile on the same board by providing the necessary libraries and toolchain. On the L3.10.31 you need to manually add GCC to your baked image. In newer releases this may not necessary.
For adding the GCC package to a Yocto image and compiling within the board itself please add the following line to the conf/local.conf file inside your build directory.
IMAGE_INSTALL_append += " gcc libgcc"
If you wish to cross compile from your host and then run on your board you first will need to extract the toolchain from the BSP, which can be done by following the instructions of the next document:
- FreeGLUT – FreeGLUT is an open source alternative to the OpenGL Utility Toolkit (GLUT), a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL. This is not necessary for drawing the model on the window but foes allow for functions such as rotating it.
You may install it on your host with the following command:
$ sudo apt-get install freeglut3-dev
For additional information and downloads of FreeGLUT please refer to the projects website:
- i.MX6D/Q/DL/S/SX GPU Demo Framework SDK – We’ll use the GPU Test examples which are available at the following link. Source code for this document implementation is attached but for more examples of OpenGL ES please refer to this SDK. (Please note that you may need to login in order to download this file)
- i.MX6Q Board – For this example we will be using the i.MX6Q SABRE Board, but you may run OpenGL ES on any i.MX6Q board provided that you provide the necessary packages to support OpenGL ES.
Brief introduction to OpenGL ES?
OpenGL is a software interface to hardware accelerated graphics. The API of this interface consists of about 150 distinct commands that allow you to specify objects and perform operations on them in order to produce interactive three-dimensional applications.
OpenGL ES is the OpenGL implementation for Embedded Systems. Somei.MX6 Processors like the i.MX6Q possess a Vivante GPU module that runs on OpenGL ES.
When using OpenGL or OpenGL ES all 3D objects are descripted as a series of triangles. This is important to mention as it will make the instructions we will need for describing our model make more sense.
Step 1 - Exporting a model as .obj
You may import a 3D model from other sources or make your own simple 3D model in blender. For this example we’ll make a simple 3D NXP logo and export it.
Once you have your model ready, select the object in objet mode with a right click.
Once selected select File > Export > Wavefront (.obj)
You may now select where and with what name to export the object file. On the left panel you will have the export options. It’s important to leave all options unchecked except for:
(You may change the scale of your model and it won’t negatively affect the process)
The .obj model may be opened with a text editor and we’ll see that it basically describes the object as a series of parameters that may include vertex data, free-form curve/surface attributes, elements, free-form curve/surface body statements, connectivity between free-form surfaces, grouping and display/render attribute information.
For our example we’ll be using a simple file that just contains:
- List of geometric vertices, with (x,y,z) coordinates
v 0.292475 0.017345 -0.152653
- List of vertex normals in (x,y,z) form
vn 0.0000 1.0000 -0.0000
- Polygonal face element
f 3//1 113//1 4//1
Step 2- Converting .obj to OpenGL compatible information
We’ll be using the following program that allows to convert from .obj format to a format compatible with OpenGL as the conversion from one format to the other is not part of the scope of this document.
This program does more than just changing the format on the file and do perform some operations to translate the parameters of the .obj file to the following OpenGL ES information:
static GLfloat v_triangles
static GLfloat vt_triangles
static GLfloat vn_triangles
We’ll be using these variables and also the number of triangles which for this example is 1440. This can be found at the end of the file on the following line which effectively draws the complete triangle array:
glDrawArrays(GL_TRIANGLES, 0, 1440);
Step 3 – Loading the model arrays to the .C program.
You will need to copy the three GLfloat arrays to your OpenGL ES .C code. (You may alternatively use an include to have it more neatly organized but this is outside the scope of this document)
In our example we’ll copy them inside void render (). Inside this function we’ll find the Draw Array instruction in which we must specify the number of triangles in our array. You may just replace your model information AND also change the number of triangles, otherwise you will receive an error when running the program.
We are using a simple rotation using glRotate and incrementing the value of rotation with each flush of the screen.
glRotatef(rlogo, 2.0f, 1.0f, 1.0f);
The variable rlogo gets increased each time the screen is drawn.
Depending on your model you may need to change the view in order to be able to see your model. This example uses a very small model so we have a viewpoint just 1.25 units away from the screen (Z axis). Depending on your model you may need to be further away in order to see the model on the screen.
glTranslatef(0.0f, 0.0f, -1.25f);
Step 4- Compiling the OpenGL example
For this simple example we will be using the examples from the GPU as base an add the information of the model we have just exported.
We’ll use the two attached files for this:
Makefile.x11 – A make file with the dependencies and attributes necessary to compile our C file.
NXPlogo.c – C file with the information of the model and instructions on to draw it on the screen.
You can compile using the following commands to first clean in case you built before and then compiling the code.
make –f Makefile.x11 clean
Once the program has compiled you can run it from the command prompt by using:
The result will be as follows, where the 3D model is rotating,
2D and 3D Graphics in NXP Devices
i.MX6D/Q/DL/S/SX GPU Demo Framework SDK – Which provides the source code for the demo in which this example was built upon and also contains good documentation for those interested in OpenGL ES.
OpenGL Redbook – Which is the most comprehensive book documenting and explaining the OpenGL API.