Win32 OpenGL programming (11) Illumination

Source: Internet
Author: User
Tags mercurial
Document directory
  • 1. Define the object's normal
  • 2. Create and select the light source, and set the position
  • 3. Create and select a Illumination Model
  • 4. Define material attributes of an object

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie


Discuss newsgroups and documents

Technorati label: OpenGL
, 3D
, Graphic
, Lighting
, Programming
, Win32

I recently followed suit and read the mind map. I used xmind to draw a mind map for this section. In fact, I feel that this book is not necessarily useless, however, in order to display the useful information, it seems too exaggerated. Basically, I agree that a picture is better than a thousand words .... In addition, xmind is relatively easy to use, especially for uploading and sharing. The basic edition is also open-source.

 

The above figure is a little simple to explain the concept, but it is used to sort the context and the concept of memories is quite useful during review.

 

God said, if there is light, there is light. ---- The old testament to Genesis

When OpenGL is used, the programmer can temporarily assume the role of God (in fact, he has made a self-proclaimed God's commandment). If you say to the computer that there is light, then there is light when it is displayed, haha. Of course, you need to use the computer language.

Relatively speaking, light is a relatively simple concept in OpenGL, but it is complicated to use because light is a factor that easily affects the display effect, therefore, the usage of illumination is too large, resulting in a considerable number of customization methods available, which deepens the complexity of use. Basically, we follow the core concepts of understanding, it is a good way to look up the document to understand the specific usage and significance of each function when necessary. Of course, if you want to be proficient, you may have to understand the situation of various lighting applications. The above Mind Map is a brief list of core concepts, which will be discussed below.

 

Why light?

Why did God create light? God does not see anything ?........ Or because God thinks that darkness is not good and does not want to stay in the darkness ..... Only God knows

Why do we need light? Because our world has the Light created by God, and OpenGL is essentially a modeling of the real world, since the real world has the sun, the light, how can OpenGL, which simulates the real world, be light-free? (The above is just boring nonsense)

When I used to draw 3D images, I intentionally set each side of the object to a different color (for example, the triangle cone ), in this way, we can clearly see that this is a 3D image. Why? Let's first look at what we see if I didn't do that. Here we use Win32 OpenGL programming (6) to step into the 3D world.
The glsmoothcolorpyramid example in the article is the revised blueprint (for the original source code, see/2009-10-21/glsmoothcolorpyramid/), and set all four colors to red.

Change the color array in the source code to the following format:

static GLfloat fPyramidColors[] = { 1.0, 0.0, 0.0,    1.0, 0.0, 0.0,    1.0, 0.0, 0.0,    1.0, 0.0, 0.0};

Let's look at the effect.

To save space, only key clips are posted. For the complete source code, see the 2009-11-9/glsimplecolorpyramid/directory of the source code of my blog. For details about how to obtain the complete source code of the blog, see the article.

In fact, even if the entire triangle cone is being rotated in 3D mode, the effect we see is similar to that of a triangle of red paper. The 3D effect disappears. Obviously, this is a problem. Can't it be said that the 3D effect of a solid color object will be affected? This is the simplest layer we need. Let's talk about a triangle cone. Let's look at a ball. This example uses the glut function to draw a solid body. In the future, we will not use all our complicated manual code to create the simple triangle cone. As an explanation of some concepts, the triangle cone is a little too simple. (Of course, it will still be used when it can be used) because of the use of glut, it is very easy to draw the ball (in fact, it is not difficult), just set the red, and then use

glutSolidSphere(0.5, 30, 16);
You can draw the following ball with a radius of 1/4 in the window width.


Someone can tell that the one above is the ball? We only see a circle. in mathematics, we often abstract the shape of the ball in a certain direction into a circle, but in reality, we can easily see that a ball is a ball rather than a circle. Why? Light ah, because of the existence of light, the same ball in our eyes will not actually be as pure red as above, the whole ball is pure red, will be based on the direction of light, different factors such as strength lead to different display of different parts. The following is an example of a red sphere with illumination added. (After uploading uku, the color changes -_-! The possible reason is that ucool performed two compression encoding operations after the upload, resulting in color confusion. The light source movement effect is basically the same. You can download the source code and compile it before running it)

In order to save space, only key clips are posted. For the complete source code, see the 2009-11-9/lightsimple/directory of the source code of my blog. For more information about obtaining the complete source code of my blog, see the article.

 

The main source code is as follows:

// Starts OpenGL initialization.
Void sceneinit (int w, int H)
{
Glenum err = glewinit ();
If (Err! = Glew_ OK)
{
MessageBox (null, _ T ("error"), _ T ("glew init failed."), mb_ OK );
Exit (-1 );
}

Glfloat mat_specular [] = {1.0, 0.0, 0.0, 1.0 };
Glfloat mat_shininess [] ={ 50.0 };
Glfloat mat_ambient [] = {1.0, 0.0, 0.0, 1.0 };
Glfloat mat_diffuse [] = {1.0, 0.0, 0.0, 1.0 };

Glmaterialfv (gl_front, gl_specular, mat_specular );
Glmaterialfv (gl_front, gl_shininess, mat_shininess );
Glmaterialfv (gl_front, gl_ambient, mat_ambient );
Glmaterialfv (gl_front, gl_diffuse, mat_diffuse );

Glable (gl_lighting );
Glable (gl_light0 );

Glcolor3f (0.0, 0.0, 0.0 );
}

// Do all the plotting work here
Void sceneshow (glvoid)
{
Static glfloat light_position [] = {1.0, 0.0,-3.0, 0.0 };
Static glfloat angle = 1.0;

Glclear (gl_color_buffer_bit); // clear the color buffer.

Glpushmatrix ();
Glrotatef (angle, 0.0, 1.0, 0.0 );
Gllightfv (gl_light0, gl_position, light_position );
Glpopmatrix ();
Angle + = 1.0;

Glusolidsphere (0.5, 30, 16 );

Glflush ();
}

The order in which illumination is enabled is as shown in the figure above. The steps are described as follows:

 

1. Define the object's normal

This step is actually very important, but we cannot see it in this example, because the glusolidsphere function has completed this definition, the definition of the normal is a bit similar to that we used to determine the surface of an object using the order specified by the vertices of the inverse/Timing needle to determine the front and back of an object, the normal defines the direction of the object relative to the light source when the light is incident. This is an extra small question and will be discussed later. For details, refer to OpenGL.
Section 2.5 of the programming guide. After you know the concept of the normal, you need to call the glnormal * function, but the normal calculation is not always so easy ...... For example, the sphere in this example is okay. This example saves this step because it uses a ready-made function.

 

2. Create and select the light source, and set the position

Glenable
(Gl_lighting
);

Glenable
(Gl_light0
);

The 0th light source is enabled. The default color is white and the position is (0.0, 0.0, 1.0, 0.0 ). We use the default color.

The gllight * function is a specialized lighting configuration control function.

OpenGL programming guide
":

Gllight-set light source parameters

C Specification

Void gllightf (glenum light,

Glenum pname,

Glfloat PARAM );

Void gllighti (glenum light,

Glenum pname,

Glint PARAM );

Parameters

Light

Specifies a light.

The number of lights depends on the implementation,

But at least eight lights are supported.

They are identified by symbolic names of the form gl_light

I,

Where I ranges from 0 to the value of gl_max_lights-1.

Pname

Specifies a single-valued Light Source Parameter for light.

Gl_spot_exponent,

Gl_spot_cutoff,

Gl_constant_attenuation,

Gl_linear_attenuation, and

Gl_quadratic_attenuation are accepted.

Param

Specifies the value that parameter pname of light source light

Will be set.

In fact, this function is far less simple than it looks. As mentioned above, because illumination is a factor that easily affects the display effect, there are many lighting usage, the above pname parameter specifies n values. However, the method used in this example is very simple. Just use this function to set the position of the 0 light source. The classification of illumination actually includes environmental light, scattered light, mirror light, and emitting light (actually refers to the emission color of an object, emissive color.

glLightfv(GL_LIGHT0, GL_POSITION, light_position);

Because the model view is used to specify the position of the illumination model, we can also use the model transformation method to specify the position of the light source, just as the light source is a common object, in this example, the function of rotating the model view is used to rotate the position of the light source. The rotation is based on the Y axis, and the effect is shown in the video, similar to the animation of the month.

3. Create and select a Illumination Model

There are two models of illumination in OpenGL, one is the infinite observer mode and the other is the local observer mode, which is set through the gllightmodel * function. The default is the infinite Observer Model, which is not modified here, that is, the default value is used. The difference is whether to consider the changes caused by the observer's position on the object's viewing angle. The local observer model is more realistic, but because of additional illumination calculations, performance will also be affected .... According to OpenGL management, the default choice is usually better performance, rather than better performance. However, I cannot tell the difference in this simple example.

OpenGL programming guide
":

Gllightmodel-set the Lighting Model Parameters

C Specification

Void gllightmodelf (glenum pname,

Glfloat PARAM );

Void gllightmodeli (glenum pname,

Glint PARAM );

Parameters

Pname

Specifies a single-valued lighting model parameter.

Gl_light_model_local_viewer,

Gl_light_model_color_control, and

Gl_light_model_two_side are accepted.

Param

Specifies the value that param will be set.

We can use

Gllightmodeli (gl_light_model_local_viewer, gl_true );

To the local observer model.

4. Define material attributes of an object

Light defines the light source rather than how the object reflects light. It cannot be used in OpenGL. The material attribute of an object determines how it reacts to light, which corresponds to the property of the light source, there are also several material attributes related to reflected light that can be set. We use glmateriall * To set material attributes.

OpenGL programming guide
":

Glmaterial-specify material parameters for the Lighting Model

C Specification

Void glmaterialfv (glenum face,

Glenum pname,

Const glfloat * Params );

Void glmaterialiv (glenum face,

Glenum pname,

Const glint * Params );

Parameters

Face

Specifies which face or faces are being updated.

Must be one

Gl_front,

Gl_back, or

Gl_front_and_back.

Pname

Specifies the material parameter of the face or faces that is being updated.

Must be one

Gl_ambient,

Gl_diffuse,

Gl_specular,

Gl_emission,

Gl_shininess,

Gl_ambient_and_diffuse, or

Gl_color_indexes.

Params

Specifies a pointer to the value or values that pname will be set.

 

In the above example:

 

GLfloat mat_specular[] = { 1.0, 0.0, 0.0, 1.0 };GLfloat mat_shininess[] = { 50.0 };GLfloat mat_ambient[] = { 1.0, 0.0, 0.0, 1.0 };GLfloat mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);


In this case, eight rows define the attributes of the material in Step 4, and we use gl_front to define the front-side illumination (that is, no illumination on the back by default). gl_specular indicates the mirror color, and gl_shininess indicates the mirror index, gl_ambient indicates the environment color, and gl_diffuse indicates the scattering color. The scattering color and environmental color affect the scattered light and ambient light color of the reversed color of the object. When there is direct illumination (corresponding to the light source attribute, the main perception of human body is mainly scattering color. When there is no light, the Environmental color will take an important place. The mirror color determines the highlights produced by the mirror reflection. The gl_shininess parameter sets the size and brightness of the highlights. The emitted light color will make the objects glow slightly (this example is not used ). Because of the complexity, the above text only describes a concept. For specific parameters, refer to OpenGL programming guide.
And it is better to adjust different numeric values. Among them, OpenGL programming guide
The OpenGL tutorial of Nate Robin recommended in this book
There are two examples in the teaching program, which are related to the light and can experience the effect displayed when the value changes. If you do not want to program and debug the program yourself, you can use the lightposition and lightmateria examples in the tutorial.

For other articles in this series, see the OpenGL topic "Win32 OpenGL series topics ".
"

References

1. OpenGL Reference Manual
, OpenGL Reference Manual

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

3. nehe OpenGL tutorials, In the http://nehe.gamedev.net/
You can find the tutorials and related code to download. (The PDF version of the tutorials is available) nehe also developed an object-oriented framework. As a demo program, this framework is very suitable. There are also Chinese Versions
Take all the necessary information.

4. OpenGL getting started, by eastcowboy, this is a good tutorial I have found on the Internet. It is quite complete and popular. This is the first address: http://bbs.pfan.cn/post-184355.html

Complete source code retrieval instructions

Due to space limitations, this article generally only posts the main focus of the Code, the full version of the Code with a project (or makefile) (if any) can be downloaded in Google code using mercurial. The article is stored in different directories on the date published by the blog post. Use mercurial to clone the following database:

Https://blog-sample-code.jtianling.googlecode.com/hg/

For how to use mercurial, see Introduction and brief introduction to the distributed and next-generation version control system mercurial.
"

If you only want to browse all the code, you can go to Google Code to view it. the following URL:

Http://code.google.com/p/jtianling/source/browse? Repo = blog-Sample-code

 

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

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.