00001 #ifndef OPENTISSUE_UTILITY_GL_ON_SCREEN_DISPLAY_H
00002 #define OPENTISSUE_UTILITY_GL_ON_SCREEN_DISPLAY_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #define OSD_CUSTOM 0
00013 #define OSD_TOP_LEFT 1
00014 #define OSD_TOP_RIGHT 2
00015 #define OSD_BOTTOM_LEFT 3
00016 #define OSD_BOTTOM_RIGHT 4
00017 #define OSD_CENTER 5
00018
00019 #include <vector>
00020 #include <cassert>
00021 #include <cstdio>
00022 #include <iostream>
00023 #include <string>
00024 #include <sstream>
00025 #include <stdarg.h>
00026
00027 #ifdef DEFINE_GLUI
00028 #include <GL/glui.h>
00029 #else
00030 #include <GL/glut.h>
00031 #endif
00032
00033 namespace OpenTissue
00034 {
00035 namespace gl
00036 {
00043 template<typename math_types>
00044 class OnScreenDisplay
00045 {
00046 public:
00047
00048 typedef typename math_types::real_type real_type;
00049
00050 protected:
00051
00052 int m_pos;
00053 int m_pos_x;
00054 int m_pos_y;
00055
00056 int m_screen_width;
00057 int m_screen_height;
00058
00059 int m_display_width;
00060 int m_display_height;
00061
00062 unsigned int m_maxlen;
00063
00064 float m_alpha;
00065 float m_red;
00066 float m_green;
00067 float m_blue;
00068 bool m_visible;
00069 bool m_static;
00070
00071 std::vector< std::string > m_string;
00072
00073 public:
00074
00080 OnScreenDisplay(int x, int y)
00081 : m_pos(0)
00082 , m_pos_x(x)
00083 , m_pos_y(y)
00084 , m_alpha(0.5f)
00085 , m_visible(true)
00086 , m_static(false)
00087 {
00088 set_color(0.1f, 0.15f, 0.19f);
00089 }
00090
00096 OnScreenDisplay(int pos)
00097 : m_pos(pos)
00098 , m_pos_x(0)
00099 , m_pos_y(0)
00100 , m_alpha(0.5)
00101 , m_maxlen(0)
00102 , m_visible(true)
00103 , m_static(false)
00104 {
00105 set_color(0.1f, 0.15f, 0.19f);
00106 }
00107
00108 OnScreenDisplay()
00109 : m_pos(OSD_TOP_LEFT)
00110 , m_pos_x(0)
00111 , m_pos_y(0)
00112 , m_alpha(0.5)
00113 , m_maxlen(0)
00114 ,m_visible(true)
00115 ,m_static(false)
00116 {
00117 set_color(0.1f, 0.15f, 0.19f);
00118 }
00119
00126 void set_pos(int pos)
00127 {
00128 m_pos = pos;
00129 m_pos_x = m_pos_y = 0;
00130 }
00131
00138 void set_pos(int x, int y)
00139 {
00140 m_pos = 0;
00141 m_pos_x = x;
00142 m_pos_y = y;
00143 }
00144
00145 void set_visibility(bool visible)
00146 {
00147 m_visible = visible;
00148 }
00149
00150 bool get_visibility()
00151 {
00152 return m_visible;
00153 }
00154
00155 void set_static(bool isstatic)
00156 {
00157 m_static = isstatic;
00158 }
00159
00160 bool get_static()
00161 {
00162 return m_static;
00163 }
00164
00165 void set_color(float r, float g, float b, float alpha=0.5f)
00166 {
00167 m_red=r; m_green=g; m_blue=b; m_alpha=alpha;
00168 }
00169
00176 void printf(char * format, ...)
00177 {
00178 char buffer[256];
00179 va_list args;
00180 va_start (args, format);
00181 vsprintf (buffer,format, args);
00182
00183 va_end (args);
00184 std::string * strptr = new std::string(buffer);
00185 this->print(*strptr);
00186 }
00187
00188 void print(std::string const & str)
00189 {
00190 m_string.push_back(str);
00191
00192 if(str.length() > m_maxlen)
00193 m_maxlen = str.length();
00194 }
00195
00196 void clear()
00197 {
00198 m_string.clear();
00199 m_maxlen = 0u;
00200 }
00201
00202 void display()
00203 {
00204 if(!m_visible)
00205 return;
00206
00207 int x_offset=0;
00208 int y_offset=0;
00209
00210 #ifdef DEFINE_GLUI
00211 GLUI_Master.get_viewport_area( &x_offset, &y_offset, &m_screen_width, &m_screen_height );
00212 #elif DEFINE_GLUI_DISABLE
00213 m_screen_width = glutGet(GLUT_WINDOW_WIDTH);
00214 m_screen_height = glutGet(GLUT_WINDOW_HEIGHT);
00215 #else
00216 m_screen_width = glutGet(GLUT_WINDOW_WIDTH);
00217 m_screen_height = glutGet(GLUT_WINDOW_HEIGHT);
00218 #endif
00219
00220 m_display_width = 2 + 8 * m_maxlen + 2;
00221
00222 m_display_height = 6 + m_string.size()*(13 + 2);
00223
00224 switch(m_pos)
00225 {
00226 case OSD_CUSTOM:
00227 m_pos_x += x_offset;
00228 m_pos_y += y_offset;
00229 if(m_pos_x > m_screen_width-m_display_width)
00230 m_pos_x = m_screen_width-m_display_width;
00231 else if(m_pos_x < x_offset)
00232 m_pos_x = x_offset;
00233 if(m_pos_y > m_screen_height-m_display_height)
00234 m_pos_y = m_screen_height-m_display_height;
00235 else if(m_pos_y < y_offset)
00236 m_pos_y = y_offset;
00237 break;
00238 case OSD_TOP_LEFT: break;
00239 case OSD_TOP_RIGHT:
00240 m_pos_x = m_screen_width-m_display_width;
00241 break;
00242 case OSD_BOTTOM_LEFT:
00243 m_pos_y = y_offset + m_screen_height-m_display_height;
00244 break;
00245 case OSD_BOTTOM_RIGHT:
00246 m_pos_x = m_screen_width-m_display_width;
00247 m_pos_y = m_screen_height-m_display_height;
00248 break;
00249 case OSD_CENTER:
00250 m_pos_x = x_offset + ((m_screen_width/2)-(m_display_width/2));
00251 y_offset = 0;
00252 m_pos_y = y_offset + ((m_screen_height/2)-(m_display_height/2));
00253 break;
00254 default:
00255 break;
00256 }
00257 display_text();
00258 if(!m_static)
00259 clear();
00260 }
00261
00262 protected:
00263
00264 void renderBitmapString(float x, float y, float z, void *font, const char *string)
00265 {
00266 const char *c;
00267 glRasterPos3f(m_pos_x + x, m_pos_y + y, z);
00268 for (c=string; *c != '\0'; c++) {
00269 glutBitmapCharacter(font, *c);
00270 }
00271 }
00272
00273 void display_text()
00274 {
00275 if(m_alpha > 0.0 && m_alpha < 1.0)
00276 {
00277
00278 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
00279 glEnable(GL_BLEND);
00280 glDisable(GL_TEXTURE_2D);
00281 }
00282 glDisable(GL_DEPTH_TEST);
00283
00284 glDisable(GL_LIGHTING);
00285 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
00286
00287 glMatrixMode(GL_PROJECTION);
00288 glPushMatrix();
00289 glLoadIdentity();
00290 glOrtho(0,m_screen_width,m_screen_height,0,-10,10);
00291 glMatrixMode(GL_MODELVIEW);
00292 glPushMatrix();
00293 glLoadIdentity();
00294
00295 if(m_alpha > 0.0){
00296 gl::ColorPicker(m_red,m_green,m_blue,m_alpha);
00297 glBegin(GL_QUADS);
00298 glVertex3f( m_pos_x+0.0f, m_pos_y+0.0f, -2.1f);
00299 glVertex3f( m_pos_x+m_display_width, m_pos_y+0.0f, -2.1f);
00300 glVertex3f( m_pos_x+m_display_width, m_pos_y+m_display_height, -2.1f);
00301 glVertex3f( m_pos_x+0.0f, m_pos_y+m_display_height, -2.1f);
00302 glEnd();
00303 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
00304 gl::ColorPicker(0.9,0.9,0.9,1.0);
00305 glBegin(GL_QUADS);
00306 glVertex3f( m_pos_x+0.0f, m_pos_y+0.0f, -2.0f);
00307 glVertex3f( m_pos_x+m_display_width, m_pos_y+0.0f, -2.0f);
00308 glVertex3f( m_pos_x+m_display_width, m_pos_y+m_display_height, -2.0f);
00309 glVertex3f( m_pos_x+0.0f, m_pos_y+m_display_height, -2.0f);
00310 glEnd();
00311 }
00312 gl::ColorPicker(0.9,0.9,0.9,1.0);
00313 int count = 0;
00314 for (std::vector<std::string>::iterator it = m_string.begin(); it!=m_string.end(); ++it)
00315 {
00316 count++;
00317 renderBitmapString(3,15*count,1,GLUT_BITMAP_8_BY_13,(*it).c_str());
00318 }
00319 glEnable(GL_LIGHTING);
00320 glEnable(GL_DEPTH_TEST);
00321 glDisable(GL_BLEND);
00322 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
00323
00324
00325 glMatrixMode(GL_PROJECTION);
00326 glPopMatrix();
00327 glMatrixMode(GL_MODELVIEW);
00328 glPopMatrix();
00329 }
00330
00331 };
00332
00333 }
00334
00335 }
00336
00337
00338
00339 #endif