-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebGL.html
224 lines (179 loc) · 6.4 KB
/
WebGL.html
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
<!DOCTYPE html>
<html>
<head>
<title>WebGL Project</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 vPosition;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec2 vTextureCoord;
varying vec4 fColor;
// For the Phong Illumination Model
attribute vec3 vNormal;
uniform vec4 viewerPosition;
// Array of lights
#define MAX_LIGHTS 5
uniform int numLights;
uniform struct Light {
vec4 position;
mat4 lightSourceMatrix; // for animation
vec3 intensities; //a.k.a the color of the light
vec3 ambientIntensities;
} allLights[MAX_LIGHTS];
// The material properties
uniform vec3 k_ambient;
uniform vec3 k_diffuse;
uniform vec3 k_specular;
uniform float shininess;
void main(void) {
// Just converting the (x,y,z) vertices to Homogeneous Coord.
// And multiplying by the Projection and the Model-View matrix
gl_Position = uPMatrix * (uMVMatrix * vec4(vPosition, 1.0));
// For the fragment shader
vTextureCoord = aTextureCoord;
// Phong Illumination Model
// pos is vertex position after applying the global transformation
vec3 pos = (uMVMatrix * vec4(vPosition, 1.0)).xyz;
// ITERATING THROUGH ALL LIGHT SOURCES
fColor = vec4(0.0, 0.0, 0.0, 0.0); // To add all illumination components
// THE FOR LOOP NEEDS A STOPPING CONSTANT VALUE
for( int i = 0; i < MAX_LIGHTS; i++ )
{
if( i == numLights )
break;
// Ambient component is constant for each light source
vec4 ambient = vec4( k_ambient * allLights[i].ambientIntensities, 1.0 );
fColor += ambient;
// vector from vertex position to light source
vec3 L;
// check for directional light
if(allLights[i].position.w == 0.0)
L = normalize( (allLights[i].lightSourceMatrix * allLights[i].position).xyz );
else
L = normalize( (allLights[i].lightSourceMatrix * allLights[i].position).xyz - pos );
// Vector from the vertex position to the eye
vec3 E;
// The viewer is at the origin or at an indefinite distance
// on the ZZ axis
if(viewerPosition.w == 1.0)
// At the origin
E = -normalize( pos );
else
// On the ZZ axis
E = vec3(0,0,1);
// Halfway vector
vec3 H = normalize( L + E );
// Transform vertex normal into eye coordinates
// Added projection matrix here...
//vec4 N = normalize( uPMatrix * uMVMatrix * vec4(vNormal, 0.0));
vec4 N = normalize( uMVMatrix * vec4(vNormal, 0.0));
// Compute terms in the illumination equation
// Diffuse component
float dotProductLN = L[0] * N[0] + L[1] * N[1] + L[2] * N[2];
float cosNL = max( dotProductLN, 0.0 );
vec4 diffuse = vec4( k_diffuse * cosNL * allLights[i].intensities, 1.0 );
// Specular component
float dotProductNH = N[0] * H[0] + N[1] * H[1] + N[2] * H[2];
float cosNH = pow( max( dotProductNH, 0.0 ), shininess );
vec4 specular = vec4( k_specular * cosNH * allLights[i].intensities, 1.0 );
if( dotProductLN < 0.0 ) {
specular = vec4(0.0, 0.0, 0.0, 1.0);
}
// Adding the components
fColor += diffuse + specular;
}
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec4 fColor;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)) * fColor;
}
</script>
<!-- Default CSS file -->
<link rel="stylesheet" href="css/default.css">
<!-- The JS files -->
<!-- CSV library -->
<script type="text/javascript" src="js/csv.js"></script>
<!-- kD-Tree library -->
<script type="text/javascript" src="js/kdTree-min.js"></script>
<!-- Some useful functions for browser compatibility -->
<script type="text/javascript" src="js/webgl-utils.js"></script>
<!-- Handling vectors and matrices -->
<script type="text/javascript" src="js/maths.js"></script>
<!-- Processing triangle mesh models -->
<script type="text/javascript" src="js/models.js"></script>
<!-- Creating the light sources -->
<script type="text/javascript" src="js/lightSources.js"></script>
<!-- Creating the Textures -->
<script type="text/javascript" src="js/textures.js"></script>
<!-- Creating the scene models -->
<script type="text/javascript" src="js/sceneModels.js"></script>
<!-- WebGL code -->
<script type="text/javascript" src="js/initShaders.js"></script>
<script type="text/javascript" src="js/WebGL.js"></script>
</head>
<body onload="runWebGL();">
<div style="text-align: center">
<canvas id="my-canvas" style="border:1px solid #000000;" width="1280" height="720"></canvas>
<div>
<div class="container" style="text-align: center; width: 1280px; height: 100px; margin: 0 auto;">
<div class="fixed">
<table style="text-align: left">
<tr>
<th>Time</th>
<td id="time">0</td>
</tr>
<tr>
<th>FPS</th>
<td id="fps">0</td>
</tr>
<tr>
<th>Vx</th>
<td id="vx">0</td>
</tr>
<tr>
<th>Vy</th>
<td id="vy">0</td>
</tr>
</table>
</div>
<div class="flex-item">
<table style="text-align: left">
<tr>
<th>Choose the <b>projection type</b>:</th>
<td><select id="projection-selection">
<option value="0">Perspective Projection</option>
<option value="1">Orthogonal Projection</option>
</select>
</td>
</tr>
<tr>
<th>Choose the <b>Texture set</b>:</th>
<td><select id="texture-selection">
<option value="0">Medieval</option>
<option value="1">Nature</option>
<option value="1">Volcano</option>
</select>
</td>
</tr>
<tr>
<th></th>
<td><button id="reset-button">Reset</button></td>
</tr>
</table>
</div>
</div>
</div>
<div id="drop_zone"
style="text-align: center; background-color: white; outline: 2px dashed black; width: 1280px; height: 100px; margin: 0 auto;">
Drop files here:
</div>
</div>
</body>
</html>