2. Intro to OpenGL

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



Intro to OpenGL


OpenGL platforms

  1. Linux (J310)
  2. OpenGL for Windows, OS-X
  3. OpenGL ES: OpenGL for tablets (iOS, Android)
  4. Mesa OpenGL

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)

  • Program example: crisscross.c

    #include <GL/gl.h>
    #include <GL/glut.h>
    
    void crisscross(void) {
    
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0, 0.0, 0.0);
        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);
        glMatrixMode(GL_PROJECTION);
        gluOrtho2D(0.0, 399.0, 0.0, 399.0); 
    
        glutMainLoop();
    }
    Here's a variation : square.c and its output.

    crisscross.c: pulling it apart...



    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


    (above image copyright of original authors)
    main...
            glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX);
            glutSetColor(1, 0.0, 0.0, 0.0);
            glClearIndex(1.0);
    
    User callback...
            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


    I/O and OpenGL


    OpenGL I/O using Glut


    OpenGL programs with glut I/O:


    Style


    C programming and debugging


    Comments

  • glut library:

  • References:



    Back to COSC 3P98.

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