FREE ELECTRONIC LIBRARY - Abstracts, online materials

Pages:     | 1 || 3 | 4 |   ...   | 5 |

«*DPH 'HYHORSHUV &RQIHUHQFH $GYDQFHG 2SHQ*/ *DPH 'HYHORSPHQW Avoiding 19 Common OpenGL Pitfalls Mark J. Kilgard * NVIDIA Corporation Last updated: ...»

-- [ Page 2 ] --

In practice, because a given matrix type does tend to be updated repeatedly before switching to a different matrix, the lower overhead for matrix manipulation more than makes up for the programmer’s burden of ensuring the matrix mode is properly set before matrix manipulation.

A simple program-wide policy for OpenGL matrix manipulation helps avoid pitfalls when manipulating matrices. Such a policy would require any code manipulating a matrix to first call glMatrixMode to always update the intended matrix. However in most programs, the modelview matrix is manipulated quite frequently during rendering and the other matrices change considerably less frequently overall. If this is the case, a better policy is that routines can assume the matrix mode is set to update the modelview matrix. Routines that need to update a different matrix are responsible to switch back to the modelview matrix after manipulating one of the other matrices.

Here is an example of how OpenGL’s matrix mode can get you into trouble. Consider a program written to keep a constant aspect ratio for an OpenGL-rendered scene in a window. Maintaining the aspect ratio requires updating the projection matrix whenever the window is resized. OpenGL programs typically also adjust the OpenGL viewport in response to a window resize so the code

to handle a window resize notification might look like this:

–  –  –

If this code fragment is from a typical OpenGL program, doResize is one of the few times or even only time the projection matrix gets changed after initialization. This means that it makes sense to add to a final glMatrixMode(GL_MODELVIEW)call to doResize to switch back to the modelview matrix. This allows the window’s redraw code safely assume the current matrix mode is set to update the modelview matrix and eliminate a call to glMatrixMode. Since window redraws often repeatedly update the modelview matrix, and redraws occur considerably more frequently than window resizes, this is generally a good approach.

A tempting approach might be to call glGetIntegerv to retrieve the current matrix mode state and then only change the matrix mode when it was not what you need it to be. After performing its matrix manipulations, you could even restore the original matrix mode state.

This is however almost certainly a bad approach. OpenGL is designed for fast rendering and setting state; retrieving OpenGL state is often considerably slower than simply setting the state the way you require. As a rule, glGetIntegerv and related state retrieval routines should only be used for debugging or retrieving OpenGL implementation limits. They should never be used in performance critical code. On faster OpenGL implementations where much of OpenGL’s state is maintained within the graphics hardware, the relative cost of state retrieval commands is considerably higher than in largely software-based OpenGL implementations. This is because state retrieval calls must stall the graphics hardware to return the requested state. When users run OpenGL programs on high-performance expensive graphics hardware and do not see the performance gains they expect, in many cases the reason is invocations of state retrieval commands that end up stalling the hardware to retrieve OpenGL state.

*DPH 'HYHORSHUV &RQIHUHQFH $GYDQFHG 2SHQ*/ *DPH 'HYHORSPHQW In cases where you do need to make sure that you restore the previous matrix mode after changing it, try using glPushAttrib with the GL_TRANSFORM_BIT bit set and then use glPopAttrib to restore the matrix mode as needed. Pushing and popping attributes on the attribute stack can be more efficient than reading back the state and later restoring it. This is because manipulating the attribute stack can completely avoid stalling the hardware if the attribute stack exists within the hardware. Still the attribute stack is not particularly efficient since all the OpenGL transform state (including clipping planes and the normalize flag) must also be pushed and popped.

The advice in this section is focused on the matrix mode state, but pitfalls that relate to state changing and restoring are common in OpenGL. OpenGL’s explicit state model is extremely well suited to the stateful nature of graphics hardware, but can be an unwelcome burden for programmers not used to managing graphics state. With a little experience though, managing OpenGL state becomes second nature and helps ensure good hardware utilization.

The chief advantage of OpenGL’s stateful approach is that well-written OpenGL rendering code can minimize state changes so that OpenGL can maximize rendering performance. A graphicsinterface that tries to hide the inherently stateful nature of well-designed graphics hardware ends up either forcing redundant state changes or adds extra overhead by trying to eliminate such redundant state changes. Both approaches give up performance for convenience. A smarter approach is relying on the application or a high-level graphics library to manage graphics state.

Such a high-level approach is typically more efficient in its utilization of fast graphics hardware when compared to attempts to manage graphics state in a low-level library without high-level knowledge of how the operations are being used.

If you want more convenient state management, consider using a high-level graphics library such as Open Inventor or IRIS Performer that provide both a convenient programming model and efficient high-level management of OpenGL state changes.

4. Overflowing the Projection Matrix Stack OpenGL’s glPushMatrix and glPopMatrix commands make it very easy to perform a set of cumulative matrix operations, do rendering, and then restore the matrix state to that before the matrix operations and rendering. This is very handy when doing hierarchical modeling during rendering operations.

For efficiency reasons and to permit the matrix stacks to exist within dedicated graphics hardware, the size of OpenGL’s various matrix stacks are limited. OpenGL mandates that all implementations must provide at least a 32-entry modelview matrix stack, a 2-entry projection matrix stack, and a 2-entry texture matrix stack. Implementations are free to provide larger stacks, and glGetIntergerv provides a means to query an implementation’s actual maximum depth.

Calling glPushMatrix when the current matrix mode stack is already at its maximum depth generates a GL_STACK_UNDERFLOW error and the responsible glPushMatrix is ignored.

OpenGL applications guaranteed to run correctly on all OpenGL implementations should respect the minimum stack limits cited above (or better yet, query the implementation’s true stack limit and respect that).

This can become a pitfall when software-based OpenGL implementations implement stack depth limits that exceed the minimum limits. Because these stacks are maintained in general purpose memory and not within dedicated graphics hardware, there is no substantial expense to permitting larger or even unlimited matrix stacks as there is when the matrix stacks are implemented in dedicated hardware. If you write your OpenGL program and test it against such implementations with large or unlimited stack sizes, you may not notice that you exceeded a

–  –  –

matrix stack limit that would exist on an OpenGL implementation that only implemented OpenGL’s mandated minimum stack limits.

The 32 required modelview stack entries will not be exceeded by most applications (it can still be done so be careful). However, programmers should be on guard not to exceed the projection and texture matrix stack limits since these stacks may have as few as 2 entries. In general, situations where you actually need a projection or texture matrix that exceed two entries are quite rare and generally avoidable.

Consider this example where an application uses two projection matrix stack entries for updating

a window:

–  –  –

The window renders a 3D scene with a 3D perspective projection matrix (initialization not shown), then switches to a simple 2D orthographic projection matrix to draw a 2D overlay.

Be careful because if the render2Doverlay tries to push the projection matrix again, the projection matrix stack will overflow on some machines. While using a matrix push, cumulative matrix operations, and a matrix pop is a natural means to accomplish hierarchical modeling, the projection and texture matrices rarely require this capability. In general, changes to the projection matrix are to switch to an entirely different view (not to make a cumulative matrix change to later be undone). A simple matrix switch (reload) does not need a push and pop stack operation.

If you find yourself attempting to push the projection or texture matrices beyond two entries, consider if there is a simpler way to accomplish your manipulations that will not overflow these stacks. If not, you are introducing a latent interoperability problem when you program is run on high-performance hardware-intensive OpenGL implementations that implement limited projection and texture matrix stacks.

5. Not Setting All Mipmap Levels

When you desire high-quality texture mapping, you will typically specify a mipmapped texture filter. Mipmapping lets you specify multiple levels of detail for a texture image. Each level of detail is half the size of the previous level of detail in each dimension. So if your initial texture image is an image of size 32x32, the lower levels of detail will be of size 16x16, 8x8, 4x4, 2x2, and 1x1. Typically, you use the gluBuild2DMipmaps routine to automatically construct the lower levels of details from you original image. This routine re-samples the original image at each level of detail so that the image is available at each of the various smaller sizes.

*DPH 'HYHORSHUV &RQIHUHQFH $GYDQFHG 2SHQ*/ *DPH 'HYHORSPHQW Mipmap texture filtering means that instead of applying texels from a single high-resolution texture image, OpenGL automatically selects from the best pre-filtered level of detail.

Mipmapping avoids distracting visual artifacts that occur when a distant textured object undersamples its associated texture image. With a mipmapped minimization filter enabled, instead of under-sampling a single high resolution texture image, OpenGL will automatically select the most appropriate levels of detail.

One pitfall to be aware of is that if you do not specify every necessary level of detail, OpenGL will silently act as if texturing is not enabled. The OpenGL specification is very clear about this: “If texturing is enabled (and TEXTURE_MIN_FILTER is one that requires a mipmap) at the time a primitive is rasterized and if the set of arrays 0 through n is incomplete, based on the dimensions of array 0, then it is as if texture mapping were disabled.” The pitfall typically catches you when you switch from using a non-mipmapped texture filter (like GL_LINEAR) to a mipmapped filter, but you forget to build complete mipmap levels. For example,

say you enabled non-mipmapped texture mapping like this:



glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);

At this point, you could render non-mipmapped textured primitives. Where you could get tripped

up is if you naively simply enabled a mipmapped minification filter. For example:


The problem is that you have changed the minification filter, but not supplied a complete set of mipmap levels. Not only do you not get the filtering mode you requested, but also subsequent rendering happens as if texture mapping were not even enabled.

The simple way to avoid this pitfall is to use gluBuild2DMipmaps (or gluBuild1DMipmaps for 1D texture mapping) whenever you are planning to use a mipmapped minification filter. So this




gluBuild2DMipmaps(GL_TEXTURE_2D, depth, width, height, GL_RGB, GL_UNSIGNED_BYTE, imageData);

The above code uses a mipmap filter and uses gluBuild2DMipmaps to make sure all the levels are populated correctly. Subsequent rendering is not just textured, but properly uses mipmapped filtering.

Also, understand that OpenGL considers the mipmap levels incomplete not simply because you have not specified all the mipmap levels, but also if the various mipmap levels are inconsistent.

This means that you must consistently specify border pixels and each successive level must be half the size of the previous level in each dimension.


6. Reading Back Luminance Pixels You can use OpenGL’s glReadPixels command to read back rectangular regions of a window into your program’s memory space. While reading back a color buffer as RGB or RGBA values is straightforward, OpenGL also lets you read back luminance values, but it can a bit tricky to get what you probably expect. Retrieving luminance values is useful if you want to generate a grayscale image.

When you read back luminance values, the conversion to luminance is done as a simple addition of the distinct red, green, and blue components with result clamped between 0.0 and 1.0. There is a subtle catch to this. Say the pixel you are reading back is 0.5 red, 0.5 green, and 0.5 blue.

You would expect the result to then be a medium gray value. However, just adding these components would give 1.5 that would be clamped to 1.0. Instead of being a luminance value of 0.5, as you would expect, you get pure white.

A naive reading of luminance values results in a substantially brighter image than you would expect with a high likelihood of many pixels being saturated white.

The right solution would be to scale each red, green, and blue component appropriately.

Fortunately, OpenGL’s pixel transfer operations allow you to accomplish this with a great deal of flexibility. OpenGL lets you scale and bias each component separately when you send pixel data through OpenGL.

For example, if you wanted each color component to be evenly averaged during pixel read back,

you would change OpenGL’s default pixel transfer state like this:



Pages:     | 1 || 3 | 4 |   ...   | 5 |

Similar works:

«Catalogue No. 11F0019MIE — No. 190 ISSN: 1205-9153 ISBN: 0-662-32223-1 Research Paper Financing Innovation in New Small Firms: New Evidence From Canada by John R. Baldwin, Guy Gellatly and Valérie Gaudreault Micro-Economic Analysis Division 24-B R.H. Coats Building, Ottawa K1A 0T6 Telephone: 1 613 951-3962 Fax: 1 613 951-5403 This paper represents the views of the authors and does not necessarily reflect the opinions of Statistics Canada.Financing Innovation in New Small Firms: New Evidence...»

«Vol 6 The Western Australian Jurist 241 SPHERES OF POWER: THE HIGH COURT AS CUSTODIAN OF CO-ORDINATE FEDERALISM MICHAEL OLDS* I INTRODUCTION Federalism is known as the ‘great theme’ of the Australian Constitution.1 The framers of the Constitution intended Australia to be a true federation. Indeed, federalism was of ‘utmost concern to the framers, evidenced by both the constitutional conventions of the 1890s and the text and structure of the Constitution they drafted.’ 2 Included in this...»

«Real Options, Volatility, and Stock Returns GUSTAVO GRULLON, EVGENY LYANDRES, and ALEXEI ZHDANOV∗ ABSTRACT We provide evidence that the positive relation between firm-level stock returns and firm-level return volatility is due to firms’ real options. Consistent with real option theory, we find that the positive volatilityreturn relation is much stronger for firms with more real options and that the sensitivity of firm value to changes in volatility declines significantly after firms...»

«A generalized Bachelier formula for pricing basket and spread options Ciprian Necula‡,2,3 Fulvia Fringuellotti1,2 November 16th, 2015 Abstract In this paper we propose a closed-form pricing formula for European basket and spread options. Our approach is based on approximating the risk-neutral probability density function of the terminal value of the basket using a GaussHermite series expansion around the Gaussian density. The new method is quite general as it can be applied for a basket with...»

«RESTRICTED: MANAGEMENT MINUTES OF THE ILF TRUSTEES BOARD MEETING HELD WEDNESDAY 20 November 2013 AT EQUINOX HOUSE, NOTTINGHAM.Present: Trustees Board: Stephen Jack (Chair) Peter Cooke Yogi Amin Mike Boyall Sally Sparrow Sue Winterburn In Attendance: James Sanderson, (Chief Executive) Steve Jarratt (Finance and Resources Director) Roland Knell (Head of Information and Governance) Mary Hipkin (DWP stewardship team) Pam Mellors, (Minutes) Item 1: INTRODUCTION 1. The Chair welcomed everyone to the...»


«THE KANUNGU MASSACRE The Movement for the Restoration of the Ten Commandments of God Indicted The Uganda Human Rights Commission Periodical Report © Uganda Human Rights Commission 2002 First published 2002 This publication may be quoted or reproduced with full acknowledgement of the Uganda Human Rights Commission. CONTENTS 1. Background to the Report 2. Places/Scenes Visited 3. The Cult’s Origins and Characteristics 4. The Cult’s Doctrine 5. The Cult and Violation of Human Rights 6. Signs...»

«BROWN.FINAL 8/4/2010 10:28 AM NON-PECUNIARY INTERESTS AND THE INJUDICIOUS LIMITS OF APPELLATE STANDING IN BANKRUPTCY S. Todd Brown* I. INTRODUCTION II. BANKRUPTCY STANDING UNDER THE CODE A. The Form and Function of Standing Principles and the Separation of Powers 1. Article III Standing and the Judicial Role.573 2. Statutory Standing and Legislative Design.574 3. Judicial Self-Governance and Prudential Standing.576 B. Bankruptcy Standing Today 1. Standing in Bankruptcy Court a. Party in...»

«SPARK—A state-of-the-practice approach to the Common Criteria implementation requirements Roderick Chapman Praxis Critical Systems Limited, 20 Manvers Street, Bath BA1 1PX, U.K. +44 1225 466991 rod@praxis-cs.co.uk 1. ABSTRACT The Common Criteria (CC) require the use of programming languages whose statements have an unambiguous meaning. This presentation considers SPARK[1]: a widely-used language that is perhaps unique in actually meeting this requirement. While SPARK has its roots in security...»

«PROVINCIAL COURT OF NOVA SCOTIA Citation: R v. Jesso, 2016 NSPC 61 Date: 2016-10-31 Docket: 2966612, 2966613 Registry: Bridgewater Between: R. v. MANUEL JOSEPH JESSO The Honourable Judge Paul B. Scovil, J.P.C.Judge: October 31, 2016, in Bridgewater, Nova Scotia Heard: 253(1)(a) CC, 253(1)(b) CC Charge: Michelle MacDonald, for the Crown Counsel: Chris Manning, for the Defence Page 2 By the Court: [1] Section 255(5) of the Criminal Code provides that if the Court considers a person is in need of...»

«“The Contrails Streaked Cleanly in the High Sky” An Ecocritical Reading of Don DeLillo’s White Noise and Underworld By Thomas Gårder Brendefur Master’s Thesis University of Bergen Department of Foreign Languages November 2012         Samandrag Masteroppgåva tek for seg romanane White Noise (1985) og Underworld (1997) av den amerikanske forfattaren Don DeLillo (1936-), og gjev ein kort diskusjon av den siste romanen hans Point Omega (2010) i konklusjonskappittelet. Romanane er...»

«Speech Sound Disorders Table of Contents Introduction: Speech Sound Disorders..2 Articulation Disorders...3 Phonological Disorders...3 Accented Speech...4 Treatment of Speech Sound Disorders..4 I. Speech Referral Guidelines..5 I.a. Common Etiologies...5 I.b. Potential Consequences and Impact of Speech Impairment.5 I.c. Major Milestones for Speech Development..6 I.d. Behaviors that Should Trigger an SLP Referral..6 I.e. World Health Organization Model (WHO) International.7 II. Screening...9...»

<<  HOME   |    CONTACTS
2017 www.abstract.dislib.info - Abstracts, online materials

Materials of this site are available for review, all rights belong to their respective owners.
If you do not agree with the fact that your material is placed on this site, please, email us, we will within 1-2 business days delete him.