Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / 02-Practice-17.10.2022 / Problem 0205.html
blob: 37c4127bc51a730f304cc844f9b4d0fc3ab27189 (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
<!DOCTYPE html>

<head>
	<meta charset="utf-8"/>
	<title>GLSL пеперуда</title>

	<script src="webgl-fmi.js"></script>	
	
	<script id="vshader" type="x-shader/x-vertex">
		#define PI 3.1415926538
		#define ASPECT_RATIO 1.5

		attribute float aPoint;
		attribute float aPointCnt;

		attribute vec3 aRGB;
		varying vec3 vRGB;

		void main ()
		{
			float alpha = aPoint * (360.0 / aPointCnt) * (PI / 180.0);
			float mag = (exp(cos(alpha)) - 2.0 * cos(4.0 * alpha) + pow(sin(alpha / 15.0), 5.0)) / 2.475;
			gl_Position = vec4(mag * sin(alpha) / ASPECT_RATIO, mag * cos(alpha) - 0.268, 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");

			aPoint = gl.getAttribLocation(glprog, "aPoint");
			aPointCnt = gl.getAttribLocation(glprog, "aPointCnt");
			aRGB = gl.getAttribLocation(glprog,"aRGB");
			
			gl.clearColor(1,1,1,1);
			gl.clear(gl.COLOR_BUFFER_BIT);

			var n = 250;
			var data = [ ];
			for(var i=0; i<n; i++) data.push(i);

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

			gl.vertexAttrib3f(aRGB,1,0.2,0);

			gl.vertexAttribPointer(aPoint, 1, gl.FLOAT, false, 0, 0);
			gl.enableVertexAttribArray(aPoint);
			gl.vertexAttrib1f(aPointCnt, n);
			gl.drawArrays(gl.LINE_LOOP,0,n);
		}
	</script>	
</head>

<body onload="start()">
	<h2>GLSL пеперуда</h2>

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