1
2/*
3Copyright (c) 2019 The Khronos Group Inc.
4Use of this source code is governed by an MIT-style license that can be
5found in the LICENSE.txt file.
6*/
7
8
9attribute vec4 gtf_Vertex;
10attribute vec4 gtf_Color;
11uniform mat4 gtf_ModelViewProjectionMatrix;
12varying vec4 color;
13
14void main (void)
15{
16	const float M_PI = 3.14159265358979323846;
17	float x = 2.0 * (gtf_Color.g - 0.5);
18	float y = 2.0 * (gtf_Color.b - 0.5);
19	float atan_c = 0.0;
20	float scale = 1.0;
21	float sign = 1.0;
22	vec4 result = vec4(0.0, 0.0, 0.0, 1.0);
23	const float epsilon = 1.0e-4;
24
25	// Avoid evaluating atan(0, x) for x < epsilon because it's implementation-dependent
26	if(x > epsilon || abs(y) > epsilon)
27	{
28		if(x < 0.0 ^^ y < 0.0)
29		{
30			sign = -1.0;
31		}
32
33		if(abs(y) <= abs(x))
34		{
35			float c = abs(y / x);
36
37			// Taylors series expansion for atan
38			for(int i = 1; i < 12; i += 2)
39			{
40				atan_c += scale * pow(c, float(i)) / float(i);
41				scale *= -1.0;
42			}
43
44			result = vec4(sign * atan_c / (2.0 * M_PI) + 0.5, 0.0, 0.0, 1.0);
45		}
46		else
47		{
48			float c = abs(x / y);
49
50			// Taylors series expansion for atan
51			for(int i = 1; i < 12; i += 2)
52			{
53				atan_c += scale * pow(c, float(i)) / float(i);
54				scale *= -1.0;
55			}
56
57			result = vec4(sign * (M_PI / 2.0 - atan_c) / (2.0 * M_PI) + 0.5, 0.0, 0.0, 1.0);
58		}
59
60		if(x < 0.0)
61			if(y < 0.0) result.r -= 0.5;
62			else if(y > 0.0) result.r += 0.5;
63	}
64
65	color = result;
66	gl_Position = gtf_ModelViewProjectionMatrix * gtf_Vertex;
67}
68
69