diff options
[WGL/P07] Solved some of the exercises from this weeks practicum
1 files changed, 149 insertions, 0 deletions
| -rw-r--r-- | Year 2/Computer Graphics with WebGL/07-Practice-21.11.2022/Problem 0703.html | 149 |
diff --git a/Year 2/Computer Graphics with WebGL/07-Practice-21.11.2022/Problem 0703.html b/Year 2/Computer Graphics with WebGL/07-Practice-21.11.2022/Problem 0703.html new file mode 100644 index 0000000..60288c1 --- /dev/null +++ b/Year 2/Computer Graphics with WebGL/07-Practice-21.11.2022/Problem 0703.html @@ -0,0 +1,149 @@ +<!DOCTYPE html>
+
+<head>
+ <meta charset="utf-8"/>
+ <title>Тройно махало</title>
+
+ <script src="webgl-fmi.js"></script>
+
+ <script id="vshader" type="x-shader/x-vertex">
+ uniform mat4 uProjectionMatrix;
+ uniform mat4 uViewMatrix;
+ uniform mat4 uModelMatrix;
+ uniform mat4 uNormalMatrix; // матрица за нормалните вектори
+ uniform bool uUseNormalMatrix; // дали матрицата се подава отвън
+
+ uniform bool uUseAmbient;
+ uniform vec3 uAmbientColor;
+
+ uniform bool uUseDiffuse;
+ uniform vec3 uDiffuseColor;
+
+ uniform vec3 uLightDir;
+
+ attribute vec3 aXYZ;
+ attribute vec3 aColor;
+ attribute vec3 aNormal;
+
+ varying vec3 vColor;
+
+ void main ()
+ {
+ mat4 mvMatrix = uViewMatrix * uModelMatrix;
+ vec4 pos = mvMatrix * vec4(aXYZ,1);
+ gl_Position = uProjectionMatrix * pos;
+ mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix;
+
+ // околна светлина
+ if (uUseAmbient)
+ vColor = uAmbientColor*aColor;
+ else
+ vColor = aColor;
+
+ // дифузно отражение
+ vec3 light = normalize(-uLightDir);
+ vec3 normal = vec3(normalize(nMatrix*vec4(aNormal,0)));
+ if (uUseDiffuse)
+ {
+ vColor += aColor*uDiffuseColor*max(dot(normal,light),0.0);
+ }
+
+ // огледално отражение
+ vec3 reflectedLight = normalize(reflect(light,normal));
+ vec3 viewDir = normalize( pos.xyz/pos.w );
+ float cosa = max(dot(reflectedLight,viewDir),0.0);
+ vColor += vec3(1,1,1)*pow(cosa,50.0);
+ }
+ </script>
+
+ <script id="fshader" type="x-shader/x-fragment">
+ precision mediump float;
+ varying vec3 vColor;
+ void main( )
+ {
+ gl_FragColor = vec4(vColor,1);
+ }
+ </script>
+
+ <script>
+ function start( )
+ {
+ var canvas = document.getElementById("picasso");
+ canvas.addEventListener('webglcontextlost',function(event){event.preventDefault();},false);
+ canvas.addEventListener('webglcontextrestored',function(){init();},false);
+
+ init();
+ drawFrame();
+ }
+
+ function init()
+ {
+ gl = getContext("picasso");
+ glprog = getProgram("vshader","fshader");
+
+
+ aXYZ = gl.getAttribLocation(glprog,"aXYZ");
+ uProjectionMatrix = gl.getUniformLocation(glprog,"uProjectionMatrix");
+ uViewMatrix = gl.getUniformLocation(glprog,"uViewMatrix");
+ uModelMatrix = gl.getUniformLocation(glprog,"uModelMatrix");
+ uNormalMatrix = gl.getUniformLocation(glprog,"uNormalMatrix");
+ uUseNormalMatrix = gl.getUniformLocation(glprog,"uUseNormalMatrix");
+
+ aColor = gl.getAttribLocation(glprog,"aColor");
+ uAmbientColor = gl.getUniformLocation(glprog,"uAmbientColor");
+ uUseAmbient = gl.getUniformLocation(glprog,"uUseAmbient");
+
+ aNormal = gl.getAttribLocation(glprog,"aNormal");
+ uDiffuseColor = gl.getUniformLocation(glprog,"uDiffuseColor");
+ uUseDiffuse = gl.getUniformLocation(glprog,"uUseDiffuse");
+ uLightDir = gl.getUniformLocation(glprog,"uLightDir");
+
+ gl.enable(gl.DEPTH_TEST);
+ gl.clearColor(0.9,1,0.9,1);
+
+ identity();
+ perspective(30,gl.canvas.width/gl.canvas.height,1,40000);
+ gl.uniform1i(uUseNormalMatrix,false);
+
+ gl.uniform3f(uAmbientColor,0.3,0.3,0.3);
+ gl.uniform1i(uUseAmbient,true);
+
+ gl.uniform3f(uDiffuseColor,1,1,1);
+ gl.uniform1i(uUseDiffuse,true);
+
+ gl.uniform3f(uLightDir,0,0,-1);
+
+
+ // създаване на махало - масив от съответните елементи
+ pendulum = [];
+ }
+
+
+ var frame = 0;
+
+ function drawFrame()
+ {
+ frame++;
+ gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);
+ var a = 0.3*sin(frame/100);
+ lookAt([130*Math.cos(a),130*Math.sin(a),-36], [0,0,-36], [0,0,1]);
+
+ // рисуване на три навързани махала
+ // ...
+
+ requestAnimationFrame(drawFrame);
+ }
+ </script>
+</head>
+
+<body onload="start()">
+ <h2>Тройно махало</h2>
+
+ <noscript>
+ Искаме JavaScript, но няма!
+ </noscript>
+
+ <canvas id="picasso" width="600" height="400" style="border: solid;">
+ Искаме canvas, но няма!
+ </canvas>
+</body>
|
