1
#ifndef CAMERASENSORBASE_H_
2
#define CAMERASENSORBASE_H_
4
// Open scene graph files
5
#include <osgDB/WriteFile>
7
#include <osg/Texture2D>
8
#include <osg/Material>
9
#include <osg/ShapeDrawable>
10
#include <osgGA/StateSetManipulator>
11
#include <osgViewer/Viewer>
12
#include <osgViewer/ViewerEventHandlers>
13
#include <osgShadow/ShadowedScene>
14
#include <osgShadow/SoftShadowMap>
17
#include <dtGame/gameapplication.h>
18
#include <dtGame/gamemanager.h>
19
#include <dtABC/application.h>
20
#include <dtCore/deltawin.h>
23
#include <srCore/sensor/sensorBase.h>
25
//TODO: camera based sensors need to be simulated completly on the GPU, as the data is already there and this would bring a huge speed up.
29
//! screen shot call back class
30
class CameraScreenShotCallback : public osg::Camera::DrawCallback
33
CameraScreenShotCallback(glType type) :
34
image (new osg::Image),
35
mTakeScreenShotNextFrame(false),
40
void setRequestDataToTrue()
42
mTakeScreenShotNextFrame = true;
45
// overwrite operator() of DrawCallback to retrieve the right image
46
// image will be retrieved after the simulation step, right after it is written
47
// to the viewport. Only then, it is possible to get the corresponding image, as
48
// many cameras use the same viewport.
49
virtual void operator()(const osg::Camera &camera) const
52
if(mTakeScreenShotNextFrame)
54
mTakeScreenShotNextFrame = false;
57
int x = static_cast<int>(camera.getViewport()->x());
58
int y = static_cast<int>(camera.getViewport()->y());
59
unsigned int width = static_cast<unsigned int>(camera.getViewport()->width());
60
unsigned int height = static_cast<unsigned int>(camera.getViewport()->height());
62
// std::cout << "cam viewport: " << x << ", " << y << ", " << width << ", " << height << std::endl;
67
image->allocateImage(width, height, 1, GL_DEPTH_COMPONENT, GL_FLOAT); //GL_RGB, GL_UNSIGNED_BYTE
68
image->readPixels(x, y, width, height, GL_DEPTH_COMPONENT, GL_FLOAT); //GL_DEPTH_COMPONENT
72
image->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE); //GL_RGB, GL_UNSIGNED_BYTE
73
image->readPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE); //GL_DEPTH_COMPONENT
80
// unsigned char *dst;
81
// glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, dst);
86
osg::ref_ptr<osg::Image> getOSGImage() const
93
setRequestDataToTrue();
98
osg::ref_ptr<osg::Image> image;
101
mutable bool mTakeScreenShotNextFrame;
109
//! Camera sensor base class.
113
* Used for all sensor devices, which need to retrieve information via osg::camera.
115
* @author Lutz Winkler
118
class CameraSensorBase : public SensorBase
121
friend class SimulationGUIComponent;
126
* \brief Initializes the sensor
128
* \param gm GameManager, which handles the corresponding robot
129
* \param attachedTo Body, where the camera is attached to
130
* \param pos Local position of the camera
131
* \param orient Local orientation of the camera
132
* \param flareAngle flare angle of the camera
133
* \param resolution resolution of the camera
134
* \param name name of the camera
138
CameraSensorBase(dtCore::RefPtr<BodyBase> attachedTo,
139
osg::Vec3f pos, osg::Vec3f orient, std::string name, std::string type, glType cameraType, bool active):
140
SensorBase (attachedTo, pos, orient, name, type, cameraType, active),
148
clearColor (osg::Vec4(0,0,0,0)),
149
sensorImage (new osg::Image),
150
mScreenShotTaker(new CameraScreenShotCallback(cameraType)){mScreenShotTaker->requestData();};
154
//! sends command to retrieve data
155
virtual void evaluate(ParameterReturnValue srv) = 0;
157
//! returns the last image taken by mScreenShotTaker
158
osg::ref_ptr<osg::Image> getRawData();
160
//! Request to take a new image after the simulation step
163
//! Will be called in RobotActorBase::TickLocal()
164
void update(float deltaSimTme);
166
virtual void getNewSensorValue() = 0;
171
* \brief Sets a parameter
173
* See also getParameter()
176
virtual void setParameter(const std::string &name, ParameterReturnValue parameter);
180
* \brief Writes the parameter value in parameter
182
* The available parameters can be retrieved by calling getParameterList()
183
* See also: ObjectBase::getParameter()
187
void getParameter(const std::string &name, ParameterReturnValue parameter);
190
//! sets up the camera sensor and put it into the scene
193
// Set rendering order
194
void setRenderOrder(int renderOrder) {view->SetRenderOrder(renderOrder);}
196
int getRenderOrder() {return view->GetRenderOrder();}
200
virtual ~CameraSensorBase();
202
float minDistance, maxDistance;
203
float frustumLeft, frustumRight, frustumUp, frustumDown;
205
osg::Vec4 clearColor;
209
dtCore::RefPtr<dtCore::Transformable> sensor;
210
osg::Vec2 resolution;
211
dtCore::DeltaWin* win;
213
osg::ref_ptr<osg::Camera> mOsgCamera;
214
osg::ref_ptr<osg::Image> sensorImage;
215
osg::ref_ptr<CameraScreenShotCallback> mScreenShotTaker;
220
#endif /*CAMERASENSORBASE_H_*/