OpenGL graphics programming

xiaoxiao2021-03-19  250

http://www.gameres.com/articles/program/visual/3d/openglhw.htm

Document Directory: OpenGL Expansion Graphics Difference Vertex / Pieces Programming CG / RenderMonkey / and Other Documentation Content:

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.

extension name

Each extension has an extension, the extension is similar to the following form:

GL_ARB_MULTITEXTURE

The first GL is used to expand the extension of which of OpenGL, there are several values:

GL - Extended WGL for OpenGL - Extended GLX for Windows Platform - Extended GLU for UNIX / Linux platform - Out 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 (OpenGL Administrative Agency) is expanding, often developed by the manufacturer, if there is a manufacturer development extension and ARB extension, you should prefer ARB expansion EXT - by multiple hardware vendors Supported expansion NV - NVIDIA Development Extended ATI - ATI-ATI developed experimental expansion of ATIX-ATI developed experimental expansion of SGI - Silicon Graphics (SGI), Development of SGIX-Silicon Graphics (SGI), developed experimental expansion

The third paragraph Multitexture is a real extension, such as MultiteXTure is multiple texture extensions.

Use OpenGL to expand

To use an OpenGL extension, you must first check if the graphics card supports this extension. The following code can get a graphic card supported OpenGL extension: const char * str = glgetstring (GL_EXTENSIONS); the function returns a string pointer, this string is the graphics All extended extensions supported, separated from spaces between different extensions, such as:

"GL_ARB_imaging GL_ARB_multitexture GL_ARB_point_parameters ......" typedef void (APIENTRY * PFNGLPOINTPARAMETERFARBPROC) (GLenum pname, GLfloat param); typedef void (APIENTRY * PFNGLPOINTPARAMETERFVARBPROC) (GLenum pname, const GLfloat * params); OpenGL extensions often will add some functions in Windows On the platform, these functions are not connected to the program through the .lib library, and dynamically get a pointer to function at runtime. We use GL_ARB_POINT_PARAMETERS extensions to see how to get function pointers.

First, you want to define the function pointer type,

This work SGI has been done for us, it provides a header file Glext.h, there is currently currently most extended constants and function pointer definitions, download it under the include / GL folder of the compiler, and then in the program Inside: You can use constants and function pointer types in the program.

Then you want to define a function pointer: check if the graphics card supports GL_ARB_POINT_PARAMETERS extensions, where ISEXtensionsionSupported is a custom function, which is to find whether there is a specified extension in the string returned in GlgetString (GL_EXTensions): If support, you can use the WGLGetProcAddress function. acquired extended function pointer: {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);} WGL extensions extended string glGetString (GL_EXTENSIONS) does not include the acquired for Windows platforms WGL extension, WGL extension string is available through the WGL_ARB_EXTENSIONS_STRING extension, the following code demonstrates how to get a WGL extension string: define the WGL_ARB_EXTENSIONS_STRING extension new additive WGlGetextensionstringarb's function pointer type, the same work SGI has been done for us, but not in Glext. H in the other header file Wglext.h provided in it: Define function pointer: typedef const char * (apientry * pfnwglgetextensionsionstringarbproc) HDC hdc); 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 (" WGlgetExtensionsSStringarb); const char * wglext = WGLGETEXTENSIONSSTRINGARB (HDC);

......} OpenGL version Some common OpenGL extensions are added to the OpenGL core in the new version of OpenGL, become part of the OpenGL standard, simplifying program development, and programs don't have to do cumbersome initialization work when using these features. For example, multiple texture features, add multiple textures, before you use multiple textures, first check whether GL_ARB_MULTITEXTURE extensions are supported, then initialize functionality such as GLACTIVETEXTUREARB, it is very troublesome, while OpenGL1.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. 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 often adds new extensions, throwing some discarded extensions.

OpenGL hardware acceleration

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 very 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, it is very negative for OpenGL. Its OpenGL pure software implementation only supports OpenGL1.1, and the latest version of OpenGL is 1.4 MCD (MINI Client Driver): MCD is early Microsoft in Windows NT When OpenGL is supported, a model used in simplifies the 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 limited, and now the 3D acceleration card on the market supports hardware transform and light, but MCD cannot take this feature, it seems that MCD has no existence ICD (Installable Client Driver): ICD 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.4Windows how to implement OpenGL hardware? 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, an OpenGL program uses hardware acceleration by three factors, which is not possible, otherwise the program will run in pure software mode:

Is there a 3D acceleration card installed the latest drivers provided by the graphics card manufacturer, and the Windows comes with the graphics driver does not provide the pixel format specified by OpenGL hardware acceleration capabilities.

To determine whether a pixel format is supported by the graphics card, you can use the function DescribepixElFormat to get the data of the pixel format, 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. (pfd.dwflags & pfd_generic_format) &&! (Pfd.dwflags & pfd_generic_accelerated) indicates that the pixel format is not supported by the graphics card, using the pure software mode using the pure software mode using this pixel format rendering PFD_GENERIC_FORMAT is set, and PFD_GENERIC_ACCELERATED is set, i.e., (pfd.dwFlags & PFD_GENERIC_FORMAT) && (pfd.dwFlags & PFD_GENERIC_ACCELERATED) indicates that the pixel format supported by the graphics hardware, and the program mode rendering PFD_GENERIC_FORMAT using MCD is set to 0 and PFD_GENERIC_ACCELERATED Veter 0,! (Pfd.dwflags & pfd_generic_format) & pfd_gener_accelerated indicates that the pixel format is supported by the graphics card, and the program uses ICD mode rendering graphics differences, as mentioned earlier, different graphics manufacturers may be The same function is developed different OpenGL extensions, making OpenGL extension programming complications, even if the same function, it is possible to develop different programs 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? Design execution path should consider two main factors:

Decide the graphics card chip manufacturer to be supported

Now there are two major manufacturers NVIDIA and ATI, their respective OpenGL extensions must also 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.

Program supported graphics

The graphics card chip 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:

nVIDIA ATI chip, code-card model chip, code-card model NV1 NV2 NV3 NV4 NV5 NV1Riva 128Riva 128ZXRiva TnTRiva TnT2 RAGE (?) RAGE PRORAGE 128RAGE 128 PRO NV10 NV11 NV15 NV17 NV18 GeForce 256GeForce2 MX * GeForce2 * GeForce4 MX * GeForce4 MX AGP8X * R100 RV200RADEON 7000RADEONRADEON 7200RADEON 7500 NV20 NV25 NV28 Geforce3 Ti200 GeForce3GeForce3 Ti500GeForce4 Ti * GeForce4 Ti AGP 8X * RV250 R200 RV280 RADEON 9000RADEON 8500RADEON 9100RADEON 9200 AGP 8X NV34 NV31 NV30 NV35 GeForce FX 5200GeForce FX 5600GeForce FX 5800GeForce FX 5900 RV300 RV350 R300 R350 RADEON 9500RADEON 9600RADEON 9700RADEON 9800 · which Labeling * is the product line, the model is also subdivided · Usually everyone is used to collect one generation chip with the main version of the chip code, such as NV20, NV20 / NV25 / NV28, R200 collectively referred to RV250 / R200 / RV280 · Although ATI radeon The chip model of 7500 is RV 200, but it is actually a product of R100 grade. We will see an example of execution paths, and IDSoftware is about to introduce the execution path of the DOOM3. Doom 3 has 6 execution paths:

ARB: Almost no extension, no mirror highlight effect, no vertex program, guarantee the program to run NV10 on the low-end graphics card: support all functions, each frame needs to be rendered 5 Pass (Five Rendering Passes, refer to an object Many fed a rendering pipeline, such as the first rendering diffusum Diffuse, the second rendering mirror high-gloss specular ..., each rendering result is formed together with Glblend to form the final effect), no vertex programming NV20: support all functions, 2 Or 3 PASS NV30: Support all features, 1 PASS R200: Support all features, only 1 Pass Arb2 in most cases: support all functions, Fragment Program, 1 pass nvidia graphics can be executed ARB / NV10 / NV20 / NV30 / ARB2 five paths, 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. Vertex / Piece Programming 1999, NVIDIA launched GeForce 256 and started using GRAPU (Graphics Processing Unit) to call the graphics chip (also manufacturer called VPU (Visual Processing Unit). 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 extension and DirectX8 introduce programmable power to rendering pipelines, graphics software developers can write assemblers running on graphics chips to control specific rendering processes, which gives graphics software to develop greater flexibility, and due to hardware support, Getting these flexibility does not sacrifice performance. GPU's programmable ability will have a profound impact on real-time graphical rendering. OpenGL supports two programmable models: vertex programming Vertex Program, corresponding to VertEx Shader in Direct3D, provides programmable T & L capabilities, instead of T & L in the traditional rendering pipe, processing vertex, light calculation fragment programming Fragment Program, corresponding The Direct 3D Pixel Shader provides programmable rasterization operations, instead of texture mapping in traditional pipelines, pixel color and fog handling current vertex programming related extensions:

GL_NV_vertex_program: nVIDIA NV10-end graphics software simulation, hardware support on NV20 and higher quality graphics GL_NV_vertex_program1_1: nVIDIA NV10-end graphics software simulation, hardware support on NV20 and higher quality graphics GL_EXT_vertex_shader: support GL_ARB_vertex_program on the ATI R200 and above grade cards: the The above three extensions come to support the above three extended graphics card installations, which will support this extension after installing the latest drivers, so there is no need 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 grades, support branch cycles, estimates that the prototype of GL_ARB_VERTEX_PROGRAM2 extension actually ATI R300 level graphics card has supported the branch cycle in vertex (hardware support DirectX9 vs2 .0), but no development extension is provided to the OpenGL programmer, it is estimated that the GL_ARB_VERTEX_PROGRAM2 extension in support branch cycles can now use OpenGL's Vertex Program, just support GL_ARB_VERTEX_PROGRAM and GL_NV_VERTEX_PROGRAM2 two extensions. The current extension related to interruption is:

GL_NV_REGISTER_COMBINERS: Support on NVIDIA NV10 and above graphics card, handle multi-pixel color and fog calculation GL_NV_REGISTER_COMBINERS2: Support on NVIDIA NV20 and above graphics card, make simple extensions on GL_NV_REGISTER_COMBINERS, support two constant registers in each level Combiner You can have different values ​​GL_NV_TEXTURE_SHADER / GL_NV_TEXTURE_SHADER2 / GL_NV_TEXTURE_SHADER2 / GL_NV_TEXTURE_SHADER3: Support on NVIDIA NV20 and above, provide a variety of powerful texture extraction operations GL_ATI_Fragment_Shader: Support on ATI R200 and above graphics card

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: in fact, the Department of the encoded version GL_ATI_fragment_shader, we have not see the support information on the PC, it seems only support GL_ARB_fragment_program on Mac: Support GL_NV_fragment_program on nVIDIA NV30 / ATI R300 and above grade Graphics: nVIDIA in Support on NV30 and above grades, more powerful than GL_ARB_FRAGMENT_PROGRAM

Compared with the vertex programming, the fragment programming is much more complicated:

On the NV10 series, only part of the fragment programmability GL_NV_register_combiners available on the NV20 series, you can use the register combiners and texture shader programming realization clips on the NV30 series, you can use GL_ARB_fragment_program and GL_NV_fragment_program on ATI R200 series, in use GL_ATI_fragment_shader On the ATI R300 series, use GL_ARB_FRAGMENT_PROGRAM to see that we don't even understand why DOOM3 will divide the rendering execution path into ARB, NV10, NV20, NV30, R200, ARB2:

ARB has no vertex programming and no discode programming NV10 is not used to program (although NV10 supports vertex programming, but is software simulation, and the graphics hardware is not supported), but uses the register Combiners to implement an embedded map (BUMP mapping) NV20 use vertices program, and implemented with register combiners and texture shader program fragment NV30 vertex program, and implemented with a fragment program R200 GL_NV_fragment_program programming vertex and fragment implemented by programming GL_ATI_fragment_shader ARB2 vertex programming, and programming to achieve fragment GL_ARB_fragment_program

Schedule:

NVIDIA graphics card vertices, fragment programming support

Chip texture unit number code Register CombinerTexture ShaderVertex ProgramFragment Program NVX2XXXX NV1022 stage XNVvp1.1 / ARBvp1.0RC NV2048 Support NVvp1.1 / ARBvp1.0RC / TS NV3048 Support NVvp2.0 / ARBvp1.0ARBfp1.0 / NVfp

ATI graphics card vertex, pieces programming support

Chip code texture unit number Vertex ProgramFragment Program Rage2XX R1003XX R2006EXTVP / Arbvp1.0Atifp R3008EXTVP / Arbvp1.0ARBFP1.0

CG (c for graphics) Direct use of extended written Vertex Program and Fragment Program are not convenient, or function call, either assembly code, equivalent to writing PC programs with X86, and now there is already Vertex Program and Fragment Program Advanced languages, called HLSL (Advanced Coloring Language, High Level Shading 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:

Assembly code is compiled into GL_NV_vertex_program / GL_NV_vertex_program1_1 / GL_ARB_vertex_program / GL_NV_vertex_program2 compiled into assembly code GL_ARB_fragment_program / GL_NV_fragment_program compiled into a nvParse of RC (Register Combiners) and TS (Texture Shader) script calls the API Cg provided directly in the program, run Cg The program compiled into DirectX Vertex Shader / Pixel Shader We can see that CG is only better to support NVIDIA's product, while other vendors only support GL_ARB_VERTEX_PROGRAM / GL_ARB_RAGMENT_PROGRAM / GL_ARB_FRAGRAM can be benefited from CG, and these two ARB extensions are not supported. The procedure written by CG cannot be run, which greatly reduces 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 approved by the ARB, and it is estimated that you can write Vertex Program and Fragment Program in Glslang, and the location of CG should be quite embarrassing, because OpenGL and DirectX have already have Hership HLSL. However, if you come back, CG can support OpenGL and DirectX at the same time, which is also its advantage. RenderMonkey RenderMonkey is not a language, but an integrated development environment in which ATI is launched, the integrated development environment of 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 support. In addition, RenderMonkey is not only designed for programmers, but also can be used. 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.

The simulation NV30 does not have a NV30 graphics card, but NVIDIA's latest detonator driver (Version 40.41 and later) support software simulation NV30 architecture, but it is only very slow, but the programmer without NV30 graphics is already a gospel, as long as We have a GeForce grade graphics card, install the latest detonator driver, then download a NVEMULATE.EXE (52 kB), run it, open NV30 analog, your graphics support all the features of NV30, so no NV30 graphics card You can develop programs for NV30. It should be noted that there is no need for NV30 simulation to remember to turn off it. After all, it is software simulation, and the speed is very slow. After writing these just wants to refer to a direction for later friends, it is very simple, because I am studying, hoho, more things still need everyone to see more :) I will slowly put some slowly in the future. Detailed articles and source code, I hope to help everyone. Regarding the questions or suggestions of this article, you can write to me, my contact information. if (hasPointParams) int hasPointParams = isExtensionSupported ( "GL_ARB_point_parameters"); PFNGLPOINTPARAMETERFARBPROC glPointParameterfARB; PFNGLPOINTPARAMETERFVARBPROC glPointParameterfvARB; #include

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

New Post(0)