rayfract
diff src/gui.cc @ 4:e4349f5804b9
switched to imtk
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Fri, 29 Apr 2011 07:30:31 +0300 |
parents | bf1d56975cc9 |
children | 48e0e7d33d9e |
line diff
1.1 --- a/src/gui.cc Tue Oct 26 09:52:57 2010 +0300 1.2 +++ b/src/gui.cc Fri Apr 29 07:30:31 2011 +0300 1.3 @@ -6,294 +6,65 @@ 1.4 #include <GLUT/glut.h> 1.5 #endif 1.6 1.7 -#include <ubertk.h> 1.8 +#include <imtk.h> 1.9 #include "gui.h" 1.10 -#include "utktext.h" 1.11 #include "sdr.h" 1.12 #include "vmath.h" 1.13 1.14 -#ifndef GL_BGRA 1.15 -#define GL_BGRA 0x80E1 1.16 -#endif 1.17 - 1.18 -#define TEXT_PT_SIZE 18 1.19 - 1.20 -void huecb(utk::Event *ev, void *cls); 1.21 -static void cbfunc(utk::Event *ev, void *cls); 1.22 - 1.23 -void utk_color(int r, int g, int b, int a); 1.24 -void utk_clip(int x1, int y1, int x2, int y2); 1.25 -void utk_image(int x, int y, const void *pix, int xsz, int ysz); 1.26 - 1.27 -void utk_rect(int x1, int y1, int x2, int y2); 1.28 -void utk_line(int x1, int y1, int x2, int y2, int width); 1.29 - 1.30 -void utk_text(int x, int y, const char *txt, int sz); 1.31 -int utk_text_spacing(); 1.32 -int utk_text_width(const char *txt, int sz); 1.33 - 1.34 1.35 int xsz, ysz; 1.36 -static utk::Container *utkroot; 1.37 -static float max_descent; 1.38 -utk::Window *win_seed, *win_material; 1.39 - 1.40 +static int show_gui = 1; 1.41 1.42 extern unsigned int sdr; 1.43 extern Vector4 seed; 1.44 extern int iter; 1.45 extern float err_thres; 1.46 extern float reflectivity; 1.47 -extern Vector3 color; 1.48 1.49 int gui_init(int width, int height) 1.50 { 1.51 xsz = width; 1.52 ysz = height; 1.53 1.54 - if(!CreateFont("georgia.ttf", TEXT_PT_SIZE)) { 1.55 - fprintf(stderr, "failed to load font\n"); 1.56 - return -1; 1.57 - } 1.58 - max_descent = GetMaxDescent(); 1.59 + imtk_set_viewport(width, height); 1.60 1.61 - utk::gfx::color = utk_color; 1.62 - utk::gfx::clip = utk_clip; 1.63 - utk::gfx::image = utk_image; 1.64 - utk::gfx::rect = utk_rect; 1.65 - utk::gfx::line = utk_line; 1.66 - utk::gfx::text = utk_text; 1.67 - utk::gfx::text_spacing = utk_text_spacing; 1.68 - utk::gfx::text_width = utk_text_width; 1.69 - 1.70 - utkroot = utk::init(xsz, ysz); 1.71 - 1.72 - win_seed = utk::create_window(utkroot, 5, 25, 220, 175, "julia parameters"); 1.73 - win_seed->set_alpha(128); 1.74 - win_seed->show(); 1.75 - 1.76 - utk::VBox *vbox = utk::create_vbox(win_seed); 1.77 - utk::HBox *hbox; 1.78 - 1.79 - hbox = utk::create_hbox(vbox); 1.80 - utk::create_label(hbox, "seed x"); 1.81 - utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)0)->set_value(seed.x); 1.82 - hbox = utk::create_hbox(vbox); 1.83 - utk::create_label(hbox, "seed y"); 1.84 - utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)1)->set_value(seed.y); 1.85 - hbox = utk::create_hbox(vbox); 1.86 - utk::create_label(hbox, "seed z"); 1.87 - utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)2)->set_value(seed.z); 1.88 - hbox = utk::create_hbox(vbox); 1.89 - utk::create_label(hbox, "seed w"); 1.90 - utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)3)->set_value(seed.w); 1.91 - 1.92 - hbox = utk::create_hbox(vbox); 1.93 - utk::create_label(hbox, "iterations"); 1.94 - utk::Slider *iter_slider = utk::create_slider(hbox, 0, 32, cbfunc, (void*)5); 1.95 - iter_slider->set_value(iter); 1.96 - iter_slider->set_vis_decimal(0); 1.97 - 1.98 - hbox = utk::create_hbox(vbox); 1.99 - utk::create_label(hbox, "max error"); 1.100 - utk::Slider *err_slider = utk::create_slider(hbox, 0, 0.075, cbfunc, (void*)6); 1.101 - err_slider->set_value(err_thres); 1.102 - err_slider->set_vis_decimal(4); 1.103 - 1.104 - win_material = utk::create_window(utkroot, 250, 25, 220, 210, "material"); 1.105 - win_material->set_alpha(128); 1.106 - win_material->show(); 1.107 - ((utk::WinFrame*)win_material->get_parent())->set_shade(true); 1.108 - 1.109 - vbox = utk::create_vbox(win_material); 1.110 - 1.111 - utk::ColorBox *colbox = new utk::ColorBox; 1.112 - vbox->add_child(colbox); 1.113 - utk::HueBox *huebox = new utk::HueBox; 1.114 - vbox->add_child(huebox); 1.115 - huebox->set_callback(utk::EVENT_MODIFY, huecb, colbox); 1.116 - 1.117 - float hue, sat, val; 1.118 - utk::rgb_to_hsv(color.x, color.y, color.z, &hue, &sat, &val); 1.119 - colbox->set_color_hsv(hue, sat, val); 1.120 - huebox->set_h(hue); 1.121 - 1.122 - hbox = utk::create_hbox(vbox); 1.123 - utk::create_label(hbox, "reflectivity"); 1.124 - utk::create_slider(hbox, 0, 1.0, cbfunc, (void*)4)->set_value(reflectivity); 1.125 - 1.126 + /*imtk_set_alpha(0.5);*/ 1.127 return 0; 1.128 } 1.129 1.130 void gui_draw() 1.131 { 1.132 - glMatrixMode(GL_MODELVIEW); 1.133 - glPushMatrix(); 1.134 - glLoadIdentity(); 1.135 - glMatrixMode(GL_PROJECTION); 1.136 - glPushMatrix(); 1.137 - glLoadIdentity(); 1.138 + if(!show_gui) { 1.139 + return; 1.140 + } 1.141 1.142 - glPushAttrib(GL_ENABLE_BIT); 1.143 - glDisable(GL_LIGHTING); 1.144 - glDisable(GL_DEPTH_TEST); 1.145 - glDisable(GL_CULL_FACE); 1.146 - glEnable(GL_BLEND); 1.147 + imtk_begin(); 1.148 1.149 - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1.150 + imtk_label("seed x", 10, 20); 1.151 + seed.x = imtk_slider(IMUID, seed.x, -1.0, 1.0, 80, 20); 1.152 1.153 - utk::draw(); 1.154 + imtk_label("seed y", 10, 50); 1.155 + seed.y = imtk_slider(IMUID, seed.y, -1.0, 1.0, 80, 50); 1.156 1.157 - glPopAttrib(); 1.158 + imtk_label("seed z", 10, 80); 1.159 + seed.z = imtk_slider(IMUID, seed.z, -1.0, 1.0, 80, 80); 1.160 1.161 - glMatrixMode(GL_PROJECTION); 1.162 - glPopMatrix(); 1.163 - glMatrixMode(GL_MODELVIEW); 1.164 - glPopMatrix(); 1.165 + imtk_label("seed w", 10, 110); 1.166 + seed.w = imtk_slider(IMUID, seed.w, -1.0, 1.0, 80, 110); 1.167 + 1.168 + imtk_label("iterations", 10, 140); 1.169 + iter = imtk_slider(IMUID, iter, 0, 32, 80, 140); 1.170 + 1.171 + imtk_label("max error", 10, 170); 1.172 + err_thres = imtk_slider(IMUID, err_thres, 0, 0.075, 80, 170); 1.173 + 1.174 + imtk_label("reflectivity", 280, 20); 1.175 + reflectivity = imtk_slider(IMUID, reflectivity, 0, 1.0, 350, 20); 1.176 + 1.177 + imtk_end(); 1.178 } 1.179 1.180 void gui_set_visible(bool vis) 1.181 { 1.182 - if(vis) { 1.183 - win_seed->show(); 1.184 - win_material->show(); 1.185 - } else { 1.186 - win_seed->hide(); 1.187 - win_material->hide(); 1.188 - } 1.189 + show_gui = vis; 1.190 } 1.191 - 1.192 -void huecb(utk::Event *ev, void *cls) 1.193 -{ 1.194 - utk::HueBox *huebox = (utk::HueBox*)ev->widget; 1.195 - utk::ColorBox *colbox = (utk::ColorBox*)cls; 1.196 - 1.197 - colbox->set_h(huebox->get_h()); 1.198 -} 1.199 - 1.200 -void cbfunc(utk::Event *ev, void *cls) 1.201 -{ 1.202 - int id = (int)(intptr_t)cls; 1.203 - 1.204 - switch(id) { 1.205 - case 0: 1.206 - case 1: 1.207 - case 2: 1.208 - case 3: 1.209 - { 1.210 - utk::Slider *slider = (utk::Slider*)ev->widget; 1.211 - 1.212 - seed[id] = slider->get_value(); 1.213 - set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w); 1.214 - glutPostRedisplay(); 1.215 - } 1.216 - break; 1.217 - 1.218 - case 4: 1.219 - { 1.220 - utk::Slider *slider = (utk::Slider*)ev->widget; 1.221 - 1.222 - reflectivity = slider->get_value(); 1.223 - set_uniform_float(sdr, "reflectivity", reflectivity); 1.224 - glutPostRedisplay(); 1.225 - } 1.226 - break; 1.227 - 1.228 - case 5: 1.229 - { 1.230 - utk::Slider *slider = (utk::Slider*)ev->widget; 1.231 - 1.232 - iter = (int)slider->get_value(); 1.233 - set_uniform_int(sdr, "iter", iter); 1.234 - glutPostRedisplay(); 1.235 - } 1.236 - break; 1.237 - 1.238 - case 6: 1.239 - { 1.240 - utk::Slider *slider = (utk::Slider*)ev->widget; 1.241 - 1.242 - err_thres = slider->get_value(); 1.243 - set_uniform_float(sdr, "err_thres", err_thres); 1.244 - glutPostRedisplay(); 1.245 - } 1.246 - break; 1.247 - 1.248 - default: 1.249 - fprintf(stderr, "unhandled callback (id: %d)\n", id); 1.250 - break; 1.251 - } 1.252 -} 1.253 - 1.254 - 1.255 -#define CONVX(x) (2.0 * (float)(x) / (float)xsz - 1.0) 1.256 -#define CONVY(y) (1.0 - 2.0 * (float)(y) / (float)ysz) 1.257 - 1.258 -void utk_color(int r, int g, int b, int a) 1.259 -{ 1.260 - glColor4ub(r, g, b, a); 1.261 - SetTextColor(Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0)); 1.262 -} 1.263 - 1.264 -void utk_clip(int x1, int y1, int x2, int y2) 1.265 -{ 1.266 - if(x1 == x2 && y1 == y2 && x1 == y1 && x1 == 0) { 1.267 - glDisable(GL_SCISSOR_TEST); 1.268 - } else { 1.269 - glEnable(GL_SCISSOR_TEST); 1.270 - } 1.271 - glScissor(x1, ysz - y2, x2 - x1, y2 - y1); 1.272 -} 1.273 - 1.274 -void utk_image(int x, int y, const void *pix, int w, int h) 1.275 -{ 1.276 - glPixelZoom(1, -1); 1.277 - glRasterPos2f(CONVX(x), CONVY(y)); 1.278 - glDrawPixels(w, h, GL_BGRA, GL_UNSIGNED_BYTE, pix); 1.279 -} 1.280 - 1.281 -void utk_rect(int x1, int y1, int x2, int y2) 1.282 -{ 1.283 - glRectf(CONVX(x1), CONVY(y1), CONVX(x2), CONVY(y2)); 1.284 -} 1.285 - 1.286 -void utk_line(int x1, int y1, int x2, int y2, int width) 1.287 -{ 1.288 - glPushAttrib(GL_LINE_BIT); 1.289 - 1.290 - glLineWidth((float)width); 1.291 - glBegin(GL_LINES); 1.292 - glVertex2f(CONVX(x1), CONVY(y1)); 1.293 - glVertex2f(CONVX(x2), CONVY(y2)); 1.294 - glEnd(); 1.295 - 1.296 - glPopAttrib(); 1.297 -} 1.298 - 1.299 -void utk_text(int x, int y, const char *txt, int sz) 1.300 -{ 1.301 - float fx = (float)x / (float)xsz; 1.302 - float fy = (float)y / (float)ysz; 1.303 - 1.304 - SetTextPos(Vector2(fx, fy - max_descent)); 1.305 - //SetTextPos(Vector2(0.25, 0.25)); 1.306 - SetTextSize((float)sz / (float)TEXT_PT_SIZE); 1.307 - PrintString(txt); 1.308 -} 1.309 - 1.310 -int utk_text_spacing() 1.311 -{ 1.312 - return (int)(GetLineAdvance() * (float)ysz); 1.313 -} 1.314 - 1.315 -int utk_text_width(const char *txt, int sz) 1.316 -{ 1.317 - float prev_sz = GetTextSize(); 1.318 - SetTextSize((float)sz / (float)TEXT_PT_SIZE); 1.319 - 1.320 - int width = (int)(GetTextWidth(txt) * (float)xsz); 1.321 - 1.322 - SetTextSize(prev_sz); 1.323 - return width; 1.324 -} 1.325 -