Learning to program OpenGL is not difficult. Its great simplicity of use made ​​it the most widely used graphics library, relying on implementations on Windows, Linux, Mac and some handheld devices. The purpose of this special is to provide an introduction to the fundamental characteristics of the library.


Introduction

With the technological progress for the game development are used more and more powerful 3D accelerators graphic cards.But how to pick up all this power and use it for our purposes?

Many libraries allow you to use the features offered by the hardware of our computers among which the are names such as Direct3D and OpenGL.

The idea behind this special edition of DEV ​is to provide a full and rich presentation, with many examples, about the basic features of OpenGL, demonstrating its true power and ease of use.

A bit of history

Originally was called IRIS GL and was only a programming library of 2D graphics. Later extended to the new world of 3D, was used in the company’s proprietary machines that possessed various types of dedicated hardware, for the ultra-fast calculations execution of matrix transformations or display management.

With the technological advancement that has occurred in years Silicon Graphics has felt the need to facilitate the use of IRIS GL on other machines siplifying the portability of the library. The first version of OpenGL was born in July 1992. The term “open” represented the intention of creating an open standard that could promote the diffusion of the library to other hardware and operating systems.

When the hardware performance was a luxury that only few companies could afford, OpenGL was used mainly for CAD, but after the costs of 3D accelerator cards decreased considerably they became available to everyone an so its use was extended to many applications, especially to the world of video games.

By the time the developers were increasingly attracted to OpenGL because of the ease of use and the ease with which them could port the code on other platforms and right now there are lots of products on the market that make use of OpenGL aside from much documentation and examples. Most modern video cards implements both OpenGL and Direct3D and this fact is representing the developers are continuing to choose this library for their new products.

What is OpenGL

OpenGL is defined as “a software interface to graphics hardware” and is a portable library, fast and powerful enough to touch the quality of a ray-trace program. We should not think of it as a programming language or a precompiled library, rather a series of APIs (Application Programming Interface) comparable to the calls to Windows system functions and as such follows the conventions of the C. And is possible to use them with all existing languages ​​such as Visual Basic, Delphi, C#, but only with the aid of a special interface that deals with C calls.

It can be used on systems that have access to dedicated hardware acceleration of 3D graphics operations and is used in almost every software field that uses the graphics functions: CAD, scientific applications, modern video games and so on.

How it works

The OpenGL API is characterized by a system of procedural calls rather than descriptive one as in other areas. I mean that the programmer rather than to describe the appearance of a scene he has to indicate the steps that must be performed to obtain the desired result. Each of these steps is nothing but a call to functions that OpenGL provides and through those you can draw objects, usually called primitives which make up a scene, apply to them light effects and others that we will see in the next section.

However it’s good to remember that OpenGL was designed exclusively for programming graphics and does not have features that enable you to manage the operating system’s GUI, I/O devices such as joystick or audio playing. The problem can be circumvented by supplementing into applications other dedicated libraries including DirectX.

We now ask ourselves how is possible to managed the “steps” to which a reference was made earlier. Comes into play thr famous pipeline a mechanism that process, at successive stages, all the commands given to the API. Figure 1 shows a simplified diagram of the modules of the pipeline.

Figure 1 – Simplified pipeline schema

It’s possible to note how the data relating to the vertices, the transformations, lighting and so on are originally stored within a buffer until it is launched a special command to emptying (flush). When this happens all set operations are performed in a single shot and the final image is produced. Such an architectural choice was adopted to optimize the processes of communication with the hardware.

In every stage of the pipeline run complex mathematical calculations designed to transform raw data entered by the programmer. This should not be cause for concern by those who “fear” of having to brush up on the mathematics texts. To program the OpenGL is not necessary to penetrate through the maze of computer graphics and you will see how, unlike what happens with Direct3D, it will take a few lines of code to achieve amazing results.

An overview of the effects

Let give us, therefore, an idea of the essential features of OpenGL, that functionality – or effects – required for the construction of our scenes from more schematic to the most realistic ones.

The first term to know is rendering that indicates the action to transform the geometric description of a three-dimensional object in a 2D image. The term three-dimensional or 3D indicates that a object is represented by in terms of of height, width and depth values. Unfortunately the existing display can only display images in two dimensions, height and width. How, then, is possible to achieve the illusion of depth? Simply acting as painters did for centuries, who despite having available only a canvas or a sheet of paper they painted scenes with a high level of realism by adding perspective. Looking at the image in Figure 2, we realize now how it is sufficient to give the illusion of third dimension.

Figure 2 – How a solid appears when drawn in wireframe mode

Unfortunately the object so represented may cause problems of perception, can you tell if the front face of the cube is oriented up or down? To avoid this effect you must make sure to delete the lines that are not necessary. In Figure 3 is shown the same object treated by the effect of removing the hidden faces or hidden surface removal. Notice how the cube looks solid now.

Figure 3 – Using polygons instead of lines it is possible to eliminate segments that should be hidden

We want to go beyond the simple line drawings. By adding color, an adequate source light and shadows the object in Figure 4 earns a very high level of realism.

Figure 4 – Here a color and a light source was applied

Finally, instead of coloring the faces with a plain color, OpenGL allows us to apply, for each, an image or texture. We can, thus, make our object appears to be constituted by a material. Figure 5 shows a collage of images where there have been used different textures.

Figure 5 – Using images instead of solid colors, a perspective transform, the result reaches a good level of realism.

Other effects are available Fogging, Blending, and Antialiasing. The Fog effect is the imitation of the atmospheric phenomenon which tends to change the color of objects according to their distance from the point of observation. Blending means mixing or layering multiple images. For example, applying this effect to the objects of a scene can make it appear transparent.

The images that appear on the screen are formed by discrete points and for this reason, when drawing geometric figures by the lines they will appear jagged. The Antialiasing is an effect that fades slightly the contours of the lines giving them a more delicate appearance.

Principles of 3D programming

In order to describe objects in three dimensions is necessary to have a system of reference for specifying the location, measurements, etc. To draw a point on the screen, for example, you must specify the position in terms of row and column; this information are called coordinates.

Looking back at the computer monitor with a resolution of 800×600 its center point is represented by the pair of coordinates (400.300), ie 400 pixels right and 300 pixels down from the upper left corner of the screen. After this example it is necessary to introduce a new concept, the system of coordinates.

A coordinate system is a reference method which allows to uniquely identify a given point in a plane or in space. The most used and known is the Cartesian coordinate system that found the references on two perpendicular lines called X-axis and Y-axis and where each measurement is made via the determination of the distance from these to a point under examination. The distance from the X-axis takes the name of x-coordinate, while y-coordinate is the respective from th Y-axis. The coordinate values ​​vary from negative to positive infinity as shown in Figure 6.

Figure 6 – An example of a cartesian coordinate system

When using the OpenGL drawing functions you must specify the coordinate system and how to project it on the physical device. The windows are physically measured in terms of pixels, for this reason OpenGL, in order to operate properly needs to know how to translate the pairs of Cartesian coordinates in the corresponding coordinate system of the screen. It possible to achieve this by specifying the area occupied by the window on the Cartesian plane; this area takes the name of clipping-area. A clipping-area represents, ultimately, the range of x and y coordinates that are visible in the window.

It is not necessary, however, that the dimensions of clipping-area are equal to window’s one. If we have specified one that goes from 0 to 100 points for both coordinates on a window of 200 x 200 pixels, the output will be resized and scaled appropriately to fill the window. In this way, each increase of 1 unit in the Cartesian plane will correspond to 2 pixels in the window.

Often, however, one needs to not display the entire output window, and thus enters into the game the viewport. Appropriately sizing the viewport is possible to reduce the size of the output or display only a particular of it zooming past the size of the window. Figure 7 carries an example of the last concepts exposed.

Figure 7 – A clipping area represents the ranges of coordinates that are visible.

We extend now the concept of the 3D world coordinate system by adding a third axis called Z. For this system apply the same principles just now exposed except for the fact that the coordinates are expressed by the triad (x, y, z).

Where to find material on the Web

There is plenty of material on the Internet. Many sites have sections full of tutorials, downloads, source files, links and more so it’s worth taking a look around for details.

However, both experts and beginners can refer in particular to the following sites:

www.opengl.org : the official OpenGL site contains virtually all the news of this world as well as various documents, applications, games and forums.

nehe.gamedev.net : an excellent site that has, among other things, a series of tutorials covering all aspects of OpenGL programming, and some templates for use in various programming languages ​​like C #, Delphi.

www.gamedev.net : dedicated to video game programming in general.

Conclusions

In this first article, very theoretical, we have presented in a concise manner everything is needed to know to start our adventure in the wonderful world of 3D real time graphics.

We will continue in the next article, with the introduction of the GLUT library which allows you to create with few instructions complete and portable applications across all platforms Windows, Linux and Mac OS.

On the FTP site you will also find numerous examples including programs that have generated some of the pictures presented here.

Bibliography

  1. Richard S.Wright jr.Lipchak and Benjamin, “OpenGL SuperBible – Third Edition”, SAMS, 2005, ISBN 0-672-32601-9

Original article

Downloads