OpenGL tutorial (20) Point Light Source

Source: Internet
Author: User

Original post address: http://ogldev.atspace.co.uk/www/tutorial20/tutorial20.html

In the previous tutorial, we studied the basic illumination model (Environment light, diffuse reflection light, and high light) on the premise of the direction light ). Direction light has no starting point, and all light goes in one direction. Its Intensity does not change with the increase of distance. In this tutorial, we start to study the point light source. The point light source starts from a point, which is exposed to four sides and degrades as the transmission distance increases, we will add a light source location.

Generally, the degree of attenuation of the point light source is reversed from the distance of the object. The formula is as follows:

However, when the distance is relatively small, the above formula does not work well in 3D graphics. Therefore, when calculating the attenuation coefficient of the point light source, the following formula is often used: it includes three factors, constant factor, linear factor, and quadratic factor.

Next we will summarize the steps required to calculate the point light source:

  1. The computing of ambient light is the same as that of ambient light.
  2. In the world coordinate system, the point (pixel) to the direction of the light source is calculated as the direction vector of the light source.
  3. Calculate the distance from the pixel to the light source to calculate the attenuation factor.
  4. Add ambient light, diffuse light, and highlights, and multiply them by the attenuation factor.
 

Lighting_technique.h

struct BaseLight
{
Vector3f Color;
float AmbientIntensity;
float DiffuseIntensity;
};
.
.
.
struct PointLight : public BaseLight
{
Vector3f Position;
struct
{
float Constant;
float Linear;
float Exp;
} Attenuation;
}

Create a baselight class, abstract the common attributes of the light source, and then derive the point light source and the direction light source. They all have their own unique attributes, such as the position and attenuation coefficient of the point light source, the direction of light.

Lighting_technique.h

void SetPointLights(unsigned int NumLights, const PointLight* pLights);

In addition to the demonstration point light source, this tutorial also implements multiple light sources, including one direction light and Multiple electric light sources.

struct {
GLuint Color;
GLuint AmbientIntensity;
GLuint DiffuseIntensity;
GLuint Position;
struct
{
GLuint Constant;
GLuint Linear;
GLuint Exp;
} Atten;
} m_pointLightsLocation[MAX_POINT_LIGHTS];

We use a structure array to implement multiple point light sources. max_point_lights is a constant value and specifies the number of light sources. The default value is 2.

Lighting_technique.cpp

vec4 CalcLightInternal(BaseLight Light, vec3 LightDirection, vec3 Normal)
{
vec4 AmbientColor = vec4(Light.Color, 1.0f) * Light.AmbientIntensity;
float DiffuseFactor = dot(Normal, -LightDirection);
vec4 DiffuseColor = vec4(0, 0, 0, 0);
vec4 SpecularColor = vec4(0, 0, 0, 0);
if (DiffuseFactor > 0) {
DiffuseColor = vec4(Light.Color, 1.0f) * Light.DiffuseIntensity * DiffuseFactor;
vec3 VertexToEye = normalize(gEyeWorldPos - WorldPos0);
vec3 LightReflect = normalize(reflect(LightDirection, Normal));
float SpecularFactor = dot(VertexToEye, LightReflect);
SpecularFactor = pow(SpecularFactor, gSpecularPower);
if (SpecularFactor > 0) {
SpecularColor = vec4(Light.Color, 1.0f) *
gMatSpecularIntensity * SpecularFactor;
}
}
return (AmbientColor + DiffuseColor + SpecularColor);
}

The above is a general function for calculating the point light source. We can call this function to calculate the point light source and the direction light.

vec4 CalcDirectionalLight(vec3 Normal)
{
return CalcLightInternal(gDirectionalLight.Base, gDirectionalLight.Direction, Normal);
}

The above is to call this function to implement direction light computing.

vec4 CalcPointLight(int Index, vec3 Normal)
{
vec3 LightDirection = WorldPos0 - gPointLights[Index].Position;
float Distance = length(LightDirection);
LightDirection = normalize(LightDirection);
vec4 Color = CalcLightInternal(gPointLights[Index].Base, LightDirection, Normal);
float Attenuation = gPointLights[Index].Atten.Constant +
gPointLights[Index].Atten.Linear * Distance +
gPointLights[Index].Atten.Exp * Distance * Distance;
return Color / Attenuation;
}

The above code calls this function to calculate the point light source.

void main()
{
vec3 Normal = normalize(Normal0);
vec4 TotalLight = CalcDirectionalLight(Normal);
for (int i = 0 ; i < gNumPointLights ; i++) {
TotalLight += CalcPointLight(i, Normal);
}
FragColor = texture2D(gSampler, TexCoord0.xy) * TotalLight;
}

When the final pixel color is output, we add all the effects of the light source and use the texture color for modulation.

void LightingTechnique::SetPointLights(unsigned int NumLights, const PointLight* pLights) {
glUniform1i(m_numPointLightsLocation, NumLights);
for (unsigned int i = 0 ; i < NumLights ; i++) {
glUniform3f(m_pointLightsLocation[i].Color, pLights[i].Color.x, pLights[i].Color.y, pLights[i].Color.z);
glUniform1f(m_pointLightsLocation[i].AmbientIntensity, pLights[i].AmbientIntensity);
glUniform1f(m_pointLightsLocation[i].DiffuseIntensity, pLights[i].DiffuseIntensity);
glUniform3f(m_pointLightsLocation[i].Position, pLights[i].Position.x, pLights[i].Position.y, pLights[i].Position.z);
glUniform1f(m_pointLightsLocation[i].Atten.Constant, pLights[i].Attenuation.Constant);
glUniform1f(m_pointLightsLocation[i].Atten.Linear, pLights[i].Attenuation.Linear);
glUniform1f(m_pointLightsLocation[i].Atten.Exp, pLights[i].Attenuation.Exp);
}
}

The above is to assign values to various uniform variables.

After the program is executed, the interface is shown below. We use two point light sources:

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.