Chapter 2: Graphics Programming


</TBODY>
  • Angel uses a fractal shape, the Sierpinski gasket, as a device to teach some of the first parts of graphics programming. We need to understand it a bit in order to follow the development.
  • Angel begins by discussing a pen-plotter model. It is inherently two-dimensional, but useful in many settings; specifically, LOGO, GKS, and PostScript.
  • The discussion then moves to OpenGL
  • OpenGL renames data types by preceding many C types with the letters (capital) GL. So we have the types below (the 'u' indicated "unsigned" int/short/byte).

<TBODY>Glfloat /

Glint

/

Glshort

/

GLbyte

Gldouble

/

Gluint

/

Glushort

/ GLubyte</TBODY>
  • In OpenGL we often specify objects by listing their vertices.
  • Each vertex is a point in 2- 3- or 4-dimensional space, and its components (coordinates) can be any of the usual numeric data types above (or most, anyway). For example.We may say

glVertex2f(20.3,88.2);

'2' means that we have two dimensions

 'f' means floating point

the numbers give the x-y coordinates of the point

  • It is not uncommon to use arrays to hold points.
  • Consider the following code fragmenttaken from gasket.c:

typedef GLfloat point2[2];

point2 vertices[3] = {{0.0,0.0}, {250.0,500.0},{500.0,0.0}};

  • The first of these statements defines a type called point2 (as in 2D point); it is defined as an array of 2 GLfloats.
  • The second statement defines a variable vertices which is an array of three point2's, and initialized

vertices[0] = {0.0, 0.0} /* Lower left corner of a window */
vertices[1] = {250.0, 500.0} /* Halfway over and at the top of a 500x500 pixel window */
vertices[2] = {500.0, 0.0}/* Lower right corner of that window */

  • The definition of a point in the interior of the triangle formed above is defined as:

point2 p = {75.0, 50.0};

  • We construct an object by listing some vertices between a glBegin() and a glEnd() statement as indicated by the following examples:

glBegin (GL_POINTS); /* to display the point p above */

glVertex2fv(p); /* 'v' for 'vector' as p is defined with an array */

glEnd();

/***********************************************/

glBegin(GL_TRIANGLES); /* filled triangle with vertices */

glVertex2fv(vertices[0]);
glVertex2fv(vertices[1]);
glVertex2fv(vertices[2]);

glEnd();

  • In addition to GL_POINTS & GL_TRIANGLES, there are eight other figures that can be drawn, they are

-GL_LINES

-GL_LINE_STRIP

-GL_LINE_LOOP

-GL_POLYGON

-GL_QUADS

-GL_QUAD_STRIP

-GL_TRIANGLLE_STRIP

-GL_TRIANGLE_FAN

  • All the closed primitives are solid-filled, except for GL_LINE_LOOP, which only draws lines connecting the vertices.

Note: Although the examples above both used the 'v' form of glVertex, that is by no means the most common; there are many examples in the text to show the scalar case.

GLU & GLUT

The basic library, GL, used by OpenGL is pretty basic, or low-level. There are many kinds of things a programmer would want to do frequently -- things that are not included in GL.

Some less elementary shapes and capabilities are included in GLU, a Graphics Library Utility.

 In addition, in order to be able to work with a computer's windowing system, it is convenient to make use of some standard utilities. We use GLUT (Utility Toolkit) in this course, so we will need to learn some of its functions along with those of GLU and OpenGL itself.

The library organization is shown in Figure 2.7. In particular, the program template we use gathers the GLUT calls within main().

  • The following main program works for most noninteractive applications.

#include <GL/Glut.h>
void main (int argc, char ** argv) {

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("OpenGL Template");
glutDisplayFunc(display);
myinit();
glutMainLoop();

}

  • Before we can open a window, there must be interaction between the windowing system and OpenGL. In GLUT, this interaction is initiated by the function call

glutInit(int *argcp, char **argv)

  • The two arguments allow the user to pass command-line arguments, as in the standard C main function, and are usually the same as in main.
  • We can now open an OpenGL window using

glutCreateWindow( char *title)

  • Where the title at top of the window is given by the string title.

glutCreateWindow("OpenGL Template");

  • The window that we create has a default size, a position on the screen, and characteristics such as use of RGB color. We can also use GLUT functions before window creation to specify these parameters.

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);

  • Graphics are sent to the screen through a function called the display callback. The function is specified through the GLUT function

void glutDisplayFunc(void (*func) (void))

  • Here, func is the name of the function that will be called whenever the windowing system determines that the OpenGL window needs to be redisplayed.

glutDisplayFunc(display);

  • Examples of when a display callback occurs include

-when the window is first opened

-when the window is moved from one location on the screen to another.

-when a window in front of the OpenGL window is destroyed, making visible the whole OpenGL window.

  • In most programs, most of the graphics output will be generated in the display callback.
  • We use myinit to set the OpenGl state variables dealing with viewing and attributes—parameters that we prefer to set once, independently of the display function. These user options are set up through OpenGL functions on the GL and GLU libraries.

myinit();

  • The execution of glutMainLoop() will cause the program to begin an event-processing loop. If there are no events to process, the program will sit in a wait state, with our graphics on the screen, until we terminate the program through some external means, such as by hitting the “kill” key.
  • We need the function call glutMainLoop() to keep the display on the screen.
  • At some point, the values in world coordinates must be mapped into device coordinates. The graphics system, rather than the user, is responsible for this task, and the mapping is performed automatically as part of the rendering process. The user needs to specify only a few parameters to define this mapping.
  • A good API provides hundreds of functions; we divide the functions into six groups by their functionality:

1. primitive functions

2. attribute functions

3. viewing functions

4. transformation functions

5. input functions

6. control functions


  • glBegin(type) may have type be one of ten

GL_POINTS

GL_LINES

GL_LINE_STRIP

GL_LINE_LOOP

GL_TRIANGLES

GL_POLYGON

GL_QUADS

GL_TRIANGLE_STRIP

GL_QUAD_STRIP

GL_TRIANGLE_FAN

  • Polygon is an object that has a border that can be described by a line loop, but has an interior.
  • Polygons play a special role in computer graphics because we can display them rapidly and can use them to approximate curves surfaces.
  • The performance of graphics system is measured by the number of polygons per second that can be displayed.
  • We can display a polygon in a variety of ways.

-display only its edges

-fill its interior with a solid color, or pattern

-can display or not display the edges

  • Three properties will ensure that a polygon will be displayed correctly: It must be simple, convex, and flat.
  • In 2D, as long as no pair of edges of polygon crosses each other, we have a simple polygon.
  • An object is convex if all points on the line segment between any two points inside the object, or on its boundary, are inside the object.
  • There are two forms of text: Stroke text and Raster (bitmap) text.
  • Curved Objects can be produced via tessellation or mathematical definitions (parametric curves & quadric surfaces). GLU does this for us.
    An attribute is any property that determines how a geometric primitive is to be rendered. Color, size/thickness, texture, patterns.
    Additive (RGB) color. A color as a point in a color solid

- glColor3f(r, g, b); /* 0.0 <= r, g, b <= 1.0 */

- glClearColor(r, g, b, a); /* "alpha" channel -- opaque vs transparent */

- glPointSize(2.0); /* points are 2 pixels wide */

Indexed color: limited-depth buffer, so index into a color-lookup table of colors chosen from a palette.
- glIndexi(element);

- glutSetColor(int color, r, g, b); /* to build the table */
Viewing: Typically, not an object’s entire possible image "fits" onto our viewing plane. Instead, we have a viewing rectangle (clipping rectangle), outside of which parts of objects are clipped out of our image.AGE

PAGE 9