OpenGL / GLUT example

There are many ways to draw graphical objects and shapes, for example, using JavaFX and JavaFX2 (those are the old posts I wrote a few years back).

OpenGL (Open Graphics Library) is a cross-language, multi-platform application programming interface(API) for rendering 2D and 3D computer graphics. The API is typically used to interact with a Graphics processing unit (GPU), to achieve hardware accelerated rendering. In the simple term,  OpenGL is based on Standard to achieve relatively portable (Windows, Linux, Mac, etc).

OpenGL is mainly drawing things on screen in 2D and 3D, and it doesn’t define what the things are on screen like menus, windows, shapes. In order to compensate what OpenGL doesn’t define, the associate library GLUT (freeglut now) does include defining those shapes and objects to ease the programming. (see my study note)

There are many OpenGL tutorial resources on the web. I used http://www.opengl-tutorial.org/ and http://profs.sci.univr.it/~colombar/html_openGL_tutorial/en/index.html

To start playing OpenGL, FreeGLUT can be download and installed  on most of platforms such as Linux, Windows, and iOS. I  tried my example on both Ubuntu Linux (10.04 LTS) and Oracle Enterprise Linux (5 and 6).

The following is a very simple program to start with.

1. Verify lib

henry@brimley:~/Test/gl$ ls -l /usr/lib/libGL*
lrwxrwxrwx 1 root root     13 2013-11-05 18:55 /usr/lib/libGL.so -> mesa/libGL.so
-rw-r--r-- 1 root root 716562 2012-07-09 19:52 /usr/lib/libGLU.a
lrwxrwxrwx 1 root root     11 2013-11-05 18:55 /usr/lib/libGLU.so -> libGLU.so.1
lrwxrwxrwx 1 root root     20 2013-06-02 21:48 /usr/lib/libGLU.so.1 -> libGLU.so.1.3.070701
-rw-r--r-- 1 root root 456380 2012-07-09 19:52 /usr/lib/libGLU.so.1.3.070701

2. Verify include

henry@brimley:~/Test/gl$ ls -l /usr/include/GL
total 824
-rw-r--r-- 1 root root   7830 2010-01-18 13:31 freeglut_ext.h
-rw-r--r-- 1 root root    681 2010-01-18 13:31 freeglut.h
-rw-r--r-- 1 root root  26152 2010-01-18 13:31 freeglut_std.h
-rw-r--r-- 1 root root 504124 2012-07-09 19:50 glext.h
-rw-r--r-- 1 root root  84058 2012-07-09 19:50 gl.h
-rw-r--r-- 1 root root 110027 2012-07-09 19:50 gl_mangle.h
-rw-r--r-- 1 root root  17251 2012-07-09 19:50 glu.h
-rw-r--r-- 1 root root   3315 2012-07-09 19:50 glu_mangle.h
-rw-r--r-- 1 root root    639 2010-01-18 13:31 glut.h
-rw-r--r-- 1 root root  39904 2012-07-09 19:50 glxext.h
-rw-r--r-- 1 root root  17155 2012-07-09 19:50 glx.h
-rw-r--r-- 1 root root   3463 2012-07-09 19:50 glx_mangle.h
drwxr-xr-x 2 root root   4096 2013-11-05 18:55 internal

3. RedSquare.c

/*
* RedSquare.c
*
* 	This program draws a red rectangle on a blue background.
*
*/

#include "GL/glut.h"

void display(void)
{
	/* clear window */
	glClear(GL_COLOR_BUFFER_BIT);

	/* draw unit square polygon */
	glBegin(GL_POLYGON);
	glVertex2f(-0.5, -0.5);
	glVertex2f(-0.5, 0.5);
	glVertex2f(0.5, 0.5);
	glVertex2f(0.5, -0.5);
	glEnd();

	/* flush GL buffers */
	glFlush();
}

void init()
{
	/* set clear color to blue */
	glClearColor (0.0, 0.0, 1.0, 0.0);

	/* set fill color to red */
	glColor3f(1.0, 0.0, 0.0);

	/* set up standard orthogonal view with clipping */
	/* box as cube of side 2 centered at origin */
	/* This is default view and these statement could be removed */
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
	/* Initialize mode and open a window in upper left corner of screen */
	glutInit(&argc,argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(0,0);
	glutCreateWindow("A simple GLUT program: Red Square");
	glutDisplayFunc(display);
	init();
	glutMainLoop();
}

4. Compile & execute

henry@brimley:~/Test/gl$ gcc RedSquare.c -lglut -lGL -lGLU
henry@brimley:~/Test/gl$ ./a.out &

5. Result
RedSquare

About henry416
I am a computer technology explorer and an university student based on Toronto. If you have any question, please feel free to discuss and comment here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s