]> git.sesse.net Git - movit/blob - widgets.cpp
In EffectChainTester, there is no point in making the FBO and texture persistent.
[movit] / widgets.cpp
1 #include <epoxy/gl.h>
2 #include <math.h>
3
4 #include "widgets.h"
5 #include "util.h"
6
7 #define HSV_WHEEL_SIZE 128
8
9 namespace movit {
10
11 GLuint hsv_wheel_num;
12
13 void draw_hsv_wheel(float y, float rad, float theta, float value)
14 {
15         glUseProgram(0);
16         check_error();
17         glActiveTexture(GL_TEXTURE0);
18         check_error();
19         glEnable(GL_TEXTURE_2D);
20         check_error();
21         glBindTexture(GL_TEXTURE_2D, hsv_wheel_num);
22         check_error();
23         glActiveTexture(GL_TEXTURE1);
24         check_error();
25         glBindTexture(GL_TEXTURE_2D, 0);
26         check_error();
27         glActiveTexture(GL_TEXTURE0);
28         glEnable(GL_BLEND);
29         check_error();
30         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
31         check_error();
32
33         // wheel
34         glBegin(GL_QUADS);
35
36         glTexCoord2f(0.0f, 1.0f);
37         glVertex2f(0.0f, y);
38
39         glTexCoord2f(1.0f, 1.0f);
40         glVertex2f(0.2f * 9.0f / 16.0f, y);
41
42         glTexCoord2f(1.0f, 0.0f);
43         glVertex2f(0.2f * 9.0f / 16.0f, y + 0.2f);
44
45         glTexCoord2f(0.0f, 0.0f);
46         glVertex2f(0.0f, y + 0.2f);
47
48         glEnd();
49
50         // wheel selector
51         glDisable(GL_TEXTURE_2D);
52         glColor3f(0.0f, 0.0f, 0.0f);
53         glPointSize(5.0f);
54         glBegin(GL_POINTS);
55         glVertex2f((0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f, y + 0.1f - rad * sin(theta) * 0.1f);
56         glEnd();
57         
58         // value slider
59         glDisable(GL_TEXTURE_2D);
60         glBegin(GL_QUADS);
61
62         glColor3f(0.0f, 0.0f, 0.0f);
63         glVertex2f(0.22f * 9.0f / 16.0f, y);
64         glVertex2f(0.24f * 9.0f / 16.0f, y);
65
66         glColor3f(1.0f, 1.0f, 1.0f);
67         glVertex2f(0.24f * 9.0f / 16.0f, y + 0.2f);
68         glVertex2f(0.22f * 9.0f / 16.0f, y + 0.2f);
69
70         glEnd();
71
72         // value selector
73         glColor3f(0.0f, 0.0f, 0.0f);
74         glPointSize(5.0f);
75         glBegin(GL_POINTS);
76         glVertex2f(0.23f * 9.0f / 16.0f, y + value * 0.2f);
77         glEnd();
78
79         glColor3f(1.0f, 1.0f, 1.0f);
80 }
81
82 void draw_saturation_bar(float y, float saturation)
83 {
84         glUseProgram(0);
85         check_error();
86
87         // value slider
88         glDisable(GL_TEXTURE_2D);
89         glBegin(GL_QUADS);
90
91         glColor3f(0.0f, 0.0f, 0.0f);
92         glVertex2f(0.0f * 9.0f / 16.0f, y + 0.02f);
93         glVertex2f(0.0f * 9.0f / 16.0f, y);
94
95         glColor3f(1.0f, 1.0f, 1.0f);
96         glVertex2f(0.2f * 9.0f / 16.0f, y);
97         glVertex2f(0.2f * 9.0f / 16.0f, y + 0.02f);
98
99         glEnd();
100
101         // value selector
102         glColor3f(0.0f, 0.0f, 0.0f);
103         glPointSize(5.0f);
104         glBegin(GL_POINTS);
105         glVertex2f(0.2f * saturation * 9.0f / 16.0f, y + 0.01f);
106         glEnd();
107
108         glColor3f(1.0f, 1.0f, 1.0f);
109 }
110
111 void make_hsv_wheel_texture()
112 {
113         glGenTextures(1, &hsv_wheel_num);
114
115         static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
116         for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
117                 for (int x = 0; x < HSV_WHEEL_SIZE; ++x) {
118                         float yf = 2.0f * y / (float)(HSV_WHEEL_SIZE) - 1.0f;
119                         float xf = 2.0f * x / (float)(HSV_WHEEL_SIZE) - 1.0f;
120                         float rad = hypot(xf, yf);
121                         float theta = atan2(yf, xf);
122
123                         float r, g, b;
124                         hsv2rgb(theta, rad, 1.0f, &r, &g, &b);
125                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 0] = lrintf(r * 255.0f);
126                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 1] = lrintf(g * 255.0f);
127                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 2] = lrintf(b * 255.0f);
128
129                         if (rad > 1.0f) {
130                                 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
131                         } else {
132                                 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
133                         }
134                 }
135         }
136
137         glBindTexture(GL_TEXTURE_2D, hsv_wheel_num);
138         check_error();
139         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
140         check_error();
141         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
142         check_error();
143 }
144
145 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
146 {
147         if (xf < 0.2f && yf < 0.2f) {
148                 float xp = 2.0f * xf / 0.2f - 1.0f;
149                 float yp = -(2.0f * yf / 0.2f - 1.0f);
150                 *rad = hypot(xp, yp);
151                 *theta = atan2(yp, xp);
152                 if (*rad > 1.0) {
153                         *rad = 1.0;
154                 }
155         } else if (xf >= 0.22f && xf <= 0.24f) {
156                 *value = yf / 0.2f;
157         }
158 }
159
160
161 }  // namespace movit