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
/
Copy pathindex_Flat.html
206 lines (175 loc) · 6.69 KB
/
index_Flat.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
<!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 src="js/common.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 uPointLightingSpecularColor;
uniform vec3 uPointLightingDiffuseColor;
varying vec4 textureColor;
varying vec3 fragPosition;
varying vec3 lightDirection;
varying vec3 eyeDirection;
void main(void) {
vec3 normal = normalize(cross(
dFdx(fragPosition),
dFdy(fragPosition)
));
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
float diffuseLightWeighting = max(dot(normal, 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 vec2 aTextureCoord;
varying vec4 textureColor;
varying vec3 fragPosition;
varying vec3 lightDirection;
varying vec3 eyeDirection;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform vec3 uPointLightingLocation;
uniform sampler2D uSampler;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
fragPosition = (uMVMatrix * vec4(aVertexPosition, 1.0)).xyz;
lightDirection = normalize(uPointLightingLocation - (uMVMatrix * vec4(aVertexPosition, 1.0)).xyz);
eyeDirection = normalize(-(uMVMatrix * vec4(aVertexPosition, 1.0)).xyz);
textureColor = texture2D(uSampler, vec2(aTextureCoord.s, aTextureCoord.t));
}
</script>
<script type="text/javascript">
var teapot;
var teapotAngle = 180;
function tick() {
teapot.transform.rotate.angle = teapotAngle;
teapot.attributes.ambientColorUniform = [
parseFloat(document.getElementById('ambientR').value),
parseFloat(document.getElementById('ambientG').value),
parseFloat(document.getElementById('ambientB').value)
];
teapot.attributes.pointLightingLocationUniform = [
parseFloat(document.getElementById('lightPositionX').value),
parseFloat(document.getElementById('lightPositionY').value),
parseFloat(document.getElementById('lightPositionZ').value)
];
teapot.attributes.pointLightingSpecularColorUniform = [
parseFloat(document.getElementById('specularR').value),
parseFloat(document.getElementById('specularG').value),
parseFloat(document.getElementById('specularB').value)
];
teapot.attributes.pointLightingDiffuseColorUniform = [
parseFloat(document.getElementById('diffuseR').value),
parseFloat(document.getElementById('diffuseG').value),
parseFloat(document.getElementById('diffuseB').value)
];
teapot.attributes.materialShininessUniform = parseFloat(document.getElementById('shininess').value);
requestAnimFrame(tick);
gl.drawScene();
animate();
}
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",
materialShininessUniform: "uMaterialShininess",
ambientColorUniform: "uAmbientColor",
pointLightingLocationUniform: "uPointLightingLocation",
pointLightingSpecularColorUniform: "uPointLightingSpecularColor",
pointLightingDiffuseColorUniform: "uPointLightingDiffuseColor"
},
vertexAttrs: {
vertexPositionAttribute: "aVertexPosition",
textureCoordAttribute: "aTextureCoord"
}
});
redImg = new Image();
redImg.onload = function () {
gl.addTexture("redTexture", redImg);
}
redImg.src = "textures/red.jpg";
teapot = loadTeapot();
teapot.others.texture = 'redTexture';
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/>
Shading:
<select id="shading" onchange="window.location='index_' + this.value + '.html';">
<option value="Flat">Flat Shading</option>
<option value="Phong"> Phong Shading</option>
<option selected value="Gouraud">Gouraud Shading</option>
</select>
<br/>
<table style="border: 0; padding: 10px;">
<tr>
<td><b>Material:</b>
<td><input type="text" id="shininess" value="32.0" />
</tr>
<tr>
<td><b>Point Light Location:</b>
<td>X: <input type="text" id="lightPositionX" value="-10.0" />
<td>Y: <input type="text" id="lightPositionY" value="4.0" />
<td>Z: <input type="text" id="lightPositionZ" value="-20.0" />
</tr>
<tr>
<td><b>Specular Colour:</b>
<td>R: <input type="text" id="specularR" value="0.8" />
<td>G: <input type="text" id="specularG" value="0.8" />
<td>B: <input type="text" id="specularB" value="0.8" />
</tr>
<tr>
<td><b>Diffuse Colour:</b>
<td>R: <input type="text" id="diffuseR" value="0.8" />
<td>G: <input type="text" id="diffuseG" value="0.8" />
<td>B: <input type="text" id="diffuseB" value="0.8" />
</tr>
<tr>
<td><b>Ambient Light Colour:</b>
<td>R: <input type="text" id="ambientR" value="0.2" />
<td>G: <input type="text" id="ambientG" value="0.2" />
<td>B: <input type="text" id="ambientB" value="0.2" />
</tr>
</table>
</body>
</html>