Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
OpenGLCanvasBase.hh
1 #ifndef __OpenGLCanvasBase_hh__
2 #define __OpenGLCanvasBase_hh__
3 #include <wx/wx.h>
4 /** \author Jan Woetzel 01/2003 - 10/2005 */
5 #include <bias_config.h>
6 #ifndef BIAS_HAVE_WXWIDGETS
7 # error Please recompile BIAS with USE_WXWIDGETS to use this lib.
8 #endif // BIAS_HAVE_WXWIDGETS
9 
10 #ifndef BIAS_HAVE_OPENGL
11 # error Please recompile BIAS with USE_OPENGL to use this file.
12 #endif // BIAS_HAVE_OPENGL
13 
14 //#ifndef BIAS_HAVE_GLEW
15 //# error Please recompile BIAS with USE_GLEW to use this file.
16 //#endif // BIAS_HAVE_GLEW
17 
18 
19 // avoid VS 2005 deprecated warnings
20 #ifndef _CRT_SECURE_NO_DEPRECATE
21 # define _CRT_SECURE_NO_DEPRECATE
22 #endif
23 
24 #include <Gui/biasgl.h>
25 #include <wx/glcanvas.h>
26 // must be first
27 #include <Base/Common/W32Compat.hh>
28 #include <math.h> // avoid M_PI redefinitions in wx/defs.h
29 
30 
31 // check defines from wx/setup.h implicitly
32 #if defined(wxUSE_GLCANVAS) && wxUSE_GLCANVAS
33 // OK, wx compiled in configuration with wxgl lib
34 #else
35 # error wx has not been compiled with wxUSE_GLCANVAS=1. BIASGUi needs a ww with gl support. Please set wx/setup.h: wxUSE_GLCANVAS=1 and recompile WX.
36 #endif
37 
38 
39 // STD
40 #include <vector>
41 #include <iostream>
42 #include <iomanip>
43 
44 // BIAS
45 #include <Base/Debug/Error.hh>
46 #include <Base/Debug/DebugSimple.hh>
47 #include <Base/Math/Vector3.hh>
48 #include <Base/Math/Vector4.hh>
49 #include <Base/Image/Image.hh>
50 #include <Gui/StringConv.hh>
51 
52 // WX
53 #include <wx/wx.h>
54 #include <wx/image.h>
55 
56 
57 #include <Gui/RenderModeGL.hh>
58 #ifdef BIAS_HAVE_FONTGL
59 # include <Utils/DrawTextGL.hh>
60 #endif
61 
62 
63 // true: start with theoretically auto update of time
64 #ifndef OPENGLCANVASBASE_DEFAULT_animationUpdateEnabled
65 # define OPENGLCANVASBASE_DEFAULT_animationUpdateEnabled true
66 #endif
67 
68 // default fileformat used by screenshot function:
69 #ifndef BIAS_SCREENSHOT_DEFAULT_EXTENSION
70 # define BIAS_SCREENSHOT_DEFAULT_EXTENSION ".mip"
71 #endif
72 
73 
74 #define OpenGLCanvasBase_DEFAULT_FULLSCREEN_STYLE wxFULLSCREEN_NOMENUBAR|wxFULLSCREEN_NOTOOLBAR|wxFULLSCREEN_NOSTATUSBAR|wxFULLSCREEN_NOBORDER|wxFULLSCREEN_NOCAPTION|wxFULLSCREEN_ALL
75 
76 
77 // some exp. beta drivers don't expose their "hidden" capabilities
78 // bypass extension query and force "on" for all extensions.
79 // in general this setting is *NOT* recommended!!! (JW)
80 //#define USE_EXPERIMENTAL_SETTING_GLEW
81 
82 
83 namespace BIAS {
84 
85  /** helper to simplify setting up format "red,green,blue,alpha,depth" JW
86  to be used as (int*)&attribsRGBAD JW */
87  const int attribsRGBAD[] = { WX_GL_RGBA,
88  WX_GL_MIN_RED, 1,
89  WX_GL_MIN_GREEN, 1,
90  WX_GL_MIN_BLUE, 1,
91  WX_GL_MIN_ALPHA, 1,
92  WX_GL_STENCIL_SIZE, 0,
93  WX_GL_DEPTH_SIZE, 1,
94  WX_GL_DOUBLEBUFFER,
95  0
96  };
97 
98  /** helper to simplify setting up format "red,green,blue,alpha,stencil,depth" JW
99  to be used as (int*)&attribsRGBASD JW */
100  const int attribsRGBASD[] = { WX_GL_RGBA,
101  WX_GL_MIN_RED, 1,
102  WX_GL_MIN_GREEN, 1,
103  WX_GL_MIN_BLUE, 1,
104  WX_GL_MIN_ALPHA, 1,
105  WX_GL_STENCIL_SIZE, 1,
106  WX_GL_DEPTH_SIZE, 1,
107  WX_GL_DOUBLEBUFFER,
108  0
109  };
110 
111 
112  /**
113  @class OpenGLCanvasBase
114  @ingroup g_gui
115  @brief base class for OpenGL canvases you could derive from to get base functionality (JW)
116 
117  Has minimal dependencies and minimal event interceptions, just functionality.
118  Use a derrived class for automatic event handling.
119  Supports shared GL context.
120  The idea is that you usually just need to derive and reimplement the DisplayCalls() routine.
121  This clas is
122  -NOT writing mous epos value to status bar
123  -NOT updating periodically.
124  See OpenGLCanvas for an extended version.
125  // TODO: page flipping stereo for windows mode (no fullscreen)
126  // TODO: handle multiple rendering contexts with different caabilites (e.g 2 differen GPUs) using "GLEW MX"
127  // TODO: add application controlled VSYNC enable/disable
128  // TODO: add application controlled antialiasing settings
129  // TODO: add keys for switching between 3D stereo rendering modes
130  // TODO: Rendertexture and Framebufferobject support?
131  // TODO: support for shared display lists and vertex buffer objects (VBO,VAR)?
132 
133  Use the attriblist like RGBASD for setting the correct pixel formato rliek this:
134  \verbatim
135  int attriblist[] = {
136  WX_GL_RGBA,
137  WX_GL_MIN_RED, 1,
138  WX_GL_MIN_GREEN, 1,
139  WX_GL_MIN_BLUE, 1,
140  WX_GL_MIN_ALPHA, 1,
141  WX_GL_STENCIL_SIZE, 1,
142  WX_GL_DOUBLEBUFFER,
143  0
144  };
145  \endverbatim
146 
147  \mainpage OpenGLCanvasBase and its child classes can be used for OpenGL GUI development.
148  \author Jan Woetzel */
149  class BIASGui_EXPORT OpenGLCanvasBase : public wxGLCanvas
150  {
151  public:
152  virtual ~OpenGLCanvasBase(); ///< destructor
153 
154  // ctors
156  wxFrame* parent
157  ,wxWindowID id = -1
158  ,const wxPoint& pos = wxDefaultPosition
159  ,const wxSize& size = wxDefaultSize
160  ,long style = 0
161  ,const wxString& name = AsciiToWx("OpenGLCanvasBase")
162  ,int* attribList = NULL
163  ,const wxPalette& palette = wxNullPalette
164  );
165 
166  ///
167  /// constructor using a shared context
169  wxFrame* parent
170  ,wxGLContext* sharedContext = NULL
171  ,wxWindowID id = -1
172  ,const wxPoint& pos = wxDefaultPosition
173  ,const wxSize& size = wxDefaultSize
174  ,long style=0
175  ,const wxString& name=AsciiToWx("OpenGLCanvasBase")
176  ,int* attribList = NULL
177  ,const wxPalette& palette = wxNullPalette
178  );
179 
180 
181  /// any constructor should call this function
182  /// to initialize all data members (e.g. pointers to NULL)
183  virtual void InitMembers();
184 
185  /// init all things that need a context.
186  /// sets _InitWithContextDone
187  virtual void InitWithContext();
188 
189  /// expands parents SwapBUffers by automatic framecount increase
190  void SwapBuffers();
191 
192  /**
193  main display call to be overridden by child classes
194  usually displays:
195  -InitWithContext (must be done and must eb first because setting up context may take some time while display is not setup correctly!)
196  -Clear
197  -Camera/virtual view
198  -Geometry
199  Implements instance
200  @author Jan Woetzel.
201  */
202  virtual void Display();
203 
204  virtual void DisplayCalls();
205 
206  /// display default shade model, polygon mode,
207  virtual void DisplayRenderMode();
208 
209  /** pop up keyboard layout help. Must be virtual to let child class add their bindings */
210  virtual std::ostream & PrintHelp(std::ostream & os);
211 
212  /// useful for quadbuffered stereo, e.g. shutetr glasses
213  void DisplaySelectBuffer();
214 
215  /// clears buffers and resets virtual camera to identity
216  virtual void DisplayClear(const GLbitfield & mask=CLEAR_DEFAULT_MASK );
217 
218  /// display soem very simple Gl calls to draw something at all
219  static void DisplaySampleScene();
220 
221  // coordinate cross for orientation
222  //static
223  void DisplayCoordCross(const float & size=1.0f, const float & linewidth=1.0f);
224 
225 
226  ///
227  /// change of size and aspect ratio.
228  virtual void Reshape();
229 
230  /// return GL state information
231  virtual void GetInfoGL(std::ostream & os);
232 
233  /* onscreen read pixel values JW */
234  BIAS::Vector3<float> GetPixelValueRGB(const wxPoint & pos2d);
235  BIAS::Vector4<float> GetPixelValueRGBA(const wxPoint & pos2d);
236 
237  /* @return Z value at 2d screen position JW */
238  float GetPixelValueDepth(const wxPoint & pos2d) const;
239 
240  /// activates the current rendereing context if there is one
241  /// and @return true if gl rendering context is available, false if there is none available (yet).
242  bool IsContextAvailable();
243 
244  /** computes 3D coordinate from 2D+Z by "unprojection" JW */
245  BIAS::Vector3<float> Unproject(const wxPoint & pos2d, double & d);
246 
247  //void SetStatusText(const wxString& text, int field = 0);
248 
249  unsigned int GetWidth() const;
250 
251  unsigned int GetHeight() const;
252 
253  /** GetAspectRatio is X/Y */
254  float GetAspectRatio() const;
255 
256  /** shows FPS to status bar base don frame counter and last call to this function
257  Measures and updates only, if two conditions ar eboth met:
258  1. at least minUpdateIntervalSec seconds have passed since prvious update
259  2. at least minNewFrames were drawn since a previous update
260  Shouls handle multiplem, shared contextst correctly (jw).
261  JW */
262  void ShowFPS(const int & framecounter,
263  const float & minUpdateIntervalSec,
264  const unsigned int & minNewFrames,
265  const unsigned int nField);
266  /// simple interface for the above
267  void ShowFPS();
268 
269  /** vertical sync control on or off (VSYNC) JW*/
270  static void SetVSync( const bool & trueForOn );
271 
272  /** @return true if vertical sync is enable,
273  false for free running mode JW */
274  static bool GetVSync();
275 
276  /** try to toggle VSync setting.
277  @return true if toggling worked, false if status persisted. */
278  static bool ToggleVSync();
279 
280  void OnPaint( wxPaintEvent &event );
281  void OnSize(wxSizeEvent& event);
282  void OnEraseBackground(wxEraseEvent& event);
283  virtual void OnChar(wxKeyEvent& event);
284 
285 
286  /* make an (onscreen) screenshot JW */
287  // TODO: maybe change arg to ImageBase
288  void ScreenShot(BIAS::ImageBase & dest,
289  const bool & flipY=true,
290  const GLenum & format=GL_RGB,
291  const bool &force8Bit=false);
292 
293  void ScreenShot(const std::string & filename,
294  const bool & flipY=true,
295  const GLenum & format=GL_RGB,
296  const bool &force8Bit=false);
297 
298  /** JW automatic screenshot to file with filename based on frame, instance etc. JW */
299  void ScreenShot();
300 
301  unsigned long GetFrameCount() const {return framecount_; };
302  void SetFrameCount(const unsigned long & newVal) {framecount_ =newVal; };
303 
304  wxFrame* GetOwnerframe();
305 
306  void SetOwnerframe(wxFrame* ptr);
307 
308  /** let an OpenGL canvas use the status bar of the containing frame and cout as fallback (JW) */
309  void SetStatusText(const wxString& text, const int c_field=0 );
310 
311  void ShowValue(const wxPoint & pos);
312 
313  /* toggles between Fullscreen mode (of the frame).
314  This is a hack to make fullscreen work for singel canvas frames. JW */
315  void ToggleFullScreen();
316 
317  /** helper toggling a bool value in place
318  @return new value JW */
319  static bool ToggleIP(bool & value);
320 
321 
322  /// sets the internal time param, by default based on realtime wall clock
323  virtual void UpdateTime();
324 
325 
326 #ifdef BIAS_HAVE_STEREOI
327  /** interace to NVidias consumer Stereo NVStereo driver API.
328  to enable/disbale stereo and control convergence and separation for stereo rendering
329  @return 0 on success, negative on error.
330  @author Jan Woetzel */
331  static int StereoI_Init();
332 
333  /** query status information on current NVStereo rendering state JW */
334  static void StereoI_GetInfo(std::ostream &os=std::cout);
335 
336  /** wrappers for StereoI interface */
337  static int StereoI_GetStereoState();
338  static int StereoI_SetStereoState(const int & val);
339  static float StereoI_GetSeparation();
340  static float StereoI_SetSeparation(const float & val);
341  static float StereoI_GetConvergence();
342  static float StereoI_SetConvergence(const float & val);
343  /* 0 for Jpeg, 1 for png,
344  Stereo images are dumped to [RootDir]\NVSTEREO.IMG JW */
345  static void StereoI_CaptureStereoImage(const int & format, const int & quality);
346 
347 #endif // BIAS_HAVE_STEREOI
348 
349 
350  public:
351  // global time, useful for animation:
352  static double time;
353  // true to set time automatically based on real time or display counter (in child classes)
355 
356  protected:
357 
358  // seq. frame counter=nr. of Display call.
359  unsigned long framecount_;
360 
361  // pointer to avaible frame for output etc (if there is one), NULL otherwise.
362  wxFrame * p_ownerframe;
363 
364  // internally used for intializing once when GL conetxt is available
366  // instance counter
367  unsigned int instance_;
368  static unsigned int instancesTotalNo_;
369 
370  // GL status changeable through keys:
372 
373  // useful for text output:
374  // TODO: Linux version (JW)
375 #ifdef BIAS_HAVE_FONTGL
377 #endif
378 
379  DECLARE_EVENT_TABLE()
380 
381  }; // OpenGLCanvasBase
382 
383 } // namespace BIAS
384 #endif
static bool animationUpdateEnabled
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
BIAS::RenderModeGL rendermode
const int attribsRGBAD[]
helper to simplify setting up format &quot;red,green,blue,alpha,depth&quot; JW to be used as (int*)&amp;attribsRG...
const int attribsRGBASD[]
helper to simplify setting up format &quot;red,green,blue,alpha,stencil,depth&quot; JW to be used as (int*)...
unsigned long GetFrameCount() const
static unsigned int instancesTotalNo_
base class for OpenGL canvases you could derive from to get base functionality (JW) ...
BIAS::DrawTextGL * p_font
void SetFrameCount(const unsigned long &newVal)
GLRenderMode describes mode for rendering.
Definition: RenderModeGL.hh:45
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
Draw text to OpenGL as 2D bitmap font.
Definition: DrawTextGL.hh:71