imtk

annotate src/draw.c @ 19:11da537aeba6

blah
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 26 Apr 2011 22:53:21 +0300
parents 737e9047d9c9
children
rev   line source
nuclear@6 1 #include <string.h>
nuclear@18 2 #include <math.h>
nuclear@6 3 #include <assert.h>
nuclear@6 4 #include "draw.h"
nuclear@6 5 #include "imtk.h"
nuclear@6 6
nuclear@14 7 #define COLOR_MASK 0xff
nuclear@14 8
nuclear@6 9 /* default colors, can be changed with imtk_set_color */
nuclear@6 10 static float colors[][4] = {
nuclear@6 11 {0.0, 0.0, 0.0, 1.0}, /* text color */
nuclear@14 12 {0.75, 0.75, 0.75, 1.0}, /* top color */
nuclear@14 13 {0.56, 0.56, 0.56, 1.0}, /* bot color */
nuclear@14 14 {0.9, 0.9, 0.9, 1.0}, /* lit bevel */
nuclear@13 15 {0.3, 0.3, 0.3, 1.0}, /* shadowed bevel */
nuclear@13 16 {0.8, 0.25, 0.18, 1.0}, /* cursor color */
nuclear@15 17 {0.68, 0.85, 1.3, 1.0}, /* selection color */
nuclear@14 18 {0.75, 0.1, 0.095, 1.0} /* check color */
nuclear@6 19 };
nuclear@6 20
nuclear@15 21 static float focus_factor = 1.15;
nuclear@14 22 static float press_factor = 0.8;
nuclear@13 23 static float alpha = 1.0;
nuclear@13 24 static float bevel = 1.0;
nuclear@13 25
nuclear@14 26 void imtk_set_color(unsigned int col, float r, float g, float b, float a)
nuclear@6 27 {
nuclear@14 28 int idx = col & COLOR_MASK;
nuclear@14 29 assert(idx >= 0 && idx < sizeof colors / sizeof *colors);
nuclear@6 30
nuclear@14 31 colors[idx][0] = r;
nuclear@14 32 colors[idx][1] = g;
nuclear@14 33 colors[idx][2] = b;
nuclear@14 34 colors[idx][3] = a;
nuclear@6 35 }
nuclear@6 36
nuclear@14 37 float *imtk_get_color(unsigned int col)
nuclear@6 38 {
nuclear@13 39 static float ret[4];
nuclear@14 40 int idx = col & COLOR_MASK;
nuclear@14 41
nuclear@14 42 memcpy(ret, colors + idx, sizeof ret);
nuclear@14 43 if(col & IMTK_FOCUS_BIT) {
nuclear@14 44 ret[0] *= focus_factor;
nuclear@14 45 ret[1] *= focus_factor;
nuclear@14 46 ret[2] *= focus_factor;
nuclear@14 47 }
nuclear@14 48 if(col & IMTK_PRESS_BIT) {
nuclear@14 49 ret[0] *= press_factor;
nuclear@14 50 ret[1] *= press_factor;
nuclear@14 51 ret[2] *= press_factor;
nuclear@14 52 }
nuclear@15 53 if(col & IMTK_SEL_BIT) {
nuclear@15 54 ret[0] *= colors[IMTK_SELECTION_COLOR][0];
nuclear@15 55 ret[1] *= colors[IMTK_SELECTION_COLOR][1];
nuclear@15 56 ret[2] *= colors[IMTK_SELECTION_COLOR][2];
nuclear@15 57 }
nuclear@13 58 ret[3] *= alpha;
nuclear@13 59 return ret;
nuclear@13 60 }
nuclear@13 61
nuclear@13 62 void imtk_set_alpha(float a)
nuclear@13 63 {
nuclear@13 64 alpha = a;
nuclear@13 65 }
nuclear@13 66
nuclear@13 67 float imtk_get_alpha(void)
nuclear@13 68 {
nuclear@13 69 return alpha;
nuclear@13 70 }
nuclear@13 71
nuclear@13 72 void imtk_set_bevel_width(float b)
nuclear@13 73 {
nuclear@13 74 bevel = b;
nuclear@13 75 }
nuclear@13 76
nuclear@13 77 float imtk_get_bevel_width(void)
nuclear@13 78 {
nuclear@13 79 return bevel;
nuclear@6 80 }
nuclear@6 81
nuclear@14 82 void imtk_set_focus_factor(float fact)
nuclear@14 83 {
nuclear@14 84 focus_factor = fact;
nuclear@14 85 }
nuclear@14 86
nuclear@14 87 float imtk_get_focus_factor(void)
nuclear@14 88 {
nuclear@14 89 return focus_factor;
nuclear@14 90 }
nuclear@14 91
nuclear@14 92 void imtk_set_press_factor(float fact)
nuclear@14 93 {
nuclear@14 94 press_factor = fact;
nuclear@14 95 }
nuclear@14 96
nuclear@14 97 float imtk_get_press_factor(void)
nuclear@14 98 {
nuclear@14 99 return press_factor;
nuclear@14 100 }
nuclear@14 101
nuclear@14 102 void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot)
nuclear@8 103 {
nuclear@8 104 glBegin(GL_QUADS);
nuclear@14 105 if(ctop) {
nuclear@14 106 glColor4fv(ctop);
nuclear@8 107 }
nuclear@8 108 glVertex2f(x, y);
nuclear@8 109 glVertex2f(x + w, y);
nuclear@14 110
nuclear@14 111 if(cbot) {
nuclear@14 112 glColor4fv(cbot);
nuclear@14 113 }
nuclear@8 114 glVertex2f(x + w, y + h);
nuclear@8 115 glVertex2f(x, y + h);
nuclear@8 116 glEnd();
nuclear@8 117 }
nuclear@8 118
nuclear@6 119 void imtk_draw_frame(int x, int y, int w, int h, int style)
nuclear@6 120 {
nuclear@6 121 float tcol[4], bcol[4];
nuclear@13 122 float b = imtk_get_bevel_width();
nuclear@6 123
nuclear@14 124 if(!b) {
nuclear@14 125 return;
nuclear@14 126 }
nuclear@14 127
nuclear@14 128 x -= b;
nuclear@14 129 y -= b;
nuclear@14 130 w += b * 2;
nuclear@14 131 h += b * 2;
nuclear@14 132
nuclear@6 133 switch(style) {
nuclear@6 134 case FRAME_INSET:
nuclear@13 135 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
nuclear@13 136 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
nuclear@6 137 break;
nuclear@6 138
nuclear@6 139 case FRAME_OUTSET:
nuclear@6 140 default:
nuclear@13 141 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
nuclear@13 142 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
nuclear@6 143 }
nuclear@6 144
nuclear@13 145 glBegin(GL_QUADS);
nuclear@13 146 glColor4fv(tcol);
nuclear@13 147 glVertex2f(x, y);
nuclear@13 148 glVertex2f(x + b, y + b);
nuclear@13 149 glVertex2f(x + w - b, y + b);
nuclear@13 150 glVertex2f(x + w, y);
nuclear@13 151
nuclear@13 152 glVertex2f(x + b, y + b);
nuclear@13 153 glVertex2f(x, y);
nuclear@13 154 glVertex2f(x, y + h);
nuclear@13 155 glVertex2f(x + b, y + h - b);
nuclear@13 156
nuclear@13 157 glColor4fv(bcol);
nuclear@13 158 glVertex2f(x + b, y + h - b);
nuclear@13 159 glVertex2f(x + w - b, y + h - b);
nuclear@13 160 glVertex2f(x + w, y + h);
nuclear@13 161 glVertex2f(x, y + h);
nuclear@13 162
nuclear@13 163 glVertex2f(x + w - b, y + b);
nuclear@13 164 glVertex2f(x + w, y);
nuclear@13 165 glVertex2f(x + w, y + h);
nuclear@13 166 glVertex2f(x + w - b, y + h - b);
nuclear@13 167 glEnd();
nuclear@6 168 }
nuclear@6 169
nuclear@19 170 void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot)
nuclear@19 171 {
nuclear@19 172 int i;
nuclear@19 173 float t, dtheta, theta = 0.0;
nuclear@19 174 float color[4];
nuclear@19 175 float cx = (float)x;
nuclear@19 176 float cy = (float)y;
nuclear@19 177
nuclear@19 178 subdiv += 3;
nuclear@19 179 dtheta = 2.0 * M_PI / subdiv;
nuclear@19 180
nuclear@19 181 color[0] = (ctop[0] + cbot[0]) * 0.5;
nuclear@19 182 color[1] = (ctop[1] + cbot[1]) * 0.5;
nuclear@19 183 color[2] = (ctop[2] + cbot[2]) * 0.5;
nuclear@19 184 color[3] = (ctop[3] + cbot[3]) * 0.5;
nuclear@19 185
nuclear@19 186 glBegin(GL_TRIANGLE_FAN);
nuclear@19 187 glColor4fv(color);
nuclear@19 188 glVertex2f(cx, cy);
nuclear@19 189
nuclear@19 190 for(i=0; i<=subdiv; i++) {
nuclear@19 191 float vx, vy;
nuclear@19 192
nuclear@19 193 vx = cos(theta);
nuclear@19 194 vy = sin(theta);
nuclear@19 195 theta += dtheta;
nuclear@19 196
nuclear@19 197 t = (vy + 1.0) / 2.0;
nuclear@19 198 color[0] = ctop[0] + (cbot[0] - ctop[0]) * t;
nuclear@19 199 color[1] = ctop[1] + (cbot[1] - ctop[1]) * t;
nuclear@19 200 color[2] = ctop[2] + (cbot[2] - ctop[2]) * t;
nuclear@19 201 color[3] = ctop[3] + (cbot[3] - ctop[3]) * t;
nuclear@19 202
nuclear@19 203 glColor4fv(color);
nuclear@19 204 glVertex2f(cx + vx * rad, cy + vy * rad);
nuclear@19 205 }
nuclear@19 206 glEnd();
nuclear@19 207 }
nuclear@19 208
nuclear@19 209 void imtk_draw_disc_frame(int x, int y, float inner, float outer, int subdiv, int style)
nuclear@19 210 {
nuclear@19 211 int i;
nuclear@19 212 float t, dtheta, theta = 0.0;
nuclear@19 213 float color[4], tcol[4], bcol[4];
nuclear@19 214 float cx = (float)x;
nuclear@19 215 float cy = (float)y;
nuclear@19 216
nuclear@19 217 switch(style) {
nuclear@19 218 case FRAME_INSET:
nuclear@19 219 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
nuclear@19 220 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
nuclear@19 221 break;
nuclear@19 222
nuclear@19 223 case FRAME_OUTSET:
nuclear@19 224 default:
nuclear@19 225 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
nuclear@19 226 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
nuclear@19 227 }
nuclear@19 228
nuclear@19 229 subdiv += 3;
nuclear@19 230 dtheta = 2.0 * M_PI / subdiv;
nuclear@19 231
nuclear@19 232 glBegin(GL_QUAD_STRIP);
nuclear@19 233
nuclear@19 234 for(i=0; i<=subdiv; i++) {
nuclear@19 235 float vx, vy;
nuclear@19 236
nuclear@19 237 vx = cos(theta);
nuclear@19 238 vy = sin(theta);
nuclear@19 239
nuclear@19 240 t = (vy + 1.0) / 2.0;
nuclear@19 241 color[0] = tcol[0] + (bcol[0] - tcol[0]) * t;
nuclear@19 242 color[1] = tcol[1] + (bcol[1] - tcol[1]) * t;
nuclear@19 243 color[2] = tcol[2] + (bcol[2] - tcol[2]) * t;
nuclear@19 244 color[3] = tcol[3] + (bcol[3] - tcol[3]) * t;
nuclear@19 245
nuclear@19 246 vx = cos(theta - M_PI / 4.0);
nuclear@19 247 vy = sin(theta - M_PI / 4.0);
nuclear@19 248 theta += dtheta;
nuclear@19 249
nuclear@19 250 glColor4fv(color);
nuclear@19 251 glVertex2f(cx + vx * inner, cy + vy * inner);
nuclear@19 252 glVertex2f(cx + vx * outer, cy + vy * outer);
nuclear@19 253 }
nuclear@19 254 glEnd();
nuclear@19 255 }
nuclear@19 256
nuclear@6 257 void imtk_draw_string(int x, int y, const char *str)
nuclear@6 258 {
nuclear@6 259 glRasterPos2i(x, y);
nuclear@6 260 while(*str) {
nuclear@6 261 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
nuclear@6 262 }
nuclear@6 263 }
nuclear@6 264
nuclear@6 265 int imtk_string_size(const char *str)
nuclear@6 266 {
nuclear@6 267 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
nuclear@6 268 }