S P E C I A L F E AT U R E S I N O P E N G L
S P E C I A L F E AT U R E S I N O P E N G L
Abteilung Informatik und AngewandteKognitionswissenschaft Lehrstuhl Computergraphik u.
Wissenschaftliches RechnenFaculty of Engineering
University of Duisburg-Essen
April 24, 2009, 2009 - Juni 18, 2009
Levon Altunyan: Special features in OpenGL, Homework, April 24,2009, 2009 - Juni 18, 2009
supervisors:Prof. Dr. Wolfram Luther
time frame:April 24, 2009, 2009 - Juni 18, 2009
C O N T E N T S
i introduction 11 introduction 2
ii special features in opengl 32 improving performance 4
2.1 Display List 52.2 Vertex Arrays 62.3 Marbles Example 8
3 alpha blending and antialiasing 133.1 Changing the Blending Equation 143.2 Disk Blender 15
4 Fog 174.1 Fog Equations 184.2 Fog Coordinates 18
5 Selection and Feedback 205.1 Selection 205.2 The Basic Steps 235.3 Feedback 235.4 The Feedback Buffer 245.5 Feedback Data 24
6 Fragment Operations 266.1 Multisample Operations 266.2 Alpha Test 276.3 Stencil Test 276.4 Dissolve Effect with Stencil Buffer 28
7 summary 31
L I S T O F F I G U R E S
Figure 1 Arrays for position, color, and texture coordi-nate attributes 6
Figure 2 Triangle Strip 7Figure 3 Immediate Mode  9Figure 4 Vertex Array On  10Figure 5 Display Lists On  11Figure 6 Disk Blender - Example 1 16Figure 7 Disk Blender - Example 2 16Figure 8 Fog Example  19Figure 9 Selection Hierchy  22Figure 10 Selection Example  24Figure 11 Using Stencil to Dissolve Between Images  29
L I S T O F TA B L E S
Table 1 Valid Vertex Array Sizes and Data Types 8Table 2 OpenGL Blending Factors 14Table 3 Available Blend Equation Modes 15Table 4 Three OpenGL Supported Fog Equations 18Table 5 Feedback Buffer Types 25Table 6 Feedback Buffer Tokens 25Table 7 Fragment Test 27Table 8 Stencil Update Values 28
A C R O N Y M S
API Application Programming Interface
I N T R O D U C T I O N
1I N T R O D U C T I O N
OpenGL (Open Graphics Library) is a standard specification defininga cross-language, cross-platform API for writing applications thatproduce 2D and 3D computer graphics. The interface consists of over250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL was developedby Silicon Graphics Inc. (SGI) and is widely used in CAD, virtualreality, scientific visualization, information visualization, and flightsimulation. It is also used in video games, where it competes withDirect3D on Microsoft Windows platforms.The features which OpenGL provides can be divided to two differentbig groups - basic and special ones. This work is dealing with someof the functions which fall into the group of advanced methods. Sincethe list of topics which are under the special features category is vast,the number of discussed topics is restricted to the most importantones suggested in literature .
S P E C I A L F E AT U R E S I N O P E N G L
2I M P R O V I N G P E R F O R M A N C E
Fundamental progresshas to do
with the reinterpretation of basic ideas.
Alfred North Whitehead
In this section two techniques (display lists and vertex arrays code)for optimization of the OpenGL code will be described. Furthermore,the information and procedures needed to render geometry struc-tures will be explained. Moreover, an example which uses the listedmethods will be given.
In many graphics applications, and in virtually all games, main-taining an interactive frame rate and smooth animation is of utmostimportance. Although rapid advancements in graphics hardwarehave lessened the need to optimize every single line of code, pro-grammers still need to focus on writing efficient code that, throughthe graphics Application Programming Interface (API), harnesses thefull power of the underlying hardware. The work done every timeyou call an OpenGL command is not inconsequential. Commandsare compiled, or converted, from OpenGLs high-level commandlanguage into low-level hardware commands understood by thehardware. For complex geometry, or just large amounts of vertexdata, this process is performed many thousands of times, just todraw a single image on screen. This is, of course, the aforementionedproblem with immediate mode rendering. How does our new knowl-edge of the command buffer help with this situation? Often, thegeometry or other OpenGL data remains the same from frame toframe. For example, a spinning torus is always composed of thesame set of triangle strips, with the same vertex data, recalculatedwith expensive trigonometric functions every frame. The only thingchanging frame to frame is the model view matrix. A solution to thisneedlessly repeated overhead is to save a chunk of precomputed datafrom the command buffer that performs some repetitive renderingtask, such as drawing the torus. This chunk of data can later becopied back to the command buffer all at once, saving the manyfunction calls and compilation work done to create the data. OpenGLprovides a facility to create a preprocessed set of OpenGL commands(the chunk of data) that can then be quickly copied to the commandbuffer for more rapid execution.
2.1 display list 5
2.1 display list
This precompiled list of commands is called a display list, and cre-ating one or more of them is an easy and straightforward process,which has the following properties:
Used in immediate mode to organize data and improve perfor-mance
Maximize performance by knowing when and how to usedisplay lists
Reduce cost of repeatedly transmitting data over a network ifrepeatedly used commands are stored in a display list.
Some graphics hardware may store display lists in dedicated memoryor may store the data in an optimized form that is more compatiblewith the graphics hardware or software. The advantages of displaylist can be divided to the following groups suggested by :
Matrix operations. Most matrix operations require OpenGL tocompute inverses. Both the computed matrix and its inversemight be stored by a particular OpenGL implementation in adisplay list.
Raster bitmaps and images. The format in which the raster datais specified is not likely to be the ideal one for the hardware.When a display list is compiled, OpenGL might transform thedata into the representation preferred by the hardware. Thiscan have a significant effect on the speed of raster characterdrawing, since character strings usually consist of a series ofsmall bitmaps.
Lights, material properties, and lighting models. When a sceneis drawn with complex lighting conditions one might changethe materials for each item in the scene. Setting the materialscan be slow since it might involve significant calculations. If weput the material definitions in display lists, these calculationsdont have to be done each time we switch materials, since onlythe results of the calculations need to be stored; as a result,rendering light scene might be faster.
Polygon stipples patterns.
Furthermore, the user should also consider the following disadvan-tages:
2.2 vertex arrays 6
Very small lists may not perform well since there is someoverhead when executing a list
Immutability of the contents of a display list.
Display list cannot be changed and their contents cannotbe read.
If data needs to be maintained separately from the displaylist (e.g. continued data processing) then a lot of additionalmemory may be required.
2.2 vertex arrays
Figure 1: Arrays for position, color, and texture coordinate attributes
The second method to optimize an OpenGL code is the use ofvertex arrays. OpenGL requires many function calls to render geo-metric primitives. OpenGL has vertex array routines that allow usto specify a lot of vertex related data with just a few arrays and toaccess that data with equally few function calls. Assuming the caseof a mesh which represents a triangle strip illustrated in Figure 2,each of the circled vertices is a redundant vertex. In other words,each of these vertices is shared by more than three triangles, butsince a triangle strip can represent, at most, three triangles per vertex,each of the circled vertices needs to be sent to the video card morethan once. This results in using additional bandwidth to send the
2.2 vertex arrays 7
Figure 2: Triangle Strip
data to the video card. In addition, the vertex is likely to transformedand lit more than once. These two operations waste bandwidth andprocessing cycles. To address these issues, OpenGL include vertexarrays. Vertex arrays offer the following advantages:
Large batches of data can be sent with a small number offunction calls.
Through the use of indexed vertex arrays, vertices can be sendexactly once per triangle mesh, reducing bandwidth and po-tentially avoiding redundant transformation and lighting.
Using vertex array routines, all 20 vertices in a 20-sided polygon canbe put into one array and called with one function. If each vertexalso has a surface normal, all 20 surface normals can be put intoanother array and also called with one function. Arranging datain vertex arrays may increase the performance of the application.Using vertex arrays reduces the number of function calls, whichimproves performance. Also, using vertex arrays may allow reuseof already processed shared vertices. There are three steps to usingvertex arrays to render geometry:
1. Activate (enable) up to eight arrays, each storing a differenttype of data: vertex co