annotate src/Modules/Output/Graphics/Devices/GraphicsOutputDevicewxGLCanvas.h @ 595:2d18671876c8

Updated Matrix resizing and Zeroing using rif@google's suggestions.
author flatmax
date Tue, 26 Feb 2013 10:43:26 +0000
parents 82e0dc3dfd16
children
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@228 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@228 39 # include <OpenGL/gl.h>
tomwalters@228 40 # include <OpenGl/glext.h>
tomwalters@116 41 #elif defined (_WINDOWS)
tomwalters@228 42 # include <GL/gl.h>
tomwalters@228 43 # define GL_GET_PROC_ADDRESS wglGetProcAddress
tomwalters@116 44 #else
tomwalters@228 45 # include <GL/gl.h>
tomwalters@228 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@228 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@228 57 # include <FTGL/FTGLBitmapFont.h>
tomwalters@116 58 #else
tomwalters@228 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@228 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@228 91 /*! \param iVerticesMax Maximum number of vertices to be draw inside a gBegin()..gEnd()
tomwalters@228 92 *
tomwalters@228 93 * When iVerticesMax is zero, this variable will not be updated. Note that is _has_
tomwalters@228 94 * to be set at least once before using this class.
tomwalters@228 95 */
tomwalters@228 96 bool Initialize(unsigned int iVerticesMax);
tomwalters@228 97 bool Initialize();
tomwalters@116 98
tomwalters@228 99 void Start();
tomwalters@116 100
tomwalters@116 101 void gGrab();
tomwalters@116 102 void gBeginLineStrip();
tomwalters@116 103 void gBeginQuadStrip();
tomwalters@228 104 using GraphicsOutputDevice::gVertex3f; // Because we overload it
tomwalters@228 105 void gVertex3f(float x, float y, float z);
tomwalters@228 106 void gColor3f(float r, float g, float b);
tomwalters@116 107 void gEnd();
tomwalters@228 108 void gText3f(float x, float y, float z, const char *sStr, bool bRotated = false);
tomwalters@228 109 void gRelease();
tomwalters@116 110 protected:
tomwalters@228 111 /*! \brief Smarter SetCurrent() replacement
tomwalters@228 112 * \return true on success, false on error
tomwalters@228 113 *
tomwalters@228 114 * This function tries GetContext() first. If that fails, it returns false.
tomwalters@228 115 */
tomwalters@228 116 bool SetCurrent();
tomwalters@228 117 void Render();
tomwalters@116 118
tomwalters@228 119 /*! \brief Initialize the OpenGL environment.
tomwalters@228 120 *
tomwalters@228 121 * This must be called after the canvas is realized but before any other
tomwalters@228 122 * OpenGL operation is done. Make sure to run SetCurrent() beforehand.
tomwalters@228 123 * Usually only needed when m_init is false.
tomwalters@228 124 */
tomwalters@116 125 void InitGL();
tomwalters@116 126
tomwalters@228 127 /*! \brief Handle a resize (notify OpenGL of the new area)
tomwalters@228 128 *
tomwalters@228 129 * This is a separate function, because in multi-threading environments
tomwalters@228 130 * multiple contexts have to call it.
tomwalters@228 131 */
tomwalters@228 132 void DoResize();
tomwalters@116 133
tomwalters@228 134 /*! \brief Only need to initialize OpenGL once.
tomwalters@228 135 *
tomwalters@228 136 * This is false at start and true when OpenGL has been initialized.
tomwalters@228 137 * No mutex needed, since it's set once at InitGL() and only read afterwards.
tomwalters@228 138 */
tomwalters@116 139 bool m_init;
tomwalters@116 140
tomwalters@228 141 /*! \brief Vertex list for last drawing so it can be updated on repaint.
tomwalters@228 142 *
tomwalters@228 143 * No mutex needed, since it's set once at InitGL() and only read afterwards.
tomwalters@228 144 */
tomwalters@116 145 GLuint m_gllist;
tomwalters@116 146
tomwalters@228 147 //! \brief OpenGL context for worker thread, use when wxIsMainThread() returns false.
tomwalters@228 148 wxGLContext *m_pWorkerContext;
tomwalters@116 149
tomwalters@228 150 //! \brief Mutex for inter-thread communication
tomwalters@228 151 wxMutex s_mutexOpenGL;
tomwalters@116 152
tomwalters@228 153 //! \brief When true, OpenGL needs to be reinitialized (in the worker thread)
tomwalters@228 154 bool s_bWorkerNeedsInit;
tomwalters@116 155
tomwalters@228 156 //! \brief OpenGL attributes used for initialization.
tomwalters@116 157 static int GLAttrlist[];
tomwalters@116 158
tomwalters@228 159 //! \brief Whether to use anti-aliasing or not
tomwalters@228 160 bool m_bAntialiasing;
tomwalters@116 161
tomwalters@228 162 //! \brief FTGL Font class
tomwalters@228 163 FTFont *m_pFont;
tomwalters@228 164 //! \brief Current font filename
tomwalters@228 165 const char *m_sFontFile;
tomwalters@228 166 //! \brief Current font size
tomwalters@228 167 int m_iFontsize;
tomwalters@116 168
tomwalters@116 169 #if defined(WITH_GL_VERTEX_ARRAYS) || defined(DOXYGEN)
tomwalters@228 170 //! \brief OpenGL vertex type of the current m_pVertices, or 0xffff is outside gBegin()..gEnd()
tomwalters@228 171 int m_iVertexType;
tomwalters@228 172 //! \brief Maximum number of vertices begin gBegin()..gEnd()
tomwalters@228 173 unsigned int m_iVerticesMax;
tomwalters@228 174 //! \brief Vertex array to draw at gEnd(), this becomes m_pVertices[m_iVerticesMax*3]
tomwalters@228 175 GLfloat *m_pVertices;
tomwalters@228 176 //! \brief The current number of vertices inside m_pVertices
tomwalters@228 177 unsigned int m_iVertexCount;
tomwalters@116 178
tomwalters@228 179 /*! \brief Whether to use coloring in vertex lists
tomwalters@228 180 *
tomwalters@228 181 * This variable must not change after Initialize(), or the program may crash.
tomwalters@228 182 * When this variable is true, color information is stored in vertex lists. If
tomwalters@228 183 * it is false, only vertex data is stored.
tomwalters@228 184 *
tomwalters@228 185 * This variable exists for performance reasons, but is currently only set in
tomwalters@228 186 * the constructor of this object.
tomwalters@228 187 */
tomwalters@228 188 bool m_bStaticColor;
tomwalters@228 189 //! \brief Current color for vertex list drawing
tomwalters@228 190 float m_fCurColorR, m_fCurColorG, m_fCurColorB;
tomwalters@116 191
tomwalters@228 192 //! \brief Whether to use vertex array locking or not
tomwalters@228 193 bool m_bVertexArrayLock;
tomwalters@228 194 //! \brief Pointer to vertex array locking function; can be NULL.
tomwalters@228 195 LOCAL_PFNGLLOCKARRAYSEXTPROC m_glLockArraysEXT;
tomwalters@228 196 //! \brief Pointer to vertex array unlocking function; can be NULL.
tomwalters@228 197 LOCAL_PFNGLUNLOCKARRAYSEXTPROC m_glUnlockArraysEXT;
tomwalters@116 198 #endif
tomwalters@116 199
tomwalters@228 200 /*! \brief wxMutexGuiEnter() / wxMutexGuiLeave() wrapper
tomwalters@228 201 *
tomwalters@228 202 * This is a wxMutexLocker-alike for the main gui mutex. Any method that
tomwalters@228 203 * is public, can be called from within another thread and does OpenGL or
tomwalters@228 204 * other gui calls must use this. Example:
tomwalters@228 205 * \code
tomwalters@228 206 * void DoFoo() {
tomwalters@228 207 * AimwxGuiLocker __lock__;
tomwalters@228 208 * glAmazingMethod();
tomwalters@228 209 * }
tomwalters@228 210 * \endcode
tomwalters@228 211 *
tomwalters@228 212 * It is mostly on X-Windows (Xorg/XFree86) that the gui mutex appears to
tomwalters@228 213 * be needed. Otherwise the error "Xlib: unexpected async reply" can occur.
tomwalters@228 214 *
tomwalters@228 215 * On windows, the ui may occasionally lock up for a short while with these
tomwalters@228 216 * mutexes. Since they aren't really needed on that platform, it's left out
tomwalters@228 217 * alltogether.
tomwalters@228 218 */
tomwalters@228 219 class AimwxGuiLocker {
tomwalters@228 220 public:
tomwalters@228 221 inline AimwxGuiLocker() {
tomwalters@116 222 #ifndef _WINDOWS
tomwalters@228 223 if (!wxIsMainThread()) wxMutexGuiEnter();
tomwalters@116 224 #endif
tomwalters@228 225 }
tomwalters@228 226 inline ~AimwxGuiLocker() {
tomwalters@116 227 #ifndef _WINDOWS
tomwalters@228 228 if (!wxIsMainThread()) wxMutexGuiLeave();
tomwalters@116 229 #endif
tomwalters@228 230 }
tomwalters@228 231 };
tomwalters@116 232 DECLARE_EVENT_TABLE()
tomwalters@116 233 };
tomwalters@116 234 #endif /* __GRAPHICS_OUTPUT_DEVICE_GL_CANVAS_H__ */