fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

gl_draw.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: gl_draw.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // OpenGL drawing support routines for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 // Functions from <FL/gl.h>
00029 // See also Fl_Gl_Window and gl_start.cxx
00030 
00031 #include "flstring.h"
00032 #if HAVE_GL || defined(FL_DOXYGEN)
00033 
00034 #include <FL/Fl.H>
00035 #include <FL/gl.h>
00036 #include <FL/x.H>
00037 #include <FL/fl_draw.H>
00038 #include "Fl_Gl_Choice.H"
00039 #include "Fl_Font.H"
00040 #include <FL/fl_utf8.h>
00041 
00042 #if !defined(WIN32) && !defined(__APPLE__)
00043 #include <FL/Xutf8.h>
00044 #endif
00045 
00046 #if USE_XFT
00047 extern XFontStruct* fl_xxfont();
00048 #endif // USE_XFT
00049 
00051 int   gl_height() {return fl_height();}
00053 int   gl_descent() {return fl_descent();}
00055 double gl_width(const char* s) {return fl_width(s);}
00057 double gl_width(const char* s, int n) {return fl_width(s,n);}
00059 double gl_width(uchar c) {return fl_width(c);}
00060 
00061 static Fl_Font_Descriptor *gl_fontsize;
00062 
00063 #define GL_DRAW_USES_TEXTURES  (defined(__APPLE__) && !__ppc__) // 1 only for non-PPC OSX
00064 #ifndef __APPLE__
00065 #  define USE_OksiD_style_GL_font_selection 1  // Most hosts except OSX
00066 #else
00067 #  undef USE_OksiD_style_GL_font_selection  // OSX
00068 #endif
00069 
00070 #if USE_XFT
00071 #  undef USE_OksiD_style_GL_font_selection  // turn this off for XFT also
00072 #endif
00073 
00077 void  gl_font(int fontid, int size) {
00078   fl_font(fontid, size);
00079 #if !GL_DRAW_USES_TEXTURES
00080   if (!fl_fontsize->listbase) {
00081 
00082 #ifdef  USE_OksiD_style_GL_font_selection
00083     fl_fontsize->listbase = glGenLists(0x10000);
00084 #else // Fltk-1.1.8 style GL font selection
00085 
00086 #if defined (USE_X11) // X-windows options follow, either XFT or "plain" X
00087 // FIXME:  warning Ideally, for XFT, we really need a glXUseXftFont implementation here...
00088 // FIXME:  warning GL font selection is basically wrong here
00089 /* OksiD had a fairly sophisticated scheme for storing multiple X fonts in a XUtf8FontStruct,
00090  * then sorting through them at draw time (for normal X rendering) to find which one can
00091  * render the current glyph... But for now, just use the first font in the list for GL...
00092  */
00093     XFontStruct *font = fl_xfont;
00094     int base = font->min_char_or_byte2;
00095     int count = font->max_char_or_byte2-base+1;
00096     fl_fontsize->listbase = glGenLists(256);
00097     glXUseXFont(font->fid, base, count, fl_fontsize->listbase+base);
00098 # elif defined(WIN32)
00099     // this is unused because USE_OksiD_style_GL_font_selection == 1
00100     int base = fl_fontsize->metr.tmFirstChar;
00101     int count = fl_fontsize->metr.tmLastChar-base+1;
00102     HFONT oldFid = (HFONT)SelectObject(fl_gc, fl_fontsize->fid);
00103     fl_fontsize->listbase = glGenLists(256);
00104     wglUseFontBitmaps(fl_gc, base, count, fl_fontsize->listbase+base);
00105     SelectObject(fl_gc, oldFid);
00106 # elif defined(__APPLE_QUARTZ__)
00107 //AGL is not supported for use in 64-bit applications:
00108 //http://developer.apple.com/mac/library/documentation/Carbon/Conceptual/Carbon64BitGuide/OtherAPIChanges/OtherAPIChanges.html
00109     short font, face, size;
00110     uchar fn[256];
00111     fn[0]=strlen(fl_fontsize->q_name);
00112     strcpy((char*)(fn+1), fl_fontsize->q_name);
00113     GetFNum(fn, &font);
00114     face = 0;
00115     size = fl_fontsize->size;
00116     fl_fontsize->listbase = glGenLists(256);
00117         aglUseFont(aglGetCurrentContext(), font, face,
00118                size, 0, 256, fl_fontsize->listbase);
00119 # else 
00120 #   error unsupported platform
00121 # endif
00122 
00123 #endif // USE_OksiD_style_GL_font_selection
00124   }
00125   glListBase(fl_fontsize->listbase);
00126 #endif // !GL_DRAW_USES_TEXTURES
00127   gl_fontsize = fl_fontsize;
00128 }
00129 
00130 #ifndef __APPLE__
00131 static void get_list(int r) {
00132   gl_fontsize->glok[r] = 1;
00133 #if defined(USE_X11)
00134 # if USE_XFT
00135 // FIXME
00136 # else
00137   unsigned int ii = r * 0x400;
00138   for (int i = 0; i < 0x400; i++) {
00139     XFontStruct *font = NULL;
00140     unsigned short id;
00141     XGetUtf8FontAndGlyph(gl_fontsize->font, ii, &font, &id);
00142     if (font) glXUseXFont(font->fid, id, 1, gl_fontsize->listbase+ii);
00143     ii++;
00144    }
00145 # endif
00146 #elif defined(WIN32)
00147   unsigned int ii = r * 0x400;
00148   HFONT oldFid = (HFONT)SelectObject(fl_gc, gl_fontsize->fid);
00149   wglUseFontBitmapsW(fl_gc, ii, ii + 0x03ff, gl_fontsize->listbase+ii);
00150   SelectObject(fl_gc, oldFid);
00151 #elif defined(__APPLE_QUARTZ__)
00152 // handled by textures
00153 #else
00154 #  error unsupported platform
00155 #endif
00156 } // get_list
00157 #endif
00158 
00159 void gl_remove_displaylist_fonts()
00160 {
00161 # if HAVE_GL
00162 
00163   // clear variables used mostly in fl_font
00164   fl_font_ = 0;
00165   fl_size_ = 0;
00166 
00167   for (int j = 0 ; j < FL_FREE_FONT ; ++j)
00168   {
00169     Fl_Font_Descriptor* past = 0;
00170     Fl_Fontdesc* s    = fl_fonts + j ;
00171     Fl_Font_Descriptor* f    = s->first;
00172     while (f != 0) {
00173       if(f->listbase) {
00174         if(f == s->first) {
00175           s->first = f->next;
00176         }
00177         else {
00178           past->next = f->next;
00179         }
00180 
00181         // It would be nice if this next line was in a desctructor somewhere
00182         glDeleteLists(f->listbase, 256);
00183 
00184         Fl_Font_Descriptor* tmp = f;
00185         f = f->next;
00186         delete tmp;
00187       }
00188       else {
00189         past = f;
00190         f = f->next;
00191       }
00192     }
00193   }
00194 
00195 #endif
00196 }
00197 
00198 #if GL_DRAW_USES_TEXTURES
00199 static void gl_draw_textures(const char* str, int n);
00200 #endif
00201 
00207 void gl_draw(const char* str, int n) {
00208 #ifdef __APPLE__  
00209   
00210 #if GL_DRAW_USES_TEXTURES
00211   gl_draw_textures(str, n);
00212 #else
00213   glCallLists(n, GL_UNSIGNED_BYTE, str);
00214 #endif
00215   
00216 #else
00217   static xchar *buf = NULL;
00218   static int l = 0;
00219   int wn = fl_utf8toUtf16(str, n, (unsigned short*)buf, l);
00220   if(wn >= l) {
00221     buf = (xchar*) realloc(buf, sizeof(xchar) * (wn + 1));
00222     l = wn + 1;
00223     wn = fl_utf8toUtf16(str, n, (unsigned short*)buf, l);
00224   }
00225   n = wn;
00226 
00227   int i;
00228   for (i = 0; i < n; i++) {
00229     unsigned int r;
00230     r = (str[i] & 0xFC00) >> 10;
00231     if (!gl_fontsize->glok[r]) get_list(r);
00232   }
00233   glCallLists(n, GL_UNSIGNED_SHORT, buf);
00234 #endif
00235 }
00236 
00241 void gl_draw(const char* str, int n, int x, int y) {
00242   glRasterPos2i(x, y);
00243   gl_draw(str, n);
00244 }
00245 
00250 void gl_draw(const char* str, int n, float x, float y) {
00251   glRasterPos2f(x, y);
00252   gl_draw(str, n);
00253 }
00254 
00259 void gl_draw(const char* str) {
00260   gl_draw(str, strlen(str));
00261 }
00262 
00267 void gl_draw(const char* str, int x, int y) {
00268   gl_draw(str, strlen(str), x, y);
00269 }
00270 
00275 void gl_draw(const char* str, float x, float y) {
00276   gl_draw(str, strlen(str), x, y);
00277 }
00278 
00279 static void gl_draw_invert(const char* str, int n, int x, int y) {
00280   glRasterPos2i(x, -y);
00281   gl_draw(str, n);
00282 }
00283 
00289 void gl_draw(
00290   const char* str,      // the (multi-line) string
00291   int x, int y, int w, int h,   // bounding box
00292   Fl_Align align) {
00293   fl_draw(str, x, -y-h, w, h, align, gl_draw_invert);
00294 }
00295 
00297 void gl_measure(const char* str, int& x, int& y) {fl_measure(str,x,y);}
00298 
00304 void gl_rect(int x, int y, int w, int h) {
00305   if (w < 0) {w = -w; x = x-w;}
00306   if (h < 0) {h = -h; y = y-h;}
00307   glBegin(GL_LINE_STRIP);
00308   glVertex2i(x+w-1, y+h-1);
00309   glVertex2i(x+w-1, y);
00310   glVertex2i(x, y);
00311   glVertex2i(x, y+h-1);
00312   glVertex2i(x+w, y+h-1);
00313   glEnd();
00314 }
00315 
00316 #if HAVE_GL_OVERLAY
00317 extern uchar fl_overlay;
00318 extern int fl_overlay_depth;
00319 #endif
00320 
00327 void gl_color(Fl_Color i) {
00328 #if HAVE_GL_OVERLAY
00329 #if defined(WIN32)
00330   if (fl_overlay && fl_overlay_depth) {
00331     if (fl_overlay_depth < 8) {
00332       // only black & white produce the expected colors.  This could
00333       // be improved by fixing the colormap set in Fl_Gl_Overlay.cxx
00334       int size = 1<<fl_overlay_depth;
00335       if (!i) glIndexi(size-2);
00336       else if (i >= size-2) glIndexi(size-1);
00337       else glIndexi(i);
00338     } else {
00339       glIndexi(i ? i : FL_GRAY_RAMP);
00340     }
00341     return;
00342   }
00343 #else
00344   if (fl_overlay) {glIndexi(int(fl_xpixel(i))); return;}
00345 #endif
00346 #endif
00347   uchar red, green, blue;
00348   Fl::get_color(i, red, green, blue);
00349   glColor3ub(red, green, blue);
00350 }
00351 
00352 void gl_draw_image(const uchar* b, int x, int y, int w, int h, int d, int ld) {
00353   if (!ld) ld = w*d;
00354   glPixelStorei(GL_UNPACK_ROW_LENGTH, ld/d);
00355   glRasterPos2i(x,y);
00356   glDrawPixels(w,h,d<4?GL_RGB:GL_RGBA,GL_UNSIGNED_BYTE,(const ulong*)b);
00357 }
00358 
00359 #if GL_DRAW_USES_TEXTURES || defined(FL_DOXYGEN)
00360 
00361 #include <FL/glu.h>
00362 
00363 // manages a fifo pile of pre-computed string textures
00364 class gl_texture_fifo {
00365   friend void gl_draw_textures(const char *, int);
00366 private:
00367   typedef struct { // information for a pre-computed texture
00368     GLuint texName; // its name
00369     char *utf8; //its text
00370     Fl_Font_Descriptor *fdesc; // its font
00371     int width; // its width
00372     int height; // its height
00373   } data;
00374   data *fifo; // array of pile elements
00375   int size_; // pile height
00376   int current; // the oldest texture to have entered the pile
00377   int last; // pile top
00378   int textures_generated; // true iff glGenTextures has been called
00379   void display_texture(int rank);
00380   int compute_texture(const char* str, int n);
00381   int already_known(const char *str, int n);
00382 public:
00383   gl_texture_fifo(int max = 100); // 100 = default height of texture pile
00384   inline int size(void) {return size_; };
00385   ~gl_texture_fifo(void);
00386 };
00387 
00388 gl_texture_fifo::gl_texture_fifo(int max)
00389 {
00390   size_ = max;
00391   last = current = -1;
00392   textures_generated = 0;
00393   fifo = (data*)calloc(size_, sizeof(data));
00394 }
00395 
00396 gl_texture_fifo::~gl_texture_fifo()
00397 {
00398   for (int i = 0; i < size_; i++) {
00399     if (fifo[i].utf8) free(fifo[i].utf8);
00400     if (textures_generated) glDeleteTextures(1, &fifo[i].texName);
00401     }
00402   free(fifo);
00403 }
00404 
00405 // displays a pre-computed texture on the GL scene
00406 void gl_texture_fifo::display_texture(int rank)
00407 {
00408   //setup matrices
00409   GLint matrixMode;
00410   glGetIntegerv (GL_MATRIX_MODE, &matrixMode);
00411   glMatrixMode (GL_PROJECTION);
00412   glPushMatrix();
00413   glLoadIdentity ();
00414   glMatrixMode (GL_MODELVIEW);
00415   glPushMatrix();
00416   glLoadIdentity ();
00417   float winw = Fl_Window::current()->w();
00418   float winh = Fl_Window::current()->h();
00419   glScalef (2.0f / winw, 2.0f /  winh, 1.0f);
00420   glTranslatef (-winw / 2.0f, -winh / 2.0f, 0.0f);
00421   //write the texture on screen
00422   GLfloat pos[4];
00423   glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
00424   CGRect bounds = CGRectMake (pos[0], pos[1] - fl_descent(), fifo[rank].width, fifo[rank].height);
00425   
00426   // GL_COLOR_BUFFER_BIT for glBlendFunc, GL_ENABLE_BIT for glEnable / glDisable
00427   glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT | GL_COLOR_BUFFER_BIT); 
00428   glDisable (GL_DEPTH_TEST); // ensure text is not removed by depth buffer test.
00429   glEnable (GL_BLEND); // for text fading
00430   glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // ditto
00431   glEnable (GL_TEXTURE_RECTANGLE_EXT);  
00432   glDisable(GL_LIGHTING);
00433   glBindTexture (GL_TEXTURE_RECTANGLE_EXT, fifo[rank].texName);
00434   glBegin (GL_QUADS);
00435   glTexCoord2f (0.0f, 0.0f); // draw lower left in world coordinates
00436   glVertex2f (bounds.origin.x, bounds.origin.y);
00437   
00438   glTexCoord2f (0.0f, fifo[rank].height); // draw upper left in world coordinates
00439   glVertex2f (bounds.origin.x, bounds.origin.y + bounds.size.height);
00440   
00441   glTexCoord2f (fifo[rank].width, fifo[rank].height); // draw upper right in world coordinates
00442   glVertex2f (bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height);
00443   
00444   glTexCoord2f (fifo[rank].width, 0.0f); // draw lower right in world coordinates
00445   glVertex2f (bounds.origin.x + bounds.size.width, bounds.origin.y);
00446   glEnd ();
00447   glPopAttrib();
00448   
00449   // reset original matrices
00450   glPopMatrix(); // GL_MODELVIEW
00451   glMatrixMode (GL_PROJECTION);
00452   glPopMatrix();
00453   glMatrixMode (matrixMode);
00454   
00455   //set the raster position to end of string
00456   pos[0] += fifo[rank].width;
00457   GLdouble modelmat[16];
00458   glGetDoublev (GL_MODELVIEW_MATRIX, modelmat);
00459   GLdouble projmat[16];
00460   glGetDoublev (GL_PROJECTION_MATRIX, projmat);
00461   GLdouble objX, objY, objZ;
00462   GLint viewport[4];
00463   glGetIntegerv (GL_VIEWPORT, viewport);
00464   gluUnProject(pos[0], pos[1], pos[2], modelmat, projmat, viewport, &objX, &objY, &objZ);
00465   glRasterPos2d(objX, objY);
00466 }
00467 
00468 // pre-computes a string texture
00469 int gl_texture_fifo::compute_texture(const char* str, int n)
00470 {
00471   current = (current + 1) % size_;
00472   if (current > last) last = current;
00473   //write str to a bitmap just big enough  
00474   if ( fifo[current].utf8 ) free(fifo[current].utf8);
00475   fifo[current].utf8 = (char *)malloc(n + 1);
00476   memcpy(fifo[current].utf8, str, n);
00477   fifo[current].utf8[n] = 0;
00478   fifo[current].width = 0, fifo[current].height = 0;
00479   fl_measure(fifo[current].utf8, fifo[current].width, fifo[current].height, 0);
00480   CGColorSpaceRef lut = CGColorSpaceCreateDeviceRGB();
00481   void *base = calloc(4*fifo[current].width, fifo[current].height);
00482   if (base == NULL) return -1;
00483   fl_gc = CGBitmapContextCreate(base, fifo[current].width, fifo[current].height, 8, fifo[current].width*4, lut, kCGImageAlphaPremultipliedLast);
00484   CGColorSpaceRelease(lut);
00485   fl_fontsize = gl_fontsize;
00486   GLfloat colors[4];
00487   glGetFloatv(GL_CURRENT_COLOR, colors);
00488   fl_color(colors[0]*255, colors[1]*255, colors[2]*255);
00489   fl_draw(str, n, 0, fifo[current].height - fl_descent());
00490   //put this bitmap in a texture  
00491   glPushAttrib(GL_TEXTURE_BIT);
00492   glBindTexture (GL_TEXTURE_RECTANGLE_EXT, fifo[current].texName);
00493   glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00494   glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00495   glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA, fifo[current].width, fifo[current].height, 0,  GL_RGBA, GL_UNSIGNED_BYTE, base);
00496   glPopAttrib();
00497   CGContextRelease(fl_gc);
00498   fl_gc = NULL;
00499   free(base);
00500   fifo[current].fdesc = gl_fontsize;
00501   return current;
00502 }
00503 
00504 // returns rank of pre-computed texture for a string if it exists
00505 int gl_texture_fifo::already_known(const char *str, int n)
00506 {
00507   int rank;
00508   for ( rank = 0; rank <= last; rank++) {
00509     if ( memcmp(str, fifo[rank].utf8, n) == 0 && fifo[rank].utf8[n] == 0 &&
00510       fifo[rank].fdesc == gl_fontsize) return rank;
00511   }
00512   return -1;
00513 }
00514 
00515 static gl_texture_fifo *gl_fifo = NULL; // points to the texture pile class instance
00516 
00517 // draws a utf8 string using pre-computed texture if available
00518 static void gl_draw_textures(const char* str, int n) 
00519 {
00520   if (! gl_fifo) gl_fifo = new gl_texture_fifo();
00521   if (!gl_fifo->textures_generated) {
00522     for (int i = 0; i < gl_fifo->size_; i++) glGenTextures (1, &(gl_fifo->fifo[i].texName));
00523     gl_fifo->textures_generated = 1;
00524   }
00525   int rank = gl_fifo->already_known(str, n);
00526   if (rank == -1) {
00527     rank = gl_fifo->compute_texture(str, n);
00528   }
00529   gl_fifo->display_texture(rank);
00530 }
00531 
00540 int gl_texture_pile_height(void)
00541 {
00542   if (! gl_fifo) gl_fifo = new gl_texture_fifo();
00543   return gl_fifo->size();
00544 }
00545 
00553 void gl_texture_pile_height(int max)
00554 {
00555   if (gl_fifo) delete gl_fifo;
00556   gl_fifo = new gl_texture_fifo(max);
00557 }
00558 
00561 #endif // GL_DRAW_USES_TEXTURES
00562 
00563 #endif // HAVE_GL
00564 
00565 //
00566 // End of "$Id: gl_draw.cxx 7903 2010-11-28 21:06:39Z matt $".
00567 //