4 #include "resource_pool.h"
8 #define HSV_WHEEL_SIZE 128
12 GLuint hsv_wheel_texnum = 0;
13 GLuint textured_program_num = 0, colored_program_num = 0, hsv_vao = 0;
14 ResourcePool resource_pool;
16 void draw_black_point(float x, float y, float point_size)
18 glUseProgram(colored_program_num);
21 float vertices[] = { x, y };
22 float colors[] = { 0.0f, 0.0f, 0.0f };
24 glPointSize(point_size);
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);
29 glDrawArrays(GL_POINTS, 0, 1);
31 cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
32 cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
35 void draw_hsv_wheel(float y, float rad, float theta, float value)
37 glUseProgram(textured_program_num);
39 glActiveTexture(GL_TEXTURE0);
41 glBindTexture(GL_TEXTURE_2D, hsv_wheel_texnum);
43 glUniform1i(glGetUniformLocation(textured_program_num, "tex"), 0); // Bind the 2D sampler.
47 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
51 glGenVertexArrays(1, &vao);
53 glBindVertexArray(vao);
57 float wheel_vertices[] = {
60 0.2f * 9.0f / 16.0f, y,
61 0.2f * 9.0f / 16.0f, y + 0.2f,
63 float wheel_texcoords[] = {
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);
73 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
76 cleanup_vertex_attribute(textured_program_num, "position", position_vbo);
77 cleanup_vertex_attribute(textured_program_num, "texcoord", texcoord_vbo);
81 (0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f,
82 y + 0.1f - rad * sin(theta) * 0.1f,
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,
93 float value_colors[] = {
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);
102 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
104 cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
105 cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
108 draw_black_point(0.23f * 9.0f / 16.0f, y + value * 0.2f, 5.0f);
110 glDeleteVertexArrays(1, &vao);
116 void draw_saturation_bar(float y, float saturation)
119 glGenVertexArrays(1, &vao);
121 glBindVertexArray(vao);
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,
132 float value_colors[] = {
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);
141 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
143 cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
144 cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
147 draw_black_point(0.2f * saturation * 9.0f / 16.0f, y + 0.01f, 5.0f);
149 glDeleteVertexArrays(1, &vao);
155 void make_hsv_wheel_texture()
157 glGenTextures(1, &hsv_wheel_texnum);
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);
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);
174 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
176 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
181 glBindTexture(GL_TEXTURE_2D, hsv_wheel_texnum);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
185 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
189 void init_hsv_resources()
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();
200 void cleanup_hsv_resources()
202 resource_pool.release_glsl_program(textured_program_num);
203 resource_pool.release_glsl_program(colored_program_num);
206 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
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);
216 } else if (xf >= 0.22f && xf <= 0.24f) {