diff options
[WGL/P07] Solved some of the exercises from this weeks practicum
1 files changed, 206 insertions, 0 deletions
| -rw-r--r-- | Year 2/Computer Graphics with WebGL/07-Practice-21.11.2022/Problem 0701.html | 206 |
diff --git a/Year 2/Computer Graphics with WebGL/07-Practice-21.11.2022/Problem 0701.html b/Year 2/Computer Graphics with WebGL/07-Practice-21.11.2022/Problem 0701.html new file mode 100644 index 0000000..76a9800 --- /dev/null +++ b/Year 2/Computer Graphics with WebGL/07-Practice-21.11.2022/Problem 0701.html @@ -0,0 +1,206 @@ +<!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;
+ gl_Position = uProjectionMatrix * mvMatrix * vec4(aXYZ,1);
+ mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix;
+
+ // околна светлина
+ if (uUseAmbient)
+ vColor = uAmbientColor*aColor;
+ else
+ vColor = aColor;
+
+ // дифузно отражение
+ if (uUseDiffuse)
+ {
+ vec3 light = normalize(-uLightDir);
+ vec3 normal = vec3(normalize(nMatrix*vec4(aNormal,0)));
+ vColor += aColor*uDiffuseColor*max(dot(normal,light),0.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.4,0.5,0.3,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,0.75,0.75,0.75);
+ gl.uniform1i(uUseDiffuse,true);
+
+ gl.uniform3f(uLightDir,0,0,-1);
+
+
+ // създаване на движещо се топче
+ ball = new Sphere([0,0,0],1);
+ ball.color = [1,1,1];
+
+ // създаване на страни на n-ъгълник
+ n = 11; // брой върхове
+ p = []; // масив със сфери за върховете
+ s = []; // масив с цилиндри за страните
+ m = []; // масив с матриците на цилиндрите
+ for (var i=0; i<n; i++)
+ {
+ // създаване на сфери във върховете
+ var a = 2*PI*(i+random(-0.3,0.3))/n;
+ p.push(new Sphere([10*cos(a),10*sin(a),random(-4,4)],0.2));
+ // създаване на неориентирани цилиндри
+ s.push(new Cylinder([0,0,0],0.2,1));
+ }
+ // пресмятане на дължината и ориентиращата
+ // матрица на всеки цилиндър
+ for (var i=0; i<n; i++)
+ {
+ var j=(i+1)%n;
+ var w = vectorPoints(p[j].center,p[i].center);
+ s[i].height = Math.sqrt(scalarProduct(w,w)); // дължина на цилиндър
+ var u = [0,0,1];
+ var v = unitVector(vectorProduct(w,u));
+ u = unitVector(vectorProduct(v,w));
+ w = unitVector(w);
+ var q = p[i].center;
+ m.push(
+ [ u[0],u[1],u[2],0, // локална X координата
+ v[0],v[1],v[2],0, // локална Y координата
+ w[0],w[1],w[2],0, // локална Z координата
+ q[0],q[1],q[2],1 ]);// глобална позиция
+ }
+ }
+
+
+ function linearInterpolation(p1, p2, per) {
+ return [
+ p1[0] * (1 - per) + p2[0] * per,
+ p1[1] * (1 - per) + p2[1] * per,
+ p1[2] * (1 - per) + p2[2] * per,
+ ];
+ }
+ var t = 0;
+ var tStep = 0.01;
+ var currI = 0;
+ var nextI = 1;
+
+ var frame = 0;
+ function drawFrame()
+ {
+ frame++;
+ gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);
+ lookAt([40*Math.cos(frame/100),40*Math.sin(frame/100),5], [0,0,0], [0,0,1]);
+ yRotate(0.1);
+
+ // рисуване на многоъгълника
+ for (var i=0; i<n; i++)
+ {
+ p[i].draw();
+ pushMatrix();
+ glmatnew = true;
+ glmat = multiplyMatrix(glmat,m[i]);
+ s[i].draw();
+ popMatrix();
+ }
+
+ // тук да се направи движение на топчето
+ t += tStep;
+ if (t > 1) {
+ t = 0;
+ currI = (currI + 1) % p.length;
+ nextI = (nextI + 1) % p.length;
+ }
+ pushMatrix();
+ translate(linearInterpolation(p[currI].center, p[nextI].center, t))
+ ball.draw()
+ popMatrix();
+
+ 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>
|
