-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture_program.cpp
More file actions
110 lines (98 loc) · 3.79 KB
/
texture_program.cpp
File metadata and controls
110 lines (98 loc) · 3.79 KB
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
107
108
109
110
#include "texture_program.hpp"
#include "compile_program.hpp"
#include "gl_errors.hpp"
TextureProgram::TextureProgram() {
program = compile_program(
"#version 330\n"
"uniform mat4 object_to_clip;\n"
"uniform mat4x3 object_to_light;\n"
"uniform mat3 normal_to_light;\n"
"uniform mat4 light_to_spot;\n"
"layout(location=0) in vec4 Position;\n" //note: layout keyword used to make sure that the location-0 attribute is always bound to something
"in vec3 Normal;\n"
"in vec4 Color;\n"
"in vec2 TexCoord;\n"
"out vec3 position;\n"
"out vec3 normal;\n"
"out vec4 color;\n"
"out vec2 texCoord;\n"
"out vec4 spotPosition;\n"
"void main() {\n"
" gl_Position = object_to_clip * Position;\n"
" position = object_to_light * Position;\n"
" spotPosition = light_to_spot * vec4(position, 1.0);\n"
" normal = normal_to_light * Normal;\n"
" color = Color;\n"
" texCoord = TexCoord;\n"
"}\n"
,
"#version 330\n"
"uniform vec3 sun_direction;\n"
"uniform vec3 sun_color;\n"
"uniform vec3 sky_direction;\n"
"uniform vec3 sky_color;\n"
"uniform vec3 spot_position;\n"
"uniform vec3 spot_direction;\n"
"uniform vec3 spot_color;\n"
"uniform vec2 spot_outer_inner;\n"
"uniform float glow_amt;\n"
"uniform sampler2D tex;\n"
"uniform sampler2DShadow spot_depth_tex;\n"
"in vec3 position;\n"
"in vec3 normal;\n"
"in vec4 color;\n"
"in vec2 texCoord;\n"
"in vec4 spotPosition;\n"
"layout(location = 0) out vec4 fragColor;\n"
"layout(location = 1) out vec4 bloomColor;\n"
"void main() {\n"
" vec3 total_light = vec3(0.0, 0.0, 0.0);\n"
" vec3 n = normalize(normal);\n"
" { //sky (hemisphere) light:\n"
" vec3 l = sky_direction;\n"
" float nl = 0.5 + 0.5 * dot(n,l);\n"
" total_light += nl * sky_color;\n"
" }\n"
" { //sun (directional) light:\n"
" vec3 l = sun_direction;\n"
" float nl = max(0.0, dot(n,l));\n"
" total_light += nl * sun_color;\n"
" }\n"
" { //spot (point with fov + shadow map) light:\n"
" vec3 l = normalize(spot_position - position);\n"
" float nl = max(0.0, dot(n,l));\n"
" //TODO: look up shadow map\n"
" float d = dot(l,-spot_direction);\n"
" float amt = smoothstep(spot_outer_inner.x, spot_outer_inner.y, d);\n"
" float shadow = textureProj(spot_depth_tex, spotPosition);\n"
" total_light += shadow * nl * amt * spot_color;\n"
" }\n"
" fragColor = texture(tex, texCoord) * vec4(color.rgb * 1.08f*total_light, color.a);\n"
" bloomColor = vec4(glow_amt*fragColor.rgb, 1.0);\n"
//" fragColor = vec4(vec3(gl_FragCoord.z), 1.0);\n"
"}\n"
);
glow_amt_float = glGetUniformLocation(program, "glow_amt");
object_to_clip_mat4 = glGetUniformLocation(program, "object_to_clip");
object_to_light_mat4x3 = glGetUniformLocation(program, "object_to_light");
normal_to_light_mat3 = glGetUniformLocation(program, "normal_to_light");
sun_direction_vec3 = glGetUniformLocation(program, "sun_direction");
sun_color_vec3 = glGetUniformLocation(program, "sun_color");
sky_direction_vec3 = glGetUniformLocation(program, "sky_direction");
sky_color_vec3 = glGetUniformLocation(program, "sky_color");
spot_position_vec3 = glGetUniformLocation(program, "spot_position");
spot_direction_vec3 = glGetUniformLocation(program, "spot_direction");
spot_color_vec3 = glGetUniformLocation(program, "spot_color");
spot_outer_inner_vec2 = glGetUniformLocation(program, "spot_outer_inner");
light_to_spot_mat4 = glGetUniformLocation(program, "light_to_spot");
glUseProgram(program);
GLuint tex_sampler2D = glGetUniformLocation(program, "tex");
glUniform1i(tex_sampler2D, 0);
GLuint spot_depth_tex_sampler2D = glGetUniformLocation(program, "spot_depth_tex");
glUniform1i(spot_depth_tex_sampler2D, 1);
glUseProgram(0);
GL_ERRORS();
}
Load< TextureProgram > texture_program(LoadTagInit, [](){
return new TextureProgram();
});