rayfract

changeset 4:e4349f5804b9

switched to imtk
author John Tsiombikas <nuclear@siggraph.org>
date Fri, 29 Apr 2011 07:30:31 +0300
parents bf1d56975cc9
children 48e0e7d33d9e
files Makefile src/gui.cc src/gui.h src/rayfract.cc src/utktext.cc src/utktext.h
diffstat 6 files changed, 49 insertions(+), 768 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Tue Oct 26 09:52:57 2010 +0300
     1.2 +++ b/Makefile	Fri Apr 29 07:30:31 2011 +0300
     1.3 @@ -6,14 +6,14 @@
     1.4  ifeq ($(shell uname -s), Darwin)
     1.5  	libgl = -framework OpenGL -framework GLUT -lGLEW
     1.6  else
     1.7 -	libgl = -lGL -lglut -lGLEW
     1.8 +	libgl = -lGL -lGLU -lglut -lGLEW
     1.9  endif
    1.10  
    1.11  CC = gcc
    1.12  CXX = g++
    1.13 -CFLAGS = -pedantic -Wall -g `pkg-config --cflags vmath freetype2`
    1.14 -CXXFLAGS = -pedantic -Wall -g `pkg-config --cflags vmath freetype2` -I/usr/local/include/utk
    1.15 -LDFLAGS = $(libgl) `pkg-config --libs vmath freetype2` -lutk
    1.16 +CFLAGS = -pedantic -Wall -g `pkg-config --cflags vmath`
    1.17 +CXXFLAGS = -pedantic -Wall -g `pkg-config --cflags vmath`
    1.18 +LDFLAGS = $(libgl) `pkg-config --libs vmath freetype2` -limtk
    1.19  
    1.20  $(bin): $(obj)
    1.21  	$(CXX) -o $@ $(obj) $(LDFLAGS)
     2.1 --- a/src/gui.cc	Tue Oct 26 09:52:57 2010 +0300
     2.2 +++ b/src/gui.cc	Fri Apr 29 07:30:31 2011 +0300
     2.3 @@ -6,294 +6,65 @@
     2.4  #include <GLUT/glut.h>
     2.5  #endif
     2.6  
     2.7 -#include <ubertk.h>
     2.8 +#include <imtk.h>
     2.9  #include "gui.h"
    2.10 -#include "utktext.h"
    2.11  #include "sdr.h"
    2.12  #include "vmath.h"
    2.13  
    2.14 -#ifndef GL_BGRA
    2.15 -#define GL_BGRA	0x80E1
    2.16 -#endif
    2.17 -
    2.18 -#define TEXT_PT_SIZE	18
    2.19 -
    2.20 -void huecb(utk::Event *ev, void *cls);
    2.21 -static void cbfunc(utk::Event *ev, void *cls);
    2.22 -
    2.23 -void utk_color(int r, int g, int b, int a);
    2.24 -void utk_clip(int x1, int y1, int x2, int y2);
    2.25 -void utk_image(int x, int y, const void *pix, int xsz, int ysz);
    2.26 -
    2.27 -void utk_rect(int x1, int y1, int x2, int y2);
    2.28 -void utk_line(int x1, int y1, int x2, int y2, int width);
    2.29 -
    2.30 -void utk_text(int x, int y, const char *txt, int sz);
    2.31 -int utk_text_spacing();
    2.32 -int utk_text_width(const char *txt, int sz);
    2.33 -
    2.34  
    2.35  int xsz, ysz;
    2.36 -static utk::Container *utkroot;
    2.37 -static float max_descent;
    2.38 -utk::Window *win_seed, *win_material;
    2.39 -
    2.40 +static int show_gui = 1;
    2.41  
    2.42  extern unsigned int sdr;
    2.43  extern Vector4 seed;
    2.44  extern int iter;
    2.45  extern float err_thres;
    2.46  extern float reflectivity;
    2.47 -extern Vector3 color;
    2.48  
    2.49  int gui_init(int width, int height)
    2.50  {
    2.51  	xsz = width;
    2.52  	ysz = height;
    2.53  
    2.54 -	if(!CreateFont("georgia.ttf", TEXT_PT_SIZE)) {
    2.55 -		fprintf(stderr, "failed to load font\n");
    2.56 -		return -1;
    2.57 -	}
    2.58 -	max_descent = GetMaxDescent();
    2.59 +	imtk_set_viewport(width, height);
    2.60  
    2.61 -	utk::gfx::color = utk_color;
    2.62 -	utk::gfx::clip = utk_clip;
    2.63 -	utk::gfx::image = utk_image;
    2.64 -	utk::gfx::rect = utk_rect;
    2.65 -	utk::gfx::line = utk_line;
    2.66 -	utk::gfx::text = utk_text;
    2.67 -	utk::gfx::text_spacing = utk_text_spacing;
    2.68 -	utk::gfx::text_width = utk_text_width;
    2.69 -
    2.70 -	utkroot = utk::init(xsz, ysz);
    2.71 -
    2.72 -	win_seed = utk::create_window(utkroot, 5, 25, 220, 175, "julia parameters");
    2.73 -	win_seed->set_alpha(128);
    2.74 -	win_seed->show();
    2.75 -
    2.76 -	utk::VBox *vbox = utk::create_vbox(win_seed);
    2.77 -	utk::HBox *hbox;
    2.78 -
    2.79 -	hbox = utk::create_hbox(vbox);
    2.80 -	utk::create_label(hbox, "seed x");
    2.81 -	utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)0)->set_value(seed.x);
    2.82 -	hbox = utk::create_hbox(vbox);
    2.83 -	utk::create_label(hbox, "seed y");
    2.84 -	utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)1)->set_value(seed.y);
    2.85 -	hbox = utk::create_hbox(vbox);
    2.86 -	utk::create_label(hbox, "seed z");
    2.87 -	utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)2)->set_value(seed.z);
    2.88 -	hbox = utk::create_hbox(vbox);
    2.89 -	utk::create_label(hbox, "seed w");
    2.90 -	utk::create_slider(hbox, -1.0, 1.0, cbfunc, (void*)3)->set_value(seed.w);
    2.91 -
    2.92 -	hbox = utk::create_hbox(vbox);
    2.93 -	utk::create_label(hbox, "iterations");
    2.94 -	utk::Slider *iter_slider = utk::create_slider(hbox, 0, 32, cbfunc, (void*)5);
    2.95 -	iter_slider->set_value(iter);
    2.96 -	iter_slider->set_vis_decimal(0);
    2.97 -
    2.98 -	hbox = utk::create_hbox(vbox);
    2.99 -	utk::create_label(hbox, "max error");
   2.100 -	utk::Slider *err_slider = utk::create_slider(hbox, 0, 0.075, cbfunc, (void*)6);
   2.101 -	err_slider->set_value(err_thres);
   2.102 -	err_slider->set_vis_decimal(4);
   2.103 -
   2.104 -	win_material = utk::create_window(utkroot, 250, 25, 220, 210, "material");
   2.105 -	win_material->set_alpha(128);
   2.106 -	win_material->show();
   2.107 -	((utk::WinFrame*)win_material->get_parent())->set_shade(true);
   2.108 -
   2.109 -	vbox = utk::create_vbox(win_material);
   2.110 -
   2.111 -	utk::ColorBox *colbox = new utk::ColorBox;
   2.112 -	vbox->add_child(colbox);
   2.113 -	utk::HueBox *huebox = new utk::HueBox;
   2.114 -	vbox->add_child(huebox);
   2.115 -	huebox->set_callback(utk::EVENT_MODIFY, huecb, colbox);
   2.116 -
   2.117 -	float hue, sat, val;
   2.118 -	utk::rgb_to_hsv(color.x, color.y, color.z, &hue, &sat, &val);
   2.119 -	colbox->set_color_hsv(hue, sat, val);
   2.120 -	huebox->set_h(hue);
   2.121 -
   2.122 -	hbox = utk::create_hbox(vbox);
   2.123 -	utk::create_label(hbox, "reflectivity");
   2.124 -	utk::create_slider(hbox, 0, 1.0, cbfunc, (void*)4)->set_value(reflectivity);
   2.125 -
   2.126 +	/*imtk_set_alpha(0.5);*/
   2.127  	return 0;
   2.128  }
   2.129  
   2.130  void gui_draw()
   2.131  {
   2.132 -	glMatrixMode(GL_MODELVIEW);
   2.133 -	glPushMatrix();
   2.134 -	glLoadIdentity();
   2.135 -	glMatrixMode(GL_PROJECTION);
   2.136 -	glPushMatrix();
   2.137 -	glLoadIdentity();
   2.138 +	if(!show_gui) {
   2.139 +		return;
   2.140 +	}
   2.141  
   2.142 -	glPushAttrib(GL_ENABLE_BIT);
   2.143 -	glDisable(GL_LIGHTING);
   2.144 -	glDisable(GL_DEPTH_TEST);
   2.145 -	glDisable(GL_CULL_FACE);
   2.146 -	glEnable(GL_BLEND);
   2.147 +	imtk_begin();
   2.148  
   2.149 -	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   2.150 +	imtk_label("seed x", 10, 20);
   2.151 +	seed.x = imtk_slider(IMUID, seed.x, -1.0, 1.0, 80, 20);
   2.152  
   2.153 -	utk::draw();
   2.154 +	imtk_label("seed y", 10, 50);
   2.155 +	seed.y = imtk_slider(IMUID, seed.y, -1.0, 1.0, 80, 50);
   2.156  
   2.157 -	glPopAttrib();
   2.158 +	imtk_label("seed z", 10, 80);
   2.159 +	seed.z = imtk_slider(IMUID, seed.z, -1.0, 1.0, 80, 80);
   2.160  
   2.161 -	glMatrixMode(GL_PROJECTION);
   2.162 -	glPopMatrix();
   2.163 -	glMatrixMode(GL_MODELVIEW);
   2.164 -	glPopMatrix();
   2.165 +	imtk_label("seed w", 10, 110);
   2.166 +	seed.w = imtk_slider(IMUID, seed.w, -1.0, 1.0, 80, 110);
   2.167 +
   2.168 +	imtk_label("iterations", 10, 140);
   2.169 +	iter = imtk_slider(IMUID, iter, 0, 32, 80, 140);
   2.170 +
   2.171 +	imtk_label("max error", 10, 170);
   2.172 +	err_thres = imtk_slider(IMUID, err_thres, 0, 0.075, 80, 170);
   2.173 +
   2.174 +	imtk_label("reflectivity", 280, 20);
   2.175 +	reflectivity = imtk_slider(IMUID, reflectivity, 0, 1.0, 350, 20);
   2.176 +
   2.177 +	imtk_end();
   2.178  }
   2.179  
   2.180  void gui_set_visible(bool vis)
   2.181  {
   2.182 -	if(vis) {
   2.183 -		win_seed->show();
   2.184 -		win_material->show();
   2.185 -	} else {
   2.186 -		win_seed->hide();
   2.187 -		win_material->hide();
   2.188 -	}
   2.189 +	show_gui = vis;
   2.190  }
   2.191 -
   2.192 -void huecb(utk::Event *ev, void *cls)
   2.193 -{
   2.194 -	utk::HueBox *huebox = (utk::HueBox*)ev->widget;
   2.195 -	utk::ColorBox *colbox = (utk::ColorBox*)cls;
   2.196 -
   2.197 -	colbox->set_h(huebox->get_h());
   2.198 -}
   2.199 -
   2.200 -void cbfunc(utk::Event *ev, void *cls)
   2.201 -{
   2.202 -	int id = (int)(intptr_t)cls;
   2.203 -
   2.204 -	switch(id) {
   2.205 -	case 0:
   2.206 -	case 1:
   2.207 -	case 2:
   2.208 -	case 3:
   2.209 -		{
   2.210 -			utk::Slider *slider = (utk::Slider*)ev->widget;
   2.211 -
   2.212 -			seed[id] = slider->get_value();
   2.213 -			set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
   2.214 -			glutPostRedisplay();
   2.215 -		}
   2.216 -		break;
   2.217 -
   2.218 -	case 4:
   2.219 -		{
   2.220 -			utk::Slider *slider = (utk::Slider*)ev->widget;
   2.221 -
   2.222 -			reflectivity = slider->get_value();
   2.223 -			set_uniform_float(sdr, "reflectivity", reflectivity);
   2.224 -			glutPostRedisplay();
   2.225 -		}
   2.226 -		break;
   2.227 -
   2.228 -	case 5:
   2.229 -		{
   2.230 -			utk::Slider *slider = (utk::Slider*)ev->widget;
   2.231 -
   2.232 -			iter = (int)slider->get_value();
   2.233 -			set_uniform_int(sdr, "iter", iter);
   2.234 -			glutPostRedisplay();
   2.235 -		}
   2.236 -		break;
   2.237 -
   2.238 -	case 6:
   2.239 -		{
   2.240 -			utk::Slider *slider = (utk::Slider*)ev->widget;
   2.241 -
   2.242 -			err_thres = slider->get_value();
   2.243 -			set_uniform_float(sdr, "err_thres", err_thres);
   2.244 -			glutPostRedisplay();
   2.245 -		}
   2.246 -		break;
   2.247 -
   2.248 -	default:
   2.249 -		fprintf(stderr, "unhandled callback (id: %d)\n", id);
   2.250 -		break;
   2.251 -	}
   2.252 -}
   2.253 -
   2.254 -
   2.255 -#define CONVX(x)	(2.0 * (float)(x) / (float)xsz - 1.0)
   2.256 -#define CONVY(y)	(1.0 - 2.0 * (float)(y) / (float)ysz)
   2.257 -
   2.258 -void utk_color(int r, int g, int b, int a)
   2.259 -{
   2.260 -	glColor4ub(r, g, b, a);
   2.261 -	SetTextColor(Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0));
   2.262 -}
   2.263 -
   2.264 -void utk_clip(int x1, int y1, int x2, int y2)
   2.265 -{
   2.266 -	if(x1 == x2 && y1 == y2 && x1 == y1 && x1 == 0) {
   2.267 -		glDisable(GL_SCISSOR_TEST);
   2.268 -	} else {
   2.269 -		glEnable(GL_SCISSOR_TEST);
   2.270 -	}
   2.271 -	glScissor(x1, ysz - y2, x2 - x1, y2 - y1);
   2.272 -}
   2.273 -
   2.274 -void utk_image(int x, int y, const void *pix, int w, int h)
   2.275 -{
   2.276 -	glPixelZoom(1, -1);
   2.277 -	glRasterPos2f(CONVX(x), CONVY(y));
   2.278 -	glDrawPixels(w, h, GL_BGRA, GL_UNSIGNED_BYTE, pix);
   2.279 -}
   2.280 -
   2.281 -void utk_rect(int x1, int y1, int x2, int y2)
   2.282 -{
   2.283 -	glRectf(CONVX(x1), CONVY(y1), CONVX(x2), CONVY(y2));
   2.284 -}
   2.285 -
   2.286 -void utk_line(int x1, int y1, int x2, int y2, int width)
   2.287 -{
   2.288 -	glPushAttrib(GL_LINE_BIT);
   2.289 -
   2.290 -	glLineWidth((float)width);
   2.291 -	glBegin(GL_LINES);
   2.292 -	glVertex2f(CONVX(x1), CONVY(y1));
   2.293 -	glVertex2f(CONVX(x2), CONVY(y2));
   2.294 -	glEnd();
   2.295 -
   2.296 -	glPopAttrib();
   2.297 -}
   2.298 -
   2.299 -void utk_text(int x, int y, const char *txt, int sz)
   2.300 -{
   2.301 -	float fx = (float)x / (float)xsz;
   2.302 -	float fy = (float)y / (float)ysz;
   2.303 -
   2.304 -	SetTextPos(Vector2(fx, fy - max_descent));
   2.305 -	//SetTextPos(Vector2(0.25, 0.25));
   2.306 -	SetTextSize((float)sz / (float)TEXT_PT_SIZE);
   2.307 -	PrintString(txt);
   2.308 -}
   2.309 -
   2.310 -int utk_text_spacing()
   2.311 -{
   2.312 -	return (int)(GetLineAdvance() * (float)ysz);
   2.313 -}
   2.314 -
   2.315 -int utk_text_width(const char *txt, int sz)
   2.316 -{
   2.317 -	float prev_sz = GetTextSize();
   2.318 -	SetTextSize((float)sz / (float)TEXT_PT_SIZE);
   2.319 -
   2.320 -	int width = (int)(GetTextWidth(txt) * (float)xsz);
   2.321 -
   2.322 -	SetTextSize(prev_sz);
   2.323 -	return width;
   2.324 -}
   2.325 -
     3.1 --- a/src/gui.h	Tue Oct 26 09:52:57 2010 +0300
     3.2 +++ b/src/gui.h	Fri Apr 29 07:30:31 2011 +0300
     3.3 @@ -1,7 +1,7 @@
     3.4  #ifndef GUI_H_
     3.5  #define GUI_H_
     3.6  
     3.7 -#include <ubertk.h>
     3.8 +#include <imtk.h>
     3.9  
    3.10  int gui_init(int xsz, int ysz);
    3.11  void gui_draw();
     4.1 --- a/src/rayfract.cc	Tue Oct 26 09:52:57 2010 +0300
     4.2 +++ b/src/rayfract.cc	Fri Apr 29 07:30:31 2011 +0300
     4.3 @@ -105,6 +105,11 @@
     4.4  	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
     4.5  
     4.6  
     4.7 +	set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
     4.8 +	set_uniform_float(sdr, "reflectivity", reflectivity);
     4.9 +	set_uniform_int(sdr, "iter", iter);
    4.10 +	set_uniform_float(sdr, "err_thres", err_thres);
    4.11 +
    4.12  	glMatrixMode(GL_TEXTURE);
    4.13  	glPushMatrix();
    4.14  	glScalef(tex_scale.x, tex_scale.y, 1.0);
    4.15 @@ -204,17 +209,13 @@
    4.16  		break;
    4.17  	}
    4.18  
    4.19 -	utk::KeyboardEvent ev(key);
    4.20 -	ev.pressed = true;
    4.21 -	utk::event(&ev);
    4.22 +	imtk_inp_key(key, 1);
    4.23  	glutPostRedisplay();
    4.24  }
    4.25  
    4.26  void keyb_up(unsigned char key, int x, int y)
    4.27  {
    4.28 -	utk::KeyboardEvent ev(key);
    4.29 -	ev.pressed = false;
    4.30 -	utk::event(&ev);
    4.31 +	imtk_inp_key(key, 0);
    4.32  	glutPostRedisplay();
    4.33  }
    4.34  
    4.35 @@ -223,9 +224,11 @@
    4.36  int prev_x = -1, prev_y;
    4.37  void mouse(int bn, int state, int x, int y)
    4.38  {
    4.39 -	utk::Container *utkroot = utk::get_root_widget();
    4.40 +	int mod;
    4.41  
    4.42 -	if(utkroot->get_child_at(x, y) == utkroot) {
    4.43 +	mod = glutGetModifiers();
    4.44 +
    4.45 +	if(mod) {
    4.46  		bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
    4.47  		if(state == GLUT_DOWN) {
    4.48  			if(bn == 3) {
    4.49 @@ -242,11 +245,9 @@
    4.50  		} else {
    4.51  			prev_x = -1;
    4.52  		}
    4.53 +	} else {
    4.54 +		imtk_inp_mouse(bn, state == GLUT_DOWN ? 1 : 0);
    4.55  	}
    4.56 -
    4.57 -	utk::MButtonEvent ev(bn, x, y);
    4.58 -	ev.pressed = state == GLUT_DOWN;
    4.59 -	utk::event(&ev);
    4.60  	glutPostRedisplay();
    4.61  }
    4.62  
    4.63 @@ -275,16 +276,14 @@
    4.64  	prev_x = x;
    4.65  	prev_y = y;
    4.66  
    4.67 -	utk::MMotionEvent ev(x, y);
    4.68 -	utk::event(&ev);
    4.69 +	imtk_inp_motion(x, y);
    4.70  	glutPostRedisplay();
    4.71  }
    4.72  
    4.73  void passive_motion(int x, int y)
    4.74  {
    4.75 -	utk::MMotionEvent ev(x, y);
    4.76 -	utk::event(&ev);
    4.77 -	//glutPostRedisplay();
    4.78 +	imtk_inp_motion(x, y);
    4.79 +	glutPostRedisplay();
    4.80  }
    4.81  
    4.82  unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
     5.1 --- a/src/utktext.cc	Tue Oct 26 09:52:57 2010 +0300
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,444 +0,0 @@
     5.4 -#include <math.h>
     5.5 -#include <ctype.h>
     5.6 -#define GL_GLEXT_PROTOTYPES
     5.7 -#if defined(__APPLE__) && defined(__MACH__)
     5.8 -#include <GLUT/glut.h>
     5.9 -#else
    5.10 -#include <GL/glut.h>
    5.11 -#endif
    5.12 -#include <ft2build.h>
    5.13 -#include FT_FREETYPE_H
    5.14 -#include "utktext.h"
    5.15 -#include "utk_common.h"
    5.16 -#include "vmath.h"
    5.17 -
    5.18 -#ifndef GL_BGRA
    5.19 -#define GL_BGRA	0x80E1
    5.20 -#endif
    5.21 -
    5.22 -#ifndef GL_ABGR
    5.23 -#define GL_ABGR	0x8000
    5.24 -#endif
    5.25 -
    5.26 -#ifdef UTK_BIG_ENDIAN
    5.27 -#define PIXFMT	GL_ABGR
    5.28 -#else
    5.29 -#define PIXFMT	GL_BGRA
    5.30 -#endif
    5.31 -
    5.32 -#define MAX_CHARS		128
    5.33 -#define MAX_IMG_WIDTH	1024
    5.34 -#define SIZE_PIXELS(x)	((x) / 64)
    5.35 -
    5.36 -struct Font {
    5.37 -	unsigned int tex_id;
    5.38 -	float scale;	// this compensates if a higher res font is loaded in its stead, with a new CreateFont
    5.39 -	float line_adv;	// vertical advance to go to the next line
    5.40 -	struct {
    5.41 -		Vector2 pos, size;		// glyph position (from origin) and size in normalized coords [0, 1]
    5.42 -		float advance;			// advance in normalized coords
    5.43 -		Vector2 tc_pos, tc_sz;	// tex coord box pos/size
    5.44 -	} glyphs[MAX_CHARS];
    5.45 -};
    5.46 -
    5.47 -static void BlitFontGlyph(Font *fnt, int x, int y, FT_GlyphSlot glyph, unsigned int *img, int xsz, int ysz);
    5.48 -static void CleanUp();
    5.49 -
    5.50 -static FT_Library ft;
    5.51 -static Vector2 text_pos;
    5.52 -static float text_size = 1.0;
    5.53 -static Color text_color;
    5.54 -static Font *act_fnt;
    5.55 -
    5.56 -#if !defined(GLIBC) && !defined(__GLIBC__)
    5.57 -float log2(float x) {
    5.58 -	float res = 0.0;
    5.59 -	while(x > 1.0) {
    5.60 -		x /= 2.0;
    5.61 -		res += 1.0;
    5.62 -	}
    5.63 -	return res;
    5.64 -}
    5.65 -#endif	// _MSC_VER
    5.66 -
    5.67 -
    5.68 -static inline int NextPow2(int x)
    5.69 -{
    5.70 -	float lg2 = log2((float)x);
    5.71 -	return (int)pow(2.0f, (int)ceil(lg2));
    5.72 -}
    5.73 -
    5.74 -
    5.75 -unsigned int CreateFont(const char *fname, int font_size)
    5.76 -{
    5.77 -	if(!ft) {
    5.78 -		if(FT_Init_FreeType(&ft) != 0) {
    5.79 -			fprintf(stderr, "failed to initialize freetype\n");
    5.80 -			return 0;
    5.81 -		}
    5.82 -		atexit(CleanUp);
    5.83 -	}
    5.84 -
    5.85 -	FT_Face face;
    5.86 -	if(FT_New_Face(ft, fname, 0, &face) != 0) {
    5.87 -		fprintf(stderr, "failed to load font: %s\n", fname);
    5.88 -		return 0;
    5.89 -	}
    5.90 -
    5.91 -	FT_Set_Pixel_Sizes(face, 0, font_size);
    5.92 -
    5.93 -	Font *fnt = new Font;
    5.94 -	int max_width = MAX_CHARS * SIZE_PIXELS(face->bbox.xMax - face->bbox.xMin);
    5.95 -	int foo_xsz = MAX_IMG_WIDTH;
    5.96 -	int foo_ysz = SIZE_PIXELS(face->bbox.yMax - face->bbox.yMin) * max_width / foo_xsz;
    5.97 -
    5.98 -	int tex_xsz = NextPow2(foo_xsz);
    5.99 -	int tex_ysz = NextPow2(foo_ysz);
   5.100 -
   5.101 -	unsigned int *img;
   5.102 -	img = new unsigned int[tex_xsz * tex_ysz];
   5.103 -	memset(img, 0, tex_xsz * tex_ysz * sizeof *img);
   5.104 -
   5.105 -	extern int xsz, ysz;
   5.106 -	int vport_xsz = xsz, vport_ysz = ysz;
   5.107 -
   5.108 -	int max_glyph_y = 0;
   5.109 -	int max_glyph_x = 0;
   5.110 -	for(int i=0; i<MAX_CHARS; i++) {
   5.111 -		FT_Load_Char(face, i, 0);
   5.112 -		int width = SIZE_PIXELS(face->glyph->metrics.width);
   5.113 -		int height = SIZE_PIXELS(face->glyph->metrics.height);
   5.114 -		
   5.115 -		if(height > max_glyph_y) {
   5.116 -			max_glyph_y = height;
   5.117 -		}
   5.118 -
   5.119 -		if(width > max_glyph_x) {
   5.120 -			max_glyph_x = width;
   5.121 -		}
   5.122 -	}
   5.123 -
   5.124 -	int gx = 0, gy = 0;
   5.125 -	for(int i=0; i<MAX_CHARS; i++) {
   5.126 -		FT_Load_Char(face, i, FT_LOAD_RENDER);
   5.127 -		FT_GlyphSlot g = face->glyph;
   5.128 -
   5.129 -		int gwidth = SIZE_PIXELS(g->metrics.width);
   5.130 -		int gheight = SIZE_PIXELS(g->metrics.height);
   5.131 -
   5.132 -		if(gx > MAX_IMG_WIDTH - gwidth) {
   5.133 -			gx = 0;
   5.134 -			gy += max_glyph_y;
   5.135 -		}
   5.136 -
   5.137 -		BlitFontGlyph(fnt, gx, gy, g, img, tex_xsz, tex_ysz);
   5.138 -		fnt->scale = 1.0;
   5.139 -		fnt->line_adv = (float)SIZE_PIXELS(g->metrics.vertAdvance) / (float)vport_ysz;
   5.140 -		fnt->glyphs[i].tc_pos.x = (float)gx / (float)tex_xsz;
   5.141 -		fnt->glyphs[i].tc_pos.y = (float)gy / (float)tex_ysz;
   5.142 -		fnt->glyphs[i].tc_sz.x = (float)gwidth / (float)tex_xsz;
   5.143 -		fnt->glyphs[i].tc_sz.y = (float)gheight / (float)tex_ysz;
   5.144 -		fnt->glyphs[i].size.x = (float)gwidth / (float)vport_xsz;
   5.145 -		fnt->glyphs[i].size.y = (float)gheight / (float)vport_ysz;
   5.146 -		fnt->glyphs[i].pos.x = (float)SIZE_PIXELS(g->metrics.horiBearingX) / (float)vport_xsz;
   5.147 -		fnt->glyphs[i].pos.y = -(float)SIZE_PIXELS(g->metrics.horiBearingY) / (float)vport_ysz;
   5.148 -		fnt->glyphs[i].advance = (float)SIZE_PIXELS(g->metrics.horiAdvance) / (float)vport_xsz;
   5.149 -
   5.150 -		gx += gwidth;
   5.151 -	}
   5.152 -
   5.153 -	FT_Done_Face(face);
   5.154 -
   5.155 -	glGenTextures(1, &fnt->tex_id);
   5.156 -	glBindTexture(GL_TEXTURE_2D, fnt->tex_id);
   5.157 -	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   5.158 -	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   5.159 -	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   5.160 -	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   5.161 -	glTexImage2D(GL_TEXTURE_2D, 0, 4, tex_xsz, tex_ysz, 0, PIXFMT, GL_UNSIGNED_BYTE, img);
   5.162 -
   5.163 -#ifdef DUMP_FONT_IMG
   5.164 -	FILE *fp;
   5.165 -	unsigned int *ptr = img;
   5.166 -
   5.167 -	if((fp = fopen("fnt.ppm", "wb"))) {
   5.168 -		fprintf(fp, "P6\n%d %d\n255\n", tex_xsz, tex_ysz);
   5.169 -
   5.170 -		for(int i=0; i<tex_xsz * tex_ysz; i++) {
   5.171 -			fputc((*ptr >> 24) & 0xff, fp);
   5.172 -			fputc((*ptr >> 24) & 0xff, fp);
   5.173 -			fputc((*ptr >> 24) & 0xff, fp);
   5.174 -			ptr++;
   5.175 -		}
   5.176 -		fclose(fp);
   5.177 -	}
   5.178 -#endif
   5.179 -	
   5.180 -	delete [] img;
   5.181 -
   5.182 -	act_fnt = fnt;
   5.183 -
   5.184 -	return 1;
   5.185 -}
   5.186 -
   5.187 -void DeleteFont(unsigned int fid)
   5.188 -{
   5.189 -}
   5.190 -
   5.191 -unsigned int GetFont(const char *fname, int sz)
   5.192 -{
   5.193 -	return 1;
   5.194 -}
   5.195 -
   5.196 -bool BindFont(unsigned int fid)
   5.197 -{
   5.198 -	return true;
   5.199 -}
   5.200 -
   5.201 -void SetTextPos(const Vector2 &pos)
   5.202 -{
   5.203 -	text_pos = pos;
   5.204 -}
   5.205 -
   5.206 -Vector2 GetTextPos()
   5.207 -{
   5.208 -	return text_pos;
   5.209 -}
   5.210 -
   5.211 -void TextLineAdvance(int adv)
   5.212 -{
   5.213 -	text_pos.y += (float)adv * act_fnt->line_adv;
   5.214 -}
   5.215 -
   5.216 -void TextCRet()
   5.217 -{
   5.218 -	text_pos.x = 0.0;
   5.219 -}
   5.220 -
   5.221 -void SetTextSize(float sz)
   5.222 -{
   5.223 -	text_size = sz;
   5.224 -}
   5.225 -
   5.226 -float GetTextSize()
   5.227 -{
   5.228 -	return text_size;
   5.229 -}
   5.230 -
   5.231 -void SetTextColor(const Color &col)
   5.232 -{
   5.233 -	text_color = col;
   5.234 -}
   5.235 -
   5.236 -Color GetTextColor()
   5.237 -{
   5.238 -	return text_color;
   5.239 -}
   5.240 -
   5.241 -static void ImOverlay(const Vector2 &v1, const Vector2 &v2, const Color &col, unsigned int tex)
   5.242 -{
   5.243 -	float l = v1.x * 2.0f - 1.0f;
   5.244 -	float r = v2.x * 2.0f - 1.0f;
   5.245 -	float u = -v1.y * 2.0f + 1.0f;
   5.246 -	float d = -v2.y * 2.0f + 1.0f;
   5.247 -
   5.248 -	glMatrixMode(GL_PROJECTION);
   5.249 -	glPushMatrix();
   5.250 -	glLoadIdentity();
   5.251 -	glMatrixMode(GL_MODELVIEW);
   5.252 -	glPushMatrix();
   5.253 -	glLoadIdentity();
   5.254 -
   5.255 -	glBindTexture(GL_TEXTURE_2D, tex);
   5.256 -
   5.257 -	glDisable(GL_DEPTH_TEST);
   5.258 -	glDisable(GL_LIGHTING);
   5.259 -	glDisable(GL_CULL_FACE);
   5.260 -
   5.261 -	glBegin(GL_QUADS);
   5.262 -	glColor4f(col.r, col.g, col.b, col.a);
   5.263 -	glTexCoord2f(0, 0);
   5.264 -	glVertex2f(l, u);
   5.265 -	glTexCoord2f(1, 0);
   5.266 -	glVertex2f(r, u);
   5.267 -	glTexCoord2f(1, 1);
   5.268 -	glVertex2f(r, d);
   5.269 -	glTexCoord2f(0, 1);
   5.270 -	glVertex2f(l, d);
   5.271 -	glEnd();
   5.272 -
   5.273 -	glEnable(GL_LIGHTING);
   5.274 -	glEnable(GL_DEPTH_TEST);
   5.275 -
   5.276 -	glMatrixMode(GL_PROJECTION);
   5.277 -	glPopMatrix();
   5.278 -	glMatrixMode(GL_MODELVIEW);
   5.279 -	glPopMatrix();
   5.280 -}
   5.281 -
   5.282 -float PrintChar(char c)
   5.283 -{
   5.284 -	// get texture coordinates for the glyph, and construct the texture matrix
   5.285 -	float tx = act_fnt->glyphs[(int)c].tc_pos.x;
   5.286 -	float ty = act_fnt->glyphs[(int)c].tc_pos.y;
   5.287 -	float sx = act_fnt->glyphs[(int)c].tc_sz.x;
   5.288 -	float sy = act_fnt->glyphs[(int)c].tc_sz.y;
   5.289 -
   5.290 -	float mat[] = {
   5.291 -		sx, 0, tx, 0,
   5.292 -		0, sy, ty, 0,
   5.293 -		0, 0, 1, 0,
   5.294 -		0, 0, 0, 1
   5.295 -	};
   5.296 -
   5.297 -	glMatrixMode(GL_TEXTURE);
   5.298 -	glPushMatrix();
   5.299 -	glLoadMatrixf(mat);
   5.300 -
   5.301 -	Vector2 pos = text_pos + act_fnt->glyphs[(int)c].pos * act_fnt->scale;
   5.302 -	ImOverlay(pos, pos + act_fnt->glyphs[(int)c].size * act_fnt->scale, text_color, act_fnt->tex_id);
   5.303 -
   5.304 -	glMatrixMode(GL_TEXTURE);
   5.305 -	glPopMatrix();
   5.306 -
   5.307 -	return act_fnt->glyphs[(int)c].advance * act_fnt->scale;
   5.308 -}
   5.309 -
   5.310 -// this function contains the preamble of all block text drawing functions (i.e. not printchar above)
   5.311 -static void PreDraw()
   5.312 -{
   5.313 -	glMatrixMode(GL_PROJECTION);
   5.314 -	glPushMatrix();
   5.315 -	/*glLoadTransposeMatrixf(OrthoProj(2, 2, 0, 10).m);*/
   5.316 -	glLoadIdentity();
   5.317 -	glMatrixMode(GL_MODELVIEW);
   5.318 -	glPushMatrix();
   5.319 -	glLoadIdentity();
   5.320 -	glMatrixMode(GL_TEXTURE);
   5.321 -	glPushMatrix();
   5.322 -	glLoadIdentity();
   5.323 -
   5.324 -	glEnable(GL_TEXTURE_2D);
   5.325 -	glBindTexture(GL_TEXTURE_2D, act_fnt->tex_id);
   5.326 -
   5.327 -	glBegin(GL_QUADS);
   5.328 -	glColor4f(text_color.r, text_color.g, text_color.b, text_color.a);
   5.329 -}
   5.330 -
   5.331 -static void PostDraw()
   5.332 -{
   5.333 -	glEnd();
   5.334 -
   5.335 -	glDisable(GL_TEXTURE_2D);
   5.336 -
   5.337 -	glMatrixMode(GL_TEXTURE);
   5.338 -	glPopMatrix();
   5.339 -	glMatrixMode(GL_MODELVIEW);
   5.340 -	glPopMatrix();
   5.341 -	glMatrixMode(GL_PROJECTION);
   5.342 -	glPopMatrix();
   5.343 -}
   5.344 -
   5.345 -float PrintString(const char *str, bool standalone)
   5.346 -{
   5.347 -	if(standalone) PreDraw();
   5.348 -	
   5.349 -	float start_x = text_pos.x;
   5.350 -	while(*str) {
   5.351 -		float tx = act_fnt->glyphs[(int)*str].tc_pos.x;
   5.352 -		float ty = act_fnt->glyphs[(int)*str].tc_pos.y;
   5.353 -		float sx = act_fnt->glyphs[(int)*str].tc_sz.x;
   5.354 -		float sy = act_fnt->glyphs[(int)*str].tc_sz.y;
   5.355 -
   5.356 -		Vector2 tc1 = Vector2(tx, ty);
   5.357 -		Vector2 tc2 = Vector2(tx + sx, ty + sy);
   5.358 -
   5.359 -		Vector2 v1 = text_pos + act_fnt->glyphs[(int)*str].pos * act_fnt->scale * text_size;
   5.360 -		Vector2 v2 = v1 + act_fnt->glyphs[(int)*str].size * act_fnt->scale * text_size;
   5.361 -		float l = v1.x * 2.0f - 1.0f;
   5.362 -		float r = v2.x * 2.0f - 1.0f;
   5.363 -		float u = -v1.y * 2.0f + 1.0f;
   5.364 -		float d = -v2.y * 2.0f + 1.0f;
   5.365 -
   5.366 -		glTexCoord2f(tc1.x, tc1.y);
   5.367 -		glVertex2f(l, u);
   5.368 -		glTexCoord2f(tc2.x, tc1.y);
   5.369 -		glVertex2f(r, u);
   5.370 -		glTexCoord2f(tc2.x, tc2.y);
   5.371 -		glVertex2f(r, d);
   5.372 -		glTexCoord2f(tc1.x, tc2.y);
   5.373 -		glVertex2f(l, d);
   5.374 -
   5.375 -		text_pos.x += act_fnt->glyphs[(int)*str++].advance * act_fnt->scale * text_size;
   5.376 -	}
   5.377 -
   5.378 -	if(standalone) PostDraw();
   5.379 -	return text_pos.x - start_x;
   5.380 -}
   5.381 -
   5.382 -void PrintStringLines(const char **str, int lines)
   5.383 -{
   5.384 -	PreDraw();
   5.385 -
   5.386 -	while(lines-- > 0) {
   5.387 -		PrintString(*str++, false);
   5.388 -		TextLineAdvance();
   5.389 -		TextCRet();
   5.390 -	}
   5.391 -
   5.392 -	PostDraw();
   5.393 -}
   5.394 -
   5.395 -static void BlitFontGlyph(Font *fnt, int x, int y, FT_GlyphSlot glyph, unsigned int *img, int xsz, int ysz)
   5.396 -{
   5.397 -	if(glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY) {
   5.398 -		fprintf(stderr, "BlitFontGlyph: unsupported pixel mode: %d\n", glyph->bitmap.pixel_mode);
   5.399 -		return;
   5.400 -	}
   5.401 -
   5.402 -	unsigned int *dest = img + y * xsz + x;
   5.403 -	unsigned char *src = glyph->bitmap.buffer;
   5.404 -
   5.405 -	for(int j=0; j<glyph->bitmap.rows; j++) {
   5.406 -		for(int i=0; i<glyph->bitmap.width; i++) {
   5.407 -			dest[i] = 0x00ffffff | ((unsigned int)src[i] << 24);
   5.408 -		}
   5.409 -		dest += xsz;
   5.410 -		src += glyph->bitmap.pitch;
   5.411 -	}
   5.412 -}
   5.413 -
   5.414 -static void CleanUp()
   5.415 -{
   5.416 -	FT_Done_FreeType(ft);
   5.417 -}
   5.418 -
   5.419 -float GetMaxDescent()
   5.420 -{
   5.421 -	Font *fnt = act_fnt;
   5.422 -
   5.423 -	float max_descent = 0.0f;
   5.424 -	
   5.425 -	for(int i=0; i<MAX_CHARS; i++) {
   5.426 -		float des = fnt->glyphs[i].pos.y + fnt->glyphs[i].size.y;
   5.427 -		if(isprint(i) && des > max_descent) {
   5.428 -			max_descent = des;
   5.429 -		}
   5.430 -	}
   5.431 -
   5.432 -	return max_descent;
   5.433 -}
   5.434 -
   5.435 -float GetLineAdvance()
   5.436 -{
   5.437 -	return act_fnt->line_adv;
   5.438 -}
   5.439 -
   5.440 -float GetTextWidth(const char *str)
   5.441 -{
   5.442 -	float width = 0;
   5.443 -	while(*str) {
   5.444 -		width += act_fnt->glyphs[(int)*str++].advance * act_fnt->scale * text_size;
   5.445 -	}
   5.446 -	return width;
   5.447 -}
     6.1 --- a/src/utktext.h	Tue Oct 26 09:52:57 2010 +0300
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,45 +0,0 @@
     6.4 -#ifndef TEXT_H
     6.5 -#define TEXT_H
     6.6 -
     6.7 -#if defined(WIN32) || defined(__WIN32__)
     6.8 -#include <windows.h>
     6.9 -#endif
    6.10 -
    6.11 -#include "vmath.h"
    6.12 -
    6.13 -class Color {
    6.14 -public:
    6.15 -	float r, g, b, a;
    6.16 -
    6.17 -	Color() {r = g = b = a = 1.0f;}
    6.18 -	Color(float r, float g, float b, float a = 1.0f) {this->r = r; this->g = g; this->b = b; this->a = a;}
    6.19 -};
    6.20 -
    6.21 -
    6.22 -unsigned int CreateFont(const char *fname, int font_size);
    6.23 -void DeleteFont(unsigned int fid);
    6.24 -unsigned int GetFont(const char *fname, int sz);
    6.25 -bool BindFont(unsigned int fid);
    6.26 -
    6.27 -
    6.28 -void SetTextPos(const Vector2 &pos);
    6.29 -Vector2 GetTextPos();
    6.30 -
    6.31 -void TextLineAdvance(int adv = 1);
    6.32 -void TextCRet();
    6.33 -
    6.34 -void SetTextSize(float sz);
    6.35 -float GetTextSize();
    6.36 -
    6.37 -void SetTextColor(const Color &col);
    6.38 -Color GetTextColor();
    6.39 -
    6.40 -float PrintChar(char c);
    6.41 -float PrintString(const char *text, bool standalone = true);
    6.42 -void PrintStringLines(const char **str, int lines);
    6.43 -
    6.44 -float GetMaxDescent();
    6.45 -float GetLineAdvance();
    6.46 -float GetTextWidth(const char *str);
    6.47 -
    6.48 -#endif	// TEXT_H