Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / 03-Practice-24.10.2022 / Problem 0303.html
blob: f99c783392e8bcb14ffcfa90bd6f8bdb6ff5375b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE html>

<head>
	<meta charset="utf-8"/>
	<title>Зъбно колело</title>

	<script src="webgl-fmi.js"></script>	
	
	<script id="vshader" type="x-shader/x-vertex">
		// ъгъл по зъбното колело
		attribute float aAngle;

		attribute float aMin;
		attribute float aMax;
		attribute float aRadius;
		attribute float aFluct;
		#define TEETH 11.0

		void main ()
		{
			// тук да се оправи да се рисуват елементите на
			// зъбното колело - както зъбчатата част, така 
			// и кръглата и плътно кръглата
			float r = aRadius + aFluct * clamp(sin(aAngle * TEETH), aMin, aMax);

			gl_Position = vec4(r * cos(aAngle)/1.5, r * sin(aAngle),0,1);
		}
	</script>
	
	<script id="fshader" type="x-shader/x-fragment">
		precision mediump float;
		uniform vec3 uColor;
		void main( )
		{
			gl_FragColor = vec4(uColor,1);
		}
	</script>

	<script>
		function start( )
		{
			gl = getContext("picasso");
			glprog = getProgram("vshader","fshader");

			uColor = gl.getUniformLocation(glprog,"uColor");
			aAngle = gl.getAttribLocation(glprog,"aAngle");
			aMin = gl.getAttribLocation(glprog, "aMin");
			aMax = gl.getAttribLocation(glprog, "aMax");
			aRadius = gl.getAttribLocation(glprog, "aRadius");
			aFluct = gl.getAttribLocation(glprog, "aFluct");
			
			gl.clearColor(1,1,1,1);
			gl.clear(gl.COLOR_BUFFER_BIT);

			// точки по зъбното колело
			var n = 500;

			// n ъгъла от 0 до 2*pi
			var data = [];
			for(var i=0; i<n; i++) data.push(2*Math.PI*i/n);
			
			var buf = gl.createBuffer();
			gl.bindBuffer(gl.ARRAY_BUFFER,buf);
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);

			gl.uniform3f(uColor,0.3,0.7,0.2);
			gl.enableVertexAttribArray(aAngle);
			gl.vertexAttribPointer(aAngle,1,gl.FLOAT,false,0,0);

			gl.vertexAttrib1f(aRadius, 0.8);
			gl.vertexAttrib1f(aFluct, 0.2);
			gl.vertexAttrib1f(aMin, -0.5);
			gl.vertexAttrib1f(aMax, 0.5);
			gl.drawArrays(gl.LINE_LOOP,0,n);

			gl.vertexAttrib1f(aRadius, 0.55);
			gl.vertexAttrib1f(aFluct, 0.0);
			gl.drawArrays(gl.LINE_LOOP,0,n);

			gl.vertexAttrib1f(aRadius, 0.52);
			gl.drawArrays(gl.LINE_LOOP,0,n);

			var radius = 0.1;
			var steps = 100;
			for (var i = 0; i < 100; i++) {
				gl.vertexAttrib1f(aRadius, radius * i / steps);
				gl.drawArrays(gl.LINE_LOOP,0,n);
			}
		}
	</script>	
</head>

<body onload="start()">
	<h2>Зъбно колело</h2>

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