Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / 05-Practice-07.11.2022 / Problem 0501.html
diff options
context:
space:
mode:
Commita56c679cbe8ebc932d1d2d5ee31ec3134e391f83 (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-11-13 17:38:16 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-11-13 17:38:16 +0200
Files4630c326bee3baf41e91da27dd9a184c72d8af05 /Year 2/Computer Graphics with WebGL/05-Practice-07.11.2022/Problem 0501.html
uni-projects-a56c679cbe8ebc932d1d2d5ee31ec3134e391f83.tar
uni-projects-a56c679cbe8ebc932d1d2d5ee31ec3134e391f83.tar.gz
uni-projects-a56c679cbe8ebc932d1d2d5ee31ec3134e391f83.zip
Parent99aebf351d639a615fa989855a1993013f09c40e (diff)
[WGL/P05] Solved first exercise
1 files changed, 124 insertions, 0 deletions
-rw-r--r--Year 2/Computer Graphics with WebGL/05-Practice-07.11.2022/Problem 0501.html124
diff --git a/Year 2/Computer Graphics with WebGL/05-Practice-07.11.2022/Problem 0501.html b/Year 2/Computer Graphics with WebGL/05-Practice-07.11.2022/Problem 0501.html
new file mode 100644
index 0000000..3fe5e25
--- /dev/null
+++ b/Year 2/Computer Graphics with WebGL/05-Practice-07.11.2022/Problem 0501.html
@@ -0,0 +1,124 @@
+<!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;
+
+ attribute vec3 aXYZ;
+ attribute vec3 aColor;
+ varying vec3 vColor;
+
+ void main ()
+ {
+ mat4 mvMatrix = uViewMatrix * uModelMatrix;
+ gl_Position = uProjectionMatrix * mvMatrix * vec4(aXYZ,1);
+ vColor = aColor;
+ }
+ </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");
+
+ aColor = gl.getAttribLocation(glprog,"aColor");
+ gl.vertexAttrib3f(aColor,1,0.5,0);
+
+ gl.clearColor(1,1,1,1);
+
+ identity();
+ perspective(30,gl.canvas.width/gl.canvas.height,1,40000);
+
+ const N = 3;
+ const M = 8;
+
+ var data = [0,1,0];
+ for (var y = 0; y < M; y++) {
+ for (var n = y%2, x = y%2; n < N; x+=2, n++) {
+ data.push(x ,y,0, x+1,y+1,0, x+1,y ,0);
+ /* gl.TRIANGLE_STRIP работи, като поглежда последните две точки, за да направи
+ * триъгълник. Знаем, че ако някои две от 3те точки за един триъгълник съвпадат,
+ * няма да има триъгълник. Тоест, ако повторим последната точка и следващата
+ * (горе-ляво на следващия квадрат), то ще имаме дупката
+ */
+ data.push(x+1,y,0, x+2,y+1,0, x+2,y+1,0);
+ }
+ if (y < M - 1) {
+ if (y % 2 === 0)
+ data.push(N*2 ,y+1,0, 1,y+2,0, 1,y+2,0);
+ else
+ data.push(N*2-1,y+1,0, 0,y+2,0, 0,y+2,0);
+ }
+ }
+
+ var buf = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER,buf);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
+
+ gl.enableVertexAttribArray(aXYZ);
+ gl.vertexAttribPointer(aXYZ,3,gl.FLOAT,false,3*FLOATS,0*FLOATS);
+ }
+
+ var frame = 0;
+
+ function drawFrame()
+ {
+ frame++;
+ gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);
+
+ lookAt([1/2,1/2,30], [1/2,1/2,0], [Math.sin(frame/200),Math.cos(frame/200),0]);
+ useMatrix();
+
+ // рисуване на мрежата от квадрати с едно
+ // извикване на drawArrays
+ gl.drawArrays(gl.TRIANGLE_STRIP,0,142);
+
+ 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>