You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
17KB

  1. //
  2. // "$Id: gl_draw.cxx 8573 2011-04-10 09:10:40Z manolo $"
  3. //
  4. // OpenGL drawing support routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2011 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. // Functions from <FL/gl.h>
  28. // See also Fl_Gl_Window and gl_start.cxx
  29. #include "flstring.h"
  30. #if HAVE_GL || defined(FL_DOXYGEN)
  31. #include <FL/Fl.H>
  32. #include <FL/gl.h>
  33. #include <FL/x.H>
  34. #include <FL/fl_draw.H>
  35. #include <FL/Fl_Device.H>
  36. #include "Fl_Gl_Choice.H"
  37. #include "Fl_Font.H"
  38. #include <FL/fl_utf8.h>
  39. #if !defined(WIN32) && !defined(__APPLE__)
  40. #include <FL/Xutf8.h>
  41. #endif
  42. #if USE_XFT
  43. //extern XFontStruct* fl_xxfont();
  44. #endif // USE_XFT
  45. /** Returns the current font's height */
  46. int gl_height() {return fl_height();}
  47. /** Returns the current font's descent */
  48. int gl_descent() {return fl_descent();}
  49. /** Returns the width of the string in the current fnt */
  50. double gl_width(const char* s) {return fl_width(s);}
  51. /** Returns the width of n characters of the string in the current font */
  52. double gl_width(const char* s, int n) {return fl_width(s,n);}
  53. /** Returns the width of the character in the current font */
  54. double gl_width(uchar c) {return fl_width(c);}
  55. static Fl_Font_Descriptor *gl_fontsize;
  56. #define GL_DRAW_USES_TEXTURES (defined(__APPLE__) && !__ppc__) // 1 only for non-PPC OSX
  57. #ifndef __APPLE__
  58. # define USE_OksiD_style_GL_font_selection 1 // Most hosts except OSX
  59. #else
  60. # undef USE_OksiD_style_GL_font_selection // OSX
  61. #endif
  62. #if USE_XFT
  63. # undef USE_OksiD_style_GL_font_selection // turn this off for XFT also
  64. #endif
  65. /**
  66. Sets the current OpenGL font to the same font as calling fl_font()
  67. */
  68. void gl_font(int fontid, int size) {
  69. fl_font(fontid, size);
  70. Fl_Font_Descriptor *fl_fontsize = fl_graphics_driver->font_descriptor();
  71. #if !GL_DRAW_USES_TEXTURES
  72. if (!fl_fontsize->listbase) {
  73. #ifdef USE_OksiD_style_GL_font_selection
  74. fl_fontsize->listbase = glGenLists(0x10000);
  75. #else // Fltk-1.1.8 style GL font selection
  76. #if defined (USE_X11) // X-windows options follow, either XFT or "plain" X
  77. // FIXME: warning Ideally, for XFT, we really need a glXUseXftFont implementation here...
  78. // FIXME: warning GL font selection is basically wrong here
  79. /* OksiD had a fairly sophisticated scheme for storing multiple X fonts in a XUtf8FontStruct,
  80. * then sorting through them at draw time (for normal X rendering) to find which one can
  81. * render the current glyph... But for now, just use the first font in the list for GL...
  82. */
  83. XFontStruct *font = fl_xfont;
  84. int base = font->min_char_or_byte2;
  85. int count = font->max_char_or_byte2-base+1;
  86. fl_fontsize->listbase = glGenLists(256);
  87. glXUseXFont(font->fid, base, count, fl_fontsize->listbase+base);
  88. # elif defined(WIN32)
  89. // this is unused because USE_OksiD_style_GL_font_selection == 1
  90. int base = fl_fontsize->metr.tmFirstChar;
  91. int count = fl_fontsize->metr.tmLastChar-base+1;
  92. HFONT oldFid = (HFONT)SelectObject(fl_gc, fl_fontsize->fid);
  93. fl_fontsize->listbase = glGenLists(256);
  94. wglUseFontBitmaps(fl_gc, base, count, fl_fontsize->listbase+base);
  95. SelectObject(fl_gc, oldFid);
  96. # elif defined(__APPLE_QUARTZ__)
  97. //AGL is not supported for use in 64-bit applications:
  98. //http://developer.apple.com/mac/library/documentation/Carbon/Conceptual/Carbon64BitGuide/OtherAPIChanges/OtherAPIChanges.html
  99. short font, face, size;
  100. uchar fn[256];
  101. fn[0]=strlen(fl_fontsize->q_name);
  102. strcpy((char*)(fn+1), fl_fontsize->q_name);
  103. GetFNum(fn, &font);
  104. face = 0;
  105. size = fl_fontsize->size;
  106. fl_fontsize->listbase = glGenLists(256);
  107. aglUseFont(aglGetCurrentContext(), font, face,
  108. size, 0, 256, fl_fontsize->listbase);
  109. # else
  110. # error unsupported platform
  111. # endif
  112. #endif // USE_OksiD_style_GL_font_selection
  113. }
  114. glListBase(fl_fontsize->listbase);
  115. #endif // !GL_DRAW_USES_TEXTURES
  116. gl_fontsize = fl_fontsize;
  117. }
  118. #ifndef __APPLE__
  119. static void get_list(int r) {
  120. gl_fontsize->glok[r] = 1;
  121. #if defined(USE_X11)
  122. # if USE_XFT
  123. // FIXME
  124. # else
  125. unsigned int ii = r * 0x400;
  126. for (int i = 0; i < 0x400; i++) {
  127. XFontStruct *font = NULL;
  128. unsigned short id;
  129. XGetUtf8FontAndGlyph(gl_fontsize->font, ii, &font, &id);
  130. if (font) glXUseXFont(font->fid, id, 1, gl_fontsize->listbase+ii);
  131. ii++;
  132. }
  133. # endif
  134. #elif defined(WIN32)
  135. unsigned int ii = r * 0x400;
  136. HFONT oldFid = (HFONT)SelectObject(fl_gc, gl_fontsize->fid);
  137. wglUseFontBitmapsW(fl_gc, ii, ii + 0x03ff, gl_fontsize->listbase+ii);
  138. SelectObject(fl_gc, oldFid);
  139. #elif defined(__APPLE_QUARTZ__)
  140. // handled by textures
  141. #else
  142. # error unsupported platform
  143. #endif
  144. } // get_list
  145. #endif
  146. void gl_remove_displaylist_fonts()
  147. {
  148. # if HAVE_GL
  149. // clear variables used mostly in fl_font
  150. fl_graphics_driver->font(0, 0);
  151. for (int j = 0 ; j < FL_FREE_FONT ; ++j)
  152. {
  153. Fl_Font_Descriptor* past = 0;
  154. Fl_Fontdesc* s = fl_fonts + j ;
  155. Fl_Font_Descriptor* f = s->first;
  156. while (f != 0) {
  157. if(f->listbase) {
  158. if(f == s->first) {
  159. s->first = f->next;
  160. }
  161. else {
  162. past->next = f->next;
  163. }
  164. // It would be nice if this next line was in a desctructor somewhere
  165. glDeleteLists(f->listbase, 256);
  166. Fl_Font_Descriptor* tmp = f;
  167. f = f->next;
  168. delete tmp;
  169. }
  170. else {
  171. past = f;
  172. f = f->next;
  173. }
  174. }
  175. }
  176. #endif
  177. }
  178. #if GL_DRAW_USES_TEXTURES
  179. static void gl_draw_textures(const char* str, int n);
  180. #endif
  181. /**
  182. Draws an array of n characters of the string in the current font
  183. at the current position.
  184. \see On the Mac OS X platform, see gl_texture_pile_height(int)
  185. */
  186. void gl_draw(const char* str, int n) {
  187. #ifdef __APPLE__
  188. #if GL_DRAW_USES_TEXTURES
  189. gl_draw_textures(str, n);
  190. #else
  191. glCallLists(n, GL_UNSIGNED_BYTE, str);
  192. #endif
  193. #else
  194. static xchar *buf = NULL;
  195. static int l = 0;
  196. int wn = fl_utf8toUtf16(str, n, (unsigned short*)buf, l);
  197. if(wn >= l) {
  198. buf = (xchar*) realloc(buf, sizeof(xchar) * (wn + 1));
  199. l = wn + 1;
  200. wn = fl_utf8toUtf16(str, n, (unsigned short*)buf, l);
  201. }
  202. n = wn;
  203. int i;
  204. for (i = 0; i < n; i++) {
  205. unsigned int r;
  206. r = (str[i] & 0xFC00) >> 10;
  207. if (!gl_fontsize->glok[r]) get_list(r);
  208. }
  209. glCallLists(n, GL_UNSIGNED_SHORT, buf);
  210. #endif
  211. }
  212. /**
  213. Draws n characters of the string in the current font at the given position
  214. \see On the Mac OS X platform, see gl_texture_pile_height(int)
  215. */
  216. void gl_draw(const char* str, int n, int x, int y) {
  217. glRasterPos2i(x, y);
  218. gl_draw(str, n);
  219. }
  220. /**
  221. Draws n characters of the string in the current font at the given position
  222. \see On the Mac OS X platform, see gl_texture_pile_height(int)
  223. */
  224. void gl_draw(const char* str, int n, float x, float y) {
  225. glRasterPos2f(x, y);
  226. gl_draw(str, n);
  227. }
  228. /**
  229. Draws a nul-terminated string in the current font at the current position
  230. \see On the Mac OS X platform, see gl_texture_pile_height(int)
  231. */
  232. void gl_draw(const char* str) {
  233. gl_draw(str, strlen(str));
  234. }
  235. /**
  236. Draws a nul-terminated string in the current font at the given position
  237. \see On the Mac OS X platform, see gl_texture_pile_height(int)
  238. */
  239. void gl_draw(const char* str, int x, int y) {
  240. gl_draw(str, strlen(str), x, y);
  241. }
  242. /**
  243. Draws a nul-terminated string in the current font at the given position
  244. \see On the Mac OS X platform, see gl_texture_pile_height(int)
  245. */
  246. void gl_draw(const char* str, float x, float y) {
  247. gl_draw(str, strlen(str), x, y);
  248. }
  249. static void gl_draw_invert(const char* str, int n, int x, int y) {
  250. glRasterPos2i(x, -y);
  251. gl_draw(str, n);
  252. }
  253. /**
  254. Draws a string formatted into a box, with newlines and tabs expanded,
  255. other control characters changed to ^X. and aligned with the edges or
  256. center. Exactly the same output as fl_draw().
  257. */
  258. void gl_draw(
  259. const char* str, // the (multi-line) string
  260. int x, int y, int w, int h, // bounding box
  261. Fl_Align align) {
  262. fl_draw(str, x, -y-h, w, h, align, gl_draw_invert);
  263. }
  264. /** Measure how wide and tall the string will be when drawn by the gl_draw() function */
  265. void gl_measure(const char* str, int& x, int& y) {fl_measure(str,x,y);}
  266. /**
  267. Outlines the given rectangle with the current color.
  268. If Fl_Gl_Window::ortho() has been called, then the rectangle will
  269. exactly fill the given pixel rectangle.
  270. */
  271. void gl_rect(int x, int y, int w, int h) {
  272. if (w < 0) {w = -w; x = x-w;}
  273. if (h < 0) {h = -h; y = y-h;}
  274. glBegin(GL_LINE_STRIP);
  275. glVertex2i(x+w-1, y+h-1);
  276. glVertex2i(x+w-1, y);
  277. glVertex2i(x, y);
  278. glVertex2i(x, y+h-1);
  279. glVertex2i(x+w, y+h-1);
  280. glEnd();
  281. }
  282. #if HAVE_GL_OVERLAY
  283. extern uchar fl_overlay;
  284. extern int fl_overlay_depth;
  285. #endif
  286. /**
  287. Sets the curent OpenGL color to an FLTK color.
  288. For color-index modes it will use fl_xpixel(c), which is only
  289. right if the window uses the default colormap!
  290. */
  291. void gl_color(Fl_Color i) {
  292. #if HAVE_GL_OVERLAY
  293. #if defined(WIN32)
  294. if (fl_overlay && fl_overlay_depth) {
  295. if (fl_overlay_depth < 8) {
  296. // only black & white produce the expected colors. This could
  297. // be improved by fixing the colormap set in Fl_Gl_Overlay.cxx
  298. int size = 1<<fl_overlay_depth;
  299. if (!i) glIndexi(size-2);
  300. else if (i >= size-2) glIndexi(size-1);
  301. else glIndexi(i);
  302. } else {
  303. glIndexi(i ? i : FL_GRAY_RAMP);
  304. }
  305. return;
  306. }
  307. #else
  308. if (fl_overlay) {glIndexi(int(fl_xpixel(i))); return;}
  309. #endif
  310. #endif
  311. uchar red, green, blue;
  312. Fl::get_color(i, red, green, blue);
  313. glColor3ub(red, green, blue);
  314. }
  315. void gl_draw_image(const uchar* b, int x, int y, int w, int h, int d, int ld) {
  316. if (!ld) ld = w*d;
  317. glPixelStorei(GL_UNPACK_ROW_LENGTH, ld/d);
  318. glRasterPos2i(x,y);
  319. glDrawPixels(w,h,d<4?GL_RGB:GL_RGBA,GL_UNSIGNED_BYTE,(const ulong*)b);
  320. }
  321. #if GL_DRAW_USES_TEXTURES || defined(FL_DOXYGEN)
  322. #include <FL/glu.h>
  323. // manages a fifo pile of pre-computed string textures
  324. class gl_texture_fifo {
  325. friend void gl_draw_textures(const char *, int);
  326. private:
  327. typedef struct { // information for a pre-computed texture
  328. GLuint texName; // its name
  329. char *utf8; //its text
  330. Fl_Font_Descriptor *fdesc; // its font
  331. int width; // its width
  332. int height; // its height
  333. } data;
  334. data *fifo; // array of pile elements
  335. int size_; // pile height
  336. int current; // the oldest texture to have entered the pile
  337. int last; // pile top
  338. int textures_generated; // true iff glGenTextures has been called
  339. void display_texture(int rank);
  340. int compute_texture(const char* str, int n);
  341. int already_known(const char *str, int n);
  342. public:
  343. gl_texture_fifo(int max = 100); // 100 = default height of texture pile
  344. inline int size(void) {return size_; };
  345. ~gl_texture_fifo(void);
  346. };
  347. gl_texture_fifo::gl_texture_fifo(int max)
  348. {
  349. size_ = max;
  350. last = current = -1;
  351. textures_generated = 0;
  352. fifo = (data*)calloc(size_, sizeof(data));
  353. }
  354. gl_texture_fifo::~gl_texture_fifo()
  355. {
  356. for (int i = 0; i < size_; i++) {
  357. if (fifo[i].utf8) free(fifo[i].utf8);
  358. if (textures_generated) glDeleteTextures(1, &fifo[i].texName);
  359. }
  360. free(fifo);
  361. }
  362. // displays a pre-computed texture on the GL scene
  363. void gl_texture_fifo::display_texture(int rank)
  364. {
  365. //setup matrices
  366. GLint matrixMode;
  367. glGetIntegerv (GL_MATRIX_MODE, &matrixMode);
  368. glMatrixMode (GL_PROJECTION);
  369. glPushMatrix();
  370. glLoadIdentity ();
  371. glMatrixMode (GL_MODELVIEW);
  372. glPushMatrix();
  373. glLoadIdentity ();
  374. float winw = Fl_Window::current()->w();
  375. float winh = Fl_Window::current()->h();
  376. glScalef (2.0f / winw, 2.0f / winh, 1.0f);
  377. glTranslatef (-winw / 2.0f, -winh / 2.0f, 0.0f);
  378. //write the texture on screen
  379. GLfloat pos[4];
  380. glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
  381. CGRect bounds = CGRectMake (pos[0], pos[1] - fl_descent(), fifo[rank].width, fifo[rank].height);
  382. // GL_COLOR_BUFFER_BIT for glBlendFunc, GL_ENABLE_BIT for glEnable / glDisable
  383. glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT | GL_COLOR_BUFFER_BIT);
  384. glDisable (GL_DEPTH_TEST); // ensure text is not removed by depth buffer test.
  385. glEnable (GL_BLEND); // for text fading
  386. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // ditto
  387. glEnable (GL_TEXTURE_RECTANGLE_EXT);
  388. glDisable(GL_LIGHTING);
  389. glBindTexture (GL_TEXTURE_RECTANGLE_EXT, fifo[rank].texName);
  390. glBegin (GL_QUADS);
  391. glTexCoord2f (0.0f, 0.0f); // draw lower left in world coordinates
  392. glVertex2f (bounds.origin.x, bounds.origin.y);
  393. glTexCoord2f (0.0f, fifo[rank].height); // draw upper left in world coordinates
  394. glVertex2f (bounds.origin.x, bounds.origin.y + bounds.size.height);
  395. glTexCoord2f (fifo[rank].width, fifo[rank].height); // draw upper right in world coordinates
  396. glVertex2f (bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height);
  397. glTexCoord2f (fifo[rank].width, 0.0f); // draw lower right in world coordinates
  398. glVertex2f (bounds.origin.x + bounds.size.width, bounds.origin.y);
  399. glEnd ();
  400. glPopAttrib();
  401. // reset original matrices
  402. glPopMatrix(); // GL_MODELVIEW
  403. glMatrixMode (GL_PROJECTION);
  404. glPopMatrix();
  405. glMatrixMode (matrixMode);
  406. //set the raster position to end of string
  407. pos[0] += fifo[rank].width;
  408. GLdouble modelmat[16];
  409. glGetDoublev (GL_MODELVIEW_MATRIX, modelmat);
  410. GLdouble projmat[16];
  411. glGetDoublev (GL_PROJECTION_MATRIX, projmat);
  412. GLdouble objX, objY, objZ;
  413. GLint viewport[4];
  414. glGetIntegerv (GL_VIEWPORT, viewport);
  415. gluUnProject(pos[0], pos[1], pos[2], modelmat, projmat, viewport, &objX, &objY, &objZ);
  416. glRasterPos2d(objX, objY);
  417. }
  418. // pre-computes a string texture
  419. int gl_texture_fifo::compute_texture(const char* str, int n)
  420. {
  421. current = (current + 1) % size_;
  422. if (current > last) last = current;
  423. //write str to a bitmap just big enough
  424. if ( fifo[current].utf8 ) free(fifo[current].utf8);
  425. fifo[current].utf8 = (char *)malloc(n + 1);
  426. memcpy(fifo[current].utf8, str, n);
  427. fifo[current].utf8[n] = 0;
  428. fifo[current].width = 0, fifo[current].height = 0;
  429. fl_measure(fifo[current].utf8, fifo[current].width, fifo[current].height, 0);
  430. CGColorSpaceRef lut = CGColorSpaceCreateDeviceRGB();
  431. void *base = calloc(4*fifo[current].width, fifo[current].height);
  432. if (base == NULL) return -1;
  433. fl_gc = CGBitmapContextCreate(base, fifo[current].width, fifo[current].height, 8, fifo[current].width*4, lut, kCGImageAlphaPremultipliedLast);
  434. CGColorSpaceRelease(lut);
  435. fl_graphics_driver->font_descriptor(gl_fontsize);
  436. GLfloat colors[4];
  437. glGetFloatv(GL_CURRENT_COLOR, colors);
  438. fl_color((uchar)(colors[0]*255), (uchar)(colors[1]*255), (uchar)(colors[2]*255));
  439. fl_draw(str, n, 0, fifo[current].height - fl_descent());
  440. //put this bitmap in a texture
  441. glPushAttrib(GL_TEXTURE_BIT);
  442. glBindTexture (GL_TEXTURE_RECTANGLE_EXT, fifo[current].texName);
  443. glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  444. glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  445. glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA, fifo[current].width, fifo[current].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, base);
  446. glPopAttrib();
  447. CGContextRelease(fl_gc);
  448. fl_gc = NULL;
  449. free(base);
  450. fifo[current].fdesc = gl_fontsize;
  451. return current;
  452. }
  453. // returns rank of pre-computed texture for a string if it exists
  454. int gl_texture_fifo::already_known(const char *str, int n)
  455. {
  456. int rank;
  457. for ( rank = 0; rank <= last; rank++) {
  458. if ( memcmp(str, fifo[rank].utf8, n) == 0 && fifo[rank].utf8[n] == 0 &&
  459. fifo[rank].fdesc == gl_fontsize) return rank;
  460. }
  461. return -1;
  462. }
  463. static gl_texture_fifo *gl_fifo = NULL; // points to the texture pile class instance
  464. // draws a utf8 string using pre-computed texture if available
  465. static void gl_draw_textures(const char* str, int n)
  466. {
  467. if (! gl_fifo) gl_fifo = new gl_texture_fifo();
  468. if (!gl_fifo->textures_generated) {
  469. for (int i = 0; i < gl_fifo->size_; i++) glGenTextures (1, &(gl_fifo->fifo[i].texName));
  470. gl_fifo->textures_generated = 1;
  471. }
  472. int rank = gl_fifo->already_known(str, n);
  473. if (rank == -1) {
  474. rank = gl_fifo->compute_texture(str, n);
  475. }
  476. gl_fifo->display_texture(rank);
  477. }
  478. /** \addtogroup group_macosx
  479. @{ */
  480. /**
  481. \brief Returns the current height of the pile of pre-computed string textures
  482. *
  483. The default value is 100
  484. */
  485. int gl_texture_pile_height(void)
  486. {
  487. if (! gl_fifo) gl_fifo = new gl_texture_fifo();
  488. return gl_fifo->size();
  489. }
  490. /**
  491. \brief Changes the height of the pile of pre-computed string textures
  492. *
  493. Strings that are often re-displayed can be processed much faster if
  494. this pile is set high enough to hold all of them.
  495. \param max Height of the texture pile
  496. */
  497. void gl_texture_pile_height(int max)
  498. {
  499. if (gl_fifo) delete gl_fifo;
  500. gl_fifo = new gl_texture_fifo(max);
  501. }
  502. /** @} */
  503. #elif defined(__APPLE__)
  504. // used only if __ppc__
  505. int gl_texture_pile_height(void) {return 0;}
  506. void gl_texture_pile_height(int max) {}
  507. #endif // GL_DRAW_USES_TEXTURES
  508. #if defined(__APPLE__)
  509. void gl_texture_reset()
  510. {
  511. #if GL_DRAW_USES_TEXTURES
  512. if (gl_fifo) gl_texture_pile_height(gl_texture_pile_height());
  513. #endif // GL_DRAW_USES_TEXTURES
  514. }
  515. #endif // __APPLE__
  516. #endif // HAVE_GL
  517. //
  518. // End of "$Id: gl_draw.cxx 8573 2011-04-10 09:10:40Z manolo $".
  519. //