13.016 Introduction to Geometric Modeling and Computation
OpenGL Graphics Library Utility Toolkit (GLUT)
(Continued)
Here is the remainder of the example program:
#include <GL/gl.h> /* OpenGL library */
#include <GL/glu.h> /* OpenGL utility library */
#include "glut.h" /* GLUT library */
#define DEG_TO_RAD 0.017453293 /* convert from degrees to radians */
/* DEG_TO_RAD = PI/180 */
void main(int argc, char **argv)
{
...
}
void reshapeCB(int width, int height)
{
glViewport(0, 0, width, height); /* set viewport to fill window */
}
/***************************************************************
* This callback function is automatically called whenever
* the window needs to be redrawn.
**************************************************************/
void displayCB(void)
{
float deg = 18.0, scale, width, xy[5][2];
int i;
for (i=0; i<5; i++) { /* find the coordinates of vertices */
xy[i][0] = cos(deg*DEG_TO_RAD); /* the cos and sin functions require */
xy[i][1] = sin(deg*DEG_TO_RAD); /* the angle argument in radians */
deg += 72.0;
}
glClearColor(0.0, 0.0, 0.0, 0.0); /* set the clear color to black */
glClear(GL_COLOR_BUFFER_BIT); /* erase the window to black */
gluOrtho2D(-1.0, 1.0, -1.0, 1.0); /* define a 2D orthographic projection */
for (i=0; i<4; i++) { /* draw the star 4 times */
switch (i) {
case 0:
glColor3f(1.0, 1.0, 1.0); /* first star is white */
break;
case 1:
glColor3f(1.0, 0.0, 0.0); /* second star is red */
break;
case 2:
glColor3f(0.0, 1.0, 0.0); /* third star is green */
break;
case 3:
glColor3f(0.0, 0.0, 1.0); /* fourth star is blue */
break;
}
width = 2.0*(4-i); /* set the line width */
glLineWidth(width); /* i = 0, width = 8 */
/* 1, 6 */
/* 2, 4 */
/* 3, 2 */
scale = 1.0 - i*0.2; /* scale factor */
/* i = 0, scale = 1.0 */
/* 1 0.8 */
/* 2 0.6 */
/* 3 0.4 */
glBegin(GL_LINE_LOOP); /* start a GL_LINE_LOOP */
glVertex2f(xy[0][0]*scale, xy[0][1]*scale); /* add the first vertex */
glVertex2f(xy[2][0]*scale, xy[2][1]*scale); /* add second vertex ... */
glVertex2f(xy[4][0]*scale, xy[4][1]*scale);
glVertex2f(xy[1][0]*scale, xy[1][1]*scale);
glVertex2f(xy[3][0]*scale, xy[3][1]*scale); /* we don't need to add */
glEnd(); /* the first vertex again */
}
glFlush(); /* actually draw on screen */
}
On to making the programs...
Back to GLUT...
Back to the overview...