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