How to do it...

To render a scene with diffuse image-based lighting, the process is fairly simple. We simply need to read from our diffuse map using the normal vector.

The vertex shader just converts the position and normal to world coordinates and passes them along:

// Input attributes...

out vec3 Position; // world coords
out vec3 Normal; // world coords.
out vec2 TexCoord;

// Matrix uniforms ...

void main() {
TexCoord = VertexTexCoord;
Position = (ModelMatrix * vec4(VertexPosition,1)).xyz;
Normal = normalize( ModelMatrix * vec4(VertexNormal,0) ).xyz;
gl_Position = MVP * vec4(VertexPosition,1.0);
}

The fragment shader then uses the diffuse convolution map to determine the value of the integral, multiplies it by a color taken from a texture map, and applies gamma correction:

const float PI = 3.14159265358979323846;

in vec3 Position;
in vec3 Normal;
in vec2 TexCoord;

layout(binding=0) uniform samplerCube DiffConvTex;
layout(binding=1) uniform sampler2D ColorTex;

layout( location = 0 ) out vec4 FragColor;

const float gamma = 2.2;

void main() {
vec3 n = normalize(Normal);

// Look up reflected light from diffuse cube map
vec3 light = texture(DiffConvTex, n).rgb;
vec3 color = texture(ColorTex, TexCoord).rgb;
color = pow(color, vec3(gamma)); // decode
color *= light;

color = pow( color, vec3(1.0/gamma)); // gamma encode

FragColor = vec4( color, 1 );
}

Results for the environment shown in the introduction to this recipe are shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.151.141