Introduction to OpenGL by bagavathikumar

OpenGL is the abbreviation of Open Graphics Library. OpenGL is a standard which is cross platform application programming interface (API) for creating the 2D or 3D graphics application. It is a software interface to graphics hardware. OpenGL API is a hardware and system independent interface. OpenGL API was developed by Silicon Graphics Inc. (SGI) in 1992.

Open GL

Faces of OpenGL

OpenGL exists in two forms that are OpenGL Specification and OpenGL Implementation (OpenGL API).

  1. OpenGL Specification – The specification is complete and specific terms, and the API also well defined. Hardware vendor take the specification and implement it. Basically OpenGL is a state machine; you can download the state diagram here. The OpenGL 1.1 Specification was developed and maintained by Silicon Graphics Inc. you can see the Specification here.
  2. OpenGL Implementation – The Implementation is in the form of software developers and customers can use to develop the real-time graphics like software driver and a graphics card.

    Note: The Latest version of the OpenGL specification is OpenGL 3.1 Specification revised on 24/Mar/2009. The PDF version of the document is available here.

Getting Started

If you’re going to write an OpenGL program, you need to get the OpenGL library for compile and build the program. You can get this from the OpenGL.org Download Page. Put it somewhere in your system and insist in your compiler to refer the library. The important files and libraries for running the OpenGL application is

  • gl.h
  • glu.h
  • opengl32.lib
  • glu32.lib

For window handling, we can use Win32 or MFC (Microsoft Foundation Classes), GLUT or any other window system.

OpenGL API

The OpenGL is the operating system independent API as well as Window system independent. That is OpenGL don’t have any window handling mechanisms. It Relies on window system for window management, event handling, color map operations, etc. you can use Xlib, Motif, Microsoft Win32, IBM Presentation Manager, etc. for windowing operations.

The WGL API supports the OpenGL in Microsoft Windows Operating System Environment. This is called wiggle functions. There are many wiggle functions and some aren’t even documented in MSDN (Microsoft developer Network). All the WGL APIs start with wgl, for example:

  • wglCreateContext
  • wglDeleteContext
  • wglChoosePixelFormat
  • wglDescribePixelFormat

The main purpose of the OpenGL is to render the two or three dimensional objects into a frame buffer. These objects are described in the computer screen as a pixels or vertices. The Basic operation of the OpenGL API is to accept the geometric primitives such as points, lines and polygons and convert them into pixels. OpenGL makes it easy to build geometric primitives, change the viewing position, apply the color and lighting of geometric primitives, and manipulate pixel and texture map images.

    Note: EGL (Embedded OpenGL) Specification or Khronos Native Platform Graphics Interface is another flavor of OpenGL Specification. It describes the EGL API for use of OpenGL ES (OpenGL Embedded Systems) on mobile and embedded devices.

OpenGL ES is a cross-platform API for full-function 2D and 3D graphics on embedded systems – including consoles, phones, appliances and vehicles. It consists of well-defined subsets of desktop OpenGL, creating a flexible and powerful low-level interface between software and graphics acceleration. OpenGL ES is an open source API developed by Khronos Group. The Khronos Group is a member-funded consortium founded in January 2000. This Group is focused on the creation of royalty-free open standards for parallel computing, graphics and dynamic media on a wide variety of platforms and devices.

    Note: The latest version of OpenGL ES Specification is OpenGL ES 2.0. OpenGL ES 2.0 does not support the fixed function transformation and fragment pipeline of OpenGL ES 1.x.

OpenGL ARB

The OpenGL Architecture Review Board (ARB), was an independent consortium formed in 1992, that governed the future of OpenGL, proposing and approving changes to the specification, new releases, and conformance testing.

    Note: After 2006, the OpenGL ARB is part of the Khronos Group. The OpenGL ARB and the Khronos Group have long collaborated to ensure consistency in the OpenGL, OpenGL ES standards.

OpenGL Utility Library (GLU)

The OpenGL Utility Library is a set of commonly used graphics routine built on the top of the OpenGL. It consists of a number of functions that use the base OpenGL library to provide higher-level drawing routines from the more primitive routines. It is usually distributed with the base OpenGL package (glu.h and glu32.lib). It contains Quadric surface routines (spheres, cones, cylinders, and tessellated disks), polygonal surface (concave polygons, polygons with holes, self-intersecting polygons and etc), and NURBS (Non-uniform rational B-spline).

What is GLUT?

The OpenGL Utility Toolkit (GLUT) is a library used to simplify the process of creating an OpenGL window as well as the process of capturing keyboard and mouse input. A great advantage of the library is that it is cross-platform, allowing programs to be easily ported to other operating systems. The library may also be used free of charge. You can download the GLUT library along with source code here.

GLUT Vs Win32 or MFC

If you don’t know win32 or MFC program, it is recommended that you use GLUT. If you feel that you must use Win32 or MFC, it is advisable to visit this tutorial.

If you want to write a code like platform independent, it is strongly recommended that you use GLUT. It is extremely easy to set up an OpenGL window and more time can be spent concentrating on OpenGL.

Win32 or MFC is a complex process to do the OpenGL programming. But it is much more flexible allowing you to process additional windows messages, create more flexible user interfaces, and modifies window properties and much more. A disadvantage of using this method is that your programs will run in Windows operating system only.

OpenGL Command Syntax

OpenGL API having its own command syntax like Java. OpenGL commands use the prefix gl and initial capital letters for each word making up the command name (for example glColor3dv() or glLoadIdentity()). glColor3dv() is one of the OpenGL function, which is used to specify the color whatever with this color then.

glColor3dv gl OpenGL Command Prefix
Color Property or functionality
3 No of Attribute (2,3 or 4)
d Data Type

b – character

ub – unsigned char

us – unsigned short

ui – unsigned int

f – float

v Vector format (optional)

 

What is Projection?

The 3D real world coordinates are flattened or projected onto a 2D surface is called projection.
The OpenGL supports two type of projection.

  • Orthographic projection
  • Perspective projection

All the object that have the same size and same dimensional, regardless of whether they are far away from you or nearby. This is called Orthographic projection or parallel projection. To setup this type of projection we use the OpenGL provided glOrtho() function.

glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);

Left and right specify the x-coordinate clipping planes, bottom and top specify the y-coordinate clipping planes, and near and far specify the distance to the z-coordinate clipping planes. Together these coordinates provide a box shaped viewing volume. Orthographic projections are commonly used in 2D scenes, so the Utility Library provides an additional routine to set them up for scenes without the z-coordinate.

gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);

Perspective projection adds the effect of the object whether it is nearby you or far from you.
For example if you have looked down a tree from 500 meter height, you will notice that the tree gets smaller and smaller the further up you look. But in the Orthographic projection, the tree would remain the same width as far as you could see.

void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);

Using glFrustum enables you to specify an asymmetrical frustum, which can be very useful in some instances. For a different solution we again turn to the Utility Library:

void gluPerspective(GLdouble fov, GLdouble aspect, GLdouble near, GLdouble far);

fov specifies, in degrees, the angle in the y direction that is visible to the user; aspect is the aspect ratio of the scene, which is divided by the height. This will determine the field of view in the x direction.

Example Program

Download the project source, unzip the files to some folder, build the project and execute it. This example program present here is explained about the deferent type of projection system. Initially this program will display the scene using "Ortho Graphic Projection". If you want to change the projection system, you can change it by doing the below steps:

  • Ortho Graphic Project Press Ctrl + O
  • Frustum (Perspective) Press Ctrl + F
  • Perspective Press Ctrl + P

The main purpose of this example is to display the same scene in deferent projection system. So that, the beginners can easily understands the projection systems.

    Note:
    The Example Program is created using Visual Studio 6 and Visual Studio .Net 2003. It means the program contains workspace file (*.dsw) and project file (*.dsp) for Visual Studio 6 and the solution file (*.sln) and project file (*.vcproj) for Visual Studio .Net 2003. If you are using Visual Studio .Net 2005 or higher version, you have to convert the project using Visual Studio conversion wizard. For converting the project, you just open the visual studio (whatever the version you have, apart from I mentioned earlier) and select the open option in file menu. Browse the solution file (*.sln),
    the conversion wizard will guide you to convert the project.

How to Run the
Program

For running the OpenGL Program in windows Environment, you need a

  • Visual Studio 6.0 (or higher).
  • OpenGL Library.

But In this example program, I used the glutSolidCube API which is belongs to GLUT. For this we need an OpenGL Utility Tool Kit (GLUT). We have gone through about the GLUT and where can we download the GLUT. Anyway you can also download the GLUT here:

http://www.opengl.org/resources/libraries/glut/glut37.zip

And you can also refer the general installation procedure here.

http://www.cs.csustan.edu/~rsc/SDSU/GLUTinstall.html

In Visual Studio .Net 2008,

  • Paste the glut.h into <drive>:<VC++ path>includeGLglut.h. Generally it will be C:Program FilesMicrosoft Visual Studio 9.0VCinclude
  • Paste the glut32.lib into C:Program FilesMicrosoft Visual Studio 9.0VClib
  • Paste the glut32.dll into windows system32 folder.

In Visual Studio .Net 2003 or Visual Studio .Net 2005,

  • Paste the glut.h into C:Program Files<VC++ Version>VCPlatformSDKinclude
  • Paste the glut32.lib into C:Program Files<VC++ Version>VCPlatformSDKlib
  • Paste the glut32.dll into windows system32 folder.

Now you run the program.


Attachments

Project Files OpenGL Sample