Introduction to OpenGL

Tom Kelliher, CS 320

Feb. 9, 2000

Administrivia

Announcements

Assignment

Read 2.4--2.6.

From Last Time

Experimentation with Sierpinski gasket.

Outline

  1. Pen-Plotter model, vertex specification, coordinate systems.

  2. OpenGL API.

  3. Primitive basics.

Coming Up

Viewing, control functions.

Introduction

Consider the Sierpinski gasket problem for a triangle:

// Initialize.
clear the viewing window;
set vertices of the triangle;
let currentPoint = random point in interior of triangle;
randomly select one of the three triangle vertices;

// Run.
while (notDone)
{
   nextPoint = point half-way between currentPoint and
      selected vertex;

   display nextPoint;

   currentPoint = nextPoint;
   randomly select one of the three triangle vertices;
}
Questions:
  1. In what coordinate system do we work?

  2. In how many dimensions?

  3. How do we deal with events: mouse, keyboard, window system, ``idle''?

A schematic of glutMainLoop():

while (alive)
{
   if (windowsEvent && user registered a display callback)
      call display callback;
   else if (keyboardActive && user registered a keyboard callback)
      call keyboard callback;
   else if (mouseActive && user registered a mouse callback)
      call mouse callback;

   ...

   else if (user registered an idle callback)
      call idle callback;
}
Points:
  1. It's not a good idea to register your initialize window function as the idle function. Why?

  2. To support interactive use, the idle function should finish quickly. Why?

  3. Some of the callbacks can be programmatically triggered. For example: glutPostRedisplay() should be called instead of calling clear() in the Sierpinski gasket program.

The Pen-Plotter Model

Note that all our output devices are 2-D.

  1. Uses a gantry to move a pen.

  2. Gantry moves in x and y.

  3. Pen can be raised and lowered.

  4. Operations:
    1. moveto(x, y);

    2. lineto(x, y);

  5. This model is still in use with pen-plotters and PDLs such as Postscript.

  6. How do we extend to 3-D?
    1. Must compute the 2-D projection of 3-D points. (Application of trigonometry, which you should review for later.)

    2. Should application or API perform the projections?

  7. We can set z=0 so that we're working 2-D in 3-D.

Vertex Specification in OpenGL

Can't do much without vertices.

Indicator for the general form: glVertex*() (see man page on phoenix for glvertex .

Some examples:

  1. glVertex2i(GLint xi, GLint yi);

  2. glVertex3f(GLfloat x, GLfloat y, GLfloat z);

  3. Using an array to store the vertex coordinates:
    GLfloat vertex[3] = { 0.0f, 1.0f, 2.0f };
    
    glVertex3fv(vertex);
    

Vertices are grouped using glBegin()/glEnd():

glBegin(GL_LINES);
   glVertex2f(x1, y1);
   glVertex2f(x2, y2);
   glVertex2f(x3, y3);
   glVertex2f(x4, y4);
glEnd();
or
glBegin(GL_POINTS);
   glVertex2f(x1, y1);
   glVertex2f(x2, y2);
   glVertex2f(x3, y3);
   glVertex2f(x4, y4);
glEnd();

Heart of the Sierpinski gasket program:

void display(void)
{
   static point2 p = { x, y }; /* Initial point.
   int i;

   i = rand() % 3;

   p[0] = (p[0] + triangle[0]) / 2.0;
   p[1] = (p[1] + triangle[1]) / 2.0;

   glBegin(GL_POINTS);
      glVertex2fv(p);
   glEnd();
}

Coordinate Systems

What are our units for x, y, and z?

  1. Originally, graphics systems worked in coordinates of display device.

  2. Portability?

  3. Much more convenient to work in problem coordinates, let API convert: device independence.

  4. Mapping from world coordinates to device coordinates. For us, window coordinates.

OpenGL API

Support provided by a graphics API:

  1. Primitive functions: points, line segments, polygons, text, curves, surfaces.

  2. Attribute functions: color, fill, type face.

  3. Viewing functions: attributes of the synthetic camera.

  4. Transformation functions: rotation, translation, scaling. Matrix computations.

  5. Input functions: keyboard, pointing device.

  6. System communication: window system, OS, other workstations, other users.

OpenGL Library Organization:

Primitive Basics

Consider:

glBegin(primitiveType)
   glVertex*( ... );
   // ...
   glVertex*( ... );
glEnd();
for the primitiveTypes:
  1. GL_POINTS .

  2. GL_LINES .

  3. GL_LINE_STRIP .

  4. GL_LINE_LOOP . When is this not a polygon?

Polygon Basics

  1. Polygons have interiors which can be filled.

  2. What is filling? Color, pattern (bitmap) fills.

  3. Simple polygon: no edges cross.

  4. When filling, how do we determine if a point is interior? Crossing test.

  5. Fill example: five-pointed star. Scanlines. Is this the effect we want?

  6. Winding test improvement:
    1. Pick any vertex and direction.

    2. Traverse the polygon, labeling each edge with direction of traversal.

    3. Scanline crosses ``up'' edge: add 1. Crosses ``down'' edge: subtract 1.

    4. Point's winding number starts at 0. In interior if final winding number not 0.

  7. Complications: lines parallel to scanline, intersections with vertices.

  8. Convex and concave polygons.
    1. Definitions.

    2. Concave polygons can present problems.

    3. Often, hardware and software supports triangles.

    4. 2-D polygon convexity test: for any interior point, it is ``to the right'' when traversing the edges clockwise.



Thomas P. Kelliher
Tue Feb 8 17:12:29 EST 2000
Tom Kelliher