How to use OpenGL extension

Source: Internet
Author: User

If you develop OpenGL programs on Windows, the self-contained OpenGL library is 1.1. If you want to use OpenGL libraries of version 1.2 or higher, you can only use OpenGL extensions, there are a lot of articles on how to use OpenGL extension on the Internet, but most of them are simply taken, copied and copied, and there are very few actual code. I also encountered the same problem at the time, after several twists and turns, I finally found a solution and did not dare to enjoy it exclusively. I would like to share it with you. If there is anything inappropriate, please let me know. Thank you!

 

I.Concepts clarification

I often see people in the forum asking:

Where can I download the header file and library file of opengl2.0?

In fact, for a newbie who has just started to use OpenGL, if it is developed on a Windows platform, this problem will certainly be encountered. Why? Because opengl2.0 is mentioned in many reference books, but Microsoft only supports OpenGL by 1.1. After 1.1, Microsoft no longer supports it. Why? Microsoft wants to develop its own DirectX. Therefore, if you want to use functions or functions above opengl1.1, you can only use OpenGL extensions. These extensions are some functions and functions developed by OpenGL groups or individuals after opengl1.1 that can be used on Windows. Therefore, there is no opengl2.0 header file or library file in windows. Everything after opengl1.1 already exists in the form of extensions, and there is no unified standard, you can use glex, glew, Glee, and so on. Below are two common methods:

Most people encounter this problem when running the programs in the redbooks. For example, this function glblendequation is a function in the gl_arb_imaging extension. Therefore, before using an extension, you must first determine whether your video card supports the extension.

 

II. How to determine the extensions supported by the video card:

Use the glgetstring function to see its prototype:

const     GLubyte   * glGetString( GLenum name )

We useGl_extensionsCall this function as a parameter to obtain all the extensions supported by the current video card, as shown below:

Const glubyte * STR = glgetstring (gl_extensions );

Cout <STR <Endl;

The extensions of the output are separated by spaces.If these extensions contain gl_arb_imaging, you can use the glblendequation function.

Of course, only the GL extension can be queried.GlugetstringObtainGLUExtension.

III.Use extended functions

After judging the extensions supported by the video card, you can use the functions included in the extension. The following describes the specific methods for using the extension functions:

1. UseGlext

Here download :( http://graphics.ethz.ch/pointshop3d/sourcedoc/html/glext_8h-source.html)

A) the header file containing this file is not original in windows and needs to be downloaded online. Note that glut is also used in the program. h file, you must set glext. put h in glut. h, because glext. h uses GL. h, while glut. h contains GL. h. If the order is wrong, there will be a lot of errors during compilation. The correct order is as follows:

# Include <iostream>

# Include <windows. h>

# Include <Gl/glut. h>

# Include <Gl/glext. h>

B) obtain the function pointer.

First define the function pointer, pfnglblendequationproc glblendequation = NULL;

Obtain the function address and use the wglgetprocaddress function. Note that this sentence is valid only before the statements that use the glblendequation function. Finally, follow the function statement that uses glblendequation, do not add the pointer to all subprograms. Otherwise, the obtained pointer is invalid.

Glblendequation = (pfnglblendequationproc) wglgetprocaddress ("glblendequation ");

For example, you can add:

Pfnglblendequationproc glblendequation = (pfnglblendequationproc) wglgetprocaddress ("glblendequation ");

Switch (key)

{

Case 'A ':

Case 'A ':

// Note: glblendequation is a subset of gl_arb_imaging, please call glgetstring

// First to confirm whether your video card support this extension.

Glblendequation (gl_func_add );

Break;

Case's ':

Case's ':

Glblendequation (gl_func_subtract );

Break;

If the above method is troublesome, you can use the following method: I recommend this method:

 

2.UseGlew

Glew is also an extension library that contains many core and extension functions in OpenGL. The current version is 1.3.5 and supports opengl2.1. You can download it here:

Http://glew.sourceforge.net/

The above method may be a bit difficult. If you have already downloaded the glew library, you can directly use this extension, but before using it, you must first determine whether your graphics card supports this extension. The method is as follows: first, determine whether the video card supports the extension. We assume that the extension is supported,

1. Include the header file glew. H. Note that this is different from above. This time, glut. H will be placed behind glew. h.

As shown below

# Include <Gl/glew. h>

# Include <Gl/glut. h>

Then you can directly use glblendequation in the program. The judgment and usage of other extensions are similar.

Finally, remember! That is, you need to call glewinit (); to initialize it before using the extension, as shown below:

Glenum err = glewinit ();

If (glew_ OK! = ERR)

{

Messageboxa (null, "error", "my window", 1 );

}

Now you can use the extension with confidence. Please continue with your OpenGL journey!

 

Note: The above links are links from official sites outside China. If you do not go to the Internet, I can send you a copy.

A few better OpenGL sites are provided:

First: Of course, http://www.opengl.org/

Second: entry of good site: http://www.opengl.org/wiki/index.php/Main_Page

 

 

For the complete code of the above two methods, please refer to the next article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.