lighting

说明

内容

  • Rendering a geometry with a vertex diffuse lighting
  • Rendering a geometry with a fragment specular lighting
  • Rendering a normal mapped geometry
  • Drawing a reflective and refractive geometry using cubemaps
  • Adding shadows to the scene

介绍

Rendering a geometry with a vertex diffuse lighting

vertex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

layout( location = 0 ) in vec4 app_position;
layout( location = 1 ) in vec3 app_normal;
layout( set = 0, binding = 0 ) uniform UniformBuffer {
mat4 ModelViewMatrix;
mat4 ProjectionMatrix;
};
layout( location = 0 ) out float vert_color;
void () {
gl_Position = ProjectionMatrix * ModelViewMatrix *
app_position;
vec3 normal = mat3( ModelViewMatrix ) * app_normal;
vert_color = max( 0.0, dot( normal, vec3( 0.58, 0.58, 0.58 ) )
) + 0.1;
}

fragment

1
2
3
4
5
6

layout( location = 0 ) in float vert_color;
layout( location = 0 ) out vec4 frag_color;
void () {
frag_color = vec4( vert_color );
}

Rendering a geometry with a fragment specular lighting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

layout( location = 0 ) in vec3 vert_position;
layout( location = 1 ) in vec3 vert_normal;
layout( push_constant ) uniform LightParameters {
vec4 Position;
} Light;
layout( location = 0 ) out vec4 frag_color;
void () {
vec3 normal_vector = normalize( vert_normal );
vec3 light_vector = normalize( Light.Position.xyz -
vert_position );
float diffuse_term = max( 0.0, dot( normal_vector, light_vector
) );
frag_color = vec4( diffuse_term + 0.1 );
if( diffuse_term > 0.0 ) {
vec3 view_vector = normalize( vec3( 0.0, 0.0, 0.0 ) -
vert_position.xyz );
vec3 half_vector = normalize( view_vector + light_vector );
float shinniness = 60.0;
float specular_term = pow( dot( half_vector, normal_vector ),
shinniness );
frag_color += vec4( specular_term );
}
}

normal mapeed geometry

vertex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

layout( location = 0 ) in vec4 app_position;
layout( location = 1 ) in vec3 app_normal;
layout( location = 2 ) in vec2 app_texcoord;
layout( location = 3 ) in vec3 app_tangent;
layout( location = 4 ) in vec3 app_bitangent;
layout( set = 0, binding = 0 ) uniform UniformBuffer {
mat4 ModelViewMatrix;
mat4 ProjectionMatrix;
};
layout( location = 0 ) out vec3 vert_position;
layout( location = 1 ) out vec2 vert_texcoord;
layout( location = 2 ) out vec3 vert_normal;
layout( location = 3 ) out vec3 vert_tanget;
layout( location = 4 ) out vec3 vert_bitanget;
void () {
vec4 position = ModelViewMatrix * app_position;
gl_Position = ProjectionMatrix * position;
vert_position = position.xyz;
vert_texcoord = app_texcoord;
vert_normal = mat3( ModelViewMatrix ) * app_normal;
vert_tanget = mat3( ModelViewMatrix ) * app_tangent;
vert_bitanget = mat3( ModelViewMatrix ) * app_bitangent;
}

fragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

layout( location = 0 ) in vec3 vert_position;
layout( location = 1 ) in vec2 vert_texcoord;
layout( location = 2 ) in vec3 vert_normal;
layout( location = 3 ) in vec3 vert_tanget;
layout( location = 4 ) in vec3 vert_bitanget;
layout( set = 0, binding = 1 ) uniform sampler2D ImageSampler;
layout( push_constant ) uniform LightParameters {
vec4 Position;
} Light;
layout( location = 0 ) out vec4 frag_color;
void () {
vec3 normal = 2 * texture( ImageSampler, vert_texcoord ).rgb -
1.0;
vec3 normal_vector = normalize( mat3( vert_tanget,
vert_bitanget, vert_normal) * normal );
vec3 light_vector = normalize( Light.Position.xyz -
vert_position );
float diffuse_term = max( 0.0, dot( normal_vector, light_vector
) ) * max( 0.0, dot( vert_normal, light_vector ) );
frag_color = vec4( diffuse_term + 0.1 );
if( diffuse_term > 0.0 ) {
vec3 half_vector = normalize(normalize( -vert_position.xyz )
+ light_vector);
float specular_term = pow( dot( half_vector, normal_vector ),
60.0 );
frag_color += vec4( specular_term );
}
}

Drawing a reflective and refractive geometry using cubemaps

order:+X,-X,+Y,-Y,+Z,-Z

vertex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#version 450
layout( location = 0 ) in vec4 app_position;
layout( location = 1 ) in vec3 app_normal;
layout( set = 0, binding = 0 ) uniform UniformBuffer {
mat4 ModelViewMatrix;
mat4 ProjectionMatrix;
};
layout( location = 0 ) out vec3 vert_position;
layout( location = 1 ) out vec3 vert_normal;
void main() {
vert_position = app_position.xyz;
vert_normal = app_normal;
gl_Position = ProjectionMatrix * ModelViewMatrix *
app_position;
}

fragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#version 450
layout( location = 0 ) in vec3 vert_position;
layout( location = 1 ) in vec3 vert_normal;
layout( set = 0, binding = 1 ) uniform samplerCube Cubemap;
layout( push_constant ) uniform LightParameters {
vec4 Position;
} Camera;
layout( location = 0 ) out vec4 frag_color;
void main() {
vec3 view_vector = vert_position - Camera.Position.xyz;
float angle = smoothstep( 0.3, 0.7, dot( normalize( -
view_vector ), vert_normal ) );
vec3 reflect_vector = reflect( view_vector, vert_normal );
vec4 reflect_color = texture( Cubemap, reflect_vector );
vec3 refrac_vector = refract( view_vector, vert_normal, 0.3 );
vec4 refract_color = texture( Cubemap, refrac_vector );
frag_color = mix( reflect_color, refract_color, angle );
}

add shadow to the scene