While SDL is capable of handling 2D graphics on its own, there may be a situation where 3D graphics are preferred. This is where OpenGL is often used since it contains functions which make rendering 3D scenes easy and fast. Luckily, with the help of SDL, we can make use of the OpenGL library which you may already have.
Setup:
OpenGL, or Open Graphics Library, uses the processor and various features of the graphics card to create 2D and 3D visuals. Because of its wide spread use in graphics cards, most operating systems are compatible with the OpenGL library and some C compiler setups will have OpenGL library files available already. Also, OpenGL often comes with GLU, or OpenGL Utility Library, which can also be setup.
I will attempt to cover how to setup OpenGL and GLU to work with C programs compiled with various compilers on various operating systems. This mostly means the windows, mac, and linux operating systems and the compilers and IDE's I mentioned earlier: GCC, MinGW, Visual Studio, Code::Blocks, Eclipse, and Xcode.
For Linux:
According to the page http://ubuntu-gamedev.wikispaces.com/How-To+Setup+SDL+for+games+development, Ubuntu requires the packages mesa-common-dev and libglu1-mesa-dev in addition to libsdl-1.2dev and build-essentials. It may also help to install packages like mesa-utils, libgl1-mesa, libgl1-mesa-glx, and libglu1-mesa. Also, I have had problems with nvidia drivers, so look out for an error message about not finding a GLX visual and try uninstalling or reinstalling nvidia-current.
For other distributions, like debian and fedora, the pages http://www.larsen-b.com/Article/231.html, http://www.opengl.org/discussion_boards/showthread.php/175875-Install-OpenGL-in-Fedora, http://www.evl.uic.edu/arao/cs594/sdlglsl.html, and http://forums.fedoraforum.org/showthread.php?t=155503 discuss potentially needed libraries. I am not familiar with these versions of linux, so I apologize if these were not helpful :(.
For Mac:
The page http://www.evl.uic.edu/arao/cs594/sdlglsl.html seems to have an in-depth process for setting up OpenGL and SDL on Mac computers. Also, the page http://www.meandmark.com/sdlopenglpart2.html discusses how to setup SDL and OpenGL in xcode. Unfortunately, I'm not familiar with problems setting up SDL or OpenGL on the Mac platform, but I will try to provide starter code in this post that can help you debug errors.
However, a friend of mine who is more familiar with the Mac OS has recommended I inform people setting up Xcode with SDL and OpenGL to download command line tools for Xcode. It apparently used to be included by default, but now they must be setup. The page http://stackoverflow.com/questions/9329243/xcode-4-4-command-line-tools may be helpful for getting these command line tools.
For Windows:
Windows will allow the use of the Visual C++ or the MinGW C compiler. If you plan to use the Visual C++ compiler, you must first ensure you have Microsoft's Visual C++ compiler installed and have SDL setup. Luckily, this means you are ready to start using the OpenGL and GLU libraries already installed with the compiler.
If you want to use MinGW, you will also have OpenGL and GLU libraries already installed. You should also ensure you have SDL setup with your compiler, and you can use an optional IDE like Code::Blocks or Eclipse.
compiling and linking:
The last thing to do is link the new libraries with the program. This is similar to linking SDL and SDLmain into your programs, but for OpenGL and GLU we must add GL and GLU. If you are using GCC, this can be accomplished by adding "-lGL -lGLU" to the compiling command gcc. For example, "gcc file_name.c -o program_name -lSDL -lGL -lGLU" should work fine. For MinGW, some extra or alternative libraries are needed: "gcc file_name.c -o program_name.exe -lmingw32 -lSDLmain -lSDL -lOpenGL32 -lglu32" should compile and link well.
For IDE's, the command line operations are not necessary. Instead, you can open the linker options and add the library files manually. For Visual C++, these files will probably be OpenGL32.lib and GLU32.lib and be located in program files in the folder Microsoft SDKs. For Code::Blocks or Eclipse, these files may be libGL.o and libGLU.o for Mac/Linux and libOpenGL32.a and libglu32.a for MinGW on Windows.
Sample Code:
To verify that your compiler works with SDL and OpenGL, here is a sample program which will check for errors when starting up:
This code was created based on the concepts covered in the page http://lazyfoo.net/SDL_tutorials/lesson36/index.php. Another page which may prove helpful can be found at http://content.gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL. For documentation about SDL_GetError and glGetError see http://sdl.beuc.net/sdl.wiki/SDL_GetError and http://www.ugrad.cs.ubc.ca/~cs314/opengl/glGetError.html respectively.
For this example code, a new header called SDL_opengl.h is needed to access the functions of OpenGL in C. However, OpenGL still requires a working window which it can render to, which is where SDL_SetVideoMode can help. By setting the last argument to SDL_OPENGL, SDL will attempt to build an OpenGL-enabled window. If this fails, the function returns NULL and the code above will print an error and exit.
Next, some OpenGL code is tested. glClearColor will set the color which is used to clear the screen: each argument is a float, the first being red, the second green, the third blue, and finally alpha. The range of values is from 0.0 to 1.0 and any real number in between (that a float type can store). After this first call, glGetError will check for errors in OpenGL and the above code will print any using gluErrorString and exit.
If there were no SDL or OpenGL errors, then the code will clear the color buffer (the visible surface) with the color input to glClearColor. If OpenGL uses double buffering, it will draw to a second surface which is currently not shown; by swapping the buffers with SDL_GL_SwapBuffers, we see the result of the glClear operation and prepare the other buffer to be drawn on.
Finally, the code will wait 3 seconds with an orange screen and quit if it did not already encounter an error. If an error was encountered, take note of what the text output says and search the web for this problem (most likely someone has had a similar issue). I plan to cover more topics related to OpenGL in the next posts and possibly revisit C and SDL topics.
For more information about OpenGL, visit the pages http://www.swiftless.com/opengltuts.html and http://www.videotutorialsrock.com/. Be advised that these sites use GLUT, the OpenGL Utility Toolkit, as an alternative to SDL. There is also the legacy tutorials and forums at http://nehe.gamedev.net/, but this uses win32 code which can be fairly messy and is not cross-platform (windows only). They can still be a helpful reference for OpenGL concepts though.
The next post will discuss drawing and coloring geometric shapes in OpenGL. Thank you for reading!