Project: Mieke - 2D projection

When I am learning something new, there is a chance that what I want to do is not documented, or I have not enough experience to derive the solution. This is one of those experiences.

I want to run graphical experiments for my research in multi-agent systems, so I am writing Mieke, a little program to run a 2D OpenGL context and show what my little agents are doing. I want to develop it with C++ in order to get the best of my hardware because I plan to run hundreds of agents with heavy communication and optimization algorithms.

My first goal was to run a 2D environment inside a game loop. My first approach was to use transformation matrix to get a 2D representation of my 3D objects, even if my objects were planar. But I found myself doing lots of maths formulas and equations that it started to feel wrong. There should be a better way.

And indeed there is. The function glOrtho(left, right, bottom, top, nearVal, farVal) makes a 2D projection matrix with values left, right, bottom and top mapped to the four corners of the current window. Moreover, I also found this Stack Overflow question on how to setup a one to one pixel mapping from the 2D projection matrix to the window. Finally, my simple game loop with a 2D OpenGl context became:

GLFWwindow* window = glfwCreateWindow(width, height, "mieke", NULL, NULL);
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glOrtho(0.0f, width, height, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    // update agents
    // draw agents
    glfwSwapBuffers(window);
    glfwPollEvents();
}

The full code can be found here.