-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader_handler.js
257 lines (193 loc) · 9.55 KB
/
shader_handler.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Script para cargar shaders
function ShaderHandler() {
var lastRead;
// Para leer un archivo
this.cargarArchivo = function(path) {
var request = new XMLHttpRequest();
request.open('GET', path, false);
request.addEventListener('load', function() {
lastRead = request.responseText;
});
request.send();
}
// Compila un shader a prtir de un string fuente, un tipo y una
// string de parámetros de tipo "#define ALGO \n #define OTRA_COSA"
this.compilarShader = function(src, type, params) {
var shader;
var header;
if (type == "fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
header = "#define FRAGMENTOS\n"
} else if (type == "vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
header = "#define VERTICES\n"
} else {
console.log("Tipo de shader desconocido");
return null;
}
// Asignamos el fuente y compilamos
src = header + params + "\n" + src;
gl.shaderSource(shader, src);
gl.compileShader(shader);
// Chequeamos y reportamos si hubo algún error.
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert("Error compilando los shaders: " +
gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
// Crea un shader a partir de un archivo en path, aplicando
// los parámetros especificados.
this.crearPrograma = function(path, params) {
lastRead = null;
this.cargarArchivo(path);
var src = lastRead;
var frag_shader = this.compilarShader(src, "fragment", params);
var vert_shader = this.compilarShader(src, "vertex", params);
// Creo el programa y le asigno los shader
var program = gl.createProgram();
gl.attachShader(program, vert_shader);
gl.attachShader(program, frag_shader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert("Error cargando los shaders. " + gl.getProgramInfoLog(program));
return null;
}
return program;
}
this.setupCommons = function(shader) {
shader.aVertexPosition = gl.getAttribLocation(shader, "aVertexPosition");
shader.aVertexNormal = gl.getAttribLocation(shader, "aVertexNormal");
shader.aVertexColor = gl.getAttribLocation(shader, "aVertexColor");
shader.aVertexTangent = gl.getAttribLocation(shader, "aVertexTangent");
shader.uVMatrix = gl.getUniformLocation(shader, "uVMatrix");
shader.uMMatrix = gl.getUniformLocation(shader, "uMMatrix");
shader.uPMatrix = gl.getUniformLocation(shader, "uPMatrix");
shader.uNMatrix = gl.getUniformLocation(shader, "uNMatrix");
shader.uAmbientColor = gl.getUniformLocation(shader, "uAmbientColor");
shader.uDirectionalColor = gl.getUniformLocation(shader, "uDirectionalColor");
shader.uLightPosition = gl.getUniformLocation(shader, "uLightPosition");
shader.uUseSpecular = gl.getUniformLocation(shader, "uUseSpecular");
shader.uUseNormals = gl.getUniformLocation(shader, "uUseNormals");
shader.uUseTexture = gl.getUniformLocation(shader, "uUseTexture");
}
this.loadColorShader = function() {
var shader = this.crearPrograma("shaders/color_shader.glsl", "");
gl.linkProgram(shader);
// Punteros a los atributos del shader
shader.aVertexPosition = gl.getAttribLocation(shader, "aVertexPosition");
shader.aVertexNormal = gl.getAttribLocation(shader, "aVertexNormal");
shader.aVertexColor = gl.getAttribLocation(shader, "aVertexColor");
// Punteros a las matrices y parámetros uniform
shader.uVMatrix = gl.getUniformLocation(shader, "uVMatrix");
shader.uMMatrix = gl.getUniformLocation(shader, "uMMatrix");
shader.uPMatrix = gl.getUniformLocation(shader, "uPMatrix");
shader.uNMatrix = gl.getUniformLocation(shader, "uNMatrix");
// Parámetros de iluminación
shader.uUseLighting = gl.getUniformLocation(shader, "uUseLighting");
shader.uAmbientColor = gl.getUniformLocation(shader, "uAmbientColor");
shader.uLightPosition = gl.getUniformLocation(shader, "uLightPosition");
shader.uDirectionalColor = gl.getUniformLocation(shader, "uDirectionalColor");
shader.uUseSpecular = gl.getUniformLocation(shader, "uUseSpecular");
shader.uUseReflection = gl.getUniformLocation(shader, "uUseReflection");
shader.uUseNormals = gl.getUniformLocation(shader, "uUseNormals");
shader.uUseTexture = gl.getUniformLocation(shader, "uUseTexture");
glShaders["color"] = shader;
}
this.loadTextureShader = function() {
var shader = this.crearPrograma("shaders/texturas_simples.glsl", "");
gl.linkProgram(shader);
this.setupCommons(shader);
shader.aVertexUV = gl.getAttribLocation(shader, "aVertexUV");
shader.uSampler1 = gl.getUniformLocation(shader, "uSampler1");
shader.uSampler2 = gl.getUniformLocation(shader, "uSampler2");
glShaders["single_texture"] = shader;
var shader = this.crearPrograma("shaders/texturas_simples.glsl", "#define MULTIPLE_TEXTURA\n");
gl.linkProgram(shader);
this.setupCommons(shader);
shader.aVertexUV = gl.getAttribLocation(shader, "aVertexUV");
shader.aTextureIndex = gl.getAttribLocation(shader, "aTextureIndex");
shader.uSampler1 = gl.getUniformLocation(shader, "uSampler1");
shader.uSampler2 = gl.getUniformLocation(shader, "uSampler2");
shader.uSampler3 = gl.getUniformLocation(shader, "uSampler3");
shader.uSampler4 = gl.getUniformLocation(shader, "uSampler4");
glShaders["multi_texture"] = shader;
var shader = this.crearPrograma("shaders/texturas_simples.glsl", "#define SPECULAR\n #define REFLECTION\n");
gl.linkProgram(shader);
this.setupCommons(shader);
shader.aVertexUV = gl.getAttribLocation(shader, "aVertexUV");
shader.uSampler1 = gl.getUniformLocation(shader, "uSampler1");
shader.uSampler2 = gl.getUniformLocation(shader, "uSampler2");
shader.uCameraPos = gl.getUniformLocation(shader, "uCameraPos");
shader.uSamplerReflection = gl.getUniformLocation(shader, "uSamplerReflection");
shader.uUseReflection = gl.getUniformLocation(shader, "uUseReflection");
glShaders["specular"] = shader;
}
this.loadTerrainShader = function() {
var shader = this.crearPrograma("shaders/terrain_shader.glsl", "");
gl.linkProgram(shader);
this.setupCommons(shader);
shader.aVertexUV = gl.getAttribLocation(shader, "aVertexUV");
shader.aVertexUVBig = gl.getAttribLocation(shader, "aVertexUVBig");
shader.uSamplerSand = gl.getUniformLocation(shader, "uSamplerSand");
shader.uSamplerSandNorm = gl.getUniformLocation(shader, "uSamplerSandNorm");
shader.uSamplerGrass = gl.getUniformLocation(shader, "uSamplerGrass");
shader.uSamplerGrassNorm = gl.getUniformLocation(shader, "uSamplerGrassNorm");
shader.uSamplerStone = gl.getUniformLocation(shader, "uSamplerStone");
shader.uSamplerStoneNorm = gl.getUniformLocation(shader, "uSamplerStoneNorm");
shader.uSamplerAlt = gl.getUniformLocation(shader, "uSamplerAlt");
shader.uSamplerAltNorm = gl.getUniformLocation(shader, "uSamplerAltNorm");
shader.uSamplerBlend = gl.getUniformLocation(shader, "uSamplerBlend");
shader.uMaxHeight = gl.getUniformLocation(shader, "uMaxHeight");
glShaders["terrain"] = shader;
}
this.loadWaterShader = function() {
var shader = this.crearPrograma("shaders/water_shader.glsl", "");
gl.linkProgram(shader);
shader.aVertexPosition = gl.getAttribLocation(shader, "aVertexPosition");
shader.aVertexColor = gl.getAttribLocation(shader, "aVertexColor");
shader.uVMatrix = gl.getUniformLocation(shader, "uVMatrix");
shader.uMMatrix = gl.getUniformLocation(shader, "uMMatrix");
shader.uPMatrix = gl.getUniformLocation(shader, "uPMatrix");
shader.uNMatrix = gl.getUniformLocation(shader, "uNMatrix");
shader.uAmbientColor = gl.getUniformLocation(shader, "uAmbientColor");
shader.uDirectionalColor = gl.getUniformLocation(shader, "uDirectionalColor");
shader.uLightPosition = gl.getUniformLocation(shader, "uLightPosition");
shader.uCameraPos = gl.getUniformLocation(shader, "uCameraPos");
shader.aVertexUV = gl.getAttribLocation(shader, "aVertexUV");
shader.uSampler1 = gl.getUniformLocation(shader, "uSamplerNormal");
shader.uSamplerReflection = gl.getUniformLocation(shader, "uSamplerReflection");
shader.uUseSpecular = gl.getUniformLocation(shader, "uUseSpecular");
shader.uUseReflection = gl.getUniformLocation(shader, "uUseReflection");
shader.uUseNormals = gl.getUniformLocation(shader, "uUseNormals");
shader.uUseTexture = gl.getUniformLocation(shader, "uUseTexture");
shader.uTime = gl.getUniformLocation(shader, "uTime");
glShaders["water"] = shader;
}
this.loadSkyBoxShader = function() {
var shader = this.crearPrograma("shaders/skybox_shader.glsl", "");
gl.linkProgram(shader);
shader.aVertexPosition = gl.getAttribLocation(shader, "aVertexPosition");
shader.aVertexColor = gl.getAttribLocation(shader, "aVertexColor");
shader.uVMatrix = gl.getUniformLocation(shader, "uVMatrix");
shader.uMMatrix = gl.getUniformLocation(shader, "uMMatrix");
shader.uPMatrix = gl.getUniformLocation(shader, "uPMatrix");
shader.uNMatrix = gl.getUniformLocation(shader, "uNMatrix");
shader.uSkyboxLight = gl.getUniformLocation(shader, "uSkyboxLight");
shader.aVertexUV = gl.getAttribLocation(shader, "aVertexUV");
shader.uSampler1 = gl.getUniformLocation(shader, "uSampler1");
shader.uUseSpecular = gl.getUniformLocation(shader, "uUseSpecular");
shader.uUseReflection = gl.getUniformLocation(shader, "uUseReflection");
shader.uUseNormals = gl.getUniformLocation(shader, "uUseNormals");
shader.uUseTexture = gl.getUniformLocation(shader, "uUseTexture");
glShaders["skybox"] = shader;
}
this.load = function() {
this.loadColorShader();
this.loadTextureShader();
this.loadTerrainShader();
this.loadWaterShader();
this.loadSkyBoxShader();
}
}