annotate src/Modules/Output/Graphics/Devices/GraphicsOutputDevicewxGLCanvas.h @ 169:78cbca1cf218

- AWS fixes (part 1)
author tomwalters
date Wed, 11 Aug 2010 08:55:29 +0000
parents c5ac2f0c7fc5
children 73c6d61440ad
rev   line source
tomwalters@116 1 // Copyright 2006, Willem van Engen
tomwalters@116 2 //
tomwalters@116 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@116 4 // http://www.acousticscale.org/AIMC
tomwalters@116 5 //
tomwalters@116 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@116 7 // you may not use this file except in compliance with the License.
tomwalters@116 8 // You may obtain a copy of the License at
tomwalters@116 9 //
tomwalters@116 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@116 11 //
tomwalters@116 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@116 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@116 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@116 15 // See the License for the specific language governing permissions and
tomwalters@116 16 // limitations under the License.
tomwalters@116 17
tomwalters@116 18 /*!
tomwalters@116 19 * \file
tomwalters@116 20 * \brief Output device for output to a wxWidgets OpenGL canvas
tomwalters@116 21 *
tomwalters@116 22 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@116 23 * \date created 2006/09/21
tomwalters@116 24 * \version \$Id: $
tomwalters@116 25 */
tomwalters@116 26
tomwalters@116 27 #ifndef __GRAPHICS_OUTPUT_DEVICE_GL_CANVAS_H__
tomwalters@116 28 #define __GRAPHICS_OUTPUT_DEVICE_GL_CANVAS_H__
tomwalters@116 29
tomwalters@116 30 // Precompiled wxWidgets headers
tomwalters@116 31 #include "stdwx.h"
tomwalters@116 32
tomwalters@116 33 // Make sure GLCANVAS is compiled into wxWidgets
tomwalters@116 34 #if !wxUSE_GLCANVAS
tomwalters@117 35 # error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
tomwalters@116 36 #endif
tomwalters@116 37
tomwalters@116 38 #if defined (_MACOSX)
tomwalters@117 39 # include <OpenGL/gl.h>
tomwalters@117 40 # include <OpenGl/glext.h>
tomwalters@116 41 #elif defined (_WINDOWS)
tomwalters@117 42 # include <GL/gl.h>
tomwalters@117 43 # define GL_GET_PROC_ADDRESS wglGetProcAddress
tomwalters@116 44 #else
tomwalters@117 45 # include <GL/gl.h>
tomwalters@117 46 # define GL_GET_PROC_ADDRESS(x) glXGetProcAddress((const GLubyte*)x)
tomwalters@116 47 #endif
tomwalters@116 48 /* Define them just ourselves, easiest way to get it working cross-platform
tomwalters@116 49 * and -Mesa/OpenGL-version. */
tomwalters@116 50 #ifndef APIENTRY
tomwalters@117 51 # define APIENTRY
tomwalters@116 52 #endif
tomwalters@116 53 typedef void (APIENTRY * LOCAL_PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
tomwalters@116 54 typedef void (APIENTRY * LOCAL_PFNGLUNLOCKARRAYSEXTPROC) (void);
tomwalters@116 55
tomwalters@116 56 #ifdef FTGL_SUBDIR
tomwalters@117 57 # include <FTGL/FTGLBitmapFont.h>
tomwalters@116 58 #else
tomwalters@117 59 # include <FTGLBitmapFont.h>
tomwalters@116 60 #endif
tomwalters@116 61
tomwalters@116 62 #include "Output/GraphicsOutputDevice.h"
tomwalters@116 63
tomwalters@116 64 // Use by default
tomwalters@116 65 #define WITH_GL_VERTEX_ARRAYS
tomwalters@116 66
tomwalters@116 67 /*!
tomwalters@116 68 * \class GraphicsOutputDevicewxGLCanvas "Output/GraphicsOutputDevicewxGLCanvas.h"
tomwalters@116 69 * \brief Output class for output to a wxWidgets OpenGL canvas
tomwalters@116 70 *
tomwalters@116 71 * On windows, OpenGL needs a different context when two different threads
tomwalters@116 72 * want to issue OpenGL commands. This is handled automatically for two
tomwalters@116 73 * wxThread s.
tomwalters@116 74 */
tomwalters@116 75 class GraphicsOutputDevicewxGLCanvas : public wxGLCanvas,
tomwalters@116 76 public GraphicsOutputDevice {
tomwalters@116 77 public:
tomwalters@116 78 GraphicsOutputDevicewxGLCanvas(Parameters *pParam,
tomwalters@117 79 wxWindow *parent,
tomwalters@116 80 wxWindowID id = wxID_ANY,
tomwalters@116 81 const wxPoint& pos = wxDefaultPosition,
tomwalters@116 82 const wxSize& size = wxDefaultSize,
tomwalters@116 83 long style = 0,
tomwalters@116 84 const wxString& name
tomwalters@116 85 = _T("GraphicsOutputDeviceGLCanvas"));
tomwalters@116 86 virtual ~GraphicsOutputDevicewxGLCanvas();
tomwalters@116 87 void OnPaint(wxPaintEvent& event);
tomwalters@116 88 void OnSize(wxSizeEvent& event);
tomwalters@116 89 void OnEraseBackground(wxEraseEvent& event);
tomwalters@116 90
tomwalters@117 91 /*! \param iVerticesMax Maximum number of vertices to be draw inside a gBegin()..gEnd()
tomwalters@117 92 *
tomwalters@117 93 * When iVerticesMax is zero, this variable will not be updated. Note that is _has_
tomwalters@117 94 * to be set at least once before using this class.
tomwalters@117 95 */
tomwalters@117 96 bool Initialize(unsigned int iVerticesMax);
tomwalters@117 97 bool Initialize();
tomwalters@116 98
tomwalters@117 99 void Start();
tomwalters@116 100
tomwalters@116 101 void gGrab();
tomwalters@116 102 void gBeginLineStrip();
tomwalters@116 103 void gBeginQuadStrip();
tomwalters@117 104 using GraphicsOutputDevice::gVertex3f; // Because we overload it
tomwalters@117 105 void gVertex3f(float x, float y, float z);
tomwalters@117 106 void gColor3f(float r, float g, float b);
tomwalters@116 107 void gEnd();
tomwalters@117 108 void gText3f(float x, float y, float z, const char *sStr, bool bRotated = false);
tomwalters@117 109 void gRelease();
tomwalters@116 110 protected:
tomwalters@117 111 /*! \brief Smarter SetCurrent() replacement
tomwalters@117 112 * \return true on success, false on error
tomwalters@117 113 *
tomwalters@117 114 * This function tries GetContext() first. If that fails, it returns false.
tomwalters@117 115 */
tomwalters@117 116 bool SetCurrent();
tomwalters@117 117 void Render();
tomwalters@116 118
tomwalters@117 119 /*! \brief Initialize the OpenGL environment.
tomwalters@117 120 *
tomwalters@117 121 * This must be called after the canvas is realized but before any other
tomwalters@117 122 * OpenGL operation is done. Make sure to run SetCurrent() beforehand.
tomwalters@117 123 * Usually only needed when m_init is false.
tomwalters@117 124 */
tomwalters@116 125 void InitGL();
tomwalters@116 126
tomwalters@117 127 /*! \brief Handle a resize (notify OpenGL of the new area)
tomwalters@117 128 *
tomwalters@117 129 * This is a separate function, because in multi-threading environments
tomwalters@117 130 * multiple contexts have to call it.
tomwalters@117 131 */
tomwalters@117 132 void DoResize();
tomwalters@116 133
tomwalters@117 134 /*! \brief Only need to initialize OpenGL once.
tomwalters@117 135 *
tomwalters@117 136 * This is false at start and true when OpenGL has been initialized.
tomwalters@117 137 * No mutex needed, since it's set once at InitGL() and only read afterwards.
tomwalters@117 138 */
tomwalters@116 139 bool m_init;
tomwalters@116 140
tomwalters@117 141 /*! \brief Vertex list for last drawing so it can be updated on repaint.
tomwalters@117 142 *
tomwalters@117 143 * No mutex needed, since it's set once at InitGL() and only read afterwards.
tomwalters@117 144 */
tomwalters@116 145 GLuint m_gllist;
tomwalters@116 146
tomwalters@117 147 //! \brief OpenGL context for worker thread, use when wxIsMainThread() returns false.
tomwalters@117 148 wxGLContext *m_pWorkerContext;
tomwalters@116 149
tomwalters@117 150 //! \brief Mutex for inter-thread communication
tomwalters@117 151 wxMutex s_mutexOpenGL;
tomwalters@116 152
tomwalters@117 153 //! \brief When true, OpenGL needs to be reinitialized (in the worker thread)
tomwalters@117 154 bool s_bWorkerNeedsInit;
tomwalters@116 155
tomwalters@117 156 //! \brief OpenGL attributes used for initialization.
tomwalters@116 157 static int GLAttrlist[];
tomwalters@116 158
tomwalters@117 159 //! \brief Whether to use anti-aliasing or not
tomwalters@117 160 bool m_bAntialiasing;
tomwalters@116 161
tomwalters@117 162 //! \brief FTGL Font class
tomwalters@117 163 FTFont *m_pFont;
tomwalters@117 164 //! \brief Current font filename
tomwalters@117 165 const char *m_sFontFile;
tomwalters@117 166 //! \brief Current font size
tomwalters@117 167 int m_iFontsize;
tomwalters@116 168
tomwalters@116 169 #if defined(WITH_GL_VERTEX_ARRAYS) || defined(DOXYGEN)
tomwalters@117 170 //! \brief OpenGL vertex type of the current m_pVertices, or 0xffff is outside gBegin()..gEnd()
tomwalters@117 171 int m_iVertexType;
tomwalters@117 172 //! \brief Maximum number of vertices begin gBegin()..gEnd()
tomwalters@117 173 unsigned int m_iVerticesMax;
tomwalters@117 174 //! \brief Vertex array to draw at gEnd(), this becomes m_pVertices[m_iVerticesMax*3]
tomwalters@117 175 GLfloat *m_pVertices;
tomwalters@117 176 //! \brief The current number of vertices inside m_pVertices
tomwalters@117 177 unsigned int m_iVertexCount;
tomwalters@116 178
tomwalters@117 179 /*! \brief Whether to use coloring in vertex lists
tomwalters@117 180 *
tomwalters@117 181 * This variable must not change after Initialize(), or the program may crash.
tomwalters@117 182 * When this variable is true, color information is stored in vertex lists. If
tomwalters@117 183 * it is false, only vertex data is stored.
tomwalters@117 184 *
tomwalters@117 185 * This variable exists for performance reasons, but is currently only set in
tomwalters@117 186 * the constructor of this object.
tomwalters@117 187 */
tomwalters@117 188 bool m_bStaticColor;
tomwalters@117 189 //! \brief Current color for vertex list drawing
tomwalters@117 190 float m_fCurColorR, m_fCurColorG, m_fCurColorB;
tomwalters@116 191
tomwalters@117 192 //! \brief Whether to use vertex array locking or not
tomwalters@117 193 bool m_bVertexArrayLock;
tomwalters@117 194 //! \brief Pointer to vertex array locking function; can be NULL.
tomwalters@117 195 LOCAL_PFNGLLOCKARRAYSEXTPROC m_glLockArraysEXT;
tomwalters@117 196 //! \brief Pointer to vertex array unlocking function; can be NULL.
tomwalters@117 197 LOCAL_PFNGLUNLOCKARRAYSEXTPROC m_glUnlockArraysEXT;
tomwalters@116 198 #endif
tomwalters@116 199
tomwalters@117 200 /*! \brief wxMutexGuiEnter() / wxMutexGuiLeave() wrapper
tomwalters@117 201 *
tomwalters@117 202 * This is a wxMutexLocker-alike for the main gui mutex. Any method that
tomwalters@117 203 * is public, can be called from within another thread and does OpenGL or
tomwalters@117 204 * other gui calls must use this. Example:
tomwalters@117 205 * \code
tomwalters@117 206 * void DoFoo() {
tomwalters@117 207 * AimwxGuiLocker __lock__;
tomwalters@117 208 * glAmazingMethod();
tomwalters@117 209 * }
tomwalters@117 210 * \endcode
tomwalters@117 211 *
tomwalters@117 212 * It is mostly on X-Windows (Xorg/XFree86) that the gui mutex appears to
tomwalters@117 213 * be needed. Otherwise the error "Xlib: unexpected async reply" can occur.
tomwalters@117 214 *
tomwalters@117 215 * On windows, the ui may occasionally lock up for a short while with these
tomwalters@117 216 * mutexes. Since they aren't really needed on that platform, it's left out
tomwalters@117 217 * alltogether.
tomwalters@117 218 */
tomwalters@117 219 class AimwxGuiLocker {
tomwalters@117 220 public:
tomwalters@117 221 inline AimwxGuiLocker() {
tomwalters@116 222 #ifndef _WINDOWS
tomwalters@117 223 if (!wxIsMainThread()) wxMutexGuiEnter();
tomwalters@116 224 #endif
tomwalters@117 225 }
tomwalters@117 226 inline ~AimwxGuiLocker() {
tomwalters@116 227 #ifndef _WINDOWS
tomwalters@117 228 if (!wxIsMainThread()) wxMutexGuiLeave();
tomwalters@116 229 #endif
tomwalters@117 230 }
tomwalters@117 231 };
tomwalters@116 232 DECLARE_EVENT_TABLE()
tomwalters@116 233 };
tomwalters@116 234 #endif /* __GRAPHICS_OUTPUT_DEVICE_GL_CANVAS_H__ */