ViSP  2.9.0
vpViewer.cpp
1 /****************************************************************************
2  *
3  * $Id: vpViewer.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Simulator based on Coin3d.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
47 #include <visp/vpConfig.h>
48 
49 #ifdef VISP_HAVE_COIN_AND_GUI
50 #include <visp/vpViewer.h>
51 #include <visp/vpSimulator.h>
52 
53 
54 #include <Inventor/events/SoKeyboardEvent.h>
55 #include <Inventor/nodes/SoEventCallback.h>
56 
57 
58 #if defined(VISP_HAVE_SOWIN)
59 vpViewer::vpViewer(HWND parent, vpSimulator *_simu, vpViewerType viewerType):
60  SoWinExaminerViewer(parent,(char *)NULL,false)
61 #elif defined(VISP_HAVE_SOQT)
62 vpViewer::vpViewer(QWidget * parent, vpSimulator *_simu, vpViewerType viewerType) :
63  SoQtExaminerViewer(parent,(char *)NULL,false)
64 #elif defined(VISP_HAVE_SOXT)
65 vpViewer::vpViewer(Widget parent, vpSimulator *_simu, vpViewerType viewerType):
66  SoXtExaminerViewer(parent,(char *)NULL,false)
67 #endif
68 {
69  this->simu = _simu ;
70  this->viewerType = viewerType;
71  // Coin should not clear the pixel-buffer, so the background image
72  // is not removed.
73 
74  this->setClearBeforeRender(FALSE, TRUE);
75  // this->setAntialiasing(true, 2) ;
76  setAutoRedraw(false);
77 }
78 
80 {
81 
82 }
83 
84 void
86 {
87 
88  {
89  const SbViewportRegion vp = this->getViewportRegion();
90  SbVec2s origin = vp.getViewportOriginPixels();
91  SbVec2s size = vp.getViewportSizePixels();
92  glViewport(origin[0], origin[1], size[0], size[1]);
93 
94  const SbColor col = this->getBackgroundColor();
95  glClearColor(col[0], col[1], col[2], 0.0f);
96  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
97 
98  // this should be used only with the vpAR:vpSimulator
99  // to diplay an image background
100  if (simu->image_background != NULL)
101  {
102  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
103  if (simu->typeImage == vpSimulator::grayImage)
104  glDrawPixels((GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(),
105  (GLenum)GL_LUMINANCE,
106  GL_UNSIGNED_BYTE,
107  simu->image_background );
108  else
109  glDrawPixels((GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(),
110  (GLenum)GL_RGB,
111  GL_UNSIGNED_BYTE,
112  simu->image_background );
113 
114  glEnable(GL_DEPTH_TEST);
115  glClear(GL_DEPTH_BUFFER_BIT); // clear the z-buffer
116  glClearDepth(100.0); // Profondeur du Z-Buf
117  }
118  // Render normal scenegraph.
119 #if defined(VISP_HAVE_SOWIN)
120  SoWinExaminerViewer::actualRedraw();
121 #elif defined(VISP_HAVE_SOQT)
122  SoQtExaminerViewer::actualRedraw();
123 #elif defined(VISP_HAVE_SOXT)
124  SoXtExaminerViewer::actualRedraw();
125 #endif
126  glSwapBuffers() ;
127  if(viewerType == vpViewer::internalView){
128  simu->get = 0 ;
129  glReadPixels(0, 0, (GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(),
130  (GLenum)GL_RGB,
131  GL_UNSIGNED_BYTE,
132  simu->bufferView ) ;
133  simu->get =1 ;
134  }
135  }
136 
137 }
138 
146 void
147 #if defined(VISP_HAVE_SOWIN) || defined (VISP_HAVE_SOQT)
148 vpViewer::resize(int x, int y, bool fixed)
149 #else
150 vpViewer::resize(int x, int y, bool /*fixed*/)
151 #endif
152 {
153  SbVec2s size(x,y) ;
154  setSize(size);
155  setGLSize(size) ;
156 
157 #if defined(VISP_HAVE_SOWIN)
158  HWND parent = getParentWidget();
159 
160  RECT rcClient, rcWindow;
161  POINT ptDiff;
162  GetClientRect(parent, &rcClient);
163  GetWindowRect(parent, &rcWindow);
164  ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
165  ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
166  MoveWindow(parent,rcWindow.left, rcWindow.top,
167  x + ptDiff.x, y + ptDiff.y, TRUE);
168  if(fixed){
169  DWORD dwStyle = GetWindowLong(parent, GWL_STYLE);
170  dwStyle &= ~(WS_SIZEBOX);
171  SetWindowLong(parent, GWL_STYLE,dwStyle);
172  }
173 #elif defined(VISP_HAVE_SOQT)
174  if(fixed){
175  QWidget * parent = getParentWidget();
176  parent->setFixedSize(x, y);
177  }
178 #endif
179 }
180 
189 SbBool
190 vpViewer::processSoEvent(const SoEvent * const event)
191 {
192  if ( this->isViewing() &&
193  event->getTypeId() == SoKeyboardEvent::getClassTypeId() )
194  {
195  SoKeyboardEvent * kbevent = (SoKeyboardEvent *) event;
196  switch ( kbevent->getKey() ) {
197  case SoKeyboardEvent::H:
198  if ( kbevent->getState() == SoButtonEvent::DOWN )
199  {
200  std::cout << "H : this help "<<std::endl ;
201  std::cout << "M : get and save the external camera location (matrix)"<<std::endl;
202  std::cout << "V : get and save the external camera location (vector)"<<std::endl;
203  std::cout << "M : load camera location (vector)"<<std::endl;
204  std::cout << "P : get external camera location and set the internal one"<<std::endl;
205  }
206  return TRUE;
207 
208  case SoKeyboardEvent::M:
209  if ( kbevent->getState() == SoButtonEvent::DOWN )
210  {
211  vpHomogeneousMatrix cMf ;
212  simu->getExternalCameraPosition(cMf) ;
213  std::ofstream f("cMf.dat") ;
214  cMf.save(f) ;
215  f.close() ;
216  }
217  return TRUE;
218  case SoKeyboardEvent::V:
219  if ( kbevent->getState() == SoButtonEvent::DOWN )
220  {
221  vpHomogeneousMatrix cMf ;
222  simu->getExternalCameraPosition(cMf) ;
223  vpPoseVector vcMf(cMf) ;
224  std::ofstream f("vcMf.dat") ;
225  vcMf.save(f) ;
226  f.close() ;
227  }
228  return TRUE;
229  case SoKeyboardEvent::L:
230  if ( kbevent->getState() == SoButtonEvent::DOWN )
231  {
232  vpPoseVector vcMf;
233  std::ifstream f("vcMf.dat") ;
234  vcMf.load(f) ;
235  f.close() ;
236  vpHomogeneousMatrix cMf(vcMf) ;
237  simu->setCameraPosition(cMf) ;
238  simu->moveInternalCamera(cMf) ;
239  }
240  return TRUE;
241  case SoKeyboardEvent::P:
242  if ( kbevent->getState() == SoButtonEvent::DOWN )
243  {
244  vpHomogeneousMatrix cMf ;
245  simu->getExternalCameraPosition(cMf) ;
246  vpPoseVector vcMf(cMf) ;
247  vcMf.print() ;
248  simu->setCameraPosition(cMf) ;
249  simu->moveInternalCamera(cMf) ;
250  }
251  return TRUE;
252  default:
253  break;
254  }
255  }
256 #if defined(VISP_HAVE_SOWIN)
257  return SoWinExaminerViewer::processSoEvent(event);
258 #elif defined(VISP_HAVE_SOQT)
259  return SoQtExaminerViewer::processSoEvent(event);
260 #elif defined(VISP_HAVE_SOXT)
261  return SoXtExaminerViewer::processSoEvent(event);
262 #endif
263 
264 }
265 
266 #endif
void load(std::ifstream &f)
virtual ~vpViewer()
Definition: vpViewer.cpp:79
void resize(int x, int y, bool fixed=false)
Definition: vpViewer.cpp:148
int get
Flag to protect the read and write of the framebuffer (between the simulator and the viewer)...
Definition: vpSimulator.h:287
void setCameraPosition(vpHomogeneousMatrix &cMf)
set the camera position (from an homogeneous matrix)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Implementation of a simulator based on Coin3d (www.coin3d.org).
Definition: vpSimulator.h:102
unsigned int getInternalHeight() const
Definition: vpSimulator.h:186
void getExternalCameraPosition(vpHomogeneousMatrix &cMf)
get the external camera position
void moveInternalCamera(vpHomogeneousMatrix &cMf)
modify the position of the camera in the scene graph
vpViewerType
Definition: vpViewer.h:131
vpImageType typeImage
Definition: vpSimulator.h:135
GLubyte * image_background
Definition: vpSimulator.h:137
Viewer used by the simulator.
Definition: vpViewer.h:120
virtual void actualRedraw(void)
Definition: vpViewer.cpp:85
void save(std::ofstream &f) const
unsigned char * bufferView
image of the internal view
Definition: vpSimulator.h:284
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
unsigned int getInternalWidth() const
Definition: vpSimulator.h:180
vpViewer(HWND parent, vpSimulator *simu, vpViewerType viewerType)
Definition: vpViewer.cpp:59