diff options
[WGL/P02] Solved practicum exercises
1 files changed, 83 insertions, 0 deletions
| -rw-r--r-- | Year 2/Computer Graphics with WebGL/02-Practice-17.10.2022/Problem 0209.html | 83 |
diff --git a/Year 2/Computer Graphics with WebGL/02-Practice-17.10.2022/Problem 0209.html b/Year 2/Computer Graphics with WebGL/02-Practice-17.10.2022/Problem 0209.html new file mode 100644 index 0000000..51728d9 --- /dev/null +++ b/Year 2/Computer Graphics with WebGL/02-Practice-17.10.2022/Problem 0209.html @@ -0,0 +1,83 @@ +<!DOCTYPE html>
+
+<head>
+ <meta charset="utf-8"/>
+ <title>Колело на цветовете</title>
+
+ <script src="webgl-fmi.js"></script>
+
+ <script id="vshader" type="x-shader/x-vertex">
+ attribute vec2 aXY;
+ attribute vec3 aRGB;
+ varying vec3 vRGB;
+ void main ()
+ {
+ gl_Position = vec4(aXY,0,1);
+ gl_PointSize = 4.0;
+ vRGB = aRGB;
+ }
+ </script>
+
+ <script id="fshader" type="x-shader/x-fragment">
+ precision mediump float;
+ varying vec3 vRGB;
+ void main( )
+ {
+ gl_FragColor = vec4(vRGB,1);
+ }
+ </script>
+
+ <script>
+ function start( )
+ {
+ gl = getContext("picasso");
+ glprog = getProgram("vshader","fshader");
+
+ aXY = gl.getAttribLocation(glprog,"aXY");
+ aRGB = gl.getAttribLocation(glprog,"aRGB");
+
+ gl.clearColor(1,1,1,1);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+
+ // брой сегменти n
+ var n = 60;
+
+ var points = [];
+ const aspectRatio = 600 / 400;
+ const r = 0.8;
+
+ points.push(0, 0, 1, 1, 1);
+ const step = 360 / n;
+ // https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative
+ function hueToRgbf(n, H) {
+ var k = (n + H / 60) % 6;
+ return 1 - Math.max(0, Math.min(k, 4-k, 1));
+ }
+ const offset = 90;
+ for (var i = 0; i < 361; i += step) {
+ points.push(Math.sin(2 * Math.PI * (i + offset) / 360) * r / aspectRatio, Math.cos(2 * Math.PI * (i + offset) / 360) * r, hueToRgbf(5, i), hueToRgbf(3, i), hueToRgbf(1, i));
+ }
+
+ var buf = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER, buf);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(points), gl.STATIC_DRAW);
+ gl.vertexAttribPointer(aXY, 2, gl.FLOAT, false, 5 * FLOATS, 0);
+ gl.vertexAttribPointer(aRGB, 3, gl.FLOAT, false, 5 * FLOATS, 2 * FLOATS);
+ gl.enableVertexAttribArray(aXY);
+ gl.enableVertexAttribArray(aRGB);
+ gl.drawArrays(gl.TRIANGLE_FAN, 0, n + 2);
+ }
+ </script>
+</head>
+
+<body onload="start()">
+ <h2>Колело на цветовете</h2>
+
+ <noscript>
+ Искаме JavaScript, но няма!
+ </noscript>
+
+ <canvas id="picasso" width="600" height="400" style="border: solid;">
+ Искаме canvas, но няма!
+ </canvas>
+</body>
|
