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; GLfloat Y = 0.0f;
#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); glTranslatef(X, Y, 0.0f); glutPostRedisplay();
glColor3f(1.0,1.0,1.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();
glEnable (GL_LINE_STIPPLE);
glLineStipple (1, 0x0101);
drawOneLine (-0.1, 1, -1, 1);
glLineStipple (1, 0x00FF);
drawOneLine (-0.1, 0.9, -1, 0.9);
glLineStipple (1, 0x1C47);
drawOneLine (-0.1, 0.8, -1, 0.8);
glLineWidth (5.0);
glLineStipple (1, 0x0101);
drawOneLine (-0.1, 0.7, -1, 0.7);
glLineStipple (1, 0x00FF);
drawOneLine (-0.1, 0.6, -1, 0.6);
glLineStipple (1, 0x1C47);
drawOneLine (-0.1, 0.5, -1, 0.5);
glLineWidth (1.0);
glDisable (GL_LINE_STIPPLE);
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();
glBegin(GL_LINE_STRIP);
glVertex2f(-0.6,0.3);
glVertex2f(-0.3,0.2);
glVertex2f(-.65, 0.2);
glVertex2f(-0.4, 0.05);
glEnd();
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();
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();
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();
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();
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();
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();
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();
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();
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.
void keyCB(unsigned char key, int x, int y)
{
if( key == 'q' || key == 'Q')
exit(0);
}
void specialKey(int key, int x, int y) {
switch(key) {
case GLUT_KEY_LEFT : X -= 0.1f;
break;
case GLUT_KEY_RIGHT : X += 0.1f;
break;
case GLUT_KEY_UP : Y += 0.1f;
break;
case GLUT_KEY_DOWN : Y -= 0.1f;
break;
}
The main function:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,600); glutCreateWindow("OpenGL 2 Dimensional Geometries"); glutDisplayFunc(display);
glutKeyboardFunc(keyCB); glutSpecialFunc(specialKey); glutMainLoop();
return 0;
}