-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproj3D-time-sine.html
More file actions
567 lines (437 loc) · 17.5 KB
/
proj3D-time-sine.html
File metadata and controls
567 lines (437 loc) · 17.5 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<!doctype html>
<html>
<title>CG Project - LG, CP, MC</title>
<body>
<canvas width = "1024" height = "600" id = "my_Canvas"></canvas>
<br>
Tesselation Level (<b id="tessval"></b>): <input type="range" id="tessRange" value="7" min="0" max="7" oninput="setTess(ptype);">
Wave I A (<b id="W1A"></b>): <input type="range" id="w1Range" value="0" min="0" max="10" oninput="setW1();">
Wave II A (<b id="W2A"></b>): <input type="range" id="w2Range" value="1" min="0" max="10" oninput="setW2()">
Wave III A (<b id="W3A"></b>): <input type="range" id="w3Range" value="1" min="0" max="10" oninput="setW3()">
<br>
<form action="">
Triangles: <input type="radio" name="prim" onchange="ptype=gl.TRIANGLES; setTess(ptype);" checked="checked">
Wireframe: <input type="radio" name="prim" onchange="ptype=gl.LINES; setTess(ptype);">
</form>
<form action="">
All Triangles: <input type="radio" name="tri" onchange="renType=1; setTess(ptype);" checked="checked">
Half Triangles: <input type="radio" name="tri" onchange="renType=0; setTess(ptype);">
</form>
Rotate Camera with < Up,Down,Left,Right >
<script id="wave-vertex" type="x-shader/x-vertex" >
precision mediump float;
attribute vec3 coordinates;
uniform mat4 mvp;
uniform float time;
uniform int numWaves;
uniform float amplitudeList[50];
uniform float steepnessList[50];
uniform float phaseConstantList[50];
uniform float wList[50];
uniform vec2 dirList[50];
uniform mat4 projection;
uniform mat4 modelview;
attribute vec3 coords;
varying vec3 vCoords;
varying vec3 vCoord;
varying vec3 fPosition;
varying vec3 fNormal;
varying vec3 eye;
varying mat3 invTBN;
void main(void)
{
float x = coordinates[0];
float origx = x;
float y = coordinates[1];
float origy = y;
float z = coordinates[2];
vec3 B = vec3(1, 0, 0);
vec3 T = vec3(0, 1, 0);
vec3 N = vec3(0, 0, 1);
vec4 eyeCoords = modelview * vec4(coords, 1.0);
gl_Position = projection * eyeCoords;
vCoords = coords;
for(int i = 0; i < 5000; i++)
{
if(i >= numWaves)
break;
// TBN Math
float wa = wList[i] * amplitudeList[i];
float s = sin(wList[i] * dot(dirList[i], coordinates.xy) + phaseConstantList[i] * time);
float c = cos(wList[i] * dot(dirList[i], coordinates.xy) + phaseConstantList[i] * time);
// Bitangent
float bxchange = -(steepnessList[i] * dirList[i].x * dirList[i].x * wa * s);
float bychange = -(steepnessList[i] * dirList[i].x * dirList[i].y * wa * s);
float bzchange = dirList[i].x * wa * c;
B = vec3(B.x + bxchange, B.y + bychange, B.z + bzchange);
// Tangent
float txchange = bychange; // Equivalent per GPU Gems
float tychange = -(steepnessList[i] * dirList[i].y * dirList[i].y * wa * s);
float tzchange = bzchange;
T = vec3(T.x + txchange, T.y + tychange, T.z + tzchange);
// Normal
float nxchange = -(dirList[i].x * wa * c);
float nychange = -(dirList[i].y * wa * c);
float nzchange = -(steepnessList[i] * wa * s);
N = vec3(N.x + nxchange, N.y + nychange, N.z + nzchange);
// Position Math
x = x + steepnessList[i] * amplitudeList[i] * dirList[i][0] * cos(dot(wList[i] * dirList[i].xy, vec2(origx, origy).xy) + phaseConstantList[i] * time);
y = y + steepnessList[i] * amplitudeList[i] * dirList[i][1] * cos(dot(wList[i] * dirList[i].xy, vec2(origx, origy).xy) + phaseConstantList[i] * time);
z = z + amplitudeList[i] * sin(dot(wList[i] * dirList[i].xy, vec2(origx, origy).xy) + phaseConstantList[i] * time);
}
// Normalize final vectors
mat3 mvp3x3 = mat3(mvp);
T = mvp3x3 * normalize(T);
N = mvp3x3 * normalize(N);
B = mvp3x3 * normalize(B);
// Construct invTBN
invTBN = mat3(
vec3(T.x, B.x, N.x),
vec3(T.y, B.y, N.y),
vec3(T.z, B.z, N.z));
// Pass through new normal and position
fPosition = mvp3x3 * vec3(x, y, z);
fNormal = normalize(mvp3x3 * N);
// Extraneous: toremove
vCoord = coordinates;
vCoords = coordinates;
gl_Position = mvp * vec4(x, y, z, 1.0);
eye = -gl_Position.xyz;
}
</script>
<script id="wave-fragment" type="x-shader/x-fragment" >
precision mediump float;
varying vec3 vCoords;
uniform samplerCube skybox;
varying vec3 fPosition;
varying vec3 fNormal;
varying mat3 invTBN;
varying vec3 eye;
uniform vec3 lightPos;
uniform vec3 kd;
uniform vec3 ks;
uniform vec3 I;
uniform vec4 baseColor;
uniform float shininess;
void main(void)
{
vec3 E = normalize(invTBN * eye);
vec3 R = reflect(E, fNormal);
vec3 light = normalize(invTBN * (lightPos - fPosition));
vec4 diffuse = vec4(kd * max(dot(light, fNormal), 0.0) * I , 1.0) * baseColor;
vec3 specular = ks * I * pow(clamp(dot(R, light), 0.0, 1.0), shininess);
gl_FragColor = diffuse + vec4(specular, 1.0) + 0.4*textureCube(skybox, -R);
}
</script>
<script>
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var canvas = document.getElementById('my_Canvas');
gl = canvas.getContext('experimental-webgl');
var vertices = [];
var indices = [];
//Main Tesselation Function
//tess ( level of tesselation, base length per triangle, x and y offset from top left)
function tess(level,dist,offx,offy){
//Get Number of Edge vertices
var num= Math.pow(2,level)+1;
console.log("Tesselation Level: "+level);
console.log("Edge Vertices: "+num);
//Initialize vertice array
vertices = [];
//Vertice Generation Loop
var x;
var y;
for(y=0;y<num;y++) {
for (x = 0; x < num * 3; x++) {
// console.log("x "+x+ " y "+y)
//Get X val
vertices[x +y*num*3] = -1.0+offx+(x/3)*(dist*2)/(num-1);
//Get Y val
vertices[x + 1 +y*num*3 ] = 1.0-offy-(y)*(dist*2)/(num-1);
//Get Z val (zero for uniform plane)
vertices[x + 2 +y*num*3 ] = -0.6;
x += 2;
}
}
//Indices
indices = [];
//Ver used for indices index
//Half Triangles
if (renType==0) {
var ver = 0;
for (y = 0; y < (num - 1); y++) {
for (x = 0; x < (num - 1) * 3; x++) {
//Get X
indices[x + y * (num - 1) * 3] = ver + y;
//Get Y
indices[x + 1 + y * (num - 1) * 3] = ver + 1 + y;
//Get Z
indices[x + 2 + y * (num - 1) * 3] = ver + num + y;
x += 2;
ver++;
}
}
}
//All Triangles
if (renType==1) {
var ver = 0;
for (y = 0; y < (num - 1); y++) {
for (x = 0; x < (num - 1) * 6; x++) {
//Top Triangle
//Get v0
indices[x + y * (num - 1) * 6] = ver + y;
//Get v1
indices[x + 1 + y * (num - 1) * 6] = ver + 1 + y;
//Get v2
indices[x + 2 + y * (num - 1) * 6] = ver + num + y;
// x += 2;
// ver++;
//Bottom Triangle
//Get v0
indices[x + 3 + y * (num - 1) * 6] = ver + 1+ y;
//Get v1
indices[x + 4 + y * (num - 1) * 6] = ver + num + y;
//Get v2
indices[x + 5 + y * (num - 1) * 6] = ver + num + y + 1;
x += 5;
ver++;
}
}
}
}
// console.table(indices.toString())
console.log("Total Vertices: "+vertices.length/3);
console.log("Indices: "+indices.length);
console.log("");
//tess(0,0.5);
//draw(gl.LINES);
//document.getElementById("tessval").innerHTML = 0;
function setTess(ptype) {
var x = document.getElementById("tessRange").value;
document.getElementById("tessval").innerHTML = x;
tess(x,1.0,0.0,0.0);
draw(ptype);
}
setInterval("draw(ptype);", 40);
document.onkeydown = handleKeyDown;
var renType =1;
ptype=gl.TRIANGLES;
setTess(ptype);
// Get the rotation matrix from given angles
function getTransformationMatrix(rx, ry, rz)
{
//Store Cosine, and Sin * angles for easier matrix creation
var cx = Math.cos(rx), sx = Math.sin(rx);
var cy = Math.cos(ry), sy = Math.sin(ry);
var cz = Math.cos(rz), sz = Math.sin(rz);
// Returns rotation matrix
return new Float32Array([cy*cz, (sx*sy*cz-cx*sz), (sx*sz+cx*sy*cz), 0, //Row1
cy*sz, (sx*sy*sz+cx*cz),(cx*sy*sz-sx*cz),0, //Row2
-sy,sx*cy, cx*cy, 0, //Row3
0,0,0,1]);//Row4
}
//Get time
var startTime = new Date().getTime();
// Wave Parameter Functions
function generateWaves(count)
{
numWaves = count;
steepnessList = [];
amplitudeList = [];
dirList = [];
phaseConstantList = [];
for(var i = 0; i < count; i++)
{
}
}
// Wave Parameter Bounds
var steepnessMin = 0.0;
var steepnessMax = 0.8;
var amplitudeMin = 0.01;
var amplitudeMax = 1.0;
var phaseConstMin = 1.0;
var phaseConstMax = 2.0;
// Generated Wave Parameters
var numWaves = 3;
var steepnessList = [0.15, 0.50, 0.15];
var amplitudeList = [0.1, 0.05, 0.1];
var dirList = [1, 0, 0, 1, -1, 1];
var phaseConstantList = [8.169, 5.2, 8.12];
var wList = [9.28, 2.48, 7.2];
function setW1() {
var x = document.getElementById("w1Range").value;
document.getElementById("W1A").innerHTML = x;
amplitudeList[0]=0.05*x;
}
function setW2() {
var x = document.getElementById("w2Range").value;
document.getElementById("W2A").innerHTML = x;
amplitudeList[1]=0.05*x;
}
function setW3() {
var x = document.getElementById("w3Range").value;
document.getElementById("W3A").innerHTML = x;
amplitudeList[2]=0.05*x;
}
setW1();
setW2();
setW3();
//Default Scene rotation
var ax=-110; ///-110
var ay =0;
var az=20; ///20
function handleKeyDown(event) {
if (event.keyCode == 37) {
//Left Arrow Key
az+=-5*-2*Math.PI/360;
} else if (event.keyCode == 38) {
//Up Arrow Key
ax+=5*-2*Math.PI/360;
} else if (event.keyCode == 39) {
//Right Arrow Key
az+=5*-2*Math.PI/360;
} else if (event.keyCode == 40) {
//Down Arrow Key
ax+=-5*-2*Math.PI/360;
}
//ax *= -2*Math.PI/360; ay *= -2*Math.PI/360; az *= -2*Math.PI/360;
}
//Proj matrix convert
ax *= -2*Math.PI/360; ay *= -2*Math.PI/360; az *= -2*Math.PI/360;
function draw(ptype) {
// Create an empty buffer object to store vertex buffer
var vertex_buffer = gl.createBuffer();
// Bind appropriate array buffer to it
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Pass the vertex data to the buffer
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
// Unbind the buffer
gl.bindBuffer(gl.ARRAY_BUFFER, null);
// Create an empty buffer object to store Index buffer
var Index_Buffer = gl.createBuffer();
// Bind appropriate array buffer to it
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);
// Pass the vertex data to the buffer
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
// Unbind the buffer
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
/*================ Shaders ====================*/
var vertShader = getShader(gl, "wave-vertex");
var fragShader = getShader(gl, "wave-fragment");
var shaderProgram = gl.createProgram();
// Attach a vertex shader
gl.attachShader(shaderProgram, vertShader);
// Attach a fragment shader
gl.attachShader(shaderProgram, fragShader);
// Link both the programs
gl.linkProgram(shaderProgram);
var timeNow =(new Date().getTime() -startTime)/1000;
// Use the combined shader program object
gl.useProgram(shaderProgram);
gl.uniform1f(gl.getUniformLocation( shaderProgram, 'time' ), timeNow);
/*======= Associating shaders to buffer objects =======*/
// Bind vertex buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Bind index buffer object
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);
// Get the attribute location
var coord = gl.getAttribLocation(shaderProgram, "coordinates");
// Point an attribute to the currently bound VBO
gl.vertexAttribPointer(coord, 3, gl.FLOAT, false, 0, 0);
// Enable the attribute
gl.enableVertexAttribArray(coord);
var amvp = gl.getUniformLocation(shaderProgram, "mvp");
var mat = getTransformationMatrix(ax, ay, az);
gl.uniformMatrix4fv(amvp, false, mat);
// Wave Parameters
gl.uniform1i(gl.getUniformLocation(shaderProgram, "numWaves"), numWaves);
gl.uniform1fv(gl.getUniformLocation(shaderProgram, "amplitudeList"), new Float32Array(amplitudeList));
gl.uniform1fv(gl.getUniformLocation(shaderProgram, "steepnessList"), new Float32Array(steepnessList));
gl.uniform1fv(gl.getUniformLocation(shaderProgram, "phaseConstantList"), new Float32Array(phaseConstantList));
gl.uniform2fv(gl.getUniformLocation(shaderProgram, "dirList"), new Float32Array(dirList));
gl.uniform1fv(gl.getUniformLocation(shaderProgram, "wList"), new Float32Array(wList));
// Yay Lights
gl.uniform3f(gl.getUniformLocation(shaderProgram, "lightPos"), 0.0, 0.0, 9.0);
gl.uniform3f(gl.getUniformLocation(shaderProgram, "kd"), 0.7, 0.7, 0.7);
gl.uniform3f(gl.getUniformLocation(shaderProgram, "ks"), 0.0, 0.0, 1.0);
gl.uniform3f(gl.getUniformLocation(shaderProgram, "I"), 1.0, 1.0, 1.0);
gl.uniform4f(gl.getUniformLocation(shaderProgram, "baseColor"), 0.0, 0.0, 0.6, 1.0);
gl.uniform1f(gl.getUniformLocation(shaderProgram, "shininess"), 1.0);
// Clear the canvas
gl.clearColor(0.5, 0.5, 0.5, 0.9);
// Enable the depth test
gl.enable(gl.DEPTH_TEST);
// Clear the color buffer bit
gl.clear(gl.COLOR_BUFFER_BIT);
// Set the view port
gl.viewport(0, 0, canvas.width, canvas.height);
// Draw the triangle
gl.drawElements(ptype, indices.length, gl.UNSIGNED_SHORT, 0);
}
function loadTextureCube(urls) {
var ct = 0;
var img = new Array(6);
var urls = [
"skybox/skyrender0003.jpg", "skybox/skyrender0003.jpg",
"skybox/skyrender0003.jpg", "skybox/skyrender0003.jpg",
"skybox/skyrender0003.jpg", "skybox/skyrender0003.jpg"
];
for (var i = 0; i < 6; i++) {
img[i] = new Image();
img[i].onload = function() {
ct++;
if (ct == 6) {
texID = gl.createTexture();
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texID);
var targets = [
gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
gl.TEXTURE_CUBE_MAP_POSITIVE_Z, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
];
for (var j = 0; j < 6; j++) {
gl.texImage2D(targets[j], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img[j]);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
}
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
draw();
}
}
img[i].src = urls[i];
}
}
loadTextureCube();
//GL TYPES
// POINTS, LINE_STRIP, LINE_LOOP, LINES,
// TRIANGLE_STRIP,TRIANGLE_FAN, TRIANGLES
</script>
</body>
</html>