glcanvas.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: glcanvas.h
  3. // Purpose: interface of wxGLContext and wxGLCanvas
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxGLContext
  9. An instance of a wxGLContext represents the state of an OpenGL state
  10. machine and the connection between OpenGL and the system.
  11. The OpenGL state includes everything that can be set with the OpenGL API:
  12. colors, rendering variables, display lists, texture objects, etc. Although
  13. it is possible to have multiple rendering contexts share display lists in
  14. order to save resources, this method is hardly used today any more, because
  15. display lists are only a tiny fraction of the overall state.
  16. Therefore, one rendering context is usually used with or bound to multiple
  17. output windows in turn, so that the application has access to the complete
  18. and identical state while rendering into each window.
  19. Binding (making current) a rendering context with another instance of a
  20. wxGLCanvas however works only if the other wxGLCanvas was created with the
  21. same attributes as the wxGLCanvas from which the wxGLContext was
  22. initialized. (This applies to sharing display lists among contexts
  23. analogously.)
  24. Note that some wxGLContext features are extremely platform-specific - its
  25. best to check your native platform's glcanvas header (on windows
  26. include/wx/msw/glcanvas.h) to see what features your native platform
  27. provides.
  28. wxHAS_OPENGL_ES is defined on platforms that only have this implementation
  29. available (eg the iPhone) und don't support the full specification.
  30. @library{wxgl}
  31. @category{gl}
  32. @see wxGLCanvas
  33. */
  34. class wxGLContext : public wxObject
  35. {
  36. public:
  37. /**
  38. Constructor.
  39. @param win
  40. The canvas that is used to initialize this context. This parameter
  41. is needed only temporarily, and the caller may do anything with it
  42. (e.g. destroy the window) after the constructor returned. @n
  43. It will be possible to bind (make current) this context to any
  44. other wxGLCanvas that has been created with equivalent attributes
  45. as win.
  46. @param other
  47. Context to share display lists with or @NULL (the default) for no
  48. sharing.
  49. */
  50. wxGLContext(wxGLCanvas* win, const wxGLContext* other = NULL);
  51. /**
  52. Makes the OpenGL state that is represented by this rendering context
  53. current with the wxGLCanvas @e win.
  54. @note @a win can be a different wxGLCanvas window than the one that was
  55. passed to the constructor of this rendering context. If @e RC is
  56. an object of type wxGLContext, the statements
  57. @e "RC.SetCurrent(win);" and @e "win.SetCurrent(RC);" are
  58. equivalent, see wxGLCanvas::SetCurrent().
  59. */
  60. virtual bool SetCurrent(const wxGLCanvas& win) const;
  61. };
  62. /**
  63. @anchor wxGL_FLAGS
  64. Constants for use with wxGLCanvas.
  65. @note Not all implementations support options such as stereo, auxiliary
  66. buffers, alpha channel, and accumulator buffer, use
  67. wxGLCanvas::IsDisplaySupported() to check for individual attributes
  68. support.
  69. */
  70. enum
  71. {
  72. /// Use true color (the default if no attributes at all are specified);
  73. /// do not use a palette.
  74. WX_GL_RGBA = 1,
  75. /// Specifies the number of bits for buffer if not WX_GL_RGBA.
  76. WX_GL_BUFFER_SIZE,
  77. /// Must be followed by 0 for main buffer, >0 for overlay, <0 for underlay.
  78. WX_GL_LEVEL,
  79. /// Use double buffering if present (on if no attributes specified).
  80. WX_GL_DOUBLEBUFFER,
  81. /// Use stereoscopic display.
  82. WX_GL_STEREO,
  83. /// Specifies number of auxiliary buffers.
  84. WX_GL_AUX_BUFFERS,
  85. /// Use red buffer with most bits (> MIN_RED bits)
  86. WX_GL_MIN_RED,
  87. /// Use green buffer with most bits (> MIN_GREEN bits)
  88. WX_GL_MIN_GREEN,
  89. /// Use blue buffer with most bits (> MIN_BLUE bits)
  90. WX_GL_MIN_BLUE,
  91. /// Use alpha buffer with most bits (> MIN_ALPHA bits)
  92. WX_GL_MIN_ALPHA,
  93. /// Specifies number of bits for Z-buffer (typically 0, 16 or 32).
  94. WX_GL_DEPTH_SIZE,
  95. /// Specifies number of bits for stencil buffer.
  96. WX_GL_STENCIL_SIZE,
  97. /// Specifies minimal number of red accumulator bits.
  98. WX_GL_MIN_ACCUM_RED,
  99. /// Specifies minimal number of green accumulator bits.
  100. WX_GL_MIN_ACCUM_GREEN,
  101. /// Specifies minimal number of blue accumulator bits.
  102. WX_GL_MIN_ACCUM_BLUE,
  103. /// Specifies minimal number of alpha accumulator bits.
  104. WX_GL_MIN_ACCUM_ALPHA,
  105. /// 1 for multisampling support (antialiasing)
  106. WX_GL_SAMPLE_BUFFERS,
  107. /// 4 for 2x2 antialiasing supersampling on most graphics cards
  108. WX_GL_SAMPLES
  109. };
  110. /**
  111. @class wxGLCanvas
  112. wxGLCanvas is a class for displaying OpenGL graphics. It is always used in
  113. conjunction with wxGLContext as the context can only be made current (i.e.
  114. active for the OpenGL commands) when it is associated to a wxGLCanvas.
  115. More precisely, you first need to create a wxGLCanvas window and then
  116. create an instance of a wxGLContext that is initialized with this
  117. wxGLCanvas and then later use either SetCurrent() with the instance of the
  118. wxGLContext or wxGLContext::SetCurrent() with the instance of the
  119. wxGLCanvas (which might be not the same as was used for the creation of the
  120. context) to bind the OpenGL state that is represented by the rendering
  121. context to the canvas, and then finally call SwapBuffers() to swap the
  122. buffers of the OpenGL canvas and thus show your current output.
  123. Notice that versions of wxWidgets previous to 2.9 used to implicitly create a
  124. wxGLContext inside wxGLCanvas itself. This is still supported in the
  125. current version but is deprecated now and will be removed in the future,
  126. please update your code to create the rendering contexts explicitly.
  127. To set up the attributes for the canvas (number of bits for the depth
  128. buffer, number of bits for the stencil buffer and so on) you should set up
  129. the correct values of the @e attribList parameter. The values that should
  130. be set up and their meanings will be described below.
  131. @note
  132. On those platforms which use a configure script (e.g. Linux and Mac OS)
  133. OpenGL support is automatically enabled if the relative headers and
  134. libraries are found.
  135. To switch it on under the other platforms (e.g. Windows), you need to edit
  136. the @c setup.h file and set @c wxUSE_GLCANVAS to @c 1 and then also pass
  137. @c USE_OPENGL=1 to the make utility. You may also need to add @c opengl32.lib
  138. and @c glu32.lib to the list of the libraries your program is linked with.
  139. @library{wxgl}
  140. @category{gl}
  141. @see wxGLContext
  142. */
  143. class wxGLCanvas : public wxWindow
  144. {
  145. public:
  146. /**
  147. Creates a window with the given parameters. Notice that you need to
  148. create and use a wxGLContext to output to this window.
  149. If @a attribList is not specified, double buffered RGBA mode is used.
  150. @param parent
  151. Pointer to a parent window.
  152. @param id
  153. Window identifier. If -1, will automatically create an identifier.
  154. @param pos
  155. Window position. wxDefaultPosition is (-1, -1) which indicates that
  156. wxWidgets should generate a default position for the window.
  157. @param size
  158. Window size. wxDefaultSize is (-1, -1) which indicates that
  159. wxWidgets should generate a default size for the window. If no
  160. suitable size can be found, the window will be sized to 20x20
  161. pixels so that the window is visible but obviously not correctly
  162. sized.
  163. @param style
  164. Window style.
  165. @param name
  166. Window name.
  167. @param attribList
  168. Array of integers. With this parameter you can set the device
  169. context attributes associated to this window. This array is
  170. zero-terminated: it should be set up using @ref wxGL_FLAGS
  171. constants. If a constant should be followed by a value, put it in
  172. the next array position. For example, WX_GL_DEPTH_SIZE should be
  173. followed by the value that indicates the number of bits for the
  174. depth buffer, e.g.:
  175. @code
  176. attribList[n++] = WX_GL_DEPTH_SIZE;
  177. attribList[n++] = 32;
  178. attribList[n] = 0; // terminate the list
  179. @endcode
  180. If the attribute list is not specified at all, i.e. if this
  181. parameter is @NULL, the default attributes including WX_GL_RGBA and
  182. WX_GL_DOUBLEBUFFER are used. But notice that if you do specify some
  183. attributes you also need to explicitly include these two default
  184. attributes in the list if you need them.
  185. @param palette
  186. Palette for indexed colour (i.e. non WX_GL_RGBA) mode. Ignored
  187. under most platforms.
  188. */
  189. wxGLCanvas(wxWindow* parent, wxWindowID id = wxID_ANY,
  190. const int* attribList = NULL,
  191. const wxPoint& pos = wxDefaultPosition,
  192. const wxSize& size = wxDefaultSize,
  193. long style = 0,
  194. const wxString& name = "GLCanvas",
  195. const wxPalette& palette = wxNullPalette);
  196. /**
  197. Determines if a canvas having the specified attributes is available.
  198. @param attribList
  199. See @a attribList for wxGLCanvas().
  200. @return @true if attributes are supported.
  201. */
  202. static bool IsDisplaySupported(const int* attribList);
  203. /**
  204. Returns true if the extension with given name is supported
  205. Notice that while this function is implemented for all of GLX, WGL and
  206. AGL the extensions names are usually not the same for different
  207. platforms and so the code using it still usually uses conditional
  208. compilation.
  209. */
  210. static bool IsExtensionSupported(const char *extension);
  211. /**
  212. Sets the current colour for this window (using @c glcolor3f()), using
  213. the wxWidgets colour database to find a named colour.
  214. */
  215. bool SetColour(const wxString& colour);
  216. /**
  217. Makes the OpenGL state that is represented by the OpenGL rendering
  218. context @a context current, i.e. it will be used by all subsequent
  219. OpenGL calls.
  220. This is equivalent to wxGLContext::SetCurrent() called with this window
  221. as parameter.
  222. @note This function may only be called when the window is shown on
  223. screen, in particular it can't usually be called from the
  224. constructor as the window isn't yet shown at this moment.
  225. @return @false if an error occurred.
  226. */
  227. bool SetCurrent(const wxGLContext& context) const;
  228. /**
  229. Swaps the double-buffer of this window, making the back-buffer the
  230. front-buffer and vice versa, so that the output of the previous OpenGL
  231. commands is displayed on the window.
  232. @return @false if an error occurred.
  233. */
  234. virtual bool SwapBuffers();
  235. };