forked from 15-466/15-466-f23-base5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LitColorTextureProgram.cpp
144 lines (122 loc) · 5.37 KB
/
LitColorTextureProgram.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "LitColorTextureProgram.hpp"
#include "gl_compile_program.hpp"
#include "gl_errors.hpp"
Scene::Drawable::Pipeline lit_color_texture_program_pipeline;
Load< LitColorTextureProgram > lit_color_texture_program(LoadTagEarly, []() -> LitColorTextureProgram const * {
LitColorTextureProgram *ret = new LitColorTextureProgram();
//----- build the pipeline template -----
lit_color_texture_program_pipeline.program = ret->program;
lit_color_texture_program_pipeline.OBJECT_TO_CLIP_mat4 = ret->OBJECT_TO_CLIP_mat4;
lit_color_texture_program_pipeline.OBJECT_TO_LIGHT_mat4x3 = ret->OBJECT_TO_LIGHT_mat4x3;
lit_color_texture_program_pipeline.NORMAL_TO_LIGHT_mat3 = ret->NORMAL_TO_LIGHT_mat3;
/* This will be used later if/when we build a light loop into the Scene:
lit_color_texture_program_pipeline.LIGHT_TYPE_int = ret->LIGHT_TYPE_int;
lit_color_texture_program_pipeline.LIGHT_LOCATION_vec3 = ret->LIGHT_LOCATION_vec3;
lit_color_texture_program_pipeline.LIGHT_DIRECTION_vec3 = ret->LIGHT_DIRECTION_vec3;
lit_color_texture_program_pipeline.LIGHT_ENERGY_vec3 = ret->LIGHT_ENERGY_vec3;
lit_color_texture_program_pipeline.LIGHT_CUTOFF_float = ret->LIGHT_CUTOFF_float;
*/
//make a 1-pixel white texture to bind by default:
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
std::vector< glm::u8vec4 > tex_data(1, glm::u8vec4(0xff));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
lit_color_texture_program_pipeline.textures[0].texture = tex;
lit_color_texture_program_pipeline.textures[0].target = GL_TEXTURE_2D;
return ret;
});
LitColorTextureProgram::LitColorTextureProgram() {
//Compile vertex and fragment shaders using the convenient 'gl_compile_program' helper function:
program = gl_compile_program(
//vertex shader:
"#version 330\n"
"uniform mat4 OBJECT_TO_CLIP;\n"
"uniform mat4x3 OBJECT_TO_LIGHT;\n"
"uniform mat3 NORMAL_TO_LIGHT;\n"
"in vec4 Position;\n"
"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"
"void main() {\n"
" gl_Position = OBJECT_TO_CLIP * Position;\n"
" position = OBJECT_TO_LIGHT * Position;\n"
" normal = NORMAL_TO_LIGHT * Normal;\n"
" color = Color;\n"
" texCoord = TexCoord;\n"
"}\n"
,
//fragment shader:
"#version 330\n"
"uniform sampler2D TEX;\n"
"uniform int LIGHT_TYPE;\n"
"uniform vec3 LIGHT_LOCATION;\n"
"uniform vec3 LIGHT_DIRECTION;\n"
"uniform vec3 LIGHT_ENERGY;\n"
"uniform float LIGHT_CUTOFF;\n"
"in vec3 position;\n"
"in vec3 normal;\n"
"in vec4 color;\n"
"in vec2 texCoord;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" vec3 n = normalize(normal);\n"
" vec3 e;\n"
" if (LIGHT_TYPE == 0) { //point light \n"
" vec3 l = (LIGHT_LOCATION - position);\n"
" float dis2 = dot(l,l);\n"
" l = normalize(l);\n"
" float nl = max(0.0, dot(n, l)) / max(1.0, dis2);\n"
" e = nl * LIGHT_ENERGY;\n"
" } else if (LIGHT_TYPE == 1) { //hemi light \n"
" e = (dot(n,-LIGHT_DIRECTION) * 0.5 + 0.5) * LIGHT_ENERGY;\n"
" } else if (LIGHT_TYPE == 2) { //spot light \n"
" vec3 l = (LIGHT_LOCATION - position);\n"
" float dis2 = dot(l,l);\n"
" l = normalize(l);\n"
" float nl = max(0.0, dot(n, l)) / max(1.0, dis2);\n"
" float c = dot(l,-LIGHT_DIRECTION);\n"
" nl *= smoothstep(LIGHT_CUTOFF,mix(LIGHT_CUTOFF,1.0,0.1), c);\n"
" e = nl * LIGHT_ENERGY;\n"
" } else { //(LIGHT_TYPE == 3) //directional light \n"
" e = max(0.0, dot(n,-LIGHT_DIRECTION)) * LIGHT_ENERGY;\n"
" }\n"
" vec4 albedo = texture(TEX, texCoord) * color;\n"
" fragColor = vec4(e*albedo.rgb, albedo.a);\n"
"}\n"
);
//As you can see above, adjacent strings in C/C++ are concatenated.
// this is very useful for writing long shader programs inline.
//look up the locations of vertex attributes:
Position_vec4 = glGetAttribLocation(program, "Position");
Normal_vec3 = glGetAttribLocation(program, "Normal");
Color_vec4 = glGetAttribLocation(program, "Color");
TexCoord_vec2 = glGetAttribLocation(program, "TexCoord");
//look up the locations of uniforms:
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");
LIGHT_TYPE_int = glGetUniformLocation(program, "LIGHT_TYPE");
LIGHT_LOCATION_vec3 = glGetUniformLocation(program, "LIGHT_LOCATION");
LIGHT_DIRECTION_vec3 = glGetUniformLocation(program, "LIGHT_DIRECTION");
LIGHT_ENERGY_vec3 = glGetUniformLocation(program, "LIGHT_ENERGY");
LIGHT_CUTOFF_float = glGetUniformLocation(program, "LIGHT_CUTOFF");
GLuint TEX_sampler2D = glGetUniformLocation(program, "TEX");
//set TEX to always refer to texture binding zero:
glUseProgram(program); //bind program -- glUniform* calls refer to this program now
glUniform1i(TEX_sampler2D, 0); //set TEX to sample from GL_TEXTURE0
glUseProgram(0); //unbind program -- glUniform* calls refer to ??? now
}
LitColorTextureProgram::~LitColorTextureProgram() {
glDeleteProgram(program);
program = 0;
}