This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_mixed.html
389 lines (336 loc) · 13.2 KB
/
index_mixed.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!DOCTYPE html>
<html>
<head>
<title>ICG WebGL — HW1</title>
<meta name="author" content="Chih-Yung Liang">
</head>
<script src="js/glMatrix-0.9.5.min.js"></script>
<script src="js/webgl-utils.js"></script>
<script src="js/gl.js"></script>
<script id="fragmentShader" type="x-shader/x-fragment">
//per-fragment-lighting-fs
#extension GL_OES_standard_derivatives : enable
precision highp float;
//precision mediump float;
uniform float uMaterialShininess;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;
uniform vec3 uAmbientColor;
uniform vec3 uPointLightingLocation;
uniform vec3 uPointLightingSpecularColor;
uniform vec3 uPointLightingDiffuseColor;
uniform int shader;
varying vec4 textureColor;
varying vec3 fragPosition;
varying vec3 fragNormal;
varying vec3 vlightDirection;
varying vec3 veyeDirection;
varying vec4 fragcolor;
void sssflat(void);
void sssgouraud(void);
void sssphong(void);
void main(void) {
if (shader == 2) {
sssphong();
} else if (shader == 1) {
sssgouraud();
} else {
sssflat();
}
}
void sssflat(void) {
vec3 normal = normalize(cross(
dFdx(fragPosition),
dFdy(fragPosition)
));
vec3 reflectionDirection = reflect(-vlightDirection, normal);
float specularLightWeighting = pow(max(dot(reflectionDirection, veyeDirection), 0.0), uMaterialShininess);
float diffuseLightWeighting = max(dot(normal, vlightDirection), 0.0);
vec3 lightWeighting = uAmbientColor
+ uPointLightingSpecularColor * specularLightWeighting
+ uPointLightingDiffuseColor * diffuseLightWeighting;
gl_FragColor = vec4(textureColor.rgb * lightWeighting, textureColor.a);
}
void sssgouraud(void) {
gl_FragColor = fragcolor;
}
void sssphong(void) {
vec3 lightDirection = normalize(uPointLightingLocation - fragPosition);
vec3 eyeDirection = normalize(-fragPosition);
vec3 reflectionDirection = reflect(-lightDirection, fragNormal);
float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
float diffuseLightWeighting = max(dot(fragNormal, lightDirection), 0.0);
vec3 lightWeighting = uAmbientColor
+ uPointLightingSpecularColor * specularLightWeighting
+ uPointLightingDiffuseColor * diffuseLightWeighting;
gl_FragColor = vec4(textureColor.rgb * lightWeighting, textureColor.a);
}
</script>
<script id="vertexShader" type="x-shader/x-vertex">
//per-fragment-lighting-vs
#extension GL_OES_standard_derivatives : enable
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
varying vec4 textureColor;
varying vec3 fragPosition;
varying vec3 fragNormal;
varying vec3 vlightDirection;
varying vec3 veyeDirection;
varying vec4 fragcolor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;
uniform vec3 uAmbientColor;
uniform vec3 uPointLightingLocation;
uniform vec3 uPointLightingSpecularColor;
uniform vec3 uPointLightingDiffuseColor;
uniform float uMaterialShininess;
uniform sampler2D uSampler;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
fragPosition = (uMVMatrix * vec4(aVertexPosition, 1.0)).xyz;
vlightDirection = normalize(uPointLightingLocation - (uMVMatrix * vec4(aVertexPosition, 1.0)).xyz);
veyeDirection = normalize(-(uMVMatrix * vec4(aVertexPosition, 1.0)).xyz);
fragNormal = normalize(uNMatrix * aVertexNormal);
textureColor = texture2D(uSampler, vec2(aTextureCoord.s, aTextureCoord.t));
vec3 lightWeighting;
vec3 lightDirection = normalize(uPointLightingLocation - (uMVMatrix * vec4(aVertexPosition, 1.0)).xyz);
vec3 normal = normalize(uNMatrix * aVertexNormal);
float specularLightWeighting = 0.0;
vec3 eyeDirection = normalize(-(uMVMatrix * vec4(aVertexPosition, 1.0)).xyz);
vec3 reflectionDirection = reflect(-lightDirection, normal);
specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
lightWeighting = uAmbientColor
+ uPointLightingSpecularColor * specularLightWeighting
+ uPointLightingDiffuseColor * diffuseLightWeighting;
vec4 fragmentColor;
fragmentColor = texture2D(uSampler, vec2(aTextureCoord.s, aTextureCoord.t));
fragcolor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script type="text/javascript">
function tick() {
requestAnimFrame(tick);
gl.drawScene();
for (var obj in allObjects) {
allObjects[obj].update();
}
}
function webGLStart() {
var canvas = document.getElementById("ICG-canvas");
gl.initGL(canvas);
gl.initShaders([
document.getElementById('fragmentShader'),
document.getElementById('vertexShader')
], {
uniforms: {
pMatrixUniform: "uPMatrix",
mvMatrixUniform: "uMVMatrix",
nMatrixUniform: "uNMatrix",
samplerUniform: "uSampler",
shaderUniform: "shader",
materialShininessUniform: "uMaterialShininess",
ambientColorUniform: "uAmbientColor",
pointLightingLocationUniform: "uPointLightingLocation",
pointLightingSpecularColorUniform: "uPointLightingSpecularColor",
pointLightingDiffuseColorUniform: "uPointLightingDiffuseColor"
},
vertexAttrs: {
vertexPositionAttribute: "aVertexPosition",
vertexNormalAttribute: "aVertexNormal",
textureCoordAttribute: "aTextureCoord"
}
});
galvanizedImg = new Image();
galvanizedImg.onload = function () {
gl.addTexture("galvanizedTexture", galvanizedImg);
}
galvanizedImg.src = "textures/galvanizedTexture.jpg";
redImg = new Image();
redImg.onload = function () {
gl.addTexture("redTexture", redImg);
}
redImg.src = "textures/red.jpg";
tick();
}
</script>
<style type="text/css">
#loadingtext {
position:absolute;
top:250px;
left:150px;
font-size:2em;
color: white;
}
</style>
<body onload="webGLStart();">
<canvas id="ICG-canvas" style="border: none;" width="800" height="600"></canvas>
<br/>
<button type="button" id="add-object">Add Object</button>
<div id="template">
<h2>Object:</h2>
Shading:
<select class="shading">
<option value="0">Flat Shading</option>
<option selected value="2"> Phong Shading</option>
<option value="1">Gouraud Shading</option>
</select>
<br/>
Texture:
<select class="texture">
<option selected value="galvanizedTexture">galvanized</option>
<option value="redTexture">red</option>
</select>
<br/>
Transform:
<table style="border: 0; padding: 10px;">
<tr>
<td><b>Translate:</b>
<td>X: <input type="number" class="translateX" value="0.0" step="0.1" />
<td>Y: <input type="number" class="translateY" value="0.0" step="0.1" />
<td>Z: <input type="number" class="translateZ" value="-40.0" step="0.1" />
</tr>
<tr>
<td><b>Rotate:</b>
<td>degree: <input type="number" class="degree" value="0" step="1" />
</tr>
<tr>
<td><b>Rotate Around:</b>
<td>X: <input type="number" class="aroundX" value="0" step="0.1" />
<td>Y: <input type="number" class="aroundY" value="1" step="0.1" />
<td>Z: <input type="number" class="aroundZ" value="0" step="0.1" />
</tr>
<tr>
<td><b>Scale:</b>
<td>X: <input type="number" class="scaleX" value="1" step="0.1" />
<td>Y: <input type="number" class="scaleY" value="1" step="0.1" />
<td>Z: <input type="number" class="scaleZ" value="1" step="0.1" />
</tr>
<tr>
<td><b>Shear:</b>
<td>θ: <input type="number" class="shear" value="90" step="1" />
</tr>
</table>
<table style="border: 0; padding: 10px;">
<tr>
<td><b>Material:</b>
<td><input type="number" class="shininess" value="32.0" step="0.1" />
</tr>
<tr>
<td><b>Point Light Location:</b>
<td>X: <input type="number" class="lightPositionX" value="-10.0" step="0.1" />
<td>Y: <input type="number" class="lightPositionY" value="4.0" step="0.1" />
<td>Z: <input type="number" class="lightPositionZ" value="-20.0" step="0.1" />
</tr>
<tr>
<td><b>Specular Colour:</b>
<td>R: <input type="number" class="specularR" value="0.8" step="0.1" />
<td>G: <input type="number" class="specularG" value="0.8" step="0.1" />
<td>B: <input type="number" class="specularB" value="0.8" step="0.1" />
</tr>
<tr>
<td><b>Diffuse Colour:</b>
<td>R: <input type="number" class="diffuseR" value="0.8" step="0.1" />
<td>G: <input type="number" class="diffuseG" value="0.8" step="0.1" />
<td>B: <input type="number" class="diffuseB" value="0.8" step="0.1" />
</tr>
<tr>
<td><b>Ambient Light Colour:</b>
<td>R: <input type="number" class="ambientR" value="0.2" step="0.1" />
<td>G: <input type="number" class="ambientG" value="0.2" step="0.1" />
<td>B: <input type="number" class="ambientB" value="0.2" step="0.1" />
</tr>
</table>
</div>
</body>
<script src="js/jquery-3.1.1.min.js"></script>
<script>
var allObjects = [];
var CGObject = function(obj) {
this.data = {};
this.transform = {
translate: [0, 0, -40],
rotate: {
angle: 0,
around: [0, 1, 0]
},
scale: [1, 1, 1],
shear: 90
};
this.attributes = {
ambientColorUniform: [0.0, 0.0, 0.0],
pointLightingLocationUniform: [0.0, 0.0, 0.0],
pointLightingSpecularColorUniform: [0.0, 0.0, 0.0],
pointLightingDiffuseColorUniform: [0.0, 0.0, 0.0],
materialShininessUniform: 0.0
};
this.others = {
shader: 0,
texture: 'galvanizedTexture'
};
this.listenTo = null;
var _this = this;
$.getJSON(obj, function(data) {
_this.data = data;
gl.addObject(_this);
});
allObjects.push(this);
this.update = function() {
if (this.listenTo == null) {
return;
}
this.others.shader = this.listenTo.find('.shading').val();
this.others.texture = this.listenTo.find('.texture').val();
this.transform.translate = [
parseFloat(this.listenTo.find('.translateX').val()),
parseFloat(this.listenTo.find('.translateY').val()),
parseFloat(this.listenTo.find('.translateZ').val())
];
this.transform.rotate.angle = parseFloat(this.listenTo.find('.degree').val());
this.transform.rotate.around = [
parseFloat(this.listenTo.find('.aroundX').val()),
parseFloat(this.listenTo.find('.aroundY').val()),
parseFloat(this.listenTo.find('.aroundZ').val())
];
this.transform.scale = [
parseFloat(this.listenTo.find('.scaleX').val()),
parseFloat(this.listenTo.find('.scaleY').val()),
parseFloat(this.listenTo.find('.scaleZ').val())
];
this.transform.shear = parseFloat(this.listenTo.find('.shear').val());
this.attributes.materialShininessUniform = parseFloat(this.listenTo.find('.shininess').val());
this.attributes.pointLightingLocationUniform = [
parseFloat(this.listenTo.find('.lightPositionX').val()),
parseFloat(this.listenTo.find('.lightPositionY').val()),
parseFloat(this.listenTo.find('.lightPositionZ').val())
];
this.attributes.pointLightingSpecularColorUniform = [
parseFloat(this.listenTo.find('.specularR').val()),
parseFloat(this.listenTo.find('.specularG').val()),
parseFloat(this.listenTo.find('.specularB').val())
];
this.attributes.pointLightingDiffuseColorUniform = [
parseFloat(this.listenTo.find('.diffuseR').val()),
parseFloat(this.listenTo.find('.diffuseG').val()),
parseFloat(this.listenTo.find('.diffuseB').val())
];
this.attributes.ambientColorUniform = [
parseFloat(this.listenTo.find('.ambientR').val()),
parseFloat(this.listenTo.find('.ambientG').val()),
parseFloat(this.listenTo.find('.ambientB').val())
];
}
}
$(document).ready(function() {
var template = $('#template').hide();
$('#add-object').click(function() {
var newObj = new CGObject('objects/Teapot.json'); // Current fixed...
var newOptions = template.clone().appendTo($('body')).show();
newObj.listenTo = newOptions;
});
});
</script>
</html>