CISC 3620
Computer Graphics
Chapter 2


Two-D Applications

A First OpenGL Program - The Sierpinski Gasket


Moving to an Actual Implementation

A C++/OpenGL Implementation of the Above

 1. #define GLUT_DISABLE_ATEXIT_HACK
 2. #include <windows.h>

 3. #include <GL/gl.h>
 4. #include <GL/glut.h>
 5. #include <stdlib.h>

 6. static void display(void) {

 7.     GLint vx[] = {100, 0, 200};
 8.     GLint vy[] = {0, 200, 200};

 9.     GLint px = 100;
10.     GLint py = 100;

11.     glClear(GL_COLOR_BUFFER_BIT);
12.     glColor3f(1.0, 0.0, 0.0);

13.     glBegin(GL_POINTS);
14.         for (GLint i = 0; i < 10000; i++) {
15.             int whichV = rand() % 3;
16.             int newPx = (px + vx[whichV]) / 2;
17.             int newPy = (py + vy[whichV]) / 2;
18.             glVertex2i(newPx, newPy);
19.             px = newPx;
20.             py = newPy;
21.         }
22.     glEnd();
23.     glFlush();
24. }
 
25. void init() {
26.     glClearColor(1.0, 1.0, 1.0, 1.0);
27.     gluOrtho2D(0, 500, 500, 0);
28. }

29. int main(int argc, char *argv[]) {
30.     glutInit(&argc, argv);
31.     glutInitWindowPosition(100, 100);
32.     glutInitWindowSize(500, 500);
33.     glutCreateWindow("Gasket");
 
34.     glutDisplayFunc(display);
 
35.     init();

36.     glutMainLoop();

37.     return 0;

38. }

A 'Thirty-Second' Overview of the Above

The Program Structure

The Program's Excution Lifecycle

Vertices

The OpenGL GLVertex Function

Specifying an Object as a Sequence of Vertices

Basic Structure of an OpenGL Program

The Graphics Systems vs User Interaction

The Display Callback Function

Coordinate Systems

OpenGL API

Major Categories of OpenGL's Functions

(Finite) State Machines

OpenGL Interface


Primitives

Polygons


Rendering/filling polygons quickly require that they have well-defined interiors: simple, convex, and flat.

Polygons in OpenGL

Approximating a Sphere

Text

Curved Objects

Attributes

Color

RGB Color Model

Indexed Color Model

Setting Color Attributes

Viewing

Orthographic Viewing

Orthographic Viewing in OpenGL

Matrix Modes

Aspect Ratio and Viewports

Revisiting the Gasket

Drawing a Triangle

void triangle(GLfloat *a, GLfloat *b, GLfloat *c) {
   glVertex2fv(a);
   glVertex2fv(b);
   glVertex2fv(c);
}
Notes: Splitting/Dividing a Triangle

void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c, int m) {	// m controls the recursion
   GLfloat v0[2], v1[2], v2[2];
   int j;
   if(m>0) {
      for(j=0; j < 2; j++) { 
         v0[j]=(a[j]+b[j])/2;
         v1[j]=(a[j]+c[j])/2;
         v2[j]=(b[j]+c[j])/2;
      }
      divide_triangle(a, v0, v1, m-1);
      divide_triangle(c, v1, v2, m-1);
      divide_triangle(b, v2, v0, m-1);
   }
   else 
      triangle(a,b,c); /* draw triangle at end of recursion */
}

Moving to Three Dimensions

Using midpoint method

for (GLint i = 0; i < 10000; i++) {
   int whichV = rand() % 4;
   GLfloat newPx = (px + vx[whichV]) / 2;
   GLfloat newPy = (py + vy[whichV]) / 2;
   GLfloat newPz = (pz + vz[whichV]) / 2;
   glColor3f (newPx, newPy, newPz);
   glVertex3f(newPx, newPy, newPz);
   px = newPx;
   py = newPy;
   pz = newPz;
}
		
Subdividing Tetrahedra

Code for Chapter 2