OpenGL graphics programming

xiaoxiao2021-03-06  39

OpenGL graphics programming

Out of office:

Maple Studio

[2003-06-02]

Author: maple

Directory 1 OpenGL Extended 2 graphics Difference 3 Vertection / Pieces Programming 4 CG / RM / Other

With the rapid development of graphics cards, faster speeds and more and more new features are supported by hardware, hardware progress makes graphics developers can create a more beautiful visual effect, now, the real-time rendering of film-level animations It is no longer a dream. How do we use the new characteristics of the graphics card in OpenGL? The answer is OpenGL extension. 1, OpenGL Extensions OpenGL and Direct3D compared, the biggest strength is its expansion mechanism. Hardware vendors have developed a new feature that can develop OpenGL extensions for new features, and software developers can use new hardware features through this extension. So although the development speed of the graphics card is much faster than the OpenGL version update, the programmer can still use the latest hardware function with OpenGL. Direct3D has no expansion mechanism, and the new features of the hardware should wait until Microsoft will release the new version of DirectX. OpenGL extension is not shortcoming, just because each hardware manufacturer can develop its own extension, the number of extensions is relatively large, and it is a bit confusing, some of the same features, but because it is developed by different manufacturers, the interface is different. Therefore, in order to implement this function, it is often necessary to write different programs for different graphics cards. This problem may be resolved after OpenGL 2.0, and one goal of OpenGL 2.0 is unified expansion and reduces the number of expansions. 1.1, each extension has an extension, the extension is similar to the following form: GL_ARB_MULTITEXTURE (...); the first paragraph GL, is used to expand the extension for OpenGL, there are several values:

GL - for the OpenGL core extension. WGL - extension of the Windows platform. GLX - Extension for UNIX / Linux platforms. Glu - Extension of OpenGL Utility Library. The second paragraph ARB is used to express this extension developed, and there are many of the following values:

ARB - Opengl ARCHITECTURE REVIEW BOARD is expanded, often developed by the manufacturer, if there is an extension and ARB extension, which exists at the same time, should prefer ARB extensions. EXT - extension supported by multiple hardware vendors. Expansion of NV - NVIDIA. ATI - ATI development extension. ATIX - experimental extensions developed by ATI. Extensions developed by SGI - SGI (Silicon Graphics). Experimental extensions developed by SGIX - SGI (Silicon Graphics). The third paragraph Multitexture is a real extension, such as MultiteXTure is multiple texture extensions. 1.2 The string is all extended extensions supported by the graphics. Different extensions are separated from spaces, such as: GL_ARB_IMAGING GL_ARB_MULTITEXTURE GL_ARB_POINT_PARAMETERS ... OpenGL extensions often add some functions, on the Windows platform, these functions are not passed The .lib library is connected to the program, and the pointer to function when running at runtime. We use GL_ARB_POINT_PARAMETERS extensions to see how to get function pointers. We must first define the function pointer type: typedef void (APIENTRY * PFNGLPOINTPARAMETERFARBPROC) (GLenum pname, GLfloat param); typedef void (APIENTRY * PFNGLPOINTPARAMETERFVARBPROC) (GLenum pname, const GLfloat * params); SGI has already done the job for us, it provides A header file GlexT.h, there is currently most extended constant and function pointer definition, download it down below the include / GL folder below the compiler, and then add: #include The type of constant and function pointer can be used in the program.

Then to define the function pointer: PFNGLPOINTPARAMETERFARBPROC glPointParameterfARB; PFNGLPOINTPARAMETERFVARBPROC glPointParameterfvARB; then check whether the video card supports GL_ARB_point_parameters extensions, isExtensionSupported is a custom function, it is to find whether there is a designated extension in the string glGetString (GL_EXTENSIONS) in return: int hasPointParams = isExtensionSupported ( "GL_ARB_point_parameters"); if supported, may get a pointer to the extended function with wglGetProcAddress function: if (hasPointParams) {glPointParameterfARB = (PFNGLPOINTPARAMETERFARBPROC) / wglGetProcAddress ( "glPointParameterfEXT"); glPointParameterfvARB = (PFNGLPOINTPARAMETERFVARBPROC) / wglGetProcAddress ( "glPointParameterfvEXT ");} finally can use the extended functions in the program: if (hasPointParams) {static GLfloat quadratic [3] = {0.25, 0.0, 1 / 60.0}; glPointParameterfvARB (GL_DISTANCE_ATTENUATION_ARB, quadratic); glPointParameterfARB (GL_POINT_FADE_THRESHOLD_SIZE_ARB, 1.0); } 1.3, WGL Extended GlgetString (GL_EXTensions) The extended string is not included in the WGL extension for the Windows platform, the WGL extension string is available through the WGL_ARB_EXTENSIONS_STRING extension, the following code demonstrates how to get the WGL extension: Define WGL_ARB_EXTENSIONS_STRING extension new Function wglgetextensionsSswoldsString ARB's function pointer type, the same work SGI has been done for us, but not in Glext.h, and in another header file in it provides: typedef const char * (apientry * pfnwglgetextensionsSstingstringarbProc) (HDC HDC) ); definition of function pointers: PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB; check support WGL_ARB_extensions_string expansion, if not, indicates that the card does not support WGL extensions, if supported, is obtained pointer wglGetExtensionsStringARB function, and call it to give WGL extended string: int hasWGLext = isExtensionSupported ( "WGL_ARB_EXTENSIONS_STRING"); if (Haswglext) {WGlGetextensionsStringarb = (PfnwglgetExtensionsStringarbProc) / WGlgetProcaddress ("WGlgetExtensionsStringArb);

Const char * wglext = WGLGETEXTENSIONSSSSSINGARB (HDC); ......} 1.4, OpenGL version Some common OpenGL extensions will be added to the OpenGL core in the new version of OpenGL, become part of the OpenGL standard, simplifying program development, programmers use these Function does not have to do cumbersome extension initialization work. For example, multiple texture functions, add multiple textures to OpenGL 1.2.1, before you use multiple textures, first check whether GL_ARB_MULTITEXTURE extensions are supported, then initialize the function of GLACTINTEXTUREARB, it is troubles, while OpenGL 1.2, you can use the GLACTIVETEXTURE function directly. . However, this simplified only Mac / UNIX / Linux programmer can enjoy it, not as simple as the Windows platform. Microsoft is very negative for OpenGL support to maintain Direct 3D, and its OpenGL implementation is still 1.1. Since the OpenGL program on Windows ultimately links Microsoft's OpenGL32.dll, OpenGL32.dll only supports OpenGL 1.1, so that we cannot use the new version of OpenGL, still need to use extended access to OpenGL1.1 new features. 1.5, OpenGL Extended Information All About OpenGL Extensions: Discuss the OpenGL extension mechanism, telling how to read the expansion official manual and raise some exemplary examples. Must read. OpenGL Extension Registry: Maintained by SGI, listing all current expansion and its official manual. OpenGL Hardware Registry: It is maintained by Delphi3D.net, which lists OpenGL hardware information that is currently almost all 3D acceleration cards, including its supported extensions. Of course, the extension of this column cannot be used as a basis for the program, and an extension is to be used in the program, or check whether the graphics card is supported. Because the same graphics card, if the driver is different, the supported extension is different, and the new driver is often added to the new extension, and some discarded extensions will be lost. 1.6, OpenGL hardware accelerates on the Windows platform, OpenGL drivers may have three modes: pure software, MCD and ICD.

Pure Software Mode Microsoft provides an OpenGL software implementation, all rendering operations are completed by the CPU, and the speed is slow. If you use Windows toilers when installing the system, the OpenGL program runs in software mode. And because Microsoft has its own Direct 3D, the support of OpenGL is very negative, and its OpenGL pure software implementation only supports OpenGL 1.1, and the latest version of OpenGL is 1.4. MCD (MINI Client Driver) MCD is an early Microsoft to support OpenGL on Windows NT, in order to simplify a model used in driving development. In this model, the OpenGL rendered pipeline transformation, the light portion is still implemented by the software, and the rasterization portion is implemented by hardware vendors, so as long as hardware support, the MCD can accelerate the rasterization section. Although MCD can simplify drive development, the function is too large, and the 3D acceleration card on the market supports hardware transform and light, but MCD cannot take this feature, it seems that the MCD has no value. ICD (Installable Client Driver) is a complete OpenGL drive model, which is much more complicated than MCD. Hardware vendors should achieve complete OpenGL rendering pipelines such as transformation, light, rasterization, etc., so as long as hardware support, ICD can accelerate the entire OpenGL rendering pipeline. We generally say that OpenGL hardware acceleration refers to hardware acceleration through the ICD model, and now the OpenGL driver provided by hardware vendors is also developed in accordance with ICD models. The ICD of the main hardware vendors can already support OpenGL's latest version 1.4. How does Windows implement OpenGL hardware acceleration? OpenGL32.DLL is Microsoft's OpenGL 1.1 pure software implementation, and our programs must be dynamic link to this DLL. If you install a 3D chip vendor's driver, place a DLL of a different name in the Windows system directory, such as installing the NVIDIA GeForce2 MX driver under Windows 2000, will drop NVOGLNT.DLL in the system directory (this is NVIDIA) OpenGL driver) and register nvoglnt.dll in the registry, let Windows know the name of the hardware accelerate OpenGL driver, run the OpenGL program in the future, OpenGL32.dll will transfer the OpenGL call directly to NVOGLNT.DLL. On the Windows platform, whether an OpenGL program uses hardware acceleration from three factors, which is not possible, otherwise the program will run in pure software mode: Do you have a 3D acceleration card. Whether the latest drivers provided by the graphics card manufacturer are installed, and the Windows comes with the graphics driver does not provide OpenGL hardware acceleration. The specified pixel format is supported by the graphics card hardware. To determine whether a pixel format is supported by the graphics card, you can use the function DescribepixElFormat to get the pixel format data, then look at the value of dwflags in the structure PixelformatDescriptor, if:

Pfd_generic_format is set 1, and pfd_generic_accelerated is set 0, ie: (pfd.dwflags & pfd_gener_format) &&! (Pfd.dwflags & pfd_generic_accelerated) // indicates that the pixel format is not supported by the graphics card, using the OpenGL program in this pixel format will be used Pure software mode rendering. PFD_Generic_Format is set 1, and PFD_Generic_Accelerated is set by 1, ie: (pfd.dwflags & pfd_generic_format) & (pfd.dwflags & pfd_generic_accelerated) // indicates that the pixel format is supported by graphics card, and the program uses MCD mode rendering. Pfd_generic_format is set 0, and pfd_generic_accelerated is set by 0, ie:! (Pfd.dwflags & pfd_gener_format) &&! (Pfd.dwflags & pfd_generic_accelerated) // indicates that the pixel format is supported by graphics card, and the program uses ICD mode rendering. 2, the difference between the graphics card, as mentioned earlier, different graphics vendors may develop different OpenGL extensions for the same function, so that OpenGL extension programming complicates, even if the same function, it is possible to develop different procedures for different graphics cards. Fortunately, there are only two NVIDIAs and ATI's two, so the workload is not too big. This situation will be greatly changed after OpenGL 2.0 is launched. The game's operating environment is very different, may run on an old graphics card with basic 3D accelerated capabilities, or may be run on the latest powerful 3D acceleration card, and the manufacturer of the graphics chip is different. For machines with poor graphics cards, ensure that the game can run, and for better graphics cards, give full play to its function, create a beautiful graphics effect. That is to say that the game requires multiple execution paths, and selection different execution paths according to the graphics card configuration. How do you decide how many execution paths? The design execution path should consider two main factors: Decide that the graphics chip manufacturer to focus on the 3D graphics chip market has two major manufacturers NVIDIA and ATI, their respective OpenGL extensions, and must design the corresponding execution path for them. For other vendors, because almost all vendors will try to support ARB extensions, they can design the execution path, using the ARB extensions supported by each manufacturer. The program supported graphics card chip supported by the program will have a chip code, similar to the software version number. The chip of the same main version number belongs to a grade, and the functions supported are often the same. When designing a graphics program, you can decide how many execution paths need to be designed according to the chip code. It should be noted that a graphics card must be designed, only the execution path of OpenGL basic functions, so that the program can run on a low-end graphics card. The corresponding relationship between the NVIDIA and ATI's graphics chip code and graphics models that may be used now, as follows:

Chip, code-card model NV1NV1NV2Riva 128 NV3Riva 128ZXNV4 Riva TnTNV5 Riva TnT2NV10GeForce 256NV11 GeForce2 MX * NV15GeForce2 * NV17GeForce4 MX * NV18GeForce4 MX AGP8X * NV20 Geforce3 Ti200GeForce3GeForce3 Ti500 NV25GeForce4 Ti * NV28GeForce4 Ti AGP 8X * NV34 GeForce FX 5200 NV31GeForce FX 5600NV30GeForce FX 5800NV35GeForce FX 5900

Chip, code-card model RAGE (?) RAGE PRORAGE 128RAGE 128 PROR100 RADEON 7000RADEONRADEON 7200RV200RADEON 7500RV250 RADEON 9000 R200 RADEON 8500RADEON 9100 RV280 RADEON 9200 AGP 8XRV300RADEON 9500RV350 RADEON 9600R300 RADEON 9700 R350 RADEON 9800 · where marked "*" is the product line, the model will Split. · Usually everyone is used to collective chips with the primary version of the chip code, such as NV20, NV20 / NV25 / NV28, with R200 collectively referred to as RV250 / R200 / RV280. · Although the ATI Radeon 7500 chip model is RV200, it actually belongs to the R100 grade product. Let's take a way to perform an example of the path, ID Software is about to launch the execution path of the DOM 3. Doom 3 has 6 execution paths: 1) ARB: Almost no extension, no mirror highlight effect, no vertex program, guarantee that the program can run on a low-end graphics card. 2) NV10: Support all functions, 5 Pass (Five Rendering Passs) each frame (Five Rendering Passes: Refers to an object multiple times to send rendering pipes, such as the first rendering diffusing Diffuse, the second rendering mirror high-gloss Specular ..., each The sub-rendering results are mixed together through Glblend to form the final effect), without vertex programming. 3) NV20: Supports all features, 2 or 3 pass. 4) NV30: Supports all features, 1 pass. 5) R200: Supports all functions, only 1 pass in most cases. 6) ARB2: Supports all functions, Fragment Program, 1 PASS. NVIDIA graphics cards may perform ARB / NV10 / NV20 / NV30 / ARB2 path, where NV10 / NV20 / NV30 is developed for NVIDIA graphics cards of different grades. The ATI graphics card may perform three paths of ARB / R200 / ARB2, where R200 is developed specifically for ATI. Other graphics cards perform ARB or ARB2 path depending on the grade level. ARB is used for low-end graphics cards, ARB2 is used for high-end graphics. 3, vertex / piece programming 1999, NVIDIA launched GeForce 256 and started using GRAPU (Graphics Processing Unit) to call the graphics card (also called VPU (Visual Processing Unit), is a matter). GeForce 256 The biggest selling point is hardware T & L (Transform & Lighting, transformation, and illumination), which means that transformation and illuminating calculations can be carried out in GPU, greatly reduced the pressure of the CPU, and the graphics start becomes a processor independent of the CPU. After hardware T & L, GPU most exciting advances are to introduce programmable ability. We know that OpenGL and Direct3D have a fixed rendering pipeline, define the light source, send the vertex position, normal, texture coordinates, etc., you can rendering a picture, programmers can't control the specific rendering process.

OpenGL extensions and DirectX 8 introduce programmable power to rendering pipelines, graphics software developers can write assemblers running on graphics cards to control specific rendering processes, which gives graphics software to develop greater flexibility, and due to hardware support Getting these flexibility does not sacrifice properties. GPU's programmable ability will have a profound impact on real-time graphical rendering. OpenGL supports two programmable models: Vertex Program: Vertex Shader corresponding to Direct 3D, provides programmable T & L capabilities, instead of T & L in traditional rendering pipeline, handling vertices, light calculations. Fragment program: Pixel Shader corresponding to Direct3D, provides programmable rasterization, instead of texture mapping, multi-pixel color and fog processing in traditional pipelines. The current extension related to the top program is:

GL_NV_VERTEX_PROGRAM: Support on NVIDIA NV10 and above grades. GL_NV_VERTEX_PROGRAM1_1: Support on NVIDIA NV10 and above graphics card. GL_EXT_VERTEX_SHADER: Support on the ATI R200 and above grades. GL_ARB_VERTEX_PROGRAM: These extensions are supported by three extensions above, supporting this extension after supporting the three extension graphics card installations, so the program does not have to support the above three extensions, only support this extension. This extension does not support branch cycles. GL_NV_VERTEX_PROGRAM2: Support on NVIDIA NV30 and above graphics card, support branch cycles, estimates that it will become the prototype of GL_ARB_VERTEX_PROGRAM2. In fact, the ATI R300 level graphics card has supported the branch cycle in the vertex program (hardware supports DirectX 9 Vertex Shader 2.0), but no development extension is provided to the OpenGL programmer, it is estimated that the GL_ARB_VERTEX_PROGRAM2 extension of the support branch cycle is introduced. It can be seen now using OpenGL's Vertex Program, just support both GL_ARB_VERTEX_PROGRAM and GL_NV_VERTEX_PROGRAM2 to extension. The current extension related to interruption is:

GL_NV_REGISTER_COMBINERS: Support on NVIDIA NV10 and above grade graphics cards, processing the pixel color and the fog calculation. GL_NV_REGISTER_COMBINERS2: Support on NVIDIA NV20 and above graphics cards, a simple extension for GL_NV_REGISTER_COMBINERS, supporting two constant registers can have different values ​​in each of the Combiner per level. GL_NV_TEXTURE_SHADER / GL_NV_TEXTURE_SHADER2 / GL_NV_TEXTURE_SHADER3: Support on NVIDIA NV20 and above graphics card, providing a variety of powerful texture extraction operations. Gl_ati_fragment_shader: Support on ATI R200 and above grades. The above-mentioned extension is not to write assembly that is running on the GPU, but is implemented by function calls, it is better to write assembly codes. The following extensions can be implemented by writing assembly codes.

Gl_ati_text_fragment_shader: actually is the assembly code of GL_ATI_Fragment_Shader, and has not seen support information on the PC now, it seems to be supported on the Apple machine. GL_ARB_FRAGMENT_PROGRAM: Support on NVIDIA NV30 / ATI R300 and above grades. GL_NV_FRAGMENT_PROGRAM: Support on NVIDIA NV30 and above grade graphics cards, stronger than GL_ARB_FRAGMENT_PROGRAM. Compared with the vertex programming, the fragment programming is much more complicated: On the NV10 series, you can only use the partial fragment programming capabilities provided by GL_NV_REGISTER_COMBINERS. On the NV20 Series, you can use the Register Combiners and Texture Shader to implement segment programming. On the NV30 series, GL_ARB_FRAGMENT_PROGRAM and GL_NV_FRAGMENT_PROGRAM can be used. On the ATI R200 series, GL_ATI_Fragment_Shader is used. On the ATI R300 series, GL_ARB_FRAGMENT_PROGRAM is used. Seeing this here we don't understand why DOM 3 will divide the rendering execution path into ARB, NV10, NV20, NV30, R200, ARB2:

ARB has no vertex programming and there is no fragment programming. The NV10 is not used to programmatically programming (actual NV10 supports vertex programming), but uses Register Combiners to implement an embedded map (Bump mapping). NV20 uses vertex programming and uses Register Combiners and Texture Shader to implement segment programming. NV30 uses vertex programming and implements segment programming with GL_NV_FRAGMENT_PROGRAM. R200 uses vertex programming and implements segment programming with GL_ATI_Fragment_Shader. ARB2 uses vertex programming and implements segment programming with GL_ARB_FRAGMENT_PROGRAM. Schedule: NVIDIA graphics card vertex, pieces of fragment programming.

Chip texture unit number code Register CombinerTexture ShaderVertex ProgramFragment ProgramNVX2XXXXNV1022 stage XNVvp1.1 / ARBvp1.0RCNV2048 Support NVvp1.1 / ARBvp1.0RC / TSNV3048 Support NVvp2.0 / ARBvp1.0ARBfp1.0 / NVfp ATI graphics vertex fragment Support for Programming :

Texture unit number code chip Vertex ProgramFragment ProgramRAGE2XXR1003XXR2006EXTvp / ARBvp1.0ATIfpR3008EXTvp / ARBvp1.0ARBfp1.0 4, Cg / RM / or other 4.1 Cg (C for Graphics) extended directly write vertex program and fragment program is not convenient, either function call Either assembly code, which is equivalent to writing a PC program with X86, and now there is already a high-level language for Vertex Program and Fragment Program, called HLSL (High Level Shading Language: Advanced Coloring Language). CG is an advanced coloring language proposed by NVIDIA. Its syntax and C language are similar to great simplifying the writing of Vertex Program and Fragment Program. Programs you write can:

Compile into GL_NV_VERTEX_PROGRAM / GL_NV_VERTEX_PROGRAM1_1 / GL_ARB_VERTEX_PROGRAM / GL_NV_VERTEX_PROGRAM2 assembly code. Compile the assembly code for GL_ARB_FRAGMENT_PROGRAM / GL_NV_FRAGMENT_PROGRAM. Compiled into the RC (Register Combiners) and TS (Texture Shader) script for NVPARSE. Call the CG directly to run the CG program directly in the program. Vertex Shader / Pixel Shader compiled into DirectX. We can see that CG is only better to NVIDIA's product support, while other manufacturers have only support GL_ARB_VERTEX_PROGRAM / GL_ARB_FRAGRAM / GL_ARB_FRAGMENT_PROGRAM to get benefits from CG, do not support these two ARB extended graphics cards that cannot be running CG write programs, greatly reduced The practicality of CG. Although CG provides an interface, other vendors can expand to CG to support individual manufacturers of OpenGL extensions, but CG is a company's products, other manufacturers will not support, so if they want to write a generic graphics, CG and Not suitable. Moreover, OpenGL's HLSL - GLSLANG (OpenGL Shading Language) specification has been reviewed by ARB, and it is estimated that you can use Glslang to write Vertex Program and Fragment Program. The location of CG should be quite embarrassing, because OpenGL and DirectX have yourself. HLSL. However, if you come back, CG can support OpenGL and DirectX at the same time, which is also its advantage. 4.2 RenderMonkey RenderMonkey is not a language, but an integrated development environment in which the ATI launched a debugging Vertex Program and Fragment Program, currently only supports DirectX's Vertex Shader / Pixel Shader / HLSL, but ATI is working with 3dlabs, Non-day RenderMonkey. OpenGL Vertex Program / Fragment Program / Glslang will also support. In addition, RenderMonkey is not only designed for programmers, but also can be used. 4.3 NVPARSE NVPARS is a library launched by NVIDIA that simplifies the development of RC (Register Combiners) and TS (Texture Shader). Use GL_NV_register_combiners and GL_NV_texture_shader expansion program to achieve full piece is in the form of a function call, very convenient, and nVIDIA order to simplify the RC and TS program development, the establishment of a script format, complex GL_NV_register_combiners and GL_NV_texture_shader function call is replaced by a simple script, nvParse The API is provided to explain the execution RC and TS scripts.

转载请注明原文地址:https://www.9cbs.com/read-78757.html

New Post(0)