Difference between revisions of "Tutorial1: Creating a Cross Platform OpenGL 3.2 Context in SDL (C / SDL)"

From OpenGL Wiki
Jump to navigation Jump to search
(deleting tutorial due to complaints)
Line 1: Line 1:
== Overview ==
This tutorial is designed to help explain the process of creating an OpenGL 3.2 context using libsdl.<br/>
This tutorial has the following requirements:<br/>
<li/>An OpenGL 3.2 compatible video card. (Currently an NVDIA G80 or ATI R600 or newer GPU)
<li/>An OpenGL 3.2 video driver. (Currently they are all beta versions)
<li/>[http://www.libsdl.org/svn.php libsdl-1.3] which is currently in development phase, but includes support for OpenGL 3 style contexts.
<li/>[http://www.opengl.org/registry/api/gl3.h gl3.h] installed in a 'GL3' directory. (e.g. mkdir /usr/include/GL3 ; mv gl3.h /usr/include/GL3)
The benefits of using libsdl are numerous, but in particular, it will allow our code to run on windows, mac, and unix / linux. My only computer which meets the above requirements runs linux, so I can guarantee compatibility with linux only. If you find problems compiling the code on other platforms, please make a note of it.<br/>
Creating a window in libsdl and binding an OpenGL 3.2 context to it uses these steps:
<li/>Initialize the SDL video subsystem using SDL_Init or SDL_VideoInit. (We use SDL_Init in this example)
<li/>Set the parameters we require for opengl using SDL_GL_SetAttribute.
<li/>Create a window using SDL_CreateWindow.
<li/>Bind an OpenGL context to the window using SDL_GL_CreateContext.
== The Code ==
#include <stdio.h>
#include <stdlib.h>
/* Ensure we are using opengl's core profile only */
#define GL3_PROTOTYPES 1
#include <GL3/gl3.h>
#include <SDL/SDL.h>
#define PROGRAM_NAME "Tutorial1"
/* A simple function that prints a message, the error code returned by SDL,
* and quits the application */
void sdldie(char *msg)
    printf("%s: %s\n", msg, SDL_GetError());
/* Our program's entry point */
int main(int argc, char *argv[])
    SDL_WindowID mainwindow; /* Our window handle */
    SDL_GLContext maincontext; /* Our opengl context handle */
    if (SDL_Init(SDL_INIT_VIDEO) < 0) /* Initialize SDL's Video subsystem */
        sdldie("Unable to initialize SDL"); /* Or die on error */
    /* Request an opengl 3.2 context.
    * SDL doesn't have the ability to choose which profile at this time of writing,
    * but it should default to the core profile */
    /* 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_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    /* Create our window centered at 512x512 resolution */
    if (!mainwindow) /* Die if creation failed */
        sdldie("Unable to create window");
    /* Create our opengl context and attach it to our window */
    maincontext = SDL_GL_CreateContext(mainwindow);
    /* This makes our buffer swap syncronized with the monitor's vertical refresh */
    /* Clear our buffer with a red background */
    glClearColor ( 1.0, 0.0, 0.0, 1.0 );
    glClear ( GL_COLOR_BUFFER_BIT );
    /* Swap our back buffer to the front */
    /* Wait 2 seconds */
    /* Same as above, but green */
    glClearColor ( 0.0, 1.0, 0.0, 1.0 );
    glClear ( GL_COLOR_BUFFER_BIT );
    /* Same as above, but blue */
    glClearColor ( 0.0, 0.0, 1.0, 1.0 );
    glClear ( GL_COLOR_BUFFER_BIT );
    /* Delete our opengl context, destroy our window, and shutdown SDL */
    return 0;
== Compilation ==
On linux:<br/>
gcc tutorial1.c -o tutorial1 -lGL -lSDL<br/>
If you installed libsdl-1.3 to /usr/local then do:<br/>
gcc -I/usr/local/include -L/usr/local/lib tutorial1.c -o tutorial1 -lGL -lSDL
== Execution ==
The result should be a 512x512 window centered on your display showing a red, green, then blue background.

Revision as of 19:47, 19 August 2009