diff options
[WGL/P04] Started working on exercises
1 files changed, 140 insertions, 0 deletions
| -rw-r--r-- | Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0401.html | 140 |
diff --git a/Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0401.html b/Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0401.html new file mode 100644 index 0000000..d8aafb5 --- /dev/null +++ b/Year 2/Computer Graphics with WebGL/04-Pracitce-31.10.2022/Problem 0401.html @@ -0,0 +1,140 @@ +<!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;
+ void main ()
+ {
+ gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(aXYZ,1);
+ }
+ </script>
+
+ <script id="fshader" type="x-shader/x-fragment">
+ void main( )
+ {
+ gl_FragColor = vec4(1,0,0,1);
+ }
+ </script>
+
+ <script>
+ function drawCube()
+ {
+ useMatrix();
+ gl.drawArrays(gl.LINE_LOOP,0,4);
+ gl.drawArrays(gl.LINE_LOOP,4,4);
+ gl.drawArrays(gl.LINES,8,8);
+ }
+
+ function start( )
+ {
+ var canvas = document.getElementById("picasso");
+ canvas.addEventListener('webglcontextlost',handleContextLost,false);
+ canvas.addEventListener('webglcontextrestored',handleContextRestored,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");
+
+ gl.clearColor(1,1,1,1);
+
+ var proj = perspMatrix(30,gl.canvas.width/gl.canvas.height,1,40000);
+ gl.uniformMatrix4fv(uProjectionMatrix,false,proj);
+
+ view = viewMatrix([4,3,1], [0,0,0], [0,0,1]);
+ gl.uniformMatrix4fv(uViewMatrix,false,view);
+
+ // куб 1x1x1
+ var data = [ 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,
+
+ 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 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,0,0);
+ }
+
+ var frame = 0;
+
+ var smallCubeSize = 0.2;
+ function drawFrame()
+ {
+ frame++;
+ gl.clear(gl.COLOR_BUFFER_BIT);
+
+ identity();
+ zRotate(frame);
+ drawCube(frame);
+
+ scale([smallCubeSize, smallCubeSize, smallCubeSize]);
+ var length = 2 + smallCubeSize * 5 / 2;
+ translate([0, 0, length]);
+ for (var i = 0; i < 4; i++) {
+ pushMatrix();
+
+ translate([Math.cos(Math.PI / 2 * i) * length, Math.sin(Math.PI / 2 * i) * length, 0]);
+ drawCube(frame);
+
+ popMatrix();
+ }
+
+ requestAnimationFrame(drawFrame);
+ }
+
+ function handleContextLost(event)
+ {
+ event.preventDefault();
+ }
+
+ function handleContextRestored(event)
+ {
+ init();
+ }
+
+ </script>
+</head>
+
+<body onload="start()">
+ <h2>Кубове по ръбовете</h2>
+
+ <noscript>
+ Искаме JavaScript, но няма!
+ </noscript>
+
+ <canvas id="picasso" width="600" height="400" style="border: solid;">
+ Искаме canvas, но няма!
+ </canvas>
+</body>
|
