]> git.sesse.net Git - movit/blob - widgets.cpp
Add some earlier check_error() calls so that we do not get confusing behavior if...
[movit] / widgets.cpp
1 #include <epoxy/gl.h>
2 #include <math.h>
3
4 #include "resource_pool.h"
5 #include "widgets.h"
6 #include "util.h"
7
8 #define HSV_WHEEL_SIZE 128
9
10 namespace movit {
11
12 GLuint hsv_wheel_texnum = 0;
13 GLuint textured_program_num = 0, colored_program_num = 0, hsv_vao = 0;
14 ResourcePool resource_pool;
15
16 void draw_black_point(float x, float y, float point_size)
17 {
18         glUseProgram(colored_program_num);
19         check_error();
20
21         float vertices[] = { x, y };
22         float colors[] = { 0.0f, 0.0f, 0.0f };
23
24         glPointSize(point_size);
25         check_error();
26         GLuint position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
27         GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(colors), colors);
28         check_error();
29         glDrawArrays(GL_POINTS, 0, 1);
30         check_error();
31         cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
32         cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
33 }
34
35 void draw_hsv_wheel(float y, float rad, float theta, float value)
36 {
37         glUseProgram(textured_program_num);
38         check_error();
39         glActiveTexture(GL_TEXTURE0);
40         check_error();
41         glBindTexture(GL_TEXTURE_2D, hsv_wheel_texnum);
42         check_error();
43         glUniform1i(glGetUniformLocation(textured_program_num, "tex"), 0);  // Bind the 2D sampler.
44         check_error();
45         glEnable(GL_BLEND);
46         check_error();
47         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
48         check_error();
49
50         GLuint vao;
51         glGenVertexArrays(1, &vao);
52         check_error();
53         glBindVertexArray(vao);
54         check_error();
55
56         // wheel
57         float wheel_vertices[] = {
58                 0.0f, y,
59                 0.0f, y + 0.2f,
60                 0.2f * 9.0f / 16.0f, y,
61                 0.2f * 9.0f / 16.0f, y + 0.2f,
62         };
63         float wheel_texcoords[] = {
64                 0.0f, 1.0f,
65                 0.0f, 0.0f,
66                 1.0f, 1.0f,
67                 1.0f, 0.0f,
68         };
69         GLuint position_vbo = fill_vertex_attribute(textured_program_num, "position", 2, GL_FLOAT, sizeof(wheel_vertices), wheel_vertices);
70         GLuint texcoord_vbo = fill_vertex_attribute(textured_program_num, "texcoord", 2, GL_FLOAT, sizeof(wheel_texcoords), wheel_texcoords);
71         check_error();
72
73         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
74         check_error();
75
76         cleanup_vertex_attribute(textured_program_num, "position", position_vbo);
77         cleanup_vertex_attribute(textured_program_num, "texcoord", texcoord_vbo);
78
79         // wheel selector
80         draw_black_point(
81             (0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f,
82             y + 0.1f - rad * sin(theta) * 0.1f,
83             5.0f);
84
85         // value slider
86         glUseProgram(colored_program_num);
87         float value_vertices[] = {
88                 0.22f * 9.0f / 16.0f, y,
89                 0.22f * 9.0f / 16.0f, y + 0.2f,
90                 0.24f * 9.0f / 16.0f, y,
91                 0.24f * 9.0f / 16.0f, y + 0.2f,
92         };
93         float value_colors[] = {
94                 0.0f, 0.0f, 0.0f,
95                 1.0f, 1.0f, 1.0f,
96                 0.0f, 0.0f, 0.0f,
97                 1.0f, 1.0f, 1.0f,
98         };
99         position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(value_vertices), value_vertices);
100         GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(value_colors), value_colors);
101         check_error();
102         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
103         check_error();
104         cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
105         cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
106
107         // value selector
108         draw_black_point(0.23f * 9.0f / 16.0f, y + value * 0.2f, 5.0f);
109
110         glDeleteVertexArrays(1, &vao);
111         check_error();
112         glUseProgram(0);
113         check_error();
114 }
115
116 void draw_saturation_bar(float y, float saturation)
117 {
118         GLuint vao;
119         glGenVertexArrays(1, &vao);
120         check_error();
121         glBindVertexArray(vao);
122         check_error();
123
124         // value slider
125         glUseProgram(colored_program_num);
126         float value_vertices[] = {
127                 0.0f * 9.0f / 16.0f, y + 0.02f,
128                 0.2f * 9.0f / 16.0f, y + 0.02f,
129                 0.0f * 9.0f / 16.0f, y,
130                 0.2f * 9.0f / 16.0f, y,
131         };
132         float value_colors[] = {
133                 0.0f, 0.0f, 0.0f,
134                 1.0f, 1.0f, 1.0f,
135                 0.0f, 0.0f, 0.0f,
136                 1.0f, 1.0f, 1.0f,
137         };
138         GLuint position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(value_vertices), value_vertices);
139         GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(value_colors), value_colors);
140         check_error();
141         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
142         check_error();
143         cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
144         cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
145
146         // value selector
147         draw_black_point(0.2f * saturation * 9.0f / 16.0f, y + 0.01f, 5.0f);
148
149         glDeleteVertexArrays(1, &vao);
150         check_error();
151         glUseProgram(0);
152         check_error();
153 }
154
155 void make_hsv_wheel_texture()
156 {
157         glGenTextures(1, &hsv_wheel_texnum);
158
159         static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
160         for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
161                 for (int x = 0; x < HSV_WHEEL_SIZE; ++x) {
162                         float yf = 2.0f * y / (float)(HSV_WHEEL_SIZE) - 1.0f;
163                         float xf = 2.0f * x / (float)(HSV_WHEEL_SIZE) - 1.0f;
164                         float rad = hypot(xf, yf);
165                         float theta = atan2(yf, xf);
166
167                         float r, g, b;
168                         hsv2rgb(theta, rad, 1.0f, &r, &g, &b);
169                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 0] = lrintf(r * 255.0f);
170                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 1] = lrintf(g * 255.0f);
171                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 2] = lrintf(b * 255.0f);
172
173                         if (rad > 1.0f) {
174                                 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
175                         } else {
176                                 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
177                         }
178                 }
179         }
180
181         glBindTexture(GL_TEXTURE_2D, hsv_wheel_texnum);
182         check_error();
183         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184         check_error();
185         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
186         check_error();
187 }
188
189 void init_hsv_resources()
190 {
191         textured_program_num = resource_pool.compile_glsl_program(
192                 read_version_dependent_file("vs", "vert"),
193                 read_version_dependent_file("texture1d", "frag"));
194         colored_program_num = resource_pool.compile_glsl_program(
195                 read_version_dependent_file("vs-color", "vert"),
196                 read_version_dependent_file("color", "frag"));
197         make_hsv_wheel_texture();
198 }
199
200 void cleanup_hsv_resources()
201 {
202         resource_pool.release_glsl_program(textured_program_num);
203         resource_pool.release_glsl_program(colored_program_num);
204 }
205
206 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
207 {
208         if (xf < 0.2f && yf < 0.2f) {
209                 float xp = 2.0f * xf / 0.2f - 1.0f;
210                 float yp = -(2.0f * yf / 0.2f - 1.0f);
211                 *rad = hypot(xp, yp);
212                 *theta = atan2(yp, xp);
213                 if (*rad > 1.0) {
214                         *rad = 1.0;
215                 }
216         } else if (xf >= 0.22f && xf <= 0.24f) {
217                 *value = yf / 0.2f;
218         }
219 }
220
221
222 }  // namespace movit