Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
OpenGLCanvas.cpp
1 #ifdef WIN32
2 # pragma warning (disable: 4005) // for VS8 + WX
3 #endif
4 
5 #include "OpenGLCanvas.hh"
6 
7 #include <Base/Image/ImageIO.hh>
8 #include <Base/Math/Vector2.hh>
9 #include <Base/Math/Vector3.hh>
10 #include <Base/Math/Vector4.hh>
11 
12 #include "GeometryGL.hh"
13 
14 // -----------------------------------------
15 using namespace std;
16 using namespace BIAS;
17 // -----------------------------------------
18 
19 // override parent classes event handling table behavior
20 BEGIN_EVENT_TABLE (OpenGLCanvas, OpenGLCanvasBase)
21 // --- parent EVT handlers ---
22 //EVT_PAINT (OpenGLCanvasBase::OnPaint)
23 //EVT_SIZE (OpenGLCanvasBase::OnSize)
24 //EVT_ERASE_BACKGROUND (OpenGLCanvasBase::OnEraseBackground)
25 // --- additional handlers ---
26 EVT_MOTION(OpenGLCanvas::OnMotion)
27 EVT_LEFT_DOWN(OpenGLCanvas::OnMouseDown)
28 EVT_MIDDLE_DOWN(OpenGLCanvas::OnMouseDown)
29 EVT_RIGHT_DOWN(OpenGLCanvas::OnMouseDown)
30 EVT_MOUSEWHEEL(OpenGLCanvas::OnMouseWheel)
31 #if OPENGLCANVAS_CONNECT_ONIDLE
32 EVT_IDLE (OpenGLCanvas::OnIdle)
33 #endif
34 
35 //EVT_LEFT_DCLICK(OpenGLCanvas::OnLeftDoubleClick)
36 //EVT_RIGHT_DCLICK(OpenGLCanvas::OnRightDoubleClick)
37 //EVT_MIDDLE_DCLICK(OpenGLCanvas::OnMiddleDoubleClick)
38 END_EVENT_TABLE()
39 
40 // -----------------------------------------
41 bool OpenGLCanvas::idleRepaintEnabled = OPENGLCANVAS_DEFAULT_idleRepaintEnabled;
42 bool OpenGLCanvas::realtimeAnimation = OPENGLCANVAS_DEFAULT_realtimeAnimation;
43 // -----------------------------------------
44 
45 /// destructor
47  delete p_viewCtrl_; p_viewCtrl_=NULL;
48 }
49 
50 
51 /// constructors:
52 OpenGLCanvas::OpenGLCanvas(
53  wxFrame* parent
54  ,wxWindowID id
55  ,const wxPoint& pos
56  ,const wxSize& size
57  ,long style
58  ,const wxString& name
59  ,int* attribList
60  ,const wxPalette& palette
61  )
62  : OpenGLCanvasBase(parent,id,pos,size,style,name,attribList,palette)
63  ,p_viewCtrl_(NULL)
64 {
65  //CALLINFO;
66  InitMembers();
67 }
68 
69 
71  wxFrame* parent
72  ,wxGLContext* sharedContext
73  ,wxWindowID id
74  ,const wxPoint& pos
75  ,const wxSize& size
76  ,long style
77  ,const wxString& name
78  ,int* attribList
79  ,const wxPalette& palette
80  )
81  : OpenGLCanvasBase(parent,sharedContext,id,pos,size,style,name,attribList,palette)
82  ,p_viewCtrl_(NULL)
83 {
84  //CALLINFO;
85  InitMembers();
86 }
87 
88 
90 {
91  //CALLINFO;
92  if (p_viewCtrl_==NULL)
94 
96  // additional members:
97  _polygonmodeGL=GL_FILL;
98  //_pos=wxPoint(0,0);
99 }
100 
101 
103 #ifdef BIAS_DEBUG
104  if (!idleRepaintEnabled){
105  cout<<"."; // display a dot for each frame only if no repaint onidle
106  }
107 #endif // BIAS_DEBUG
109 }
110 
111 
113 {
116 }
117 
118 
120 {
122 }
123 
126 }
127 
128 
129 
130 
131 void OpenGLCanvas::OnMotion( wxMouseEvent &event )
132 {
133  //#ifdef WIN32
134  // wxDC dc;
135  //#else
136  // // HACK
137  // wxPaintDC dc(this);
138  //#endif
139 
140  wxPoint pos= event.GetPosition(); //< mouse position
141  if ((event.LeftIsDown() && event.RightIsDown()) || event.MiddleIsDown()){
142  GetViewController().MotionTranslationCoplanar( Vector2<double>(double(pos.x), double(pos.y)) );
143  } else if (event.LeftIsDown()){
144  GetViewController().MotionTranslation( Vector2<double>(double(pos.x), double(pos.y)) );
145  } else if (event.RightIsDown()){
146  GetViewController().MotionTranslationForward( Vector2<double>(double(pos.x), double(pos.y)) );
147  } else {
148  this->ShowValue( event.GetPosition() );
149  }
150  if (!this->idleRepaintEnabled){
151  Display(); // extra Display onidle doe sit otherwise
152  }
153 }
154 
155 void OpenGLCanvas::OnMouseDown( wxMouseEvent &event )
156 {
157  wxPoint pos= event.GetPosition(); //< mouse position
159  Vector2<double>(double(GetWidth()), double(GetHeight())) );
161  Vector2<double>(double(pos.x), double(pos.y)) );
162 }
163 
164 void OpenGLCanvas::OnMouseWheel( wxMouseEvent &event )
165 {
166  const double fraction=0.25;
167  const int rot = event.GetWheelRotation();
168  if (rot>0){
169  // forward
170  GetViewController().MoveToCenter( -fraction );
171  } else if (rot<0){
172  //backward
173  GetViewController().MoveToCenter( +fraction);
174  } else {
175  BIASWARN("invalid wheel motion rot="<<rot);
176  BIASBREAK;
177  }
178  if (!this->idleRepaintEnabled){
179  Display(); // extra Display onidle doe sit otherwise
180  }
181 }
182 
183 
184 // onidle redisplay
185 // useful for animations
186 void OpenGLCanvas::OnIdle(wxIdleEvent& event)
187 {
188  // CALLINFO;
189  // TODO set global OpenGLCanvasBase::time based on system time?
190  if (!(this->idleRepaintEnabled)) {
191  return; // nothing to do, in particular do not request more OnIdle events.
192  };
193 
194  // for animation
195  UpdateTime();
196 
197  // paint again, timestamp may have changed, useful for animations:
198  Display();
199  ShowFPS();
200 
201  // usually only one Idle event is generated on when we become idle
202  // but we want to run this continuously *while* we stay idle.
203  event.RequestMore();
204 }
205 
206 
208 {
210  // next time step: increase a bit
211  if (realtimeAnimation){
213  } else {
214  // fallback that depends on frame no.
215  // should be a fixed update
216  // assume scaling for TARGET_FPS fps display...
217  time +=1.0/TARGET_FPS;
218  }
219  //cout<<"OpenGLCanvasBase::time="<<OpenGLCanvasBase::time <<endl; // DBG
220  }
221 }
222 
223 /// return GL state info
224 void OpenGLCanvas::GetInfoGL(std::ostream & os)
225 {
227 
228  // display (possibly used) CamerViewcontroller member status:
229  GetViewController().Print(os);
230 }
231 
233 {
234  if (!GetContext())
235  return; // no context available
236 
239  if (!GetViewController().GetInternals().frustum.isFixed)
240  {
242  }
243 
244  /* (re-)display on canvas decrease due to new viewport transformation */
245  Display();
246 }
247 
249  BIASASSERT(p_viewCtrl_!=NULL);
250  return *p_viewCtrl_;
251 }
252 
254  BIASASSERT(p_viewCtrl_!=NULL);
255  return *p_viewCtrl_;
256 }
257 
virtual void Display()
display call to be overridden by child classes
static bool realtimeAnimation
virtual void DisplayCameraViewExternalParams()
virtual void GetInfoGL(std::ostream &os)
return GL state information
void MotionTranslationCoplanar(const BIAS::Vector2< double > &pos)
static bool animationUpdateEnabled
virtual void DisplayCameraViewInternalParams()
virtual void InitMembers()
any constructor should call this function to initialize all data members (e.g.
virtual void InitMembers()
any constructor should call this function to initialize all data members (e.g.
std::ostream & Print(std::ostream &os=std::cout)
map update of one axis (mouse wheel, delta, key...) maps delta (e.g.
OpenGL canvases with additinal event handling (JW)
Definition: OpenGLCanvas.hh:68
void OnIdle(wxIdleEvent &event)
virtual void UpdateTime()
sets the internal time param, by default based on realtime wall clock
base for (interactively) manipulating a (virtual) camera view.
void ShowFPS()
simple interface for the above
virtual void GetInfoGL(std::ostream &os=std::cout)
return GL state info
unsigned int GetHeight() const
OpenGLCanvas(wxFrame *parent, wxWindowID id=-1, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=AsciiToWx("OpenGLCanvas"), int *attribList=NULL, const wxPalette &palette=wxNullPalette)
constructors:
void MoveToCenter(const double &fraction)
void SetStartPos(const BIAS::Vector2< double > &pos)
toplevel entry for all motion controllers
base class for OpenGL canvases you could derive from to get base functionality (JW) ...
void OnMouseWheel(wxMouseEvent &event)
CameraViewInternals GetInternals() const
unsigned int GetWidth() const
float GetAspectRatio() const
GetAspectRatio is X/Y.
void SetSize(const BIAS::Vector2< double > &sz)
interaction area size
void OnMouseDown(wxMouseEvent &event)
void MotionTranslationForward(const BIAS::Vector2< double > &pos)
void ShowValue(const wxPoint &pos)
void MotionTranslation(const BIAS::Vector2< double > &pos)
simple translation
CameraViewController * p_viewCtrl_
virtual void DisplayCameraView()
virtual void Reshape()
change of size and aspect ratio.
void OnMotion(wxMouseEvent &event)
void AdaptWidth(const double &vpAspectRatioDesired)
adapt left and right to match an aspect ratio useful to adapth frustum to viewport bound to windows s...
virtual void UpdateTime()
sets the internal time param, by default based on realtime wall clock
CameraViewController GetViewController() const
static bool idleRepaintEnabled
virtual void Display()
main display call to be overridden by child classes usually displays: -InitWithContext (must be done ...