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