-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaders.js
executable file
·105 lines (85 loc) · 4.32 KB
/
shaders.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
lambertianSingleColorShader = function (gl) {
var shaderProgram = gl.createProgram();
var request = new XMLHttpRequest();
request.open('GET', 'shaders/shader.fsh',false);
request.send();
shaderProgram.fragment_shader = request.responseText
request.open('GET', 'shaders/shader.vsh',false);
request.send();
shaderProgram.vertex_shader = request.responseText
// create the vertex shader
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, shaderProgram.vertex_shader);
gl.compileShader(vertexShader);
// create the fragment shader
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, shaderProgram.fragment_shader);
gl.compileShader(fragmentShader);
// Create the shader program
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
shaderProgram.vertexShader = vertexShader;
shaderProgram.fragmentShader = fragmentShader;
// If creating the shader program failed, alert
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Unable to initialize the shader program.");
var str = "";
str += "VS:\n" + gl.getShaderInfoLog(vertexShader) + "\n\n";
str += "FS:\n" + gl.getShaderInfoLog(fragmentShader) + "\n\n";
str += "PROG:\n" + gl.getProgramInfoLog(shaderProgram);
alert(str);
}
shaderProgram.aPosition = gl.getAttribLocation(shaderProgram, 'aPosition');
shaderProgram.aNormal = gl.getAttribLocation(shaderProgram, 'aNormal');
shaderProgram.aTexturePosition = gl.getAttribLocation(shaderProgram, 'aTexturePosition');
shaderProgram.uCameraPosition = gl.getUniformLocation(shaderProgram,"uCameraPosition");
shaderProgram.uProjectionMatrixLocation = gl.getUniformLocation(shaderProgram,"uProjectionMatrix");
shaderProgram.uModelViewMatrixLocation = gl.getUniformLocation(shaderProgram,"uModelViewMatrix");
shaderProgram.uMaterial = gl.getUniformLocation(shaderProgram,"uMaterial");
shaderProgram.uLightDirectionLocation = gl.getUniformLocation(shaderProgram,"uLightDirection");
shaderProgram.uLightColorLocation = gl.getUniformLocation(shaderProgram,"uLightColor");
shaderProgram.uColorLocation = gl.getUniformLocation(shaderProgram,"uColor");
shaderProgram.uShadowMap = gl.getUniformLocation(shaderProgram,"uShadowMap");
shaderProgram.uShadowMatrix = gl.getUniformLocation(shaderProgram,"uShadowMatrix");
shaderProgram.uMakeCheckerboardGaps = gl.getUniformLocation(shaderProgram,"uMakeCheckerboardGaps");
shaderProgram.uCheckerboardPosition = gl.getUniformLocation(shaderProgram,"uCheckerboardPosition");
return shaderProgram;
};
shadowMapShader = function (gl) {
var shaderProgram = gl.createProgram();
var request = new XMLHttpRequest();
request.open('GET', 'shaders/shadowMapShader.fsh',false);
request.send();
shaderProgram.fragment_shader = request.responseText
request.open('GET', 'shaders/shadowMapShader.vsh',false);
request.send();
shaderProgram.vertex_shader = request.responseText
// create the vertex shader
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, shaderProgram.vertex_shader);
gl.compileShader(vertexShader);
// create the fragment shader
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, shaderProgram.fragment_shader);
gl.compileShader(fragmentShader);
// Create the shader program
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
shaderProgram.vertexShader = vertexShader;
shaderProgram.fragmentShader = fragmentShader;
// If creating the shader program failed, alert
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Unable to initialize the shader program.");
var str = "";
str += "VS:\n" + gl.getShaderInfoLog(vertexShader) + "\n\n";
str += "FS:\n" + gl.getShaderInfoLog(fragmentShader) + "\n\n";
str += "PROG:\n" + gl.getProgramInfoLog(shaderProgram);
alert(str);
}
shaderProgram.aPosition = gl.getAttribLocation(shaderProgram, 'aPosition');
shaderProgram.uProjectionMatrixLocation = gl.getUniformLocation(shaderProgram,"uProjectionMatrix");
shaderProgram.uModelViewMatrixLocation = gl.getUniformLocation(shaderProgram,"uModelViewMatrix");
return shaderProgram;
};