Example 03

Example 03

This is a simple OpenGL program that draws a set of vertices using various point and line primitives.

ex_03/primitives.png

Source Code

primitives.cc:

// Peter Bui
// CSE 40166 Computer Graphics (Fall 2010)
// Example 3: fun with OpenGL primitives

#include "color.h"
 
#include <cmath>
#include <cstdio>
#include <cstdlib>

#include <unistd.h>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

// Constants -------------------------------------------------------------------

#define NVERTICES 6
GLfloat VERTICES[NVERTICES][2] = {
    { 0.0,  0.0},
    { 5.0,  0.0},
    { 7.5,  2.5},
    { 5.0,  5.0},
    { 0.0,  5.0},
    {-2.5,  2.5}
};

#define NPRIMITIVES 4
int PRIMITIVES[NPRIMITIVES] = {
    GL_POINTS,
    GL_LINES,
    GL_LINE_STRIP,
    GL_LINE_LOOP
};

// Global variables ------------------------------------------------------------

static int WindowWidth  = 640;
static int WindowHeight = 480;

static int BackgroundColor = COLOR_BLACK;

// Initialize viewing ----------------------------------------------------------

void
init()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-5.0, 10.0, -5.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    
    glClearColor(ColorTable[BackgroundColor][0],
                 ColorTable[BackgroundColor][1], 
                 ColorTable[BackgroundColor][2], 
                 1.0);

    glEnable(GL_POINT_SMOOTH);
}

// Display callback ------------------------------------------------------------

void 
display()
{
    int ViewportWidth  = WindowWidth*2  / NPRIMITIVES;
    int ViewportHeight = WindowHeight*2 / NPRIMITIVES;

    glClear(GL_COLOR_BUFFER_BIT);

    for (int p = 0; p < NPRIMITIVES; p++) {
        glViewport((p % 2) * ViewportWidth, (p < 2) ? ViewportHeight : 0, ViewportWidth, ViewportHeight);

        glColor3fv(color_rotate());
        glBegin(PRIMITIVES[p]); {
            for (int i = 0; i < NVERTICES; i++) 
                glVertex2fv(VERTICES[i]);
        }glEnd();
    }
    
    glFlush();
}

// Keyboard callback -----------------------------------------------------------

void
keyboard(unsigned char key, int x, int y)
{
    if (key == 'q' || key == 'Q')
        exit(EXIT_SUCCESS);
}

// Show usage ------------------------------------------------------------------

void
show_usage()
{
    printf("usage: primitives [options]\n");
    printf("General Options:\n");
    printf("  -h             Show this help message\n");
    printf("\nWindow Options:\n");
    printf("  -W <width>     Set window width\n");
    printf("  -H <height>    Set window height\n");
}

// Parse command line arguments ------------------------------------------------
    
void
parse_command_line_arguments(int argc, char *argv[])
{
    int c;

    while ((c = getopt(argc, argv, "W:H:h")) >= 0) {
        switch (c) {
            case 'W': 
                WindowWidth  = strtol(optarg, NULL, 10); 
                break;
            case 'H': 
                WindowHeight = strtol(optarg, NULL, 10); 
                break;
            case 'h':
                show_usage();
                exit(EXIT_SUCCESS);
                break;
            default:
                fprintf(stderr, "primitives: unknown command line argument %c\n", c);
                show_usage();
                exit(EXIT_FAILURE);
                break;
        }
    }
}

// Main execution --------------------------------------------------------------

int 
main(int argc, char *argv[])
{
    glutInit(&argc, argv);

    parse_command_line_arguments(argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(WindowWidth, WindowHeight);
    glutCreateWindow("primitives");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    init();
    glutMainLoop();     

    return (EXIT_SUCCESS);
}

// vim: sts=4 sw=4 ts=8 ft=cpp

color.h:

#ifndef COLOR_H
#define COLOR_H

#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif

GLfloat ColorTable[][3] = {
    { 0.0, 0.0, 0.0 },
    { 1.0, 0.0, 0.0 },
    { 0.0, 1.0, 0.0 },
    { 0.0, 0.0, 1.0 },
    { 1.0, 1.0, 0.0 },
    { 1.0, 0.0, 1.0 },
    { 0.0, 1.0, 1.0 },
    { 1.0, 1.0, 1.0 },
};

enum COLOR_TABLE_INDEX {
    COLOR_BLACK = 0,
    COLOR_RED,
    COLOR_GREEN,
    COLOR_BLUE,
    COLOR_YELLOW,
    COLOR_MAGENTA,
    COLOR_CYAN,
    COLOR_WHITE,
    COLOR_UNKNOWN
};

GLfloat *
color_rotate() {
    static int index = COLOR_BLACK;
    
    index = (index + 1) % COLOR_WHITE;
    return ColorTable[index];
}

#endif

Files