Partners: KDAB Whole Tomato Software CppDepend

12 May 2012

OpenGL Startup Framework - Part 1

As we all know OpenGL is wonderful technology, but what can be done to quickly jump into that world...? If you need to learn graphics programming at first you have to implement a lot of code that initializes app's window and then the graphics itself. That "init" part is quite "boring" and prone to errors. In this post I would like to review some strategies that should help with that...

Covered topics:
  1. quick project setup - programming environment
  2. additional libraries - essential libraries that will speedup coding...
  3. utilities - some useful bits of code for OpenGL
  4. simple OpenGL code - how to render spinning torus or something similar :)
The framework was quite ok, but I moved on and created a bit better version.
Please see: OpenGL Startup V3.0 for more information.

1 quick project setup
I am a Windows fan, so I will stick to MS platform... But application code will not be so dependent on MS platform - OpenGL is multiplatform solution, so it is good to use that capability in out project. On Windows I use Visual Studio C++ Express - both 2008 and 2010 editions are quite good. The project does not need advanced features like MFC, ALT, etc, etc... just plain WinApi - so Express Edition is enough for us.

When I started learning OpenGL I wrote a lot of WGL and WinApi code that glued OpenGL with Windows. That way is very good when you want to learn all the details connected with Initialization of window and rendering context. Unfortunately it is not good for teaching graphics... As I wrote in the beginning: this is very "boring" part of application. For recent project I considered using GLUT (OpenGL Utility Toolkit created by Mark J. Kilgard)  which is very popular and simple to use alternative. Moreover it is multiplatform, so it doesn't matter where you want to build projects! This is good for sample application: code can be the same and students can run it on their favourite platforms... Unfortunately GLUT is quite old and there is no source code... but there is other alternative: freeGLUT - which is 100% compatible with GLUT but recent version is from 2012 (not from 1999 like GLUT) link: You can download whole source and recompile it as you want.
I prefer to use static libs for instance - not DLL version. GLUT is not designed to make advanced projects (although why not? :)), but after some time of coding in OpenGL most people will be able to wrote their own framework. But the main advantage is that you can focus on OpenGL only part.

Init section is only several lines of code...
#include void main(int argc, char **argv) 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowPosition(100, 100); 
    glutInitWindowSize(640, 480); 
tutorials and links:

2 additional libraries
OpenGL is only API to communicate with graphics card. Usually we need some additional help:
  • texture loading - something simple, fast and without complicated classes/structures... Answer: SOIL Free (public domain), supports BMP, JPG,  PNG, TGA, DDS... cross platform. Returns int ID of opengl Texture Object and that is all (for basic usage).
  • OpenGL extensions - GLEW or GLEE. I used GLEE, but some people suggests using GLEW - So I've downloaded it, compiled as static lib and successfully connected to the project. Usage is as simple as GLEE.
  • mesh loading - maybe not for start...

if (GLEW_VERSION_3_0) // opengl 3.0 supported! 
// sample code form 
/* load an image file directly as a new OpenGL texture */ 
GLuint tex_2d = SOIL_load_OGL_texture ( "img.png", SOIL_LOAD_AUTO, 
                                         SOIL_FLAG_MIPMAPS |             
                                         SOIL_FLAG_INVERT_Y | 
                                         SOIL_FLAG_NTSC_SAFE_RGB | 
                                         SOIL_FLAG_COMPRESS_TO_DXT );

3 utilities covered in next post...

4 simple OpenGL code covered in next post...

Get my free ebook about C++17!

More than 50 pages about the new Language Standard.

C++17 in detail, by Bartlomiej Filipek

© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.