rayfract

view 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 source
1 #include <stdio.h>
3 #ifndef __APPLE__
4 #include <GL/glut.h>
5 #else
6 #include <GLUT/glut.h>
7 #endif
9 #include <vmath.h>
10 #include <ubertk.h>
11 #include "gui.h"
12 #include "utktext.h"
13 #include "sdr.h"
15 #define TEXT_PT_SIZE 18
17 void huecb(utk::Event *ev, void *cls);
18 static void cbfunc(utk::Event *ev, void *cls);
20 void utk_color(int r, int g, int b, int a);
21 void utk_clip(int x1, int y1, int x2, int y2);
22 void utk_image(int x, int y, const void *pix, int xsz, int ysz);
24 void utk_rect(int x1, int y1, int x2, int y2);
25 void utk_line(int x1, int y1, int x2, int y2, int width);
27 void utk_text(int x, int y, const char *txt, int sz);
28 int utk_text_spacing();
29 int utk_text_width(const char *txt, int sz);
32 int xsz, ysz;
33 static utk::Container *utkroot;
34 static float max_descent;
35 utk::Window *win_seed, *win_material;
38 extern unsigned int sdr;
39 extern Vector4 seed;
40 extern int iter;
41 extern float err_thres;
42 extern float reflectivity;
43 extern Vector3 color;
45 int gui_init(int width, int height)
46 {
47 xsz = width;
48 ysz = height;
50 if(!CreateFont("georgia.ttf", TEXT_PT_SIZE)) {
51 fprintf(stderr, "failed to load font\n");
52 return -1;
53 }
54 max_descent = GetMaxDescent();
56 utk::gfx::color = utk_color;
57 utk::gfx::clip = utk_clip;
58 utk::gfx::image = utk_image;
59 utk::gfx::rect = utk_rect;
60 utk::gfx::line = utk_line;
61 utk::gfx::text = utk_text;
62 utk::gfx::text_spacing = utk_text_spacing;
63 utk::gfx::text_width = utk_text_width;
65 utkroot = utk::init(xsz, ysz);
67 win_seed = utk::create_window(utkroot, 5, 25, 220, 175, "julia parameters");
68 win_seed->set_alpha(128);
69 win_seed->show();
71 utk::VBox *vbox = utk::create_vbox(win_seed);
72 utk::HBox *hbox;
74 hbox = utk::create_hbox(vbox);
75 utk::create_label(hbox, "seed x");
76 utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)0)->set_value(seed.x);
77 hbox = utk::create_hbox(vbox);
78 utk::create_label(hbox, "seed y");
79 utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)1)->set_value(seed.y);
80 hbox = utk::create_hbox(vbox);
81 utk::create_label(hbox, "seed z");
82 utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)2)->set_value(seed.z);
83 hbox = utk::create_hbox(vbox);
84 utk::create_label(hbox, "seed w");
85 utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)3)->set_value(seed.w);
87 hbox = utk::create_hbox(vbox);
88 utk::create_label(hbox, "iterations");
89 utk::Slider *iter_slider = utk::create_slider(hbox, 0, 32, cbfunc, (void*)5);
90 iter_slider->set_value(iter);
91 iter_slider->set_vis_decimal(0);
93 hbox = utk::create_hbox(vbox);
94 utk::create_label(hbox, "max error");
95 utk::Slider *err_slider = utk::create_slider(hbox, 0, 0.075, cbfunc, (void*)6);
96 err_slider->set_value(err_thres);
97 err_slider->set_vis_decimal(4);
99 win_material = utk::create_window(utkroot, 250, 25, 220, 210, "material");
100 win_material->set_alpha(128);
101 win_material->show();
102 ((utk::WinFrame*)win_material->get_parent())->set_shade(true);
104 vbox = utk::create_vbox(win_material);
106 utk::ColorBox *colbox = new utk::ColorBox;
107 vbox->add_child(colbox);
108 utk::HueBox *huebox = new utk::HueBox;
109 vbox->add_child(huebox);
110 huebox->set_callback(utk::EVENT_MODIFY, huecb, colbox);
112 float hue, sat, val;
113 utk::rgb_to_hsv(color.x, color.y, color.z, &hue, &sat, &val);
114 colbox->set_color_hsv(hue, sat, val);
115 huebox->set_h(hue);
117 hbox = utk::create_hbox(vbox);
118 utk::create_label(hbox, "reflectivity");
119 utk::create_slider(hbox, 0, 1.0, cbfunc, (void*)4)->set_value(reflectivity);
121 return 0;
122 }
124 void gui_draw()
125 {
126 glMatrixMode(GL_MODELVIEW);
127 glPushMatrix();
128 glLoadIdentity();
129 glMatrixMode(GL_PROJECTION);
130 glPushMatrix();
131 glLoadIdentity();
133 glPushAttrib(GL_ENABLE_BIT);
134 glDisable(GL_LIGHTING);
135 glDisable(GL_DEPTH_TEST);
136 glDisable(GL_CULL_FACE);
137 glEnable(GL_BLEND);
139 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
141 utk::draw();
143 glPopAttrib();
145 glMatrixMode(GL_PROJECTION);
146 glPopMatrix();
147 glMatrixMode(GL_MODELVIEW);
148 glPopMatrix();
149 }
151 void gui_set_visible(bool vis)
152 {
153 if(vis) {
154 win_seed->show();
155 win_material->show();
156 } else {
157 win_seed->hide();
158 win_material->hide();
159 }
160 }
162 void huecb(utk::Event *ev, void *cls)
163 {
164 utk::HueBox *huebox = (utk::HueBox*)ev->widget;
165 utk::ColorBox *colbox = (utk::ColorBox*)cls;
167 colbox->set_h(huebox->get_h());
168 }
170 void cbfunc(utk::Event *ev, void *cls)
171 {
172 int id = (int)(intptr_t)cls;
174 switch(id) {
175 case 0:
176 case 1:
177 case 2:
178 case 3:
179 {
180 utk::Slider *slider = (utk::Slider*)ev->widget;
182 seed[id] = slider->get_value();
183 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
184 glutPostRedisplay();
185 }
186 break;
188 case 4:
189 {
190 utk::Slider *slider = (utk::Slider*)ev->widget;
192 reflectivity = slider->get_value();
193 set_uniform_float(sdr, "reflectivity", reflectivity);
194 glutPostRedisplay();
195 }
196 break;
198 case 5:
199 {
200 utk::Slider *slider = (utk::Slider*)ev->widget;
202 iter = (int)slider->get_value();
203 set_uniform_int(sdr, "iter", iter);
204 glutPostRedisplay();
205 }
206 break;
208 case 6:
209 {
210 utk::Slider *slider = (utk::Slider*)ev->widget;
212 err_thres = slider->get_value();
213 set_uniform_float(sdr, "err_thres", err_thres);
214 glutPostRedisplay();
215 }
216 break;
218 default:
219 fprintf(stderr, "unhandled callback (id: %d)\n", id);
220 break;
221 }
222 }
225 #define CONVX(x) (2.0 * (float)(x) / (float)xsz - 1.0)
226 #define CONVY(y) (1.0 - 2.0 * (float)(y) / (float)ysz)
228 void utk_color(int r, int g, int b, int a)
229 {
230 glColor4ub(r, g, b, a);
231 SetTextColor(Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0));
232 }
234 void utk_clip(int x1, int y1, int x2, int y2)
235 {
236 if(x1 == x2 && y1 == y2 && x1 == y1 && x1 == 0) {
237 glDisable(GL_SCISSOR_TEST);
238 } else {
239 glEnable(GL_SCISSOR_TEST);
240 }
241 glScissor(x1, ysz - y2, x2 - x1, y2 - y1);
242 }
244 void utk_image(int x, int y, const void *pix, int w, int h)
245 {
246 glPixelZoom(1, -1);
247 glRasterPos2f(CONVX(x), CONVY(y));
248 glDrawPixels(w, h, GL_BGRA, GL_UNSIGNED_BYTE, pix);
249 }
251 void utk_rect(int x1, int y1, int x2, int y2)
252 {
253 glRectf(CONVX(x1), CONVY(y1), CONVX(x2), CONVY(y2));
254 }
256 void utk_line(int x1, int y1, int x2, int y2, int width)
257 {
258 glPushAttrib(GL_LINE_BIT);
260 glLineWidth((float)width);
261 glBegin(GL_LINES);
262 glVertex2f(CONVX(x1), CONVY(y1));
263 glVertex2f(CONVX(x2), CONVY(y2));
264 glEnd();
266 glPopAttrib();
267 }
269 void utk_text(int x, int y, const char *txt, int sz)
270 {
271 float fx = (float)x / (float)xsz;
272 float fy = (float)y / (float)ysz;
274 SetTextPos(Vector2(fx, fy - max_descent));
275 //SetTextPos(Vector2(0.25, 0.25));
276 SetTextSize((float)sz / (float)TEXT_PT_SIZE);
277 PrintString(txt);
278 }
280 int utk_text_spacing()
281 {
282 return (int)(GetLineAdvance() * (float)ysz);
283 }
285 int utk_text_width(const char *txt, int sz)
286 {
287 float prev_sz = GetTextSize();
288 SetTextSize((float)sz / (float)TEXT_PT_SIZE);
290 int width = (int)(GetTextWidth(txt) * (float)xsz);
292 SetTextSize(prev_sz);
293 return width;
294 }