User:Doruletz72/OpenGL GLUT SDL/Overview

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Introduction[edit | edit source]

We jump over the classic introductory blah-blah-blah about how it looks like nature around us and how our brain perceives it. We simply say: we have to model a world of three dimensions on a flat screen (only two dimensions). The same problem that painters, sculptors and architects have already solved it thousands of years ago. How do we do this? Simple, we use the fundamental element of descriptive geometry: the projection.

A
Our world, the triangle

But to understand this concept, we need to see how we can represent a world of two dimensions on a two-dimensional screen. How do we represent a two-dimensional world (a world of zero thickness, only length and width, or height and width) in a plane, or in other words in a Cartesian coordinate system with two axes: xOy. Nothing simpler. As you can easily guess, for each point Pi of our two-dimensional world we may assign a point P(xi, yi), in our plane xOy. xi and yi values ​​are projections on the x axis and y axis of Pi position. Or values ​​relative to the origin O(0,0) coordinate system.
Depending on the position of the point from the origin, these values ​​can be negative or positive. Even more, we can decide where it is spatially located (2D space) the origin O(0,0). So a point may have the different coordinates (xi, yi) value based on the spatial position of the origin O.
If in addition, we alter the measuring unit on the two axes, of course we get again different values ​​for xi and yi.

A
Altering our world

Also, we can rotate our Cartesian coordinate system. The new values for xi and yi are different, too.
The main conclusion of this brief presentation is that the first thing we need to do when we want to represent the world around us, is to inform our graphic display system about the coordinate system used, the position of the origin, axes position, the unit of measure used etc.
Then we initialize the coordinates x, y of the points (vertices) that define our triangle with proper values and will specify the order in which they will be joined by lines. Last thing is to fill it with color.
Why I chose a triangle as an example? The triangle is the simplest geometric figure. Any polygon, no matter how complex it is, in plane or space can be decomposed into triangles. Also, any closed curve can be approximated by triangles. For example a circle can be approximated by a regular polygon inscribed in it. The more points are the more accurate approximation.

A
Approximating the figures

In addition, or perhaps precisely because of this, the current graphics adapters can easily generate and process tons of triangles. Even if there are available others polygons to represent images and objects, in fact, the 2D/3D world is the world of triangles.
Quoting Einstein "everything is relative", if we look at the three transformations of the coordinate system, and analyze them from the system point of view, we get the three basic transformations used in the 3D world: translation, scaling, rotation.

  • Translation: if we add/subtract same value to x coordinates for the vertices of the triangle or/and to y coordinates will obtain a translation of the triangle position relative to the origin O(0,0).
  • Scaling: if we multiply/divide with same value the x coordinates for the vertices of the triangle or/and the y coordinates will obtain a scaled version of the our original triangle, smaller or bigger.
  • Rotation: if we alter in a trigonometric manner (using the unit circle) the coordinates for the vertices of the triangle will obtain a rotated version of our original triangle.

Now think what would happen if we combine the all three basic transformations, more or less. Anything can happen or whatever we want if we know how to do it.
Enough with the 2D geometry is time to move on to what hurts us, 3D representation of the world around us. We will introduce a new element called perspective, or the angles between the lines that lend the illusion of depth. And we decreed:

3D = 2D + perspective
A
Orthographic projection

From geometric point of view the perspective corresponds to the z axis. In practice two types of projections are used: the orthogonal/orthographic projection and the perspective projection. The orthographic projection is mainly used in CAD ​​applications and more rarely in some games or multimedia applications.
I think everybody in here, had a problem to "see" the projections in space geometry problems. At least in college.
Be careful, there is a new coordinate: z. Every point/vertex have now three coordinates. In case of orthographic projection, the z axis parallel lines remain parallel to the horizon. The line of view is parallel to the axis z.
The only notable aspect is how we perceive the distance to the origin, O(0,0,0) and the angle of view (z-axis angles with x and y axes).
The perspective projection is mainly used in games and multimedia applications. Z axis parallel lines "meet" each other at the far horizon. How "far" is the horizon? It depends on us, how far we set the eye of the observer position (position of current camera view at a time) from the near horizon. The near horizon usually defines our display screen.

A
Perspective projection

Now the only question which we must be clear on is the following: whether the projection chosen, although the resulting image of reality which we perceive it is only 2D not 3D. Who computes the actual coordinates of the points in the real 2D plan to create the illusion of 3D space? Us? This is madness. No. All the modern graphics cards (the GPUs) will do this for us. Only in the worst case, the CPU.
However, in case of animations, our intervention is required to tell our application what it is has to done.
To increase the feeling of depth, there are additional techniques besides classical adjustment of geometric dimensions, such as: adding shades, shadows and fog.

  • Shades: is similar to the geometric adjustments, but to the brightness of color of the different parts of a object. Image you have a cube with same color sides. Even if we have same color at all, because of different position of the sides from the source of light, there are different intensities of the same color, different shades of same color.
  • Shadows: adding shadows depending on how many light sources are present and their intensity, is one of the most common techniques used to increase the feeling of depth.
  • Fog: the effect of "fog" is often used for long distance views to emphasize the effect of reducing of the clarity caused by the presence of the surrounding atmosphere.

All three techniques refer in fact the second 3D graphics major challenge: lighting technique.
Those presented above are enough to introduce and familiarize our readers with the basics. Now we move to a brief overview of OpenGL. TOP

What is OpenGL[edit | edit source]

A
OpenGL location

OpenGL is defined as a software interface to graphics hardware. It is a 3D graphics and modeling library that is highly portable and very fast. Using OpenGL, you can create 3D graphic application with exceptional visual quality and performance. It is an industry standard, stable, reliable and portable, evolving, scalable, easy to use and well documented.
OpenGL is window and operating system independent. To integrate it into various window systems, additional libraries are used to modify a native window into an OpenGL capable window.
Every window system has its own unique library and functions to do this:

  • GLX for the X Windows system, common on *nix platforms
    AGL for the Apple Macintosh (Carbon)
    WGL for Microsoft Windows (GDI)
    OpenGL ES it is an extension to support embedded system such as Android, iOS etc.

In short terms, OpenGL is an independent standard operating system and window system.
OpenGL is not a "language", but is callable from Ada, C, C++, Fortran, Python, Perl and Java and offers complete independence from network protocols and topologies.

A brief history[edit | edit source]

The ancestor of OpenGL was IRIS GL from Silicon Graphics, a 2D graphics library. It evolved into the 3D programming API for that company’s high-end IRIS graphics workstations. IRIS GL had not been designed from the onset to have a vertex-style geometry processing interface, and it became apparent that to move forward SGI needed to make a clean break. OpenGL is the result of SGI’s efforts to evolve and improve IRIS GL’s portability. The new graphics API would offer the power of GL but would be an “open” standard, with input from other graphics hardware vendors, and would allow for easier adaptability to other hardware platforms and operating systems. OpenGL would be designed from the ground up for 3D geometry processing. TOP

A
OpenGL API Hierarchy

OpenGL ARB[edit | edit source]

An open standard is not really open if only one vendor controls it. SGI’s business at the time was high-end computer graphics. Once you’re at the top, you find that the opportunities for growth are somewhat limited. SGI realized that it would also be good for the company to do something good for the industry to help grow the market for high-end computer graphics hardware. A truly open standard embraced by a number of vendors would make it easier for programmers to create applications and content that is available for a wider variety of platforms. Software is what really sells computers, and if SGI wanted to sell more computers, it needed more software that would run on its computers. Other vendors realized this, too, and the OpenGL Architecture Review Board (ARB) was born. Although SGI originally controlled licensing of the OpenGL API, the founding members of the OpenGL ARB were SGI, Digital Equipment Corporation, IBM, Intel, and Microsoft. On July 1, 1992, version 1.0 of the OpenGL specification was introduced. Over time, the ARB grew to consist of many more members, many from the PC hardware community, and it met four times a year to maintain and enhance the specification and to make plans to promote the OpenGL standard. Over time, SGI’s business fortunes declined for reasons well beyond the scope of this article. In 2006, an essentially bankrupt SGI transferred control of the OpenGL standard from the ARB to a new working group at the Khronos Group (www.khronos.org). The Khronos Group is a member-funded industry consortium focused on the creation and maintenance of open media standards. Most ARB members were already members of Khronos, and the transition was essentially painless. Today, the Khronos Group continues to evolve and promote OpenGL and its sibling API. TOP

API Wars and the guerilla[edit | edit source]

Competition, economic interests, the desire to dominate the market have led to the start of the so-called API Wars. Most companies recognize that competition is good for everyone in the long run and will endorse, support, and even contribute to industry standards. An interesting diversion from this ideal occurred during OpenGL’s youth on the Windows platform. When low-cost 3D graphics accelerators began to become available for the PC, many hardware vendors and game developers were attracted to OpenGL for its ease of use compared to Microsoft’s Direct 3D. Microsoft provided a driver kit that made it very easy to make an OpenGL driver for Windows 98. This kit saved literally years of effort in creating a robust OpenGL driver for Windows NT and Windows 98. Microsoft discouraged vendors from using a more rigorous driver model, and every PC graphics card vendor had created OpenGL drivers ready to ship with Windows 98. This attention to OpenGL by game developers created quite a political stir at the 1997 SigGraph and Game Developers conferences. Just before Windows 98 was released, Microsoft announced that it would not extend the OpenGL driver code license beyond the Windows 98 beta period, and that hardware vendors were forbidden to release their drivers. Virtually every PC hardware vendor had a robust and fast OpenGL driver ready to roll for consumer PCs, but couldn’t ship them. To further complicate things, shortly thereafter a struggling SGI announced a new Windows NT–based workstation. SGI simultaneously pledged to discontinue promoting OpenGL for consumer applications, and to work with Microsoft on a new API called Fahrenheit. OpenGL was as good as dead.
A funny thing happened on the way to oblivion, and even without SGI, OpenGL began to take on a life of its own. Hardware vendors with some help from SGI (pre-Fahrenheit) continued to support OpenGL with new drivers. Games aren’t the only application that OpenGL was well suited for, and most developers wanted their Windows NT software to be able to run on the consumer version of Windows, too. When OpenGL was again widely available on consumer hardware, developers didn’t really need SGI or anyone else touting the virtues of OpenGL. OpenGL was easy to use and had been around for years. This meant there was an abundance of documentation, sample programs, SigGraph papers, and so on. OpenGL began to flourish. As more developers began to use OpenGL, it became clear who was really in charge of the industry: the developers. The more applications that shipped with OpenGL support, the more pressure mounted on hardware vendors to produce better OpenGL hardware and high-quality drivers. Consumers don’t really care about API technology. They just want software that works, and they will buy whatever graphics card runs their favorite game or application the best. Developers care about time to market, portability, and code reuse. Go back in time and try to recompile that old Direct3D programs, if you dare. Using OpenGL enabled many developers to meet customer demand better, and in the end it’s the customers who pay the bills. As time passed, Fahrenheit fell solely into Microsoft’s hands and was eventually discontinued altogether.
Direct3D has evolved further to include more and more OpenGL features, functionality, and ease of use. Today’s Direct3D bears little resemblance to the tortured API it once was. OpenGL’s popularity, however, has continued to grow as an alternative to Windows-specific rendering technology and is now widely supported across all major operating systems and hardware devices. Even embedded devices with 3D graphics technology like cellphones, pods, pads and tablets support a subset of OpenGL, called OpenGL ES. Today, all new 3D accelerated graphics cards for the PC ship with both OpenGL and Direct3D drivers. This is largely due to the fact that many developers continue to prefer OpenGL for new development. OpenGL today is widely recognized and accepted as the industry-standard API for real-time 2D and 3D graphics.
This momentum will carry OpenGL into the foreseeable future as the API of choice for a wide range of applications and hardware platforms. All this also makes OpenGL well positioned to take advantage of future 3D graphics innovations. Because of OpenGL’s extension mechanism, vendors can expose new hardware features without waiting on Microsoft or some industry committee, and cutting-edge developers can exploit them as soon as updated drivers are available. With the addition of the OpenGL shading language, OpenGL has shown its continuing adaptability to meet the challenge of an evolving 3D graphics programming pipeline. Finally, OpenGL is a specification that has shown that it can be applied to a wide variety of programming paradigms. From C/C++ to Java and Visual Basic, even newer languages such as C# are now being used to create PC games and applications using OpenGL. OpenGL is here to stay.
One of the results of the API wars is Mesa 3D. Mesa is not OpenGL, but it is an open-source implementation of the OpenGL specification. The Mesa project was originally started by Brian Paul. More details at www.mesa3d.org. TOP

GLUT support vs SDL platform[edit | edit source]

The OpenGL's predecessor, IRIS GL, had support for building and manipulation of windows, as well as managing input devices (keyboard/mouse/joystick). But to create a set of APIs with no dependencies from the operating and windowing system, this support was removed from the further development of OpenGL. This has allowed the development of OpenGL for a wide range of devices, computers and operating systems. On the other hand, lack of this support has created and creates difficulties even for the most skilled programmers. Learning the X, Microsoft or Carbon windowing system can be a pain in the ass, especially for beginners.

A
Framework for OpenGL

To cover this lack, initially have been developed TK and AUX toolkits. In the end, after corrections and improvements, GLUT (GL Utility Toolkit) support has been released. GLUT documentation available at opengl.org/documentation/specs/glut/spec3/spec3.html.
Another OpenGL major support is represented by SDL, Simple DirectMedia Layer, a more versatile multimedia platform. Sam Lantinga created the library, first releasing it in early 1998. I will not insist here on the history of SDL, but I enumerate a few achievements under this platform: OpenTTD, FreeCiv, The Battle for Wesnoth, Spring. SDL documentation available at www.libsdl.org.
Which one is better? It depends. If you are a beginner or the application you do is simple then it is recommended to use GLUT. If you work at a complex application, such as a game with advanced graphics and sound, then you need to use the SDL platform.
As you can see in the picture, regardless of the framework, an OpenGL application consists of three components:

  • Initialization - creating objects, memory allocation, registering handlers, etc.
    Main program loop - while NOT exit condition occured, get an event from queue (user input, system, program itself etc) , dispatch it to the associated routine/handler, render the scene (based on OpenGL API) and loop again.
    Exit program - stop the execution, clearing the memory, closing the opened files etc.

One of the major differences between GLUT and SDL, beyond those related to the support, is the accessibility to the main program loop. GLUT does not give you a direct access to this. It uses a callback mechanism for a number of usual functions called inside of main loop. TOP