-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
262 lines (211 loc) · 8.9 KB
/
index.html
File metadata and controls
262 lines (211 loc) · 8.9 KB
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Course-webgl by Wulonghua</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/github-light.css">
<link rel="icon" href="data:;base64,=">
<script src="javascripts/scale.fix.js"></script>
<script type="text/javascript" src="javascripts/glMatrix-0.9.5.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
precision mediump int;
uniform mat4 uV2WMatrix; // eye to world
uniform vec4 light_pos;
uniform vec4 ambient_coef;
uniform vec4 diffuse_coef;
uniform vec4 specular_coef;
uniform float mat_shininess;
uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
uniform int use_texture;
uniform sampler2D myTexture;
uniform samplerCube cubeMap;
uniform sampler2D permutation_sampler;
uniform sampler2D gradient_sampler;
varying vec4 eye_pos;
varying vec3 v_normal;
varying highp vec2 FtexCoord;
varying vec3 noise_seed;
float fade(float t)
{
return (t*(t*6.0-15.0)+10.0)*t*t*t;
}
float lerp(float t, float a, float b){
return mix(a, b, t);
}
float grad(float hash, vec3 pos){
return dot(texture2D(gradient_sampler, vec2(hash,0)).rgb, pos);
}
float permute(float x){
return texture2D(permutation_sampler, vec2(x / 256.0, 0)).r * 256.0;
}
float generatePerlinNoise(vec3 pos){
vec3 pos_t = mod(floor(pos),256.0);
float X = pos_t.x;
float Y = pos_t.y;
float Z = pos_t.z;
float x = pos.x - floor(pos.x);
float y = pos.y - floor(pos.y);
float z = pos.z - floor(pos.z);
float u = fade(x);
float v = fade(y);
float w = fade(z);
float A = permute(X)+Y;
float AA = permute(A)+Z;
float AB = permute(A+1.0)+Z;
float B = permute(X+1.0)+Y;
float BA = permute(B)+Z;
float BB = permute(B+1.0)+Z;
return lerp(w, lerp(v, lerp(u, grad(permute(AA), vec3(x , y , z)),
grad(permute(BA), vec3(x-1.0, y , z))),
lerp(u, grad(permute(AB), vec3(x , y-1.0, z)),
grad(permute(BB), vec3(x-1.0, y-1.0, z)))),
lerp(v, lerp(u, grad(permute(AA+1.0), vec3(x , y , z-1.0)),
grad(permute(BA+1.0), vec3(x-1.0, y , z-1.0))),
lerp(u, grad(permute(AB+1.0), vec3(x , y-1.0, z-1.0)),
grad(permute(BB+1.0), vec3(x-1.0, y-1.0, z-1.0)))));
}
void main(void) {
vec4 texcolor;
vec3 view_vector, ref;
vec4 env_color;
vec4 light_pos_in_eye = light_pos;
// light vector L = l-p
vec3 light_vector = normalize(vec3(light_pos_in_eye - eye_pos));
// eye vector V = e-p, where e is (0,0,0)
vec3 eye_vector = normalize(-vec3(eye_pos));
vec4 ambient = ambient_coef * light_ambient;
float ndotl = max(dot(v_normal, light_vector), 0.0);
vec4 diffuse = diffuse_coef * light_diffuse* ndotl;
vec3 R = normalize(2.0 * ndotl *v_normal-light_vector);
float rdotv = max(dot(R, eye_vector), 0.0);
vec4 specular;
if (ndotl>0.0)
specular = specular_coef* light_specular*pow(rdotv, mat_shininess);
else
specular = vec4(0.0,0.0,0.0,1);
if ( use_texture == 1 ) {
texcolor = texture2D(myTexture, FtexCoord);
gl_FragColor = texcolor *(ambient+diffuse) +specular;
}
else if (use_texture == 2) {
texcolor = texture2D(myTexture, FtexCoord);
float noise = generatePerlinNoise(noise_seed);
gl_FragColor = texcolor *(ambient+diffuse) + vec4(noise,noise,0.0,1.0);
}
else if (use_texture == 3){
view_vector = normalize(vec3(vec4(0,0,0,1)-eye_pos));
ref = normalize(reflect(-view_vector, v_normal)); // in eye space
ref = vec3(uV2WMatrix*vec4(ref,0)); // convert to world space
env_color = textureCube(cubeMap, ref);
gl_FragColor = env_color;
}
else if(use_texture ==4){
float ratio = 1.00 / 1.52;
view_vector = normalize(vec3(vec4(0,0,0,1)-eye_pos));
ref = refract(-view_vector, normalize(v_normal), ratio);
ref = vec3(uV2WMatrix*vec4(ref,0)); // convert to world space
env_color = textureCube(cubeMap, ref);
gl_FragColor = env_color;
}
else gl_FragColor = ambient+diffuse+specular;
}
</script>
<!-- ************** Vertex Shader ************* -->
<script id="shader-vs" type="x-shader/x-vertex">
precision mediump float;
precision mediump int;
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aVertexTexCoords;
uniform mat4 uMMatrix;
uniform mat4 uVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;
varying vec4 eye_pos; //vertex position in eye space
varying vec3 v_normal; // vertex normal
varying vec3 noise_seed;
varying highp vec2 FtexCoord;
void main(void) {
// transform normal from local to eye space: normal matrix is the inverse transpose of the modelview matrix
v_normal =normalize(vec3(uNMatrix*vec4(aVertexNormal,0.0)));
// transform the vertex position to eye space
eye_pos = uVMatrix*uMMatrix*vec4(aVertexPosition, 1.0);
noise_seed = aVertexPosition * 8.0;
FtexCoord = aVertexTexCoords;
gl_Position = uPMatrix*uVMatrix*uMMatrix*vec4(aVertexPosition, 1.0);
}
</script>
<script id="shader-vs-skybox" type="x-shader/x-vertex">
uniform mat4 uMMatrix;
uniform mat4 uVMatrix;
uniform mat4 uPMatrix;
attribute vec3 aVertexPosition;
varying vec3 vCoords;
void main() {
gl_Position = uPMatrix*uVMatrix*uMMatrix*vec4(aVertexPosition, 1.0);
vCoords = aVertexPosition;
}
</script>
<script id="shader-fs-skybox" type="x-shader/x-fragment">
precision mediump float;
varying vec3 vCoords;
uniform samplerCube cubeMap;
void main() {
gl_FragColor = textureCube(cubeMap, vCoords);
}
</script>
<script type="text/javascript" src="javascripts/shaders_setup.js"></script>
<script type="text/javascript" src="javascripts/trackball-rotator.js"></script>
<script type="text/javascript" src="javascripts/lab5.js"></script>
</head>
<body onload="webGLStart();">
<div class="wrapper">
<header>
<h1 class="header">Course-WebGL</h1>
<p class="header"></p>
<ul>
<li class="download"><a class="buttons" href="https://github.com/Wulonghua/Course-WebGL/zipball/master">Download
ZIP</a></li>
<li class="download"><a class="buttons" href="https://github.com/Wulonghua/Course-WebGL/tarball/master">Download
TAR</a></li>
<li><a class="buttons github" href="https://github.com/Wulonghua/Course-WebGL">View On GitHub</a></li>
</ul>
<p class="header">This project is maintained by <a class="header name" href="https://github.com/Wulonghua">Wulonghua</a>
</p>
</header>
<section>
<h3>
<a id="welcome-to-github-pages" class="anchor" href="#welcome-to-github-pages" aria-hidden="true"><span
aria-hidden="true" class="octicon octicon-link"></span></a>
Real Time Rendering Demo
</h3>
<canvas id="lab5-canvas" style="border:none;" width="650" height="650"></canvas>
<h4> Texture: </h4>
<button onclick="texture(0)"> NO </button>
<button onclick="texture(1)"> Regular </button>
<button onclick="texture(2)"> Perlin Noise </button>
<button onclick="texture(3)"> Reflection </button>
<button onclick="texture(4)"> Refraction </button>
<div>
<h4>
Click on the left mouse button and move the coursor to rotate
</h4>
<button onclick="redraw()"> Go Back! </button>
</div>
</section>
<footer>
<p>
<small>Hosted on <a href="https://pages.github.com">GitHub Pages</a> using the Dinky theme</small>
</p>
</footer>
</div>
<!--[if !IE]>
<script>fixScale(document);</script><![endif]-->
</body>
</html>