Click here to Skip to main content
6,295,667 members and growing! (10,503 online)
Email Password   helpLost your password?
Multimedia » OpenGL » General     Beginner License: The Code Project Open License (CPOL)

A Simple OpenGL Stipple Polygon Example EP_OpenGL_003

By erjan123

A Simple OpenGL Stipple Polygon Example using Glut.h
C++, C++/CLI, Windows (Vista), Win32, Visual Studio (VS2005), OpenGL, Dev
Posted:5 Feb 2008
Updated:5 Feb 2008
Views:10,704
Bookmarked:7 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 1.81 Rating: 3.00 out of 5
2 votes, 50.0%
1

2

3

4
2 votes, 50.0%
5

Introduction

This is a program from "OpenGL Programming Guide (Third Edition). It is an example of using stipples (line patterns) in polygons. I also did add another stipple pattern that I created myself. This pattern is my initials which is EP (Ercan Polat).

Using the code

Make sure to install the GLUT library. If you don't know how to do it go to my first article. (ASimpleOpenGLWindowGLUT.aspx . After installing the Glut.h file just compile the code and execute it.

// StipplingPolygons.cpp : Defines the entry point for the console application.
// Ercan Polat: 02/04/2008
// Note: This program is an example of "OpenGL Programming Guide (Third Edition)". I did
// add a stippling part with my initials (EP). 
// Project Description: This project creates a simple OpenGl window using GLUT library.
// The program is an example of stipple patterns used on rectangle polygons.

#include "stdafx.h"
#include <GL/glut.h>

// function that creates the stipple patterns and rectanguale polygons.
void display(void)
{
    // Stipple pattern of my initials (EP)
GLubyte myInitial[] = {
    0xff, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0xff, 0x01, 0xff, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0xff, 0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};

    // A stipple pattern of a fly
GLubyte fly[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
    0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
    0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
    0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
    0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
    0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
    0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
    0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
    0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
    0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
    0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};

    // Another stipple pattern
GLubyte halftone[] = {
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};

    // Clear the Color Buffer
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);        // Set the color to white 
    // draw one solid, unstippled rectangle
    glRectf(25.0, 25.0, 125.0, 125.0);
    glEnable(GL_POLYGON_STIPPLE);          // Enable POLYGON STIPPLE
    glPolygonStipple(fly);                   // Pass the stipple arrray 
    glRectf (125.0, 25.0, 225.0, 125.0);   // draw the first rectangle with the first stipple
    glPolygonStipple(halftone);               // pass the second stipple array
    glRectf(225.0, 25.0, 325.0, 125.0);    // draw the rectangle
    
    // My own code starts here
    glColor3f(1.0, 0.0,0.0);            // Set change the color to red  
    glPolygonStipple(myInitial);        // Pass the stipple my initial (EP) 
    glRectf(25.0, 125.0, 325.0, 225.0); // Draw a bigger rectangle
    glDisable(GL_POLYGON_STIPPLE);      // Disable the POLYGON STIPPLE
    glFlush();
}
void init (void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);    // Clear the backround set it to black
    glShadeModel(GL_FLAT);                 // set the shading model to FLAT
}
// Function that resets the window, each time when the window size is changed
void reshape(int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);    
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
// Main entry point of the program
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(350, 250);    // set up the window size
    glutCreateWindow(argv[0]);       // create the window
    init();                          // initiliaze the window
    glutDisplayFunc(display);        // call the display to draw the rectangles
    glutReshapeFunc(reshape);        // call the reshape function (each time when the window size is changed)
    glutMainLoop();
    return 0;
}  
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

erjan123


Member
None
Occupation: Software Developer
Location: United States United States

Other popular OpenGL articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Feb 2008
Editor:
Copyright 2008 by erjan123
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project