OpenGL extensions for OpenGL

Source: Internet
Author: User

OpenGL extensions is one of the highlights of OpenGL. The update of the video card brings more and more dazzling new features, so that we can use the outstanding hardware to draw amazing 3D images. With the extension mechanism of OpenGL, new features provided by any hardware manufacturer can be conveniently used by our OpenGL program. (Here we have to mention the most powerful opponent of OpenGL, direct3d. The update mechanism of direct3d is implemented by upgrading the DirectX SDK, which brings a lot of trouble to our learning, we have to deal with various problems brought about by version upgrades. In this regard, OpenGL has obvious advantages ).
Before using OpenGL extensions, check whether your video card supports this extension. You can use this extension only when your video card supports this extension feature. You can use some tools such as glview to do this.
The OpenGL extension mechanism is not complex to use, but some people have done a lot of useful work, simplifying the steps for us to use OpenGL extension, so that we can use OpenGL extension as convenient as the OpenGL API comes. The most famous ones are glext and glew. Here I will mainly introduce glew. the full name of glew is "OpenGL extension wrangler library ". glew mainly helps C/C ++ to complete two cumbersome tasks: 1) initializing and using extensions; 2) Compiling portable programs. For more details about glew and obtain glew, visit: http://glew.sourceforge.net /.
With the above preparations, we can use some new features in our program. The following uses arb_multitexture as an example to explain the specific operations:
Preparations:
Hardware condition: A video card that supports arb_multitexture (why not? Either upgrade the graphics card driver or change the graphics card :)
Software conditions: glu3.7.6 -- http://www.xmission.com /~ Nate/glu.html
Glew1.2.4 -- http://glew.sourceforge.net/
Procedure:
1. Ensure that the glut and glew have been correctly installed.
2. include header files and connection library files:
# Include <Gl/glew. h>
# Include <Gl/glut. h> // note that glew. h must be placed at the beginning.
Connection Library File: opengl32.lib glu32.lib glu32.lib glew32.lib
3. For the initialization of glut and glew, glew must be initialized only after the render context is correctly set. That is, glew can be initialized only after the ngcreatewindow () is called. The related code is as follows:
Int _ tmain (INT argc, _ tchar * argv [])
{
Gluinit (& argc, argv );
Valley createwindow ("multitexture ");
Gluinitwindowsize (500,500 );
Gluinitwindowposition (0, 0 );
Fig );

Gludisplayfunc (Display );
Glureshapefunc (myreshape );

Glenum err = glewinit ();
If (Err! = Glew_ OK)
{
STD: cout <"error:" <glewgeterrorstring (ERR );
Return 1;
}

Myinit ();
Glumainloop ();
Return 0;
}
4. In the initialization phase of the program, create texture objects so that the rendering phase can be switched between different textures:
Void myinit ()
{
Glshademodel (gl_flat );

Glgentextures (1, & floor );
Glbindtexture (gl_texture_2d, floor );
Loadbmp ff ("floor.bmp ");
Gltexparameterf (gl_texture_2d, gl_texture_wrap_s, gl_repeat );
Gltexparameterf (gl_texture_2d, gl_texture_wrap_t, gl_repeat );
Gltexparameterf (gl_texture_2d, gl_texture_mag_filter, gl_nearest );
Gltexparameterf (gl_texture_2d, gl_texture_min_filter, gl_nearest );

Glgentextures (1, & Light );
Glbindtexture (gl_texture_2d, light );
Loadbmp ff ("lightmap.bmp ");
Gltexparameterf (gl_texture_2d, gl_texture_wrap_s, gl_repeat );
Gltexparameterf (gl_texture_2d, gl_texture_wrap_t, gl_repeat );
Gltexparameterf (gl_texture_2d, gl_texture_mag_filter, gl_nearest );
Gltexparameterf (gl_texture_2d, gl_texture_min_filter, gl_nearest );
}
5. in the actual rendering phase, call the Extended Function glactivetexturearb () to specify the Texture unit for the current operation. The next time you call glactivetexturearb, all functions apply to the currently selected Texture unit. Specific Code:
Void display ()
{
Glclear (gl_color_buffer_bit );

Glactivetexturearb (gl_texture0_arb );
Glable (gl_texture_2d );
Gltexenvf (gl_texture_env, gl_texture_env_mode, gl_decal );
Glbindtexture (gl_texture_2d, floor );

Glactivetexturearb (gl_texturew.arb );
Glable (gl_texture_2d );
Gltexenvf (gl_texture_env, gl_texture_env_mode, gl_modulate );
Glbindtexture (gl_texture_2d, light );

Glpushmatrix ();
Glbegin (gl_quads );
Glmultitexcoord2farb (gl_texture0_arb, 0.0f, 0.0f );
Glmultitexcoord2farb (gl_texturew.arb, 0.0f, 0.0f); glvertex2f (-1.0,-1.0 );

Glmultitexcoord2farb (gl_texture0_arb, 0.0f, 1.0f );
Glmultitexcoord2farb (gl_texturew.arb, 0.0f, 1.0f); glvertex2f (-1.0, 1.0 );

Glmultitexcoord2farb (gl_texture0_arb, 1.0f, 1.0f );
Glmultitexcoord2farb (gl_texturew.arb, 1.0f, 1.0f); glvertex2f (1.0, 1.0 );

Glmultitexcoord2farb (gl_texture0_arb, 1.0f, 0.0f );
Glmultitexcoord2farb (gl_texturew.arb, 1.0f, 0.0f); glvertex2f (1.0,-1.0 );
Glend ();

Glpopmatrix ();

Glflush ();
}
In this rendering code, two texture units are specified. The two texture units work together to render the result (actually the result of multiplying the pixel values of the texture pattern) to a rectangular object. Because of two texture patterns, one is a normal texture and the other is lightmap, the rendering effect is like the light mapping effect. This technology has been widely used in games, such as the famous quake.

References:
1. http://glew.sourceforge.net
2. http://www.gamedev.net/reference/articles/article1929.asp
3. http://www.opengl.org/resources/tutorials/sig99/advanced99/notes/node61.html

--------------------------- Dedicated to multimedia technology, become a thoughtful software engineer ------------------------

This article is my original work. If you want to reprint it, please contact me or indicate the source.
You are welcome to give your valuable comments on the content of the article, and hope that you will promptly point out the mistakes in the article so that I can correct them in time.
My contact information:
QQ: 7578420
Email: jerrydong@tom.com

Bytes ----------------------------------------------------------------------------------------

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.