Basics

Display Functions

Drawing Functions

Event Handler Functions

Advanced Drawing

  • Display Lists:
  • Lighting and Materials
  • Curves and Surfaces
  • Quadric Objects
  • Timing
  • Basics

    Dev C++ Linker options:

    -lmingw32 -lSDLmain -lSDL -lopengl32 -lglu32
    
    back to the top

    Visual Studio Link Modules

    SDL.lib SDLmain.lib opengl32.lib glu32.lib
    
    back to the top

    Required Headers:

    #include <SDL.h>
    #include <SDL_opengl.h>
    #include <stdlib.h>
    back to the top

    Visual Studio Headers

    #include <SDL_opengl.h>
    #include <SDL.h>
    #include <stdlib.h>
    back to the top

    Main Function:

    #undef main // allows write to console
    
    int main(int argc, char **argv)
    back to the top

    Initialise SDL:

       SDL_Event event;
       SDL_Surface* pSurface;
       SDL_Init ( SDL_INIT_VIDEO ) ;
       atexit ( SDL_Quit ) ;
       SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
       SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 6 );
       SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
       SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
       SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
       pSurface = SDL_SetVideoMode ( SCREEN_WIDTH, SCREEN_HEIGHT , 0, 
                                     SDL_OPENGL | SDL_ANYFORMAT ) ;
       glClearColor(0.0, 0.0, 0.0, 0.0);
       reshape(SCREEN_WIDTH, SCREEN_HEIGHT);
    
    back to the top

    Main Loop:

       while(1)
       {
        if ( SDL_PollEvent ( &event ) )
        {
          switch(event.type)
          {
          }
        }
        renderScene();
       }
    
    back to the top

    Display Functions

    Setting the projection matrix:

    void reshape(int w, int h)
    {
       glViewport(0, 0, w, h);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
    
       //gluOrtho2D(LEFT, RIGHT, BOTTOM, TOP);
       //glOrtho(LEFT, RIGHT, BOTTOM, TOP, CLIP_NEAR, CLIP_FAR);
       //gluPerspective(FOV, WIDTH/HEIGHT, CLIP_NEAR, CLIP_FAR);
    
       glMatrixMode(GL_MODELVIEW);
    }
    
    back to the top

    display function:

    void renderScene(void)
    {
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
       glLoadIdentity();
    	 	 
       gluLookAt(viewcam.pos.x,viewcam.pos.y,viewcam.pos.z,
                 viewcam.at.x,viewcam.at.y,viewcam.at.z,
                 viewcam.up.x,viewcam.up.y,viewcam.up.z);
    
       SDL_GL_SwapBuffers( );
    }
    
    back to the top

    Cameras:

    Of course, with the above code, it is assumed that cameras are defined:

    
    typedef struct
    {
       float x;
       float y;
       float z;
    } VECTOR;
    
    typedef struct
    {
       VECTOR pos;
       VECTOR at;
       VECTOR up;
    } CAMERA;
    
    CAMERA viewcam;
    
    back to the top

    Drawing Functions

    Colours:

       glColor3f(1.0, 1.0, 1.0);
    
    back to the top

    Primitives:

    
       glBegin(GL_TRIANGLES);
          GL_POINTS, GL_QUADS, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUAD_STRIP ...
       glEnd();
    
       glVertex2f(X,Y);
       glVertex3f(X,Y,Z);
    
    back to the top

    Text:

    include glText header

    #include "glText.h"
    

    Link to gltext library before linking to opengl

    	-lgltext
    

    and call

       initText()
    

    (after setting the video mode.)

    switch to an orthographic projection, flip the scale and move to top left

          glMatrixMode(GL_PROJECTION);
          glLoadIdentity();
          gluOrtho2D(0, w, 0, h);
          glScalef(1, -1, 1);
          glTranslatef(0, -h, 0);
    
          glMatrixMode(GL_MODELVIEW);
          glLoadIdentity();
    
    back to the top

    To print:

        glRasterPos2i(20, 64);
        printString("Timmay!");
    
    back to the top

    Event Handler Functions

    Keyboard:

          case SDL_KEYDOWN: // SDL_KEY_UP
             processKeys(&event.key);
             break;
    
    void processKeys(SDL_KeyboardEvent *key, int state)
    {
       switch(key->keysym.sym)
       {
          case SDLK_ESCAPE: // SDLK_5 SDLK_g
             // if (state) keypressed; else notkeypressed;
             exit(0);
             break;
       }
    }
    
    back to the top

    Mouse:

             case SDL_MOUSEMOTION:
                processMotion(&event.motion);
                break;
    
    void processMotion(SDL_MouseMotionEvent *mouse)
    {
       MouseX = mouse->x;  // mouse->xrel
       MouseY = mouse->y;  // mouse->yrel
    }
    
    
    
          case SDL_MOUSEBUTTONDOWN:
             processMouse(&event.button);
             break;
    
    void processMouse(SDL_MouseButtonEvent *mouse, vector &points, SimpleColor &activeColor)
    {
       float x, y;
    
       x = (mouse->x-256);
       y = (256-mouse->y);
    
       if (mouse->button == SDL_BUTTON_LEFT)
          ;//...
       if (mouse->button == SDL_BUTTON_RIGHT)
          ;//...
    }
    
    
    
    back to the top

    Toggle Cursor Display:

       SDL_ShowCursor(SDL_DISABLE); // SDL_ENABLE
    
    back to the top

    Warp Mouse:

    back to the top

    Raw input:

       SDL_WM_GrabInput(SDL_GRAB_ON);
    

    To limit the mouse to the window, grab raw input but don’t hide the cursor.

    back to the top

    Advanced Drawing

    Display Lists:

    Display lists are referenced by Gluints:

    GLuint ground;
    

    They are drawn by calling them:

       glCallList(ground);
    

    To use a display list, first of all it must be generated:

       ground = glGenLists(1);
    

    To start adding to a list, declare the list that will be used:

       glNewList(ground, GL_COMPILE);
    

    To finish adding to the list:

       glEndList();
    
    back to the top

    Lighting and Materials:

    Steps for lighting:

    1. initialisation
    2. enable [lighting & specific lights]
    3. render scene

    1. Initialisation

    Set a normal with:

       glNormal3f(0.0, 1.0, 0.0);

    For simple material settings:

       glEnable(GL_COLOR_MATERIAL);

    2. Enable lighting

    To enable / disable lighting use:

       glEnable(GL_LIGHTING);
       glDisable(GL_LIGHTING);
    

    To enable / disable a specific light use:

       glEnable(GL_LIGHTi);
       glDisable(GL_LIGHTi);
    

    3. Render scene

    load identity...

    position camera...

    light colour and position 4

    (make sure distance is greater than size of normal!)

       glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
       glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
    

    where 'red' and 'light0_position' are vectors.

    NB. position MUST have a 4th dimension with value 1.

    draw the rest of the scene...

    Other lighting tips.

    If you are going to scale polygons that have normals, enable simple scaling of normals with:

        glEnable(GL_RESCALE_NORMAL);
    
    back to the top

    Curves and surfaces

    Steps below are for 1d lines. See notes for 2d surfaces.

    Steps for maps:

    1. Create a map
    2. Enable
    3. Evaluate

    Two alternatives: fine control, and automatic.

    1. Create a map

    Start off with an array of coordinates (data). Then...

       glMap1f(GL_MAP1_VERTEX_3, u1=0.0, u2=1.0, stride=3, order=4, data*);
    

    2. Enable

       glEnable(GL_MAP1_VERTEX_3);
    

    3. Evaluate

    Control:

    glBegin(GL_LINES);
    for (i=0; i<number of points; i++)
       glEvalCoord1f(0.0.....1.0 = i/number of points);
    glEnd();
    

    Automatic:

       map a grid	--> glMapGrid1f(partitions=30, u1=0.0, u2=1.0);
       eval mesh	--> glEvalMesh1(GL_point/line/fill, int grid step 0, int grid step 30);
    

    Lighting:

       glEnable(GL_AUTO_NORMAL);
    

    Winding order

    Goes along first line, then second line. eg. chessboard a1 -a8, followed by b1 - b8 etc is ccw

    Notes for 2d surfaces

    Change Map1, Grid1 and Mesh1 to Map2, Grid2 and Mesh2, and add additional parameters:

    Map:

        glMap2f(GL_MAP2_VERTEX_3,
                   u1=0.0, u2=1.0, stride=3, order=4,
                   v1=0.0, v2=1.0, stride=12, order=4,
                   (GLfloat*)surfacePoints);
    

    enable

    glEnable(GL_MAP2_VERTEX_3);

    Map->Grid:

        glMapGrid2f(partitions=20, u1=0.0, u2=1.0,
                    partitions=20, v1=0.0, v2=1.0);
    

    Evaluate mesh

        glEvalMesh2(GL_FILL, int grid step 0, int grid step 20, int grid step 0, int grid step 20);
    
    back to the top

    Quadric Objects

    To draw a quadric object:

       GLUquadricObj *q = gluNewQuadric();
       gluQuadricDrawStyle(q, GLU_LINE);
       gluQuadricNormals(q, GL_SMOOTH);
       gluSphere (q,10,25,25);
       gluDeleteQuadric(q);
    
    back to the top

    Timing

    Get milliseconds:

       time = SDL_GetTicks();
    
    back to the top