glcanvas.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/glcanvas.h
  3. // Purpose: wxGLCanvas base header
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created:
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_GLCANVAS_H_BASE_
  11. #define _WX_GLCANVAS_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_GLCANVAS
  14. #include "wx/app.h"
  15. #include "wx/palette.h"
  16. #include "wx/window.h"
  17. class WXDLLIMPEXP_FWD_GL wxGLCanvas;
  18. class WXDLLIMPEXP_FWD_GL wxGLContext;
  19. // ----------------------------------------------------------------------------
  20. // Constants for attributes list
  21. // ----------------------------------------------------------------------------
  22. // Notice that not all implementation support options such as stereo, auxiliary
  23. // buffers, alpha channel, and accumulator buffer, use IsDisplaySupported() to
  24. // check for individual attributes support.
  25. enum
  26. {
  27. WX_GL_RGBA = 1, // use true color palette (on if no attrs specified)
  28. WX_GL_BUFFER_SIZE, // bits for buffer if not WX_GL_RGBA
  29. WX_GL_LEVEL, // 0 for main buffer, >0 for overlay, <0 for underlay
  30. WX_GL_DOUBLEBUFFER, // use double buffering (on if no attrs specified)
  31. WX_GL_STEREO, // use stereoscopic display
  32. WX_GL_AUX_BUFFERS, // number of auxiliary buffers
  33. WX_GL_MIN_RED, // use red buffer with most bits (> MIN_RED bits)
  34. WX_GL_MIN_GREEN, // use green buffer with most bits (> MIN_GREEN bits)
  35. WX_GL_MIN_BLUE, // use blue buffer with most bits (> MIN_BLUE bits)
  36. WX_GL_MIN_ALPHA, // use alpha buffer with most bits (> MIN_ALPHA bits)
  37. WX_GL_DEPTH_SIZE, // bits for Z-buffer (0,16,32)
  38. WX_GL_STENCIL_SIZE, // bits for stencil buffer
  39. WX_GL_MIN_ACCUM_RED, // use red accum buffer with most bits (> MIN_ACCUM_RED bits)
  40. WX_GL_MIN_ACCUM_GREEN, // use green buffer with most bits (> MIN_ACCUM_GREEN bits)
  41. WX_GL_MIN_ACCUM_BLUE, // use blue buffer with most bits (> MIN_ACCUM_BLUE bits)
  42. WX_GL_MIN_ACCUM_ALPHA, // use alpha buffer with most bits (> MIN_ACCUM_ALPHA bits)
  43. WX_GL_SAMPLE_BUFFERS, // 1 for multisampling support (antialiasing)
  44. WX_GL_SAMPLES // 4 for 2x2 antialiasing supersampling on most graphics cards
  45. };
  46. #define wxGLCanvasName wxT("GLCanvas")
  47. // ----------------------------------------------------------------------------
  48. // wxGLContextBase: OpenGL rendering context
  49. // ----------------------------------------------------------------------------
  50. class WXDLLIMPEXP_GL wxGLContextBase : public wxObject
  51. {
  52. public:
  53. /*
  54. The derived class should provide a ctor with this signature:
  55. wxGLContext(wxGLCanvas *win, const wxGLContext *other = NULL);
  56. */
  57. // set this context as the current one
  58. virtual bool SetCurrent(const wxGLCanvas& win) const = 0;
  59. };
  60. // ----------------------------------------------------------------------------
  61. // wxGLCanvasBase: window which can be used for OpenGL rendering
  62. // ----------------------------------------------------------------------------
  63. class WXDLLIMPEXP_GL wxGLCanvasBase : public wxWindow
  64. {
  65. public:
  66. // default ctor doesn't initialize the window, use Create() later
  67. wxGLCanvasBase();
  68. virtual ~wxGLCanvasBase();
  69. /*
  70. The derived class should provide a ctor with this signature:
  71. wxGLCanvas(wxWindow *parent,
  72. wxWindowID id = wxID_ANY,
  73. int* attribList = 0,
  74. const wxPoint& pos = wxDefaultPosition,
  75. const wxSize& size = wxDefaultSize,
  76. long style = 0,
  77. const wxString& name = wxGLCanvasName,
  78. const wxPalette& palette = wxNullPalette);
  79. */
  80. // operations
  81. // ----------
  82. // set the given context associated with this window as the current one
  83. bool SetCurrent(const wxGLContext& context) const;
  84. // flush the back buffer (if we have it)
  85. virtual bool SwapBuffers() = 0;
  86. // accessors
  87. // ---------
  88. // check if the given attributes are supported without creating a canvas
  89. static bool IsDisplaySupported(const int *attribList);
  90. #if wxUSE_PALETTE
  91. const wxPalette *GetPalette() const { return &m_palette; }
  92. #endif // wxUSE_PALETTE
  93. // miscellaneous helper functions
  94. // ------------------------------
  95. // call glcolor() for the colour with the given name, return false if
  96. // colour not found
  97. bool SetColour(const wxString& colour);
  98. // return true if the extension with given name is supported
  99. //
  100. // notice that while this function is implemented for all of GLX, WGL and
  101. // AGL the extensions names are usually not the same for different
  102. // platforms and so the code using it still usually uses conditional
  103. // compilation
  104. static bool IsExtensionSupported(const char *extension);
  105. // deprecated methods using the implicit wxGLContext
  106. #if WXWIN_COMPATIBILITY_2_8
  107. wxDEPRECATED( wxGLContext* GetContext() const );
  108. wxDEPRECATED( void SetCurrent() );
  109. wxDEPRECATED( void OnSize(wxSizeEvent& event) );
  110. #endif // WXWIN_COMPATIBILITY_2_8
  111. #ifdef __WXUNIVERSAL__
  112. // resolve the conflict with wxWindowUniv::SetCurrent()
  113. virtual bool SetCurrent(bool doit) { return wxWindow::SetCurrent(doit); }
  114. #endif
  115. protected:
  116. // override this to implement SetColour() in GL_INDEX_MODE
  117. // (currently only implemented in wxX11 and wxMotif ports)
  118. virtual int GetColourIndex(const wxColour& WXUNUSED(col)) { return -1; }
  119. // check if the given extension name is present in the space-separated list
  120. // of extensions supported by the current implementation such as returned
  121. // by glXQueryExtensionsString() or glGetString(GL_EXTENSIONS)
  122. static bool IsExtensionInList(const char *list, const char *extension);
  123. #if wxUSE_PALETTE
  124. // create default palette if we're not using RGBA mode
  125. // (not supported in most ports)
  126. virtual wxPalette CreateDefaultPalette() { return wxNullPalette; }
  127. wxPalette m_palette;
  128. #endif // wxUSE_PALETTE
  129. #if WXWIN_COMPATIBILITY_2_8
  130. wxGLContext *m_glContext;
  131. #endif // WXWIN_COMPATIBILITY_2_8
  132. };
  133. // ----------------------------------------------------------------------------
  134. // wxGLApp: a special wxApp subclass for OpenGL applications which must be used
  135. // to select a visual compatible with the given attributes
  136. // ----------------------------------------------------------------------------
  137. class WXDLLIMPEXP_GL wxGLAppBase : public wxApp
  138. {
  139. public:
  140. wxGLAppBase() : wxApp() { }
  141. // use this in the constructor of the user-derived wxGLApp class to
  142. // determine if an OpenGL rendering context with these attributes
  143. // is available - returns true if so, false if not.
  144. virtual bool InitGLVisual(const int *attribList) = 0;
  145. };
  146. #if defined(__WXMSW__)
  147. #include "wx/msw/glcanvas.h"
  148. #elif defined(__WXMOTIF__) || defined(__WXX11__)
  149. #include "wx/x11/glcanvas.h"
  150. #elif defined(__WXGTK20__)
  151. #include "wx/gtk/glcanvas.h"
  152. #elif defined(__WXGTK__)
  153. #include "wx/gtk1/glcanvas.h"
  154. #elif defined(__WXMAC__)
  155. #include "wx/osx/glcanvas.h"
  156. #elif defined(__WXCOCOA__)
  157. #include "wx/cocoa/glcanvas.h"
  158. #else
  159. #error "wxGLCanvas not supported in this wxWidgets port"
  160. #endif
  161. // wxMac and wxMSW don't need anything extra in wxGLAppBase, so declare it here
  162. #ifndef wxGL_APP_DEFINED
  163. class WXDLLIMPEXP_GL wxGLApp : public wxGLAppBase
  164. {
  165. public:
  166. wxGLApp() : wxGLAppBase() { }
  167. virtual bool InitGLVisual(const int *attribList);
  168. private:
  169. DECLARE_DYNAMIC_CLASS(wxGLApp)
  170. };
  171. #endif // !wxGL_APP_DEFINED
  172. // ----------------------------------------------------------------------------
  173. // wxGLAPI: an API wrapper that allows the use of 'old' APIs even on OpenGL
  174. // platforms that don't support it natively anymore, if the APIs are available
  175. // it's a mere redirect
  176. // ----------------------------------------------------------------------------
  177. #ifndef wxUSE_OPENGL_EMULATION
  178. #define wxUSE_OPENGL_EMULATION 0
  179. #endif
  180. class WXDLLIMPEXP_GL wxGLAPI : public wxObject
  181. {
  182. public:
  183. wxGLAPI();
  184. ~wxGLAPI();
  185. static void glFrustum(GLfloat left, GLfloat right, GLfloat bottom,
  186. GLfloat top, GLfloat zNear, GLfloat zFar);
  187. static void glBegin(GLenum mode);
  188. static void glTexCoord2f(GLfloat s, GLfloat t);
  189. static void glVertex3f(GLfloat x, GLfloat y, GLfloat z);
  190. static void glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz);
  191. static void glColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
  192. static void glColor3f(GLfloat r, GLfloat g, GLfloat b);
  193. static void glEnd();
  194. };
  195. #endif // wxUSE_GLCANVAS
  196. #endif // _WX_GLCANVAS_H_BASE_