Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / 02-Practice-17.10.2022 / Problem 0206.html
blob: b1dbf825358cc412ebe4b4bdcfedfcc1e3623fd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!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 = 20;
			var data = [];
			
			// Axies
			data.push( 0.9,    0, 0, 0, 0);
			data.push(-0.9,    0, 0, 0, 0);
			data.push( 0,    0.9, 0, 0, 0);
			data.push( 0,   -0.9, 0, 0, 0);

			// Figure
			const aspectRatio = 600 / 400;
			const range = 3;
			var step = 1.8 / n;
			for (var x = -0.9; x < 0.899; x += step) {
				data.push(x, Math.sin(x * range) / range, 1, 0, 0);
				data.push(x, Math.cos(x * range) / range, 1, 0, 0);
				var tx = x + step / 2;
				data.push(tx, Math.cos(tx * range) / range, 1, 0, 0);
				data.push(tx, Math.sin(tx * range) / range, 1, 0, 0);
			}

			var buf = gl.createBuffer();
			gl.bindBuffer(gl.ARRAY_BUFFER,buf);
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);

			gl.enableVertexAttribArray(aXY);
			gl.enableVertexAttribArray(aRGB);
			gl.vertexAttribPointer(aXY,2,gl.FLOAT,false, 5 * FLOATS, 0);
			gl.vertexAttribPointer(aRGB, 3, gl.FLOAT, false, 5 * FLOATS, 2 * FLOATS);
			gl.drawArrays(gl.LINES, 0, 4);
			gl.drawArrays(gl.LINE_STRIP, 4, n * 4);
		}
	</script>	
</head>

<body onload="start()">
	<h2>Синус-косинус</h2>

	<noscript>
		Искаме JavaScript, но няма!
	</noscript>
	
	<canvas id="picasso" width="600" height="400" style="border: solid;">
		Искаме canvas, но няма!
	</canvas>
</body>