2. Intro to OpenGL

Brock University
COSC 3P98 Computer Graphics
Instructor: Brian J. Ross



Intro to OpenGL


OpenGL platforms


Compiling OpenGL on Linux (with GLUT I/O library)

#include <GL/gl.h>
#include <GL/glut.h>      /* or "glut.h" if copied locally */
/* plus any others you need */
cc -float -prototypes -O2 -s -o crisscross crisscross.c -L /usr/X11R6/lib -lc -lglut -lGL -lGLU -lX11 -lXmu -lXi -lXext -lm
  • See this tutorial for setting up OpenGL and GLUT on MS Visual Studio.
  • See WWW for examples, Makefile, ... (as well as many more GLUT library examples)

  • Simple OpenGL program: green.c

    /*
     *  green.c
     *  This program draws a green window. (Yawn!)
     */
    
    #include <GL/gl.h>
    #include <GL/glut.h>
    
    void green (void) {
    
    	glClear(GL_COLOR_BUFFER_BIT);
    	glFlush();
    }
    
    int main(int argc, char** argv)
    {
    	glutInit(&argc, argv);
    	glutInitWindowSize(400, 400);
    	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    	glutCreateWindow ("green");
    
    	glutDisplayFunc(green);
    	glClearColor (0.0, 1.0, 0.0, 1.0);
    
    	glutMainLoop();
    	return(0);
    }
    
    

    Green.c



    Steps in an OpenGL program

    1. Query the availability of graphics resources
    2. Initialize graphics library (global attributes, winopen)
    3. Call OpenGL routines to do things (body of application program)
    4. Terminate

    OpenGL command structure

     
     Suffix Data type Typical C equiv OpenGL type defn
    b 8-bit integer signed char GLbyte
    s 16-bit integer short GLshort
    i 32-bit integer long GLint, GLsizei
    f 32-bit float float GLfloat, GLclampf
    d 64-bit float double Gldouble, GLclampd
    ub 8-bit unsigned integer unsigned char GLubyte, GLboolean
    us 16-bit unsigned integer unsigned short GLushort
    ui 32-bit unsigned integer unsigned long GLuint, GLenum, GLbitfield

    OpenGL: vertices

    glColor3f(1.0, 0.0, 0.0);  <-- Red
    glBegin(GL_LINE_LOOP);    <-- connected closed-loop lines is object type
       glVertex2iv(vert1);  <-- each defines an endpoint for part of object
       glVertex2iv(vert2);      being drawn, def'd in a vector
       glVertex2iv(vert3);
       glVertex2iv(vert4);
    glEnd();

    OpenGL: geometric primitives


    OpenGL: coordinate systems

     
     glutInitWindowSize(400, 400); 

    gluOrtho2D(0, 500, 100, 200);

     

    OpenGL color


    OpenGL: RGB color scheme


    OpenGL: color map mode

            glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX);
            glutSetColor(1, 0.0, 0.0, 0.0);
            glClearIndex(1.0);
            glClear(GL_COLOR_BUFFER_BIT);
    
            glutSetColor(10, 1.0, 0.0, 0.0);
            glIndexi(10);
            glBegin(GL_POLYGON);
                    glVertex2i(50, 50);
                    glVertex2i(100, 100);
                    glVertex2i(0, 0);
            glEnd();  /* a red triangle was drawn */

    OpenGL: RGB mode


    OpenGL: RGB mode


    Another example: crisscross.c

    #include <GL/gl.h>
    #include <GL/glut.h>
    
    
    void crisscross(void) {
    
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_LINES);
            glVertex2i(50, 50);
            glVertex2i(350, 350);
            glVertex2i(50, 350);
            glVertex2i(350, 50);
        glEnd();
        glFlush();
    }
    
    main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitWindowSize(400,400);
        glutInitDisplayMode(GLUT_RGB);
        glutCreateWindow("Glut Crisscross");
    
        glutDisplayFunc(crisscross);
    
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glColor3f(1.0, 0.0, 0.0);
        glOrtho(0.0, 400.0, 0.0, 400.0, 0.0, 1.0); 
    
        glutMainLoop();
    }
    Here's a variation : square.c and its output.

    I/O and OpenGL


    OpenGL I/O using Glut


    OpenGL programs with glut I/O:


    Style


    C programming and debugging


    Comments


    References:



    Back to COSC 3P98 index

    COSC 3P98 Computer Graphics
    Brock University
    Dept of Computer Science
    Copyright © 2009 Brian J. Ross (Except noted figures).
    http://www.cosc.brocku.ca/Offerings/3P98/course/lectures/OpenGL/
    Last updated: September 15, 2009