OpenGL programming in QT (5) illumination and texture filtering

Source: Internet
Author: User
Tags bmp image
I. Summary
The Finnish IT service company digia OYj said that the company would invest 4 million euros to buy Nokia's QT software business, digia OYj said, the company will invest 4 million euros to purchase Nokia's QT software business.
QT is a very good team. I hope QT will develop better after leaving Nokia.
After the previous tutorial, we have a preliminary understanding of the 3D world. Today we want to learn about light and texture filtering.
Ii. Illumination and texture filtering
OpenGL has four types of illumination: ambient light, diffuse light, mirror light, and emitting light. These four components can be separately calculated and superimposed.
The environment comes from all directions. Objects in all scenarios are under ambient light. Diffuse Light is produced by a specific light source and reflected on the surface of an object in your scenario. The surface of any object directly exposed by diffuse light becomes very bright,
The area that is almost invisible is a bit darker. The mirror light comes from a specific direction, similar to a set of parallel light.
Emitting light is used to simulate a luminous object and is not affected by any light source.
It is necessary to describe the color of light and material.
For light, the number of color components corresponds to the completeness of each color
Strength Percentage. If the R, G, and B values of a light color are both 1.0, this light is the most white. If it is 0.5, the light is still white, but the intensity is only normal, so it looks gray in the mountainous area. If R = G = 1 and B = 0 (completely red and green, no blue), the light looks yellow.
The RGB value of the material corresponds to the ratio of the color to the color. Therefore, if the r = 1, G = 0.5, B = 0 of a material, it reflects all the incident light, but does not reflect any incident blue light.
In this tutorial, we will discuss ambient light and diffuse light.
Add several variables to nehewidget. h.
// Texture storage array gluint texture [3]; // The glfloat zoom distance from the scene to the screen; // the speed of the cube rotating on the X and Y axes glfloat xspeed, yspeed; // specifies the texture gluint filter to use; // determines whether to enable the light bool islighting;
Add the information array of the light source to nehewidget. cpp:
GLfloat lightAmbient[4] = { 0.5, 0.5, 0.5, 1.0 };GLfloat lightDiffuse[4] = { 1.0, 1.0, 1.0, 1.0 };GLfloat lightPosition[4] = { 0.0, 0.0, 2.0, 1.0 };
The process of creating a light source is exactly the same as that of creating a color source. The first three parameters are RGB three-color components, and the last one is the alpha channel parameter.
Therefore, the first line of code related to lightambient gives us a half-bright (0.5) White ambient light. If there is no ambient light, the area where the light is not diffuse will become very dark.
The second line of lightdiffuse Code enables us to generate the brightest diffuse light. The maximum value of all parameters is 1.0. It will shine in front of our wooden panel and looks good.
The third line of code on lightposition allows us to save the position of the light source. The first three parameters are the same as those in gltranslate. They are the displacement on the XYZ axis in turn.
Initialize the variable in the constructor:
 
Zoom =-5.0; xspeed = yspeed = 0.0; filter = 0; islighting = false; load texture, which has been implemented in the previous tutorial, but this time it will be replaced by the texture of the wooden box. Void nehewidget: loadgltextures () {qimage Tex, Buf; If (! Buf. Load (":/data/crate.bmp") {// If loading fails, a 32-Bit green image of 128*128 is automatically generated. Qwarning ("cocould not read image file! "); Qimage dummy (128,128, qimage: format_rgb32); dummy. fill (QT: Green); Buf = dummy;} // convert to Texture Type Tex = qglwidget: converttoglformat (BUF); // create texture glgentextures (3, & texture [0]); // use a typical texture generated from the bitmap data to bind the texture name texture [0] To glbindtexture (gl_texture_2d, texture [0]) on the texture target. glteximage2d (gl_texture_2d, 0, 3, Tex. width (), Tex. height (), 0, gl_rgba, gl_unsigned_byte, Tex. BITs (); gltexparameteri (temperature, temperature, gl_nearest); gltexparameteri (temperature, temperature, gl_nearest); // bind the second texture glbindtexture (gl_texture_2d, texture [1]); gltexparameteri (gl_texture_2d, cosine, gl_linear); gltexparameteri (gl_texture_2d, cosine, gl_linear); glteximage2d (gl_texture_2d, 0, 3, Tex. width (), Tex. height (), 0, gl_rgba, gl_unsigned_byte, Tex. BITs (); // bind the third texture glbindtexture (gl_texture_2d, texture [2]); gltexparameteri (callback, callback, gl_linear); gltexparameteri (gl_texture_2d, callback, callback ); glubuild2dmipmaps (gl_texture_2d, gl_rgb, Tex. width (), Tex. height (), gl_rgba, gl_unsigned_byte, Tex. BITs ());}

After loading the BMP image, we bound the texture three times and used different filtering methods.
We used linear filtering last time, which requires high processing capability from machines, but they look good. In this lesson, we will create the first texture using the gl_nearest method. In principle, this method does not actually implement filtering. It only occupies a small amount of processing capability and looks very bad. The only benefit is that our project can run normally on both very fast and very slow machines. You will notice that gl_nearest is used in both Min and Mag. You can use gl_nearest and gl_linear together. Texture looks better, but we are more concerned about speed, so we use low quality textures. Min_filter is used when the image is drawn less than the original size of the texture. Mag_filter is used when the image is drawn much larger than the original size of the texture.
This is actually used in game programming. For example, there are many models in large scenarios, but distant models are not very important, therefore, the gl_nearest method can save a lot of resources.
The last texture uses the New Texture creation method: mipmapping! You may notice that when the image becomes small on the screen, many details will be lost. The pattern that was pretty good just now has become very ugly. After you tell OpenGL to create a mipmapped texture, OpenGL will try to create high-quality textures of different sizes. When you draw a mipmapped texture to the screen, OpenGL will select the best texture (with more details) it has created to draw, instead of simply scaling the original image (this will cause loss of details ).
Glubuild2dmipmaps can use any bitmap to create a texture. OpenGL automatically scales it to a normal size.
Modify paintgl ().
Void nehewidget: paintgl () {// clear the screen and depth cache glclear (gl_color_buffer_bit | gl_depth_buffer_bit); glloadidentity (); // move to the left half of the screen, there is enough distance to push the view to the back of the screen so that we can see all the scenes gltranslatef (0.0f, 0.0f, zoom); glrotatef (xrot, 1.0, 0.0, 0.0); glrotatef (yrot, 0.0, 1.0, 0.0); // select the texture glbindtexture (gl_texture_2d, texture [filter]); glbegin (gl_quads); glnormal3f (0.0, 0.0, 1.0 ); gltexcoord2f (0.0, 0.0); glvertex3f (-1.0,-1.0, 1.0); gltexcoord2f (1.0, 0.0); glvertex3f (1.0,-1.0, 1.0); gltexcoord2f (1.0, 1.0); glvertex3f (1.0, 1.0, 1.0); gltexcoord2f (0.0, 1.0); glvertex3f (-1.0, 1.0, 1.0); glnormal3f (0.0, 0.0,-1.0); gltexcoord2f (1.0, 0.0 ); glvertex3f (-1.0,-1.0,-1.0); gltexcoord2f (1.0, 1.0); glvertex3f (-1.0, 1.0,-1.0); gltexcoord2f (0.0, 1.0 ); glvertex3f (1.0, 1.0,-1.0); gltexcoord2f (0.0, 0.0); glvertex3f (1.0,-1.0,-1.0); glnormal3f (0.0, 1.0, 0.0); gltexcoord2f (0.0, 1.0); glvertex3f (-1.0, 1.0, -1.0); gltexcoord2f (0.0, 0.0); glvertex3f (-1.0, 1.0, 1.0); gltexcoord2f (1.0, 0.0); glvertex3f (1.0, 1.0, 1.0 ); gltexcoord2f (1.0, 1.0); glvertex3f (1.0, 1.0,-1.0); glnormal3f (0.0,-1.0, 0.0); gltexcoord2f (1.0, 1.0); glvertex3f (-1.0, -1.0,-1.0); gltexcoord2f (0. 0, 1.0); glvertex3f (1.0,-1.0,-1.0); gltexcoord2f (0.0, 0.0); glvertex3f (1.0,-1.0, 1.0); gltexcoord2f (1.0, 0.0 ); glvertex3f (-1.0,-1.0, 1.0); glnormal3f (1.0, 0.0, 0.0); gltexcoord2f (1.0, 0.0); glvertex3f (1.0,-1.0,-1.0 ); gltexcoord2f (1.0, 1.0); glvertex3f (1.0, 1.0,-1.0); gltexcoord2f (0.0, 1.0); glvertex3f (1.0, 1.0, 1.0); gltexcoord2f (0.0, 0.0 ); glvertex3f (1.0,-1.0, 1.0); glnormal3f (-1.0, 0.0, 0.0); gltexcoord2f (0.0, 0.0); glvertex3f (-1.0,-1.0,-1.0); gltexcoord2f (1.0, 0.0 ); glvertex3f (-1.0,-1.0, 1.0); gltexcoord2f (1.0, 1.0); glvertex3f (-1.0, 1.0, 1.0); gltexcoord2f (0.0, 1.0); glvertex3f (-1.0, 1.0,-1.0); glend (); xrot + = 0.3; yrot + = 0.2;} the code is similar to the previous one. Then add the code about the light source in initializegl. Gllightfv (latency, latency, lightambient); gllightfv (gl_light1, gl_diffuse, lightdiffuse); gllightfv (gl_light1, gl_position, lightposition); gllightable (gl_light1); glable (gl_lighting );
The first line sets the ambient light emitting volume. The light source gl_light1 starts to emit light. At the beginning of this lesson, we stored the ambient light in the lightambient array. Now we use this array (half-brightness ambient light ).
Next, we will set the light volume of the diffuse light. It is stored in the lightdiffuse array (white light with full brightness ).
Set the position of the light source. Location is stored in the lightposition array (right in the center of the front of the wooden box, X-0.0, Y-0.0, z direction to the observer 2 units, located outside the screen ).
Finally, we enable the No. 1 light source. We have not enabled gl_lighting, so you cannot see any light. Remember: Only setting, locating, or even enabling the light source will not work. Unless we enable gl_lighting.
Next we will use the L key on the keyboard to control the light brightness and darkness, select the texture, and control the physical distance. The idea is in the nehewidget class
Add a common control interface, manwindow receives Keyboard Events, and then controls the light through the interface.
First, add the Declaration in nehewidget. h:
     
Public: // return the status bool islighting (); // enable the light void turnonlight (); // turn off the light void turnofflight (); then in nehewidget. implementation Method in CPP: bool nehewidget: islighting () {return this-> islight;} void nehewidget: turnofflight () {This-> islight = false; gldisable (gl_light1 ); qwarning ("off");} void nehewidget: turnonlight () {This-> islight = true; glable (gl_light1); qwarning ("On ");}

In the manwindow. cpp key event function, add the following code:
         case Qt::Key_L:         if ( neheWidget->isLighting()) neheWidget->turnOffLight();         else neheWidget->turnOnLight();         neheWidget->updateGL();         qWarning("LLLL");         break;         case Qt::Key_F:         neheWidget->changeFilter();         break;         case Qt::Key_PageUp:         neheWidget->zoomOut();         break;         case Qt::Key_PageDown:         neheWidget->zoomIn();         break;
Compile and run the program to see the following results:
This is the light on status
The status of the light.
Below are different texture Filters
Iv. References

1. OpenGL reference manual, OpenGL Reference Manual

2. OpenGL programming guide, by Dave shreiner, Mason Woo, Jackie neider, Tom Davis, translated Xu Bo, published by Mechanical Industry Press

3. Win32 OpenGL programming, a blog http://blog.csdn.net/vagrxie/article/category/628716/3 of Daniel
4. "OpenGL function thinking" contains a lot of OpenGL functions of the popular interpretation of http://blog.csdn.net/shuaihj
5. "nehe OpenGL tutorial" relatively easy to understand OpenGL tutorial http://nehe.gamedev.net/

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.