Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / 04-Pracitce-31.10.2022 / Problem 0404.html
diff options
context:
space:
mode:
Commitcebdee09e597d8aa2b4d69f868cafafe22ede580 (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-11-07 20:18:07 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-11-07 20:18:07 +0200
Files476181ed7e72bcde1eab4cf08fdc50d3edda278b /Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0404.html
uni-projects-cebdee09e597d8aa2b4d69f868cafafe22ede580.tar
uni-projects-cebdee09e597d8aa2b4d69f868cafafe22ede580.tar.gz
uni-projects-cebdee09e597d8aa2b4d69f868cafafe22ede580.zip
Parentfaa737bc5c9edd86f752a3b70716014f25dfee71 (diff)
[WGL/P04] Started working on exercises
1 files changed, 159 insertions, 0 deletions
-rw-r--r--Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0404.html159
diff --git a/Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0404.html b/Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0404.html
new file mode 100644
index 0000000..bdf37a4
--- /dev/null
+++ b/Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0404.html
@@ -0,0 +1,159 @@
+<!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 vec3 uAmbientColor;
+ 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);
+
+ vec3 light = normalize(-uLightDir);
+ vec3 normal = vec3(normalize(mvMatrix*vec4(aNormal,0)));
+ vColor = uAmbientColor*aColor+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 drawCube()
+ {
+ useMatrix();
+ gl.drawArrays(gl.TRIANGLES,0,36);
+ }
+
+ 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");
+ uAmbientColor = gl.getUniformLocation(glprog,"uAmbientColor");
+ aNormal = gl.getAttribLocation(glprog,"aNormal");
+ uDiffuseColor = gl.getUniformLocation(glprog,"uDiffuseColor");
+ uLightDir = gl.getUniformLocation(glprog,"uLightDir");
+
+ gl.enable(gl.DEPTH_TEST); // включваме тест за дълбочина
+ gl.clearColor(1,1,1,1);
+
+ identity();
+ perspective(30,gl.canvas.width/gl.canvas.height,1,40000);
+
+ // куб 1x1x1
+ var v = [ [+0.5,-0.5,-0.5], [+0.5,+0.5,-0.5],
+ [-0.5,+0.5,-0.5], [-0.5,-0.5,-0.5],
+ [+0.5,-0.5,+0.5], [+0.5,+0.5,+0.5],
+ [-0.5,+0.5,+0.5], [-0.5,-0.5,+0.5] ];
+ var n = [ [1,0,0], [-1,0,0],
+ [0,1,0], [0,-1,0],
+ [0,0,1], [0,0,-1] ];
+ var data = [].concat(
+ v[0],n[0],v[1],n[0],v[4],n[0],
+ v[4],n[0],v[1],n[0],v[5],n[0],
+ v[6],n[1],v[2],n[1],v[7],n[1],
+ v[7],n[1],v[2],n[1],v[3],n[1],
+ v[5],n[2],v[1],n[2],v[6],n[2],
+ v[6],n[2],v[1],n[2],v[2],n[2],
+ v[4],n[3],v[7],n[3],v[0],n[3],
+ v[0],n[3],v[7],n[3],v[3],n[3],
+ v[4],n[4],v[5],n[4],v[7],n[4],
+ v[7],n[4],v[5],n[4],v[6],n[4],
+ v[0],n[5],v[3],n[5],v[1],n[5],
+ v[1],n[5],v[3],n[5],v[2],n[5] );
+
+ 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,6*FLOATS,0*FLOATS);
+
+ gl.enableVertexAttribArray(aNormal);
+ gl.vertexAttribPointer(aNormal, 3, gl.FLOAT, false, 6 * FLOATS, 3 * FLOATS);
+
+ gl.vertexAttrib3f(aColor,0.5,0.8,1);
+ gl.uniform3f(uAmbientColor,0.2,0,0);
+ gl.uniform3f(uDiffuseColor,1,1,1);
+ gl.uniform3f(uLightDir,0,0,-1);
+ }
+
+ var frame = 0;
+
+ var N = 5;
+ function drawFrame()
+ {
+ frame++;
+ gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);
+
+ lookAt([20*Math.cos(frame/300),20*Math.sin(frame/300),15], [0,0,0], [0,0,1]);
+
+ identity();
+
+ translate([-N / 2, -N / 2, 0]);
+ for (var x = 0; x < N; x++) {
+ pushMatrix();
+ translate([x, 0, 0]);
+ for (var y = 0; y < N; y++) {
+ translate([0, 1, 0]);
+ drawCube(frame);
+ }
+ 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>