7 #include "resource_pool.h"
11 #define HSV_WHEEL_SIZE 128
17 GLuint hsv_wheel_texnum = 0;
18 GLuint textured_program_num = 0, colored_program_num = 0, hsv_vao = 0;
19 ResourcePool resource_pool;
21 void draw_black_point(float x, float y, float point_size)
23 glUseProgram(colored_program_num);
26 float vertices[] = { x, y };
27 float colors[] = { 0.0f, 0.0f, 0.0f };
29 glPointSize(point_size);
31 GLuint position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
32 GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(colors), colors);
34 glDrawArrays(GL_POINTS, 0, 1);
36 cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
37 cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
40 void draw_hsv_wheel(float y, float rad, float theta, float value)
42 glUseProgram(textured_program_num);
44 glActiveTexture(GL_TEXTURE0);
46 glBindTexture(GL_TEXTURE_2D, hsv_wheel_texnum);
48 glUniform1i(glGetUniformLocation(textured_program_num, "tex"), 0); // Bind the 2D sampler.
52 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
56 glGenVertexArrays(1, &vao);
58 glBindVertexArray(vao);
62 float wheel_vertices[] = {
65 0.2f * 9.0f / 16.0f, y,
66 0.2f * 9.0f / 16.0f, y + 0.2f,
68 float wheel_texcoords[] = {
74 GLuint position_vbo = fill_vertex_attribute(textured_program_num, "position", 2, GL_FLOAT, sizeof(wheel_vertices), wheel_vertices);
75 GLuint texcoord_vbo = fill_vertex_attribute(textured_program_num, "texcoord", 2, GL_FLOAT, sizeof(wheel_texcoords), wheel_texcoords);
78 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
81 cleanup_vertex_attribute(textured_program_num, "position", position_vbo);
82 cleanup_vertex_attribute(textured_program_num, "texcoord", texcoord_vbo);
86 (0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f,
87 y + 0.1f - rad * sin(theta) * 0.1f,
91 glUseProgram(colored_program_num);
92 float value_vertices[] = {
93 0.22f * 9.0f / 16.0f, y,
94 0.22f * 9.0f / 16.0f, y + 0.2f,
95 0.24f * 9.0f / 16.0f, y,
96 0.24f * 9.0f / 16.0f, y + 0.2f,
98 float value_colors[] = {
104 position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(value_vertices), value_vertices);
105 GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(value_colors), value_colors);
107 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
109 cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
110 cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
113 draw_black_point(0.23f * 9.0f / 16.0f, y + value * 0.2f, 5.0f);
115 glDeleteVertexArrays(1, &vao);
121 void draw_saturation_bar(float y, float saturation)
124 glGenVertexArrays(1, &vao);
126 glBindVertexArray(vao);
130 glUseProgram(colored_program_num);
131 float value_vertices[] = {
132 0.0f * 9.0f / 16.0f, y + 0.02f,
133 0.2f * 9.0f / 16.0f, y + 0.02f,
134 0.0f * 9.0f / 16.0f, y,
135 0.2f * 9.0f / 16.0f, y,
137 float value_colors[] = {
143 GLuint position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(value_vertices), value_vertices);
144 GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(value_colors), value_colors);
146 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
148 cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
149 cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
152 draw_black_point(0.2f * saturation * 9.0f / 16.0f, y + 0.01f, 5.0f);
154 glDeleteVertexArrays(1, &vao);
160 void make_hsv_wheel_texture()
162 glGenTextures(1, &hsv_wheel_texnum);
164 static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
165 for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
166 for (int x = 0; x < HSV_WHEEL_SIZE; ++x) {
167 float yf = 2.0f * y / (float)(HSV_WHEEL_SIZE) - 1.0f;
168 float xf = 2.0f * x / (float)(HSV_WHEEL_SIZE) - 1.0f;
169 float rad = hypot(xf, yf);
170 float theta = atan2(yf, xf);
173 hsv2rgb(theta, rad, 1.0f, &r, &g, &b);
174 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 0] = lrintf(r * 255.0f);
175 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 1] = lrintf(g * 255.0f);
176 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 2] = lrintf(b * 255.0f);
179 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
181 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
186 glBindTexture(GL_TEXTURE_2D, hsv_wheel_texnum);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
194 void init_hsv_resources()
196 vector<string> frag_shader_outputs;
197 textured_program_num = resource_pool.compile_glsl_program(
198 read_version_dependent_file("vs", "vert"),
199 read_version_dependent_file("texture1d", "frag"),
200 frag_shader_outputs);
201 colored_program_num = resource_pool.compile_glsl_program(
202 read_version_dependent_file("vs-color", "vert"),
203 read_version_dependent_file("color", "frag"),
204 frag_shader_outputs);
205 make_hsv_wheel_texture();
208 void cleanup_hsv_resources()
210 resource_pool.release_glsl_program(textured_program_num);
211 resource_pool.release_glsl_program(colored_program_num);
214 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
216 if (xf < 0.2f && yf < 0.2f) {
217 float xp = 2.0f * xf / 0.2f - 1.0f;
218 float yp = -(2.0f * yf / 0.2f - 1.0f);
219 *rad = hypot(xp, yp);
220 *theta = atan2(yp, xp);
224 } else if (xf >= 0.22f && xf <= 0.24f) {