rayfract

view src/gui.cc @ 3:bf1d56975cc9

- added visual studio project - removed vmath dependency
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Oct 2010 09:52:57 +0300
parents 87b6a11c920b
children e4349f5804b9
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 <ubertk.h>
10 #include "gui.h"
11 #include "utktext.h"
12 #include "sdr.h"
13 #include "vmath.h"
15 #ifndef GL_BGRA
16 #define GL_BGRA 0x80E1
17 #endif
19 #define TEXT_PT_SIZE 18
21 void huecb(utk::Event *ev, void *cls);
22 static void cbfunc(utk::Event *ev, void *cls);
24 void utk_color(int r, int g, int b, int a);
25 void utk_clip(int x1, int y1, int x2, int y2);
26 void utk_image(int x, int y, const void *pix, int xsz, int ysz);
28 void utk_rect(int x1, int y1, int x2, int y2);
29 void utk_line(int x1, int y1, int x2, int y2, int width);
31 void utk_text(int x, int y, const char *txt, int sz);
32 int utk_text_spacing();
33 int utk_text_width(const char *txt, int sz);
36 int xsz, ysz;
37 static utk::Container *utkroot;
38 static float max_descent;
39 utk::Window *win_seed, *win_material;
42 extern unsigned int sdr;
43 extern Vector4 seed;
44 extern int iter;
45 extern float err_thres;
46 extern float reflectivity;
47 extern Vector3 color;
49 int gui_init(int width, int height)
50 {
51 xsz = width;
52 ysz = height;
54 if(!CreateFont("georgia.ttf", TEXT_PT_SIZE)) {
55 fprintf(stderr, "failed to load font\n");
56 return -1;
57 }
58 max_descent = GetMaxDescent();
60 utk::gfx::color = utk_color;
61 utk::gfx::clip = utk_clip;
62 utk::gfx::image = utk_image;
63 utk::gfx::rect = utk_rect;
64 utk::gfx::line = utk_line;
65 utk::gfx::text = utk_text;
66 utk::gfx::text_spacing = utk_text_spacing;
67 utk::gfx::text_width = utk_text_width;
69 utkroot = utk::init(xsz, ysz);
71 win_seed = utk::create_window(utkroot, 5, 25, 220, 175, "julia parameters");
72 win_seed->set_alpha(128);
73 win_seed->show();
75 utk::VBox *vbox = utk::create_vbox(win_seed);
76 utk::HBox *hbox;
78 hbox = utk::create_hbox(vbox);
79 utk::create_label(hbox, "seed x");
80 utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)0)->set_value(seed.x);
81 hbox = utk::create_hbox(vbox);
82 utk::create_label(hbox, "seed y");
83 utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)1)->set_value(seed.y);
84 hbox = utk::create_hbox(vbox);
85 utk::create_label(hbox, "seed z");
86 utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)2)->set_value(seed.z);
87 hbox = utk::create_hbox(vbox);
88 utk::create_label(hbox, "seed w");
89 utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)3)->set_value(seed.w);
91 hbox = utk::create_hbox(vbox);
92 utk::create_label(hbox, "iterations");
93 utk::Slider *iter_slider = utk::create_slider(hbox, 0, 32, cbfunc, (void*)5);
94 iter_slider->set_value(iter);
95 iter_slider->set_vis_decimal(0);
97 hbox = utk::create_hbox(vbox);
98 utk::create_label(hbox, "max error");
99 utk::Slider *err_slider = utk::create_slider(hbox, 0, 0.075, cbfunc, (void*)6);
100 err_slider->set_value(err_thres);
101 err_slider->set_vis_decimal(4);
103 win_material = utk::create_window(utkroot, 250, 25, 220, 210, "material");
104 win_material->set_alpha(128);
105 win_material->show();
106 ((utk::WinFrame*)win_material->get_parent())->set_shade(true);
108 vbox = utk::create_vbox(win_material);
110 utk::ColorBox *colbox = new utk::ColorBox;
111 vbox->add_child(colbox);
112 utk::HueBox *huebox = new utk::HueBox;
113 vbox->add_child(huebox);
114 huebox->set_callback(utk::EVENT_MODIFY, huecb, colbox);
116 float hue, sat, val;
117 utk::rgb_to_hsv(color.x, color.y, color.z, &hue, &sat, &val);
118 colbox->set_color_hsv(hue, sat, val);
119 huebox->set_h(hue);
121 hbox = utk::create_hbox(vbox);
122 utk::create_label(hbox, "reflectivity");
123 utk::create_slider(hbox, 0, 1.0, cbfunc, (void*)4)->set_value(reflectivity);
125 return 0;
126 }
128 void gui_draw()
129 {
130 glMatrixMode(GL_MODELVIEW);
131 glPushMatrix();
132 glLoadIdentity();
133 glMatrixMode(GL_PROJECTION);
134 glPushMatrix();
135 glLoadIdentity();
137 glPushAttrib(GL_ENABLE_BIT);
138 glDisable(GL_LIGHTING);
139 glDisable(GL_DEPTH_TEST);
140 glDisable(GL_CULL_FACE);
141 glEnable(GL_BLEND);
143 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
145 utk::draw();
147 glPopAttrib();
149 glMatrixMode(GL_PROJECTION);
150 glPopMatrix();
151 glMatrixMode(GL_MODELVIEW);
152 glPopMatrix();
153 }
155 void gui_set_visible(bool vis)
156 {
157 if(vis) {
158 win_seed->show();
159 win_material->show();
160 } else {
161 win_seed->hide();
162 win_material->hide();
163 }
164 }
166 void huecb(utk::Event *ev, void *cls)
167 {
168 utk::HueBox *huebox = (utk::HueBox*)ev->widget;
169 utk::ColorBox *colbox = (utk::ColorBox*)cls;
171 colbox->set_h(huebox->get_h());
172 }
174 void cbfunc(utk::Event *ev, void *cls)
175 {
176 int id = (int)(intptr_t)cls;
178 switch(id) {
179 case 0:
180 case 1:
181 case 2:
182 case 3:
183 {
184 utk::Slider *slider = (utk::Slider*)ev->widget;
186 seed[id] = slider->get_value();
187 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
188 glutPostRedisplay();
189 }
190 break;
192 case 4:
193 {
194 utk::Slider *slider = (utk::Slider*)ev->widget;
196 reflectivity = slider->get_value();
197 set_uniform_float(sdr, "reflectivity", reflectivity);
198 glutPostRedisplay();
199 }
200 break;
202 case 5:
203 {
204 utk::Slider *slider = (utk::Slider*)ev->widget;
206 iter = (int)slider->get_value();
207 set_uniform_int(sdr, "iter", iter);
208 glutPostRedisplay();
209 }
210 break;
212 case 6:
213 {
214 utk::Slider *slider = (utk::Slider*)ev->widget;
216 err_thres = slider->get_value();
217 set_uniform_float(sdr, "err_thres", err_thres);
218 glutPostRedisplay();
219 }
220 break;
222 default:
223 fprintf(stderr, "unhandled callback (id: %d)\n", id);
224 break;
225 }
226 }
229 #define CONVX(x) (2.0 * (float)(x) / (float)xsz - 1.0)
230 #define CONVY(y) (1.0 - 2.0 * (float)(y) / (float)ysz)
232 void utk_color(int r, int g, int b, int a)
233 {
234 glColor4ub(r, g, b, a);
235 SetTextColor(Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0));
236 }
238 void utk_clip(int x1, int y1, int x2, int y2)
239 {
240 if(x1 == x2 && y1 == y2 && x1 == y1 && x1 == 0) {
241 glDisable(GL_SCISSOR_TEST);
242 } else {
243 glEnable(GL_SCISSOR_TEST);
244 }
245 glScissor(x1, ysz - y2, x2 - x1, y2 - y1);
246 }
248 void utk_image(int x, int y, const void *pix, int w, int h)
249 {
250 glPixelZoom(1, -1);
251 glRasterPos2f(CONVX(x), CONVY(y));
252 glDrawPixels(w, h, GL_BGRA, GL_UNSIGNED_BYTE, pix);
253 }
255 void utk_rect(int x1, int y1, int x2, int y2)
256 {
257 glRectf(CONVX(x1), CONVY(y1), CONVX(x2), CONVY(y2));
258 }
260 void utk_line(int x1, int y1, int x2, int y2, int width)
261 {
262 glPushAttrib(GL_LINE_BIT);
264 glLineWidth((float)width);
265 glBegin(GL_LINES);
266 glVertex2f(CONVX(x1), CONVY(y1));
267 glVertex2f(CONVX(x2), CONVY(y2));
268 glEnd();
270 glPopAttrib();
271 }
273 void utk_text(int x, int y, const char *txt, int sz)
274 {
275 float fx = (float)x / (float)xsz;
276 float fy = (float)y / (float)ysz;
278 SetTextPos(Vector2(fx, fy - max_descent));
279 //SetTextPos(Vector2(0.25, 0.25));
280 SetTextSize((float)sz / (float)TEXT_PT_SIZE);
281 PrintString(txt);
282 }
284 int utk_text_spacing()
285 {
286 return (int)(GetLineAdvance() * (float)ysz);
287 }
289 int utk_text_width(const char *txt, int sz)
290 {
291 float prev_sz = GetTextSize();
292 SetTextSize((float)sz / (float)TEXT_PT_SIZE);
294 int width = (int)(GetTextWidth(txt) * (float)xsz);
296 SetTextSize(prev_sz);
297 return width;
298 }