Fl_Gl_Window.H 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // "$Id: Fl_Gl_Window.H 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // OpenGL header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 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. /* \file
  28. Fl_Gl_Window widget . */
  29. #ifndef Fl_Gl_Window_H
  30. #define Fl_Gl_Window_H
  31. #include "Fl_Window.H"
  32. #ifndef GLContext
  33. /**
  34. Opaque pointer type to hide system specific implementation.
  35. */
  36. typedef void* GLContext; // actually a GLXContext or HGLDC
  37. #endif
  38. class Fl_Gl_Choice; // structure to hold result of glXChooseVisual
  39. /**
  40. The Fl_Gl_Window widget sets things up so OpenGL works.
  41. It also keeps an OpenGL "context" for that window, so that changes to the
  42. lighting and projection may be reused between redraws. Fl_Gl_Window
  43. also flushes the OpenGL streams and swaps buffers after draw() returns.
  44. OpenGL hardware typically provides some overlay bit planes, which
  45. are very useful for drawing UI controls atop your 3D graphics. If the
  46. overlay hardware is not provided, FLTK tries to simulate the overlay.
  47. This works pretty well if your graphics are double buffered, but not
  48. very well for single-buffered.
  49. Please note that the FLTK drawing and clipping functions
  50. will not work inside an Fl_Gl_Window. All drawing
  51. should be done using OpenGL calls exclusively.
  52. Even though Fl_Gl_Window is derived from Fl_Group,
  53. it is not useful to add other FLTK Widgets as children,
  54. unless those widgets are modified to draw using OpenGL calls.
  55. */
  56. class FL_EXPORT Fl_Gl_Window : public Fl_Window {
  57. int mode_;
  58. const int *alist;
  59. Fl_Gl_Choice *g;
  60. GLContext context_;
  61. char valid_f_;
  62. char damage1_; // damage() of back buffer
  63. virtual void draw_overlay();
  64. void init();
  65. void *overlay;
  66. void make_overlay();
  67. friend class _Fl_Gl_Overlay;
  68. static int can_do(int, const int *);
  69. int mode(int, const int *);
  70. public:
  71. void show();
  72. void show(int a, char **b) {Fl_Window::show(a,b);}
  73. void flush();
  74. void hide();
  75. void resize(int,int,int,int);
  76. int handle(int);
  77. /**
  78. Is turned off when FLTK creates a new context for this window or
  79. when the window resizes, and is turned on \e after draw() is called.
  80. You can use this inside your draw() method to avoid unnecessarily
  81. initializing the OpenGL context. Just do this:
  82. \code
  83. void mywindow::draw() {
  84. if (!valid()) {
  85. glViewport(0,0,w(),h());
  86. glFrustum(...);
  87. ...other initialization...
  88. }
  89. if (!context_valid()) {
  90. ...load textures, etc. ...
  91. }
  92. ... draw your geometry here ...
  93. }
  94. \endcode
  95. You can turn valid() on by calling valid(1). You
  96. should only do this after fixing the transformation inside a draw()
  97. or after make_current(). This is done automatically after
  98. draw() returns.
  99. */
  100. char valid() const {return valid_f_ & 1;}
  101. /**
  102. See char Fl_Gl_Window::valid() const
  103. */
  104. void valid(char v) {if (v) valid_f_ |= 1; else valid_f_ &= 0xfe;}
  105. void invalidate();
  106. /**
  107. Will only be set if the
  108. OpenGL context is created or recreated. It differs from
  109. Fl_Gl_Window::valid() which is also set whenever the context
  110. changes size.
  111. */
  112. char context_valid() const {return valid_f_ & 2;}
  113. /**
  114. See char Fl_Gl_Window::context_valid() const
  115. */
  116. void context_valid(char v) {if (v) valid_f_ |= 2; else valid_f_ &= 0xfd;}
  117. /** Returns non-zero if the hardware supports the given or current OpenGL mode. */
  118. static int can_do(int m) {return can_do(m,0);}
  119. /** Returns non-zero if the hardware supports the given or current OpenGL mode. */
  120. static int can_do(const int *m) {return can_do(0, m);}
  121. /** Returns non-zero if the hardware supports the given or current OpenGL mode. */
  122. int can_do() {return can_do(mode_,alist);}
  123. /**
  124. Set or change the OpenGL capabilites of the window. The value can be
  125. any of the following OR'd together:
  126. - \c FL_RGB - RGB color (not indexed)
  127. - \c FL_RGB8 - RGB color with at least 8 bits of each color
  128. - \c FL_INDEX - Indexed mode
  129. - \c FL_SINGLE - not double buffered
  130. - \c FL_DOUBLE - double buffered
  131. - \c FL_ACCUM - accumulation buffer
  132. - \c FL_ALPHA - alpha channel in color
  133. - \c FL_DEPTH - depth buffer
  134. - \c FL_STENCIL - stencil buffer
  135. - \c FL_MULTISAMPLE - multisample antialiasing
  136. FL_RGB and FL_SINGLE have a value of zero, so they
  137. are "on" unless you give FL_INDEX or FL_DOUBLE.
  138. If the desired combination cannot be done, FLTK will try turning off
  139. FL_MULTISAMPLE. If this also fails the show() will call
  140. Fl::error() and not show the window.
  141. You can change the mode while the window is displayed. This is most
  142. useful for turning double-buffering on and off. Under X this will
  143. cause the old X window to be destroyed and a new one to be created. If
  144. this is a top-level window this will unfortunately also cause the
  145. window to blink, raise to the top, and be de-iconized, and the xid()
  146. will change, possibly breaking other code. It is best to make the GL
  147. window a child of another window if you wish to do this!
  148. mode() must not be called within draw() since it
  149. changes the current context.
  150. */
  151. Fl_Mode mode() const {return (Fl_Mode)mode_;}
  152. /** See Fl_Mode mode() const */
  153. int mode(int a) {return mode(a,0);}
  154. /** See Fl_Mode mode() const */
  155. int mode(const int *a) {return mode(0, a);}
  156. /** void See void context(void* v, int destroy_flag) */
  157. void* context() const {return context_;}
  158. void context(void*, int destroy_flag = 0);
  159. void make_current();
  160. void swap_buffers();
  161. void ortho();
  162. /**
  163. Returns true if the hardware overlay is possible. If this is false,
  164. FLTK will try to simulate the overlay, with significant loss of update
  165. speed. Calling this will cause FLTK to open the display.
  166. */
  167. int can_do_overlay();
  168. /**
  169. This method causes draw_overlay() to be called at a later time.
  170. Initially the overlay is clear. If you want the window to display
  171. something in the overlay when it first appears, you must call this
  172. immediately after you show() your window.
  173. */
  174. void redraw_overlay();
  175. void hide_overlay();
  176. /**
  177. The make_overlay_current() method selects the OpenGL context
  178. for the widget's overlay. It is called automatically prior to the
  179. draw_overlay() method being called and can also be used to
  180. implement feedback and/or selection within the handle()
  181. method.
  182. */
  183. void make_overlay_current();
  184. /** Returns an Fl_Gl_Window pointer if this widget is an Fl_Gl_Window.
  185. \retval NULL if this widget is not derived from Fl_Gl_Window.
  186. \note This method is provided to avoid dynamic_cast.
  187. \todo More documentation ...
  188. */
  189. virtual Fl_Gl_Window* as_gl_window() {return this;}
  190. ~Fl_Gl_Window();
  191. /**
  192. Creates a new Fl_Gl_Window widget using the given size, and label string.
  193. The default boxtype is FL_NO_BOX. The default mode is FL_RGB|FL_DOUBLE|FL_DEPTH.
  194. */
  195. Fl_Gl_Window(int W, int H, const char *l=0) : Fl_Window(W,H,l) {init();}
  196. /**
  197. Creates a new Fl_Gl_Window widget using the given position,
  198. size, and label string. The default boxtype is FL_NO_BOX. The
  199. default mode is FL_RGB|FL_DOUBLE|FL_DEPTH.
  200. */
  201. Fl_Gl_Window(int X, int Y, int W, int H, const char *l=0)
  202. : Fl_Window(X,Y,W,H,l) {init();}
  203. protected:
  204. /**
  205. Draws the Fl_Gl_Window.
  206. You \e \b must override the draw() method.
  207. */
  208. virtual void draw();
  209. };
  210. #endif
  211. //
  212. // End of "$Id: Fl_Gl_Window.H 7903 2010-11-28 21:06:39Z matt $".
  213. //