-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraw_world_vshader.glsl
106 lines (83 loc) · 2.07 KB
/
draw_world_vshader.glsl
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#version 330 core
in vec3 position;
in vec2 texCoord;
in vec3 normal;
uniform struct Transform
{
mat4 viewProjection;
vec3 viewPosition;
}
transform;
uniform struct PointLight
{
vec3 position;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 attenuation;
}
pointLight;
uniform struct Material
{
vec4 emission;
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
}
material;
uniform vec2 meshViewFirst;
uniform vec2 meshViewSize;
uniform sampler2D texGeometry;
out Vertex
{
flat vec3 normal;
flat vec4 specular;
vec3 position;
vec4 light;
vec2 texCoord;
float under;
}
vertex;
vec4 calcLight(vec3 real_normal, vec3 to_camera)
{
vec4 ambient = material.ambient * pointLight.ambient;
vec4 diffuse = material.diffuse * pointLight.diffuse;
vec3 to_light = pointLight.position - position;
vec3 to_light_norm = normalize(to_light);
vec3 to_camera_norm = normalize(to_camera);
float to_light_length = length(to_light);
float NdotL = max(dot(real_normal, to_light_norm), 0.0);
float attenuation = clamp(1.0 / (
pointLight.attenuation[0] +
pointLight.attenuation[1] * to_light_length +
pointLight.attenuation[2] * to_light_length *
to_light_length), 0.0, 1.0);
return attenuation * (
material.emission +
ambient +
diffuse * NdotL);
}
float calcZ(vec2 xy)
{
vec2 tc = (xy - meshViewFirst) / meshViewSize;
float z = texture(texGeometry, tc).r;
return z;
}
void main(void)
{
vec3 to_camera = transform.viewPosition - position;
vec3 real_normal = normal;
if (dot(to_camera, real_normal) < 0)
{
real_normal = -real_normal;
}
vertex.normal = real_normal;
vertex.specular = material.specular * pointLight.specular;
vertex.position = position;
vertex.light = calcLight(real_normal, to_camera);
vertex.texCoord = texCoord;
vertex.under = transform.viewPosition.z -
calcZ(transform.viewPosition.xy);
gl_Position = transform.viewProjection * vec4(position, 1.0);
}