OpenGL 4 Tutorial Code Update

I’ve added a few more Red Book (8th edition) examples including fur shading and tessellation.
All these examples use OpenGL 4.3, SDL2, glm and glew (except tessellation which uses freeglut)

OpenGL 4.3 draw commands using SDL2 glew and glm
OpenGL 4.3 object instancing (Red Book Chapter 3)
OpenGL 4.3 Shadow mapping (Red Book Chapter 4)
OpenGL 4.3 Cube mapping (Red Book Chapter 6)
OpenGL 4.3 Light Models (Red Book Chapter 8)
OpenGL 4.3 Fur shading using geometry shader (RedBook Chapter 10)
Tessellated Teapot (Red Book Chapter 9)
Order Independent Transparency (Red Book Chapter 11)

OpenGL Super Bible (OpenGL 4.3 Core) Example Code

The OpenGL SuperBible (6th edition) seems to have much better example code, for OpenGL 4.3, than the Red Book (8th edition).

You can download the source from git here:
(In Linux, just type git clone
and download the media files here:

Currently there is a bug in the CmakeLists.txt that prevents linking on Linux systmes, in line 13 you need to change “elif (UNIX)” to “elseif (UNIX)” and you should be fine.

The SuperBible example code is based on glfw and in Linux you also need to install GLX and GL headers and libs.

OpenGL 4 CubeMap example (Red Book Chapter 6)

I’ve added the Red Book Cubemap example to the OpenGL 4 Tutorial Code page.
Red Book Chapter 6, OpenGL 4.3 Cube Map Example

I had to use a different 3d object because the object loader used by the Red Book segfaults (on my system) with the object originally used in this example.
Instead of trying to bugfix and change the loader code I just used another object (still from the redbook files).

OpenGL 4.3 Shadowmap (Red Book chapter 4)

I’ve added the shadowmap example to the OpenGL 4 Tutorial Code (Red Book) page.

I had to change

    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, depth_texture, 0);


    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth_texture, 0);



Not sure why this happens. (Driver is NVIDIA 340.24 on Ubuntu 14.04)

More OpenGL 4.x Red Book examples

I’ve made a page (OpenGL 4 Tutorial Code (Red Book)) with the Red Book examples I’ve ported so far.

This include a simple example showing of the different drawing commands and one showing how to instance vertex buffer objects.

Currently there’s only simple build scripts for Linux but they should compile on any system with SDL2 glew(1.10) and glm(

OpenGL 4.3/4.4 example using SDL2 and GLM

I’ve ported one of the Red Book OpenGL 4.3 code examples (ch03_drawcommands) to SDL2 using glew and glm

The main source file is below and the complete source (including shaders) is here:
It contains a simple build script for Linux. You only need to install SDL2, glew and glm using your favourite package manager.

I’m not going to explain the code beyond the comments. If you’re serious about OpenGL 4.x+ you should buy the Red Book (8th edition)

I’ve made a page with some other Red Book OpenGL 4 examples that I’ve ported. Currently there are 3d object instancing, shadowmapping and cubemapping.
They can be found here: OpenGL 4 Example Code

#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> 
#include <SDL2/SDL.h>

#include "LoadShaders.h"
extern GLuint LoadShaders(ShaderInfo *shaderinfo);

#define PROGRAM_NAME "Red Book - Chapter 3 - Draw Commands"
void Initialize();
void Display();
void Finalize(void);
void Reshape(int width, int height);

float aspect;
GLuint render_prog;
GLuint vao;
GLuint vbo;
GLuint ebo;

GLint render_model_matrix_loc;
GLint render_projection_matrix_loc;

void Initialize() {

    ShaderInfo shader_info[] = {
      { GL_VERTEX_SHADER, "primitive_restart.vs.glsl" },
      { GL_FRAGMENT_SHADER, "primitive_restart.fs.glsl" },
      { GL_NONE, NULL }

  render_prog = LoadShaders(shader_info);

  render_model_matrix_loc = glGetUniformLocation(render_prog, "model_matrix");
  render_projection_matrix_loc = glGetUniformLocation(render_prog, "projection_matrix");

  // A single triangle
  static const GLfloat vertex_positions[] = {
      -1.0f, -1.0f,  0.0f, 1.0f,
      1.0f, -1.0f,  0.0f, 1.0f,
      -1.0f,  1.0f,  0.0f, 1.0f,
      -1.0f, -1.0f,  0.0f, 1.0f,

  // Color for each vertex
  static const GLfloat vertex_colors[] = {
      1.0f, 1.0f, 1.0f, 1.0f,
      1.0f, 1.0f, 0.0f, 1.0f,
      1.0f, 0.0f, 1.0f, 1.0f,
      0.0f, 1.0f, 1.0f, 1.0f

  // Indices for the triangle strips
  static const GLushort vertex_indices[] = {
      0, 1, 2

  // Set up the element array buffer
  glGenBuffers(1, &ebo);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indices), vertex_indices, GL_STATIC_DRAW);

  // Set up the vertex attributes
  glGenVertexArrays(1, &vao);

  glGenBuffers(1, &vbo);
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions) + sizeof(vertex_colors), NULL, GL_STATIC_DRAW);
  glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertex_positions), vertex_positions);
  glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertex_positions), sizeof(vertex_colors), vertex_colors);

  glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
  glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)sizeof(vertex_positions));

  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

void Display() {
  glm::mat4 model_matrix;

  // Setup


  // Activate simple shading program

  // Set up the model and projection matrix
  glm::mat4 projection_matrix(glm::frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 500.0f));
  glUniformMatrix4fv(render_projection_matrix_loc, 1, GL_FALSE, glm::value_ptr(projection_matrix));

  // Set up for a glDrawElements call
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

  // Draw Arrays...
  model_matrix = glm::translate(glm::mat4(1.0f),glm::vec3(-3.0f, 0.0f, -5.0f));
  glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, glm::value_ptr(model_matrix));
  glDrawArrays(GL_TRIANGLES, 0, 3);

  // DrawElements
  model_matrix = glm::translate(glm::mat4(1.0f),glm::vec3(-1.0f, 0.0f, -5.0f));
  glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, glm::value_ptr(model_matrix));

  // DrawElementsBaseVertex
  model_matrix = glm::translate(glm::mat4(1.0f),glm::vec3(1.0f, 0.0f, -5.0f));
  glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, glm::value_ptr(model_matrix));
  glDrawElementsBaseVertex(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL, 1);

  // DrawArraysInstanced
  model_matrix = glm::translate(glm::mat4(1.0f),glm::vec3(3.0f, 0.0f, -5.0f));
  glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, glm::value_ptr(model_matrix));
  glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1);

void Finalize(void) {
  glDeleteVertexArrays(1, &vao);
  glDeleteBuffers(1, &vbo);

void Reshape(int width, int height) {
  glViewport(0, 0 , width, height);
  aspect = float(height) / float(width);
int main(int argc, char *argv[]){
  SDL_Window *mainwindow; /* Our window handle */
  SDL_GLContext maincontext; /* Our opengl context handle */

  if (SDL_Init(SDL_INIT_VIDEO) < 0) { /* Initialize SDL's Video subsystem */
    std::cout << "Unable to initialize SDL"; 
    return 1;

  /* Request opengl 4.4 context. */
  /* Turn on double buffering with a 24bit Z buffer.
   * You may need to change this to 16 or 32 for your system */
  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  /* Create our window centered at 512x512 resolution */
  if (!mainwindow){ /* Die if creation failed */
     std::cout << "SDL Error: " << SDL_GetError() << std::endl;
     return 1;

  /* Create our opengl context and attach it to our window */
  maincontext = SDL_GL_CreateContext(mainwindow);
  GLenum rev;
  glewExperimental = GL_TRUE; 
  rev = glewInit();

  if (GLEW_OK != rev){
      std::cout << "Error: " << glewGetErrorString(rev) << std::endl;
  } else {
      std::cout << "GLEW Init: Success!" << std::endl;

  /* This makes our buffer swap syncronized with the monitor's vertical refresh */
  bool quit=false;


  SDL_Event event;



    while( SDL_PollEvent( &event ) ){
	if( event.type == SDL_QUIT ){
            quit = true;


  /* Delete our opengl context, destroy our window, and shutdown SDL */
  return 0;

PS MOVE Track Light Ball

Anyone interested in hacking the PS MOVE in Linux can grap this file: cvtrackball.cpp

[Update] Sunshine is a killer…
If you wan’t to test the code, make sure your room is dimly lit and that you have the covers drawn.

[Update] Is Official: cvHoughCircles suck…
A detection combining shape and color would be nice.

[Update] Red, Green & Blue, we’re counting, eehh, You?
the code now works in RGB ranges, so if your ball is say cyan this line would be a good place to start:
./cvtrackball 0 0 256 200 256 200 256 1 (<- tells it to only print coordinates for 1 ball.)
to compile type:
“g++ cvtrackball.cpp -o cvtrackball -lopencv_gpu”
(you must have opencv 2.x installed)

and then:
“./cvtrackball video_device# Hue1 Hue2 Sat1 Sat2 Val1 Val2 [number_of_balls]”

The last six values are HSV values for the ball color
(a bit tricky finding the right ones, but it is possible using gimp and trial ‘n error ;-))

An example could be:
“./cvtrackball 1 10 30 0 10 250 256”

To find your video device try:
ls /dev/video*

To set the color of the light ball you need the patched version of linmctool found at:

Best of luck!

C API Bindings and Embedded Lua scripting

OME has just been blessed with a cleaned up C API exposing all member functions of the central meContext class. Using this API I’m also almost done writing support for embedded Lua scripting.

To check out the API browse this file:

Feel the source Luke!

If your brave enough you can download and compinstall the entire code in its alpha state.
Currently only Linux compiles (easily anyway)

(my system which is fairly tested is Ubuntu 10.10)

In a shell type:
hg clone OME
cd OME
sh autogen && configure && make && sudo make install

and the run MIDIumEdit

supported data formats are:
Collada ( .dae )
3ds Max 3DS ( .3ds )
3ds Max ASE ( .ase )
Wavefront Object ( .obj )
Stanford Polygon Library ( .ply )
* AutoCAD DXF ( .dxf )
LightWave ( .lwo )
Modo ( .lxo )
Stereolithography ( .stl )
AC3D ( .ac )
Milkshape 3D ( .ms3d )
* TrueSpace ( .cob,.scn )

* Valve Model ( .smd,.vta )
Quake I ( .mdl )
Quake II ( .md2 )
Quake III ( .md3 )
* Return to Castle Wolfenstein ( .mdc )
Doom 3 ( .md5* )

The Final Refactoring has begun!

The final touches are now being applied to the OME source and an anonymous mercurial http download should be available “soon”.

Below is an example of a simple viewer app to give you an idea of the API:
(Also checkout for an example of a larger App using OME)

#include "meContext.h"

meContext* m3d;

extern "C" int main(int argc, char *argv[]){
string fileName = "";
if(argc < 2){ cout << "Usage: " << argv[0] << " " << endl; exit(1); } else{ fileName = argv[1]; cout << "fileName " << fileName << endl; } m3d = new meContext(); m3d->meCoreInitEngine("config.xml");



m3d->meCoreLoadDataFromXMLFile((char *)fileName.c_str());


delete m3d;
return 0;

To infinity and beyond!


This site will be the new home of the Open Media Engine,

an Open Source GPL project aiming to become a fully featured “Do It All” framework
for creating cross-platform 3D graphics/audio/media apps.

As you can see the site is currently under construction and no source download is available yet, so stay tuned for future updates.

OME is currently being developed for use on iOS/Android but this is not publicly available yet.
If how ever you want to take a look at the old code it can be found here: (hg clone

(feedback can be sendt to and/or

Return top

Site News

If your brave enough you can download and install the entire code (alpha release). hg clone OME Feedback can be sendt to