WWW.ABSTRACT.DISLIB.INFO
FREE ELECTRONIC LIBRARY - Abstracts, online materials
 
<< HOME
CONTACTS



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

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

-- [ Page 5 ] --

The resulting diffuse material state is what the programmer intended, but the resulting ambient material state is rather unexpectedly (1.0, 1.0, 1.0, 1.0). How did that happen? Well, remember that the color material mode immediately begins tracking the current color when enabled. The initial value for the color material settings is GL_FRONT_AND_BACK and GL_AMBIENT_AND_DIFFUSE (probably not what you expected!).

Since enabling the color material mode immediately begins tracking the current color, both the ambient and diffuse material states are updated to be (1.0, 1.0, 1.0, 1.0). Note that the effect of the initial glMaterialfv is lost. Next, the color material state is updated to just change the front diffuse material. Lastly, the glColor3f invocation changes the diffuse material to (0.3, 0.5, 0.6, 1.0). The ambient material state ends up being (1.0, 1.0, 1.0, 1.0).

The problem in the code fragment above is that the color material mode is enabled before calling glColorMaterial. The color material mode is very effective for efficient simple material changes, but to avoid the above pitfall, always be careful to set glColorMaterial before you enable GL_COLOR_MATERIAL.

15. Much OpenGL State Affects All Primitives

A fragment is OpenGL’s term for the bundle of state used to update a given pixel on the screen.

When a primitive such as a polygon or image rectangle is rasterized, the result is a set of fragments that are used to update the pixels that the primitive covers. Keep in mind that all OpenGL rendering operations share the same set of per-fragment operations. The same applies to OpenGL’s fog and texturing rasterization state.

For example, if you enabled depth testing and blending when you render polygons in your application, keep in mind that when you overlay some 2D text indicating the application’s status that you probably want to disable depth testing and blending. It is easy to forget that this state also affects images drawn and copied with glDrawPixels and glCopyPixels.

You will quickly notice when this shared state screws up your rendering, but also be aware that sometimes you can leave a mode enabled such as blending without noticing the extra expense involved. If you draw primitives with a constant alpha of 1.0, you may not notice that the blending is occurring and simply slowing you down.

This issue is not unique to the per-fragment and rasterization state. The pixel path state is shared by the draw pixels (glDrawPixels), read pixels (glReadPixels), copy pixels (glCopyPixels), and texture download (glTexImage2D) paths. If you are not careful, it is easy to get into situations where a texture download is screwed up because the pixel path was left configured for a pixel read back.

–  –  –

16. Be Sure to Allocate Ancillary Buffers that You Use If you intend to use an ancillary buffer such as a depth, stencil, or accumulation buffer, be sure that you application actually requests all the ancillary buffers that you intend to use. A common interoperability issue is developing an OpenGL application on a system with only a few frame buffer configurations that provide all the ancillary buffers that you use. For example, your system has no frame buffer configuration that advertises a depth buffer without a stencil buffer. So on your development system, you “get away with” not explicitly requesting a stencil buffer.

The problem comes when you take your supposedly debugged application and run it on a new fancy hardware accelerated OpenGL system only to find out that the application fails miserably when attempting to use the stencil buffer. Consider that the fancy hardware may support extra color resolution if you do not request a stencil buffer. If you application does not explicitly request the stencil buffer that it uses, the fancy hardware accelerated OpenGL implementation determines that the frame buffer configuration with no stencil but extra color resolution is the better choice for your application. If your application would have correctly requested a stencil buffer things would be fine. Make sure that you allocate what you use.

17. Do Not Assume a Maximum Extensions String Length

Various implementation-dependent strings can be retrieved with glGetString. You can use glGetString to retrieve the OpenGL version string (GL_VERSION), vendor string (GL_VENDOR), renderer description string (GL_RENDERER), and the extensions supported string (GL_EXTENSIONS).

The pointer returned by glGetString is a character array managed by the OpenGL implementation. The string is read-only and should not be written. Your application should not attempt to free the returned pointer. You should not make any assumptions on the length of the returned strings, but you can depend on the strings being null-terminated so its length can be determined with strlen.

Because the string is write-only, applications often copy these strings to application-allocated memory. A common pitfall is to assume that one or more of these strings is no longer than a fixed size buffer. If a longer-than-expected string is copied into a fixed size buffer that is too small, the result is usually memory corruption and eventually erroneous program behavior, often times a program crash.

This pitfall is particularly common for the OpenGL extensions string. Some OpenGL implementations support a large number of extensions. Indeed, the OpenGL extensions string will only get longer in future OpenGL implementations that will support more and more extensions. The maximum length of the extensions string for today’s OpenGL implementations is sure to be exceeded by tomorrow’s OpenGL implementations. The problem with this pitfall is that it is usually several months or years in the future that some end-user finds that your application crashes when using a new OpenGL implementation. And because the pitfall results in memory corruption, the way the corruption exhibits itself is quite unpredictable.





Here is what to avoid:

–  –  –

extensions = glGetString(GL_EXTENSIONS);

sprintf(buffer, “Extensions: %s\n”, extensions); /* BAD ASSUMPTION: Buffer overflow possible! */

–  –  –

extensions = glGetString(GL_EXTENSIONS);

extensionsLen = strlen(extensions);

buffer = (char*) malloc(extensionsLen+sizeof(“Extensions: \n”));

sprintf(buffer, “Extensions: %s\n”, extensions);

While the extensions string is usually the longest string and the string must subject to growth in the future, this pitfall can equally apply to length assumptions about any of the implementationdependent strings returned by glGetString.

18. Clip Plane and Eye-Linear Texture Coordinate Generation Plane Specification OpenGL supports application-defined clip planes and a mechanism called eye-linear texture coordinate generation. Texture coordinate generation is often called texgen for short. Clip planes provide a way to clip primitives when they fall on the wrong side of an application-specified clip plane. Eye-linear texture coordinate generation is a mechanism for having OpenGL automatically generate texture coordinates (instead of you explicitly specifying the coordinates with glTexCoord) such that the texture coordinates are a linear function of a vertex’s eye-space position.

Both mechanisms require four floating-point parameters that define a plane equation. Plane equations are typically specified as Ax + By + Cz + Dw = 0 where A, B, C, and D are the parameters of the plane equation. Clip planes are specified with the glClipPlane command. Eye-linear texgen planes are specified with the glTexGen command with the GL_EYE_LINEAR parameter.

What may not be obvious to OpenGL programmers, though it is well documented, is that the parameters used to specify the clip plane or eye-linear plane equations are transformed by the inverse of the current modelview matrix. The clip plane and texgen functionality uses the transformed version of the parameters, not the actual parameters that you pass to glClipPlane or glTexGen.

If you don’t understand this, the operation of clip planes and eye-linear texgen is very confusing.

By transforming the clip plane and eye-linear texgen parameters by the inverse of the modelview matrix, the plane equation is transformed into eye space. In fact, this is very convenient in many cases. If the current modelview matrix is loaded with your “look at” transform that defines the mapping from world space to eye space, then you can specify the parameters for the clip plane or eye-linear texgen plane as a world space plane equation.

If you want to specify your clip plane or eye-linear texgen plane directly in eye coordinates, make sure the current modelview matrix is the identity matrix. If you want to specify your clip plane or eye-linear texgen plane in world coordinates, make sure that your modelview matrix is loaded with your “look at” transform (typically specified with gluLookAt).

Other OpenGL state parameters are also transformed into eye space. When you specify a light position with glLightfv and GL_POSITION, the current modelview matrix transforms the light position. When you specify a light’s spotlight direction with glLightfv and GL_SPOT_DIRECTION, the upper leftmost 3x3 portion of the modelview matrix transforms that spotlight direction vector. Again, these automatic transformations are confusing if you are not aware of them, but they are very convenient when

–  –  –

you want to specify the light position or spotlight direction in world space coordinates. In this case, specify your light position and/or spotlight direction when the modelview matrix is loaded with your “look at” transform.

19. The OpenGL header directory name is capitalized This is a pitfall for Windows programmers trying to write portable OpenGL programs. When you include OpenGL’s standard header files, it should be done as follows

–  –  –

Note that the first GL is capitalized in each line. Windows file names are case insensitive (capitalization does not matter). This means on Windows machines, you can get away with writing

–  –  –

On Linux PCs and Unix workstations that support OpenGL, file names are case sensitive so “gl” is a different directory from “GL”. Source code that includes the OpenGL header files with gl/gl.h or gl/glu.h will have to be fixed for the code to compile portably on Linux and Unix systems. Save yourself some grief and always properly capitalize the OpenGL include file names.

Also, if you use GLUT (the OpenGL Utility Toolkit) to develop portable applications, be sure that you always include GL/glut.h and do not explicitly include GL/gl.h or GL/glu.h in your source code. On Windows operating systems, the GL/gl.h and GL/glu.h header files cannot be successfully included without a bunch of Windows-dependent types and macros being setup which are normally defined in windows.h. Unfortunately (or fortunately depending on your predisposition about Microsoft operating systems), windows.h is not available on non-Windows operating systems.

And even under Windows where it is available, including windows.h will define literally thousands of macros, type definitions, and function prototypes that get in the way of writing portable programs. The good news is that GL/glut.h automatically includes GL/gl.h and GL/glu.h for you in a portable way that works on both Windows and non-Windows operating systems. By including GL/glut.h, you can maximize the portability of your GLUT programs to both Windows and nonWindows operating systems.

Conclusion I hope that this review of various OpenGL pitfalls saves you much time and debugging grief. I wish that I could have simply read about these pitfalls instead of learning most of them the hard way.

Visualization has always been the key to enlightenment. If computer graphics changes the world

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


Similar works:

«The Physical Basis of Ice Sheet Modelling (Proceedings of the Vancouver Symposium, August 1987). IAHS Publ. no. 170.Observations at the edge of the Greenland ice sheet: boundary condition implications for modellers Peter G. Knight Department of Geography University of Aberdeen St. Mary's, High Street Aberdeen, AB9 2UF, UK Abstract Basal ice sequences at the margin of the Greenland ice sheet reflect both marginal and interior basal processes, and have important boundary condition implications...»

«The morphological basis of paradigm leveling∗ Adam Albright University of California, Los Angeles 1 Introduction In older stages of Latin, many nouns exhibited paradigmatic s ∼ r alternations created by rhotacism in suffixed forms, as in (1a). In the period immediately before Classical Latin, these s ∼ r alternations were eliminated by extending the r to the nominative form. At approximately the same time, an independent change shortened long vowels before word-final sonorants,...»

«1 THYROID HORMONES IN BRAIN DEVELOPMENT AND FUNCTION September 3, 2015 Juan Bernal, MD Instituto de Investigaciones Biomedicas, Consejo Superior de Investigaciones Científicas (CSIC) and Universidad Autónoma de Madrid (UAM), and Center for Biomedical Research In Rare Diseases (CIBERER), Madrid, Spain jbernal@iib.uam.es ABSTRACT Thyroid hormones are essential for brain developat through specific time windows influencing neurogenesis, neuronal migration, neuronal and glial cell differentiation,...»

«String of Pearls: Meeting the Challenge of China's Rising Power across the Asian Littoral DISTRIBUTION STATEMENT A Approved for Public Release Christopher I. Pehrson Distribution Unlimited Intue 171 PA Calse Colg Wa Arm U.S StaegcStde STRING OF PEARLS: MEETING THE CHALLENGE OF CHINA'S RISING POWER ACROSS THE ASIAN LITTORAL Christopher J. Pehrson July 2006 This publication is a work of the U.S. Government as defined in Title 17, United States Code, section 101. As such, it is in the public...»

«Akindele, Cohesive Devices Cohesive Devices in Selected ESL Academic Papers Julianah Akindele Osun State University Abstract This paper identifies the cohesive devices obtainable in two selected ESL academic papers: S. Faniran, Paralinguistic communication in Chinua Achebe‟s “A man of the people” and S.A Fabunmi‟s “The „Nigeria project‟ and the indigenous languages of National integration”. It examines the various lexical and grammatical cohesive devices used in the papers...»

«Jarred Griffiths Administrative Vice President 2nd Quarter Report 2016 Submitted 23-06-2016 at 9:00am 2794 words PART ONE: EXECUTIVE OFFICER POSITION DESCRIPTION DUTIES Assist the President in their duties. I have been available to assist the President at all times. Support the President by maintaining oversight of all executive officers. During the quarter I have met regularly with all executive officers, and have established regularly weekly meetings with each of them. I am available at all...»

«THE ROLE OF W A T E R VAPOUR I N THE D E H Y D R O X Y L A T I O N OF CLAY M I N E R A L S By G. W. BRI~qDLE~Cand M. NAtOa~RA Department of Ceramic Technology, The Pennsylvania State University, University Park, Pa., U.S.A. [MS. Received 19th March, 1957.] ABSTRACT Water vapour atmospheres are known to raise the temperature of the endothermic reaction of clay minerals in differential thermal analyses. In isothermal kinetic studies of dehydroxylation, it is shown that a stationary state is...»

«EMSLAND NEWS Newsletter April 2012 Highlights National International People Successful Trade Fair Borax free gluing with S O S Safety, Interview with Paul Participation of Emdex 1480 Tidiness, Cleanliness Buntrock Emsland Group Summary Foreword 3 Highlights Borax free gluing in the corrugated board and core board industry 4 Solam sets its sights on future growth 5 Factory Premium no excuse for a low Pay-Out price 6 National S O S Safety, Tidiness, Cleanliness 7 Outlook on the new Staff Building...»

«A Bilfinger Real Estate company Report GVA 3 Brindleyplace Birmingham B1 2JB Tinkers Green Estate Redevelopment Statement of Consultation Tamworth Borough Council February 2015 Tamworth Borough Council Tinkers Green Statement of Consultation Prepared By: Laurence Holmes Status Principal Planner Date 04/02/2015 Reviewed By: Simon Phillips Status Associate Date For and on behalf of GVA Grimley Ltd February 2015 I gva.co.uk Tamworth Borough Council Contents Tinkers Green Statement of Consultation...»

«Programme opérationnel FSE Martinique Etat 2014-2020 Appel à projets du Fonds social européen Axe prioritaire 1 Soutenir et accompagner l’accès à l’emploi Date de lancement de l’appel à projets : 04 / 04 / 2016 Date limite de dépôt des candidatures : 30 / 04 / 2016 30 / 06 / 2016 30 / 09 / 2016 30 / 11 / 2016 L’appel à projet est permanent, la date d’échéance permet l’organisation d’une sélection au fil de l’eau des projets. Il sera renouvelé en 2017 avec au moins...»

«Texas Parasol Builders Manual Texas Parasol Copyright 1998 Richard Lamb Second Edition Revised April 2000 I, Richard E. Lamb, the original author of this work, do hereby place this manuscript in the public domain on this First day of August, in the Year of our Lord 2005, subject to the following provisions. No FEE, PAYMENT, or ANY OTHER REMUNERATION (including any so-called COPYING FEE) may be charged for acquiring this manual, including all text, photographs, and drawings. NOTICE Due to widely...»

«Stand: 12.04.01 VIth International Potsdam Symposium on Tick-borne Diseases (IPS-VI) Scientific Programme Thursday, April, 26th, 2001 10.30 Registration 12.30 12.45 Opening SPECIAL ISSUES ON TBE Moderators: P. Kimmig, Ch. Kunz Ch. Kunz (Vienna, Austria): 12.45 13.00 Vaccination against TBE in Austria: The success story continues R. Kaiser (Pforzheim, Germany): 13.00 13.15 TBE in Germany and the clinical course of the disease K. Kristiansen (Bornholm, Denmark): 13.15 13.30 TBE in Denmark – in...»





 
<<  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.