JMU
OpenGL Basics
with Examples


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Introduction
OpenGL Concepts
Clearing Buffers
Vertices
Properties of Vertices
Points, Lines and Polygons
Points, Lines and Polygons (cont.)
Controlling the Rendering Process
Vertex Arrays
  1. Enable Arrays
  2. Specify the Data
  3. Dereference
Data for an Example
openglexamples/basics/vertex-arrays.c (Fragment: data)
        /**
 * Data
 */
static GLfloat vertices[] = {
                             -1.0, -1.0, -1.0,
                              1.0, -1.0, -1.0,
                              1.0,  1.0, -1.0,
                             -1.0,  1.0, -1.0,
                             -1.0, -1.0,  1.0,
                              1.0, -1.0,  1.0,
                              1.0,  1.0,  1.0,
                             -1.0,  1.0,  1.0
                            };

static GLfloat colors[]   = {
                              0.0,  0.0,  0.0,  0.5,
                              1.0,  0.0,  0.0,  0.5,
                              1.0,  1.0,  0.0,  0.5,
                              0.0,  1.0,  0.0,  0.5,
                              0.0,  0.0,  1.0,  0.5,
                              1.0,  0.0,  1.0,  0.5,
                              1.0,  1.0,  1.0,  0.5,
                              0.0,  1.0,  1.0,  0.5
                            };
        
Vertex Arrays (cont.)
openglexamples/basics/vertex-arrays.c (Fragment: enableArrays)
          glEnableClientState(GL_COLOR_ARRAY);
  glEnableClientState(GL_VERTEX_ARRAY);
        
Vertex Arrays (cont.)
openglexamples/basics/vertex-arrays.c (Fragment: specifyData)
          // Each color has 4 elements
  glColorPointer(4, GL_FLOAT, 0, colors);

  // Each vertex has 3 elements
  glVertexPointer(3, GL_FLOAT, 0, vertices);
        
Vertex Arrays (cont.)
openglexamples/basics/vertex-arrays.c (Fragment: dereferenceArrayElements)
          // Each call to glArrayElement() has the same effect
  // as a call to glVertex3fv() and a call to glColor4fv()
  glBegin(GL_QUADS);  // front
  glArrayElement(4);
  glArrayElement(5);
  glArrayElement(6);
  glArrayElement(7);
  glEnd();
        
Vertex Arrays (cont.)

void glDrawElements(GLenum mode, GLsizei count, GLenum type, void* indices)

openglexamples/basics/vertex-arrays.c (Fragment: dereferenceDrawElements)
        static GLubyte front[]  = {4, 5, 6, 7};
static GLubyte right[]  = {1, 2, 6, 5};
static GLubyte bottom[] = {0, 1, 5, 4};
static GLubyte back[]   = {0, 3, 2, 1};
static GLubyte left[]   = {0, 4, 7, 3};
static GLubyte top[]    = {2, 3, 7, 6};
  glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, right);
  glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, bottom);
  glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, back);
  glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, left);
  glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, top);
        
Viewing
  1. ModelView Matrix:
    • From object coordinates to eye coordinates
  2. Projection Matrix
    • From eye coordinates to clip coordinates
  3. Perspective Division:
    • From clip coordinates to normalized device coordinates (NDC)
  4. Viewport Transformation
    • From NDC to window coordinates
Viewing (cont.)
ModelView Matrix
The Projection
Viewing (cont.)

An Example

openglexamples/basics/projection.c
        #include <GL/glut.h>

/**
 * \file
 * A simple OpenGL application demonstrates the use
 * of projections
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */

int window;

/**
 * Create a window.
 *
 * @param title   The title
 * @param width   The width (in pixels)
 * @param height  The height (in pixels)
 * @param x       The x position
 * @param y       The y position
 * @return        The window id
 */
int createWindow(const char* title, int width, int height, int x, int y) {
  int id;

  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(width, height);
  glutInitWindowPosition(x, y);
  id = glutCreateWindow(title);

  return id;
}

/**
 * Perform OpenGL initializations.
 */
void init() {
  glClearColor(1.0, 1.0, 1.0, 1.0);

  // Setup the projection
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  //glOrtho(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); // Orthographic
  gluPerspective(65.0, 1.0, 1.0, 20.0);  // Perspective
}

/**
 * Display/render the OpenGL window.
 */
void display() {
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(0.0, 0.0, 0.0);

  // Transform
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  gluLookAt(0.0, 0.0, 5.0,   // Camera at (0,0,5)
            0.0, 0.0, 0.0,   // Aim camera towards (0,0,0)
            0.0, 1.0, 0.0);  // Up is (0,1,0)

  // Create a cube
  glutWireCube(1.0);

  glFlush();

  // Some other useful geometric objects in GLUT:
  //
  // glutSolidSphere(), glutWireSphere()
  // glutSolidCone(), glutWireCone()
  // glutSolidTirus(), glutWireTorus()
  // glutSolidTeapot(), glutWireTeapot()
  //
  // as well as Dodecahedron, Octahedron, Tetrahedron, and Icosahedron
}

/**
 * The entry point of the application.
 *
 * This function contains calls to GLUT.
 *
 * @param argc  The number of command line arguments
 * @param argv  The array of command line arguments
 * @return      A status code
 */
int main(int argc, char** argv) {
  glutInit(&argc, argv);

  window = createWindow("Projection", 640, 480, 0, 0);

  glutDisplayFunc(display);

  init();

  glutMainLoop();
}