Click here to Skip to main content
Click here to Skip to main content

A Simple OpenGL 2D Primitives EP_OpenGL_002

, 7 Feb 2008
Rate this:
Please Sign up or sign in to vote.
2D basic OpenGL Primitives Lines, Points, Triangles, Quads and Polygons

Introduction

It is a small OpenGL program which shows how to draw 2 dimensional OpenGL primitives such as GL_POINT, GL_LINES, GL_TRIANGLES, GL_QUADS, GL_POLYGON. It is based on the "OpenGL Programming Guide Third Edition". It uses GLUT.h library.

Background

I'm not really explaining OpenGL or GLUT in detail. There are many good sources which explain the subject in detail. My goal is to show a working version of this subject.

Using the Code

If you don't have GLUT.h library installed, this program will not work. If you don't know how to install it, you can read my first article where I explained how to install GLUT.h.

A Simple OpenGL Window with GLUT Library

Once you install the GLUT.h library, just compile and execute the code.

After you install the GLUT.h library, you have to include it in the code (#include<GL/glut.h>).

Here is also an example of user defined global function (drawOneLine(x1,y1,x2,y2)):

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

GLfloat X = 0.0f;        // Translate screen to x direction (left or right)
GLfloat Y = 0.0f;        // Translate screen to y direction (up or down)

// A function to draw a line 
#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES); \
glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();

All the primitives are drawn here (display(void)):

 void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluOrtho2D (-1.1, 1.1 , -1.1 , 1.1);   	// Displays the OpenGL window area 
					// from (x1,y1,x2,y2)
    glTranslatef(X, Y, 0.0f);               	// Moves (translates) the screen left 
					// to right and up or down
     glutPostRedisplay();                   
    glColor3f(1.0,1.0,1.0);

    // This lines are the two main lines for the coordinate system
    // The crossing point of the line is the (0,0,0)
    // Since its just 2 dimensional z is 0
    glBegin(GL_LINES);
        glVertex3f(-1.0, 0.0, 0.0);
        glVertex3f(1, 0.0, 0.0);
        glVertex3f(0.0, -1.0, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
    glEnd();

    // Example of some line stipple 
    glEnable (GL_LINE_STIPPLE);
        glLineStipple (1, 0x0101); /* dotted */
        drawOneLine (-0.1, 1, -1, 1);
        glLineStipple (1, 0x00FF); /* dashed */
        drawOneLine (-0.1, 0.9, -1, 0.9);
        glLineStipple (1, 0x1C47); /* dash/dot/dash */
        drawOneLine (-0.1, 0.8, -1, 0.8);
        /* in 2nd row, 3 wide lines, each with different stipple */
        glLineWidth (5.0);
        glLineStipple (1, 0x0101); /* dotted */
        drawOneLine (-0.1, 0.7, -1, 0.7);
        glLineStipple (1, 0x00FF); /* dashed */
        drawOneLine (-0.1, 0.6, -1, 0.6);
        glLineStipple (1, 0x1C47); /* dash/dot/dash */
        drawOneLine (-0.1, 0.5, -1, 0.5);
        glLineWidth (1.0); 
    glDisable (GL_LINE_STIPPLE);
    
    // Just basic lines (wrote E my names initial)
    glBegin(GL_LINES);
        glVertex2f(-1.0, 0.4);
        glVertex2f(-1.0, 0.1);
        glVertex2f(-1.0, 0.4);
        glVertex2f(-0.8, 0.4);
        glVertex2f(-1.0, 0.25);
        glVertex2f(-0.8, 0.25);
        glVertex2f(-1.0, 0.1);
        glVertex2f(-0.8, 0.1);
    glEnd();    

    // Example of Line Strip
    // The end of the previous line is the beginning of the next line
    // The program connects the points for you basically you save time
    glBegin(GL_LINE_STRIP);
        glVertex2f(-0.6,0.3);
        glVertex2f(-0.3,0.2);
        glVertex2f(-.65, 0.2);
        glVertex2f(-0.4, 0.05);
    glEnd();

    // Example of Line Loop
    // Its similar to the line strip however at the end it connects the last point
    // with the beginning point
    glBegin(GL_LINE_LOOP);
        glVertex2f(-0.65,0.45);
        glVertex2f(-0.1,0.4);
        glVertex2f(-0.1, 0.1);
        glVertex2f(-0.4, 0.4);
    glEnd();

    // Example of points
    glBegin(GL_POINTS);
        glVertex3f(0.1, 0.1, 0.0);
        glVertex3f(0.1, 0.2, 0.0);
        glVertex3f(0.1, 0.3, 0.0);
        glVertex3f(0.1, 0.4, 0.0);
        glVertex3f(0.1, 0.5, 0.0);
        glVertex3f(0.1, 0.6, 0.0);
        glVertex3f(0.1, 0.7, 0.0);
        glVertex3f(0.1, 0.8, 0.0);
        glVertex3f(0.1, 0.9, 0.0);
        glVertex3f(0.1, 1.0, 0.0);
    glEnd();
        
    // Example of triangles
    glBegin(GL_TRIANGLES);
        glVertex3f(0.2, 1.0, 0.0);
        glVertex3f(0.5, 1.0, 0.0);
        glVertex3f(0.35, 0.6, 0.0);
    glEnd();

    // Example of triangle strip
    glBegin(GL_TRIANGLE_STRIP);
        glVertex3f(0.2, 0.5, 0.0);
        glVertex3f(0.2, 0.1, 0.0);
        glVertex3f(0.4, 0.3, 0.0);
        glVertex3f(0.5, 0.4, 0.0);
        glVertex3f(0.4, 0.1,0.0);    
    glEnd();
    
    // Example of triangle fan
    glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.5, 0.5, 0.0);
        glVertex3f(0.5, 0.7, 0.0);
        glVertex3f(0.6, 0.6, 0.0);
        glVertex3f(0.7, 0.5, 0.0);
        glVertex3f(0.6, 0.4,0.0);    
    glEnd();

    // Example of quads
    glBegin(GL_QUADS);
        glVertex3f(-1, -0.2, 0.0);
        glVertex3f(-0.6, -0.2, 0.0);
        glVertex3f(-0.5, -0.50, 0.0);
        glVertex3f(-0.8, -0.75, 0.0);
    glEnd();

    // Example of quad strips
    glBegin(GL_QUAD_STRIP);
        glVertex3f(-0.1, -0.1, 0.0);
        glVertex3f(-0.3, -0.1, 0.0);
        glVertex3f(-0.124, -0.4, 0.0);
        glVertex3f(-0.27, -0.3, 0.0);
        glVertex3f(-0.35, -0.6, 0.0);
        glVertex3f(-0.4, -0.5, 0.0);
    glEnd();

    // Example of polygon
    glBegin(GL_POLYGON);
        glVertex3f(0.10, -0.25, 0.0);
        glVertex3f(0.7, -0.3, 0.0);
        glVertex3f(1, -0.50, 0.0);
        glVertex3f(0.6, -0.8, 0.0);
        glVertex3f(0.3, -0.6, 0.0);
    glEnd();

    // Don't wait start processing buffered OpenGL routines
    glFlush();
}

Two functions are used to get user input from keyword. If the user hits 'q' or 'Q', the program exits. If the user hits the arrow key, the window translates left or right up or down.

// Function to determine which key is pressed 
// Will be passed  to glutKeyboardFunc(keyCB);
void keyCB(unsigned char key, int x, int y)    /* called on key press */
{
    // if 'q' is pressed exit the program
    if( key == 'q' || key == 'Q') 
        exit(0);                
}

// Similar function like keyCB; determines which special key is pressed
// Will be passed to glutSpecialFunc(specialKey);
void specialKey(int key, int x, int y) { // called on special key pressed
   
    // Check which (arrow) key is pressed
    switch(key) {
        case GLUT_KEY_LEFT : 	// Arrow key left is pressed
            X -= 0.1f;
          break;
        case GLUT_KEY_RIGHT :    	// Arrow key right is pressed
            X +=  0.1f;
          break;
        case GLUT_KEY_UP :        	// Arrow key up is pressed
            Y += 0.1f;
          break;
        case GLUT_KEY_DOWN :    	// Arrow key down is pressed
            Y -= 0.1f;
          break;    
    }

The main function:

// Main point of the program
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600,600);  	// Size of the OpenGL window
    glutCreateWindow("OpenGL 2 Dimensional Geometries"); // Creates OpenGL Window    
    glutDisplayFunc(display);        
    glutKeyboardFunc(keyCB);        	// set window's key callback 
    glutSpecialFunc(specialKey);    	// set window's to specialKey callback
    glutMainLoop();

    return 0;
}

License

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

Share

About the Author

erjan123
Software Developer
United States United States
None

Comments and Discussions

 
GeneralFYI PinmemberJeremy Falcon7-Feb-08 10:03 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140826.1 | Last Updated 7 Feb 2008
Article Copyright 2008 by erjan123
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid