diff options
[WGL/H05] Solved homework
1 files changed, 208 insertions, 0 deletions
| -rw-r--r-- | Year 2/Computer Graphics with WebGL/Homework 05 - The cube/Homework.html | 208 |
diff --git a/Year 2/Computer Graphics with WebGL/Homework 05 - The cube/Homework.html b/Year 2/Computer Graphics with WebGL/Homework 05 - The cube/Homework.html new file mode 100644 index 0000000..963a4df --- /dev/null +++ b/Year 2/Computer Graphics with WebGL/Homework 05 - The cube/Homework.html @@ -0,0 +1,208 @@ +<!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 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);
+
+ // околна светлина
+ if (uUseAmbient)
+ vColor = uAmbientColor*aColor;
+ else
+ vColor = aColor;
+
+ // дифузно отражение
+ if (uUseDiffuse)
+ {
+ vec3 light = normalize(-uLightDir);
+ vec3 normal = vec3(normalize(mvMatrix*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.r,vColor.gb,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");
+ 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(2/4,2/4,2/4,1);
+
+ identity();
+ perspective(30,gl.canvas.width/gl.canvas.height,1,40000);
+
+ gl.uniform3f(uAmbientColor,0.4,0.4,0.4);
+ gl.uniform1i(uUseAmbient,true);
+
+ gl.uniform3f(uDiffuseColor,1,1,1);
+ gl.uniform1i(uUseDiffuse,true);
+
+ gl.uniform3f(uLightDir,0,0,-1);
+
+ const CUBE_COLOR = [0,0,0];
+ const POSSIBLE_COLORS = [
+ [0,0.607,0.282] /* Зелено */, [0.725,0,0] /* Червено */, [0,0.270,0.678] /* Синьо */,
+ [1,0.349,0] /* Оранжево */ , [1,1,1] /* Бяло */ , [1,0.835,0] /* Жълто */];
+ c = [];
+ // тук да се генерира Куб на Рубик
+ for (var i = 1; i < 30; i++) {
+ if (i % 10 === 0) i++;
+
+ c.push(new CubePlate( i, POSSIBLE_COLORS[Math.floor(random(0,5))], CUBE_COLOR));
+ c.push(new CubePlate(-i, POSSIBLE_COLORS[Math.floor(random(0,5))], CUBE_COLOR));
+ }
+ }
+
+ CubePlate = function(rubikPosition, color, cubeColor) {
+ /* Позиционирането работи малко гадно, като най-дясната цифра определя къде е плочката
+ * по едно лица на куба (цифри 1 до 9), и най-лявата определя по коя абсциса ще се "придвижи" (завърти)
+ * стената, а знака определя дали това ще е напред или назад.
+ * Доста е неудобно ако трябва да се правят сметки, които ползват позициите на плочките, обаче е
+ * много лесно да се итерира през всичките и много лесно се изчислява позицията и завъртането. */
+ var sign = Math.sign(rubikPosition);
+ var pInd = rubikPosition % 10 * sign - 1; // индекс на плочката по страната
+ var cInd = Math.floor(rubikPosition * sign / 10); // индекс позиция по куба
+ this.position = [pInd % 3 - 1, Math.floor(pInd / 3) - 1, 1.5];
+ this.rotation = [((cInd === 0) * sign) * 90, ((cInd === 1 || cInd === 2) * sign + (cInd === 2)) * 90];
+
+ this.color = color;
+ this.cubeColor = cubeColor;
+
+ // Формата на една плочка
+ var v = [ [ 0.5, 0.5,0 ], [ 0.5,-0.5,0],
+ [-0.5,-0.5,0 ], [-0.5, 0.5,0],
+ [ 0.4, 0.4,0.1], [ 0.4,-0.4,0.1],
+ [-0.4,-0.4,0.1], [-0.4, 0.4,0.1]];
+ var n = [ [1,0,1], [0,-1,1], [-1,0,1], [0,1,1], [0,0,1] ];
+ var data = [].concat(
+ v[0],n[0],v[1],n[0],v[4],n[0],
+ v[4],n[0],v[5],n[0],v[1],n[0],
+ v[1],n[1],v[2],n[1],v[5],n[1],
+ v[5],n[1],v[6],n[1],v[2],n[1],
+ v[2],n[2],v[3],n[2],v[6],n[2],
+ v[6],n[2],v[7],n[2],v[3],n[2],
+ v[3],n[3],v[0],n[3],v[7],n[3],
+ v[7],n[3],v[4],n[3],v[0],n[3],
+ v[7],n[4],v[4],n[4],v[5],n[4],
+ v[7],n[4],v[6],n[4],v[5],n[4],
+ );
+ var buf = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER, buf);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
+ this.buf = buf;
+ }
+
+ CubePlate.prototype.draw = function() {
+ pushMatrix();
+ xRotate(this.rotation[0]);
+ yRotate(this.rotation[1]);
+ translate(this.position);
+ useMatrix();
+
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.buf);
+ 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.vertexAttrib3fv(aColor, this.cubeColor);
+ gl.drawArrays(gl.TRIANGLES, 0, 24);
+ gl.vertexAttrib3fv(aColor, this.color);
+ gl.drawArrays(gl.TRIANGLES, 24, 6);
+
+ popMatrix();
+ }
+
+ var cubePlate;
+
+ var frame = 0;
+
+ function drawFrame()
+ {
+ frame++;
+ gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);
+
+ lookAt([0,10,0], [0,0,0], [0,-1,1]);
+ xRotate(0.4); yRotate(0.5); zRotate(0.2);
+
+ // рисуване на куба
+ c.forEach((part) => part.draw());
+
+ 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>
|
