Click here to Skip to main content
15,880,608 members
Articles / Desktop Programming / Win32

A Simple OpenGL Stipple Polygon Example EP_OpenGL_003

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
5 Feb 2008CPOL 65.7K   986   11   1
A Simple OpenGL Stipple Polygon Example using Glut.h

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. After installing the Glut.h file, just compile the code and execute it.

C++
// 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 rectangular 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 array 
    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 background 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();                          // initialize 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;
}  

History

  • 5th February, 2008: Initial post

License

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


Written By
Software Developer
United States United States
None

Comments and Discussions

 
QuestionReference link to understand writing of user defined mask Pin
Member 1174772210-Mar-20 23:19
Member 1174772210-Mar-20 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.