Fl_Gl_Window.H 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // "$Id: Fl_Gl_Window.H 11794 2016-06-22 07:45:53Z manolo $"
  3. //
  4. // OpenGL header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2016 by Bill Spitzak and others.
  7. //
  8. // This library is free software. Distribution and use rights are outlined in
  9. // the file "COPYING" which should have been included with this file. If this
  10. // file is missing or damaged, see the license at:
  11. //
  12. // http://www.fltk.org/COPYING.php
  13. //
  14. // Please report all bugs and problems on the following page:
  15. //
  16. // http://www.fltk.org/str.php
  17. //
  18. /* \file
  19. Fl_Gl_Window widget . */
  20. #ifndef Fl_Gl_Window_H
  21. #define Fl_Gl_Window_H
  22. #include "Fl_Window.H"
  23. class Fl_Gl_Choice; // structure to hold result of glXChooseVisual
  24. class Fl_Gl_Window_Driver;
  25. /**
  26. The Fl_Gl_Window widget sets things up so OpenGL works.
  27. It also keeps an OpenGL "context" for that window, so that changes to the
  28. lighting and projection may be reused between redraws. Fl_Gl_Window
  29. also flushes the OpenGL streams and swaps buffers after draw() returns.
  30. OpenGL hardware typically provides some overlay bit planes, which
  31. are very useful for drawing UI controls atop your 3D graphics. If the
  32. overlay hardware is not provided, FLTK tries to simulate the overlay.
  33. This works pretty well if your graphics are double buffered, but not
  34. very well for single-buffered.
  35. Please note that the FLTK drawing and clipping functions
  36. will not work inside an Fl_Gl_Window. All drawing
  37. should be done using OpenGL calls exclusively.
  38. Even though Fl_Gl_Window is derived from Fl_Group,
  39. it is not useful to add other FLTK Widgets as children,
  40. unless those widgets are modified to draw using OpenGL calls.
  41. */
  42. class FL_EXPORT Fl_Gl_Window : public Fl_Window {
  43. friend class Fl_Gl_Window_Driver;
  44. Fl_Gl_Window_Driver *pGlWindowDriver;
  45. int mode_;
  46. const int *alist;
  47. Fl_Gl_Choice *g;
  48. GLContext context_;
  49. char valid_f_;
  50. char damage1_; // damage() of back buffer
  51. virtual void draw_overlay();
  52. void init();
  53. void *overlay;
  54. static int can_do(int, const int *);
  55. int mode(int, const int *);
  56. static int gl_plugin_linkage();
  57. protected:
  58. virtual void draw();
  59. public:
  60. void show();
  61. /** Same as Fl_Window::show(int a, char **b) */
  62. void show(int a, char **b) {Fl_Window::show(a,b);}
  63. void flush();
  64. void hide();
  65. void resize(int,int,int,int);
  66. int handle(int);
  67. /** Returns a pointer to the window's Fl_Gl_Window_Driver object */
  68. Fl_Gl_Window_Driver *gl_driver() {return pGlWindowDriver;}
  69. /**
  70. Is turned off when FLTK creates a new context for this window or
  71. when the window resizes, and is turned on \e after draw() is called.
  72. You can use this inside your draw() method to avoid unnecessarily
  73. initializing the OpenGL context. Just do this:
  74. \code
  75. void mywindow::draw() {
  76. if (!valid()) {
  77. glViewport(0,0,pixel_w(),pixel_h());
  78. glFrustum(...);
  79. ...other initialization...
  80. }
  81. if (!context_valid()) {
  82. ...load textures, etc. ...
  83. }
  84. ... draw your geometry here ...
  85. }
  86. \endcode
  87. You can turn valid() on by calling valid(1). You
  88. should only do this after fixing the transformation inside a draw()
  89. or after make_current(). This is done automatically after
  90. draw() returns.
  91. */
  92. char valid() const {return valid_f_ & 1;}
  93. /**
  94. See char Fl_Gl_Window::valid() const
  95. */
  96. void valid(char v) {if (v) valid_f_ |= 1; else valid_f_ &= 0xfe;}
  97. void invalidate();
  98. /**
  99. Will only be set if the
  100. OpenGL context is created or recreated. It differs from
  101. Fl_Gl_Window::valid() which is also set whenever the context
  102. changes size.
  103. */
  104. char context_valid() const {return valid_f_ & 2;}
  105. /**
  106. See char Fl_Gl_Window::context_valid() const
  107. */
  108. void context_valid(char v) {if (v) valid_f_ |= 2; else valid_f_ &= 0xfd;}
  109. /** Returns non-zero if the hardware supports the given OpenGL mode. */
  110. static int can_do(int m) {return can_do(m,0);}
  111. /** Returns non-zero if the hardware supports the given OpenGL mode.
  112. \see Fl_Gl_Window::mode(const int *a) */
  113. static int can_do(const int *m) {return can_do(0, m);}
  114. /** Returns non-zero if the hardware supports the current OpenGL mode. */
  115. int can_do() {return can_do(mode_,alist);}
  116. /** Returns the current OpenGL capabilites of the window.
  117. Don't use this if capabilities were set through Fl_Gl_Window::mode(const int *a).
  118. */
  119. Fl_Mode mode() const {return (Fl_Mode)mode_;}
  120. /**
  121. Set or change the OpenGL capabilites of the window. The value can be
  122. any of the following OR'd together:
  123. - \c FL_RGB - RGB color (not indexed)
  124. - \c FL_RGB8 - RGB color with at least 8 bits of each color
  125. - \c FL_INDEX - Indexed mode
  126. - \c FL_SINGLE - not double buffered
  127. - \c FL_DOUBLE - double buffered
  128. - \c FL_ACCUM - accumulation buffer
  129. - \c FL_ALPHA - alpha channel in color
  130. - \c FL_DEPTH - depth buffer
  131. - \c FL_STENCIL - stencil buffer
  132. - \c FL_MULTISAMPLE - multisample antialiasing
  133. - \c FL_OPENGL3 - use OpenGL version 3.0 or more when running Mac OS.
  134. FL_RGB and FL_SINGLE have a value of zero, so they
  135. are "on" unless you give FL_INDEX or FL_DOUBLE.
  136. If the desired combination cannot be done, FLTK will try turning off
  137. FL_MULTISAMPLE. If this also fails the show() will call
  138. Fl::error() and not show the window.
  139. You can change the mode while the window is displayed. This is most
  140. useful for turning double-buffering on and off. Under X this will
  141. cause the old X window to be destroyed and a new one to be created. If
  142. this is a top-level window this will unfortunately also cause the
  143. window to blink, raise to the top, and be de-iconized, and the xid()
  144. will change, possibly breaking other code. It is best to make the GL
  145. window a child of another window if you wish to do this!
  146. mode() must not be called within draw() since it
  147. changes the current context.
  148. \note On the <b>MSWindows and Unix/Linux platforms</b>, FLTK produces
  149. contexts for the highest OpenGL version supported by the hardware. Such contexts
  150. are also compatible with lower OpenGL versions. On the <b>Apple OS X
  151. platform</b>, it is necessary to decide whether the source code targets
  152. OpenGL versions higher or lower than 3.0. By default, FLTK
  153. creates contexts adequate for OpenGL versions 1 and 2. To get contexts
  154. for OpenGL 3.0 or higher, the <tt>FL_OPENGL3</tt> flag and Mac OS
  155. version 10.7 or higher are required (in that case the context is NOT
  156. compatible with OpenGL versions 1 or 2). The <tt>FL_OPENGL3</tt> flag has no
  157. effect on non-Apple platforms.
  158. \version the <tt>FL_OPENGL3</tt> flag appeared in version 1.3.4
  159. */
  160. int mode(int a) {return mode(a,0);}
  161. /** Set the OpenGL capabilites of the window using platform-specific data.
  162. \param a zero-ending array of platform-specific attributes and attribute values
  163. <p><b>Unix/Linux platform</b>: attributes are GLX attributes adequate for the 3rd argument of
  164. the <tt>glXChooseVisual()</tt> function (e.g., <tt>GLX_DOUBLEBUFFER</tt>, defined by including <GL/glx.h>).
  165. \note What attributes are adequate here is subject to change.
  166. The preferred, stable public API is Fl_Gl_Window::mode(int a).
  167. <p><b>MSWindows platform</b>: this member function is of no use.
  168. <p><b>Mac OS X platform</b>: attributes belong to the <tt>CGLPixelFormatAttribute</tt> enumeration
  169. (defined by including <tt><OpenGL/OpenGL.h></tt>, e.g., <tt>kCGLPFADoubleBuffer</tt>)
  170. and may be followed by adequate attribute values.
  171. */
  172. int mode(const int *a) {return mode(0, a);}
  173. /** Returns a pointer to the GLContext that this window is using.
  174. \see void context(GLContext c, int destroy_flag) */
  175. GLContext context() const {return context_;}
  176. void context(GLContext, int destroy_flag = 0);
  177. void make_current();
  178. void swap_buffers();
  179. void ortho();
  180. int can_do_overlay();
  181. void redraw_overlay();
  182. void hide_overlay();
  183. void make_overlay_current();
  184. // Note: Doxygen docs in Fl_Widget.H to avoid redundancy.
  185. virtual Fl_Gl_Window* as_gl_window() {return this;}
  186. float pixels_per_unit();
  187. /** Gives the window width in OpenGL pixels.
  188. Generally identical with the result of the w() function, but for a window mapped to
  189. an Apple 'retina' display, and if Fl::use_high_res_GL(bool) is set to true,
  190. pixel_w() returns 2 * w(). This method detects when the window has been moved
  191. between low and high resolution displays and automatically adjusts the returned value.
  192. \version 1.3.4
  193. */
  194. int pixel_w() { return int(pixels_per_unit() * w() + 0.5f); }
  195. /** Gives the window height in OpenGL pixels.
  196. Generally identical with the result of the h() function, but for a window mapped to
  197. an Apple 'retina' display, and if Fl::use_high_res_GL(bool) is set to true,
  198. pixel_h() returns 2 * h(). This method detects when the window has been moved
  199. between low and high resolution displays and automatically adjusts the returned value.
  200. \version 1.3.4
  201. */
  202. int pixel_h() { return int(pixels_per_unit() * h() + 0.5f); }
  203. ~Fl_Gl_Window();
  204. /**
  205. Creates a new Fl_Gl_Window widget using the given size, and label string.
  206. The default boxtype is FL_NO_BOX. The default mode is FL_RGB|FL_DOUBLE|FL_DEPTH.
  207. */
  208. Fl_Gl_Window(int W, int H, const char *l=0) : Fl_Window(W,H,l) {init();}
  209. /**
  210. Creates a new Fl_Gl_Window widget using the given position,
  211. size, and label string. The default boxtype is FL_NO_BOX. The
  212. default mode is FL_RGB|FL_DOUBLE|FL_DEPTH.
  213. */
  214. Fl_Gl_Window(int X, int Y, int W, int H, const char *l=0)
  215. : Fl_Window(X,Y,W,H,l) {init();}
  216. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Gl_Window, Fl_Window)
  217. };
  218. #endif // Fl_Gl_Window_H
  219. //
  220. // End of "$Id: Fl_Gl_Window.H 11794 2016-06-22 07:45:53Z manolo $".
  221. //