~robot3d-team/robot3d/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#ifndef CAMERASENSORBASE_H_
#define CAMERASENSORBASE_H_

// Open scene graph files
#include <osgDB/WriteFile>
#include <osg/Camera>
#include <osg/Texture2D>
#include <osg/Material>
#include <osg/ShapeDrawable>
#include <osgGA/StateSetManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgShadow/ShadowedScene>
#include <osgShadow/SoftShadowMap>

// Delta3D files
#include <dtGame/gameapplication.h>
#include <dtGame/gamemanager.h>
#include <dtABC/application.h>
#include <dtCore/deltawin.h>

// Robot3D files
#include <srCore/sensor/sensorBase.h>

//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.

namespace srCore {

//! screen shot call back class
class CameraScreenShotCallback : public osg::Camera::DrawCallback
{
public:
	CameraScreenShotCallback(glType type) :
		image (new osg::Image),
		mTakeScreenShotNextFrame(false),
		type (type)

		{}

	void setRequestDataToTrue()
	{
		mTakeScreenShotNextFrame  = true;
	}

	// overwrite operator() of DrawCallback to retrieve the right image
	// image will be retrieved after the simulation step, right after it is written
	// to the viewport. Only then, it is possible to get the corresponding image, as
	// many cameras use the same viewport.
	virtual void operator()(const osg::Camera &camera) const
	{

		if(mTakeScreenShotNextFrame)
		{
			mTakeScreenShotNextFrame = false;


			int x = static_cast<int>(camera.getViewport()->x());
			int y = static_cast<int>(camera.getViewport()->y());
			unsigned int width = static_cast<unsigned int>(camera.getViewport()->width());
			unsigned int height = static_cast<unsigned int>(camera.getViewport()->height());

		//	std::cout << "cam viewport: " << x << ", " << y << ", " << width << ", " << height << std::endl;

			switch (type) {

			case DEPTH_CAMERA:
				image->allocateImage(width, height, 1, GL_DEPTH_COMPONENT, GL_FLOAT);		//GL_RGB, GL_UNSIGNED_BYTE
				image->readPixels(x, y, width, height, GL_DEPTH_COMPONENT, GL_FLOAT);		//GL_DEPTH_COMPONENT
				break;

			case RGB_CAMERA:
				image->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE);		//GL_RGB, GL_UNSIGNED_BYTE
				image->readPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE);		//GL_DEPTH_COMPONENT
				break;

			default:
				break;

			}
//			unsigned char *dst;
//			glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, dst);

		}
	}

	osg::ref_ptr<osg::Image> getOSGImage() const
	{
		return image;
	}

	void requestData()
	{
		setRequestDataToTrue();
	}

protected:

	osg::ref_ptr<osg::Image> image;


	mutable bool  mTakeScreenShotNextFrame;
	glType type;


};



//! Camera sensor base class.

/*!
 *
 * Used for all sensor devices, which need to retrieve information via osg::camera.
 *
 * @author Lutz Winkler
 *
 */
class CameraSensorBase : public SensorBase
{

	friend class SimulationGUIComponent;

public:

	/**
	 * \brief  Initializes the sensor
	 *
	 * \param			gm							GameManager, which handles the corresponding robot
	 * \param			attachedTo					Body, where the camera is attached to
	 * \param     		pos			   				Local position of the camera
	 * \param			orient 						Local orientation of the camera
	 * \param			flareAngle					flare angle of the camera
	 * \param			resolution					resolution of the camera
	 * \param			name						name of the camera
	 *
	 *
	 */
	CameraSensorBase(dtCore::RefPtr<BodyBase> attachedTo,
			osg::Vec3f pos, osg::Vec3f orient, std::string name, std::string type, glType cameraType, bool active):
		SensorBase		(attachedTo, pos, orient, name, type, cameraType, active),
		minDistance		(1),
		maxDistance		(1000),
		frustumLeft		(-1),
		frustumRight	(1),
		frustumUp		(-1),
		frustumDown		(1),
		flareAngle		(0.5),
		clearColor		(osg::Vec4(0,0,0,0)),
		sensorImage 	(new osg::Image),
		mScreenShotTaker(new CameraScreenShotCallback(cameraType)){mScreenShotTaker->requestData();};



	//! sends command to retrieve data
	virtual void evaluate(ParameterReturnValue srv) = 0;

	//! returns the last image taken by mScreenShotTaker
	osg::ref_ptr<osg::Image> getRawData();

	//! Request to take a new image after the simulation step
	void requestData();

	//! Will be called in RobotActorBase::TickLocal()
	void update(float deltaSimTme);

	virtual void getNewSensorValue() = 0;



	/**
	 * \brief  Sets a parameter
	 *
	 *		See also getParameter()
	 *
	 */
	virtual void setParameter(const std::string &name, ParameterReturnValue  parameter);


	/**
	 * \brief  Writes the parameter value in parameter
	 *
	 *		The available parameters can be retrieved by calling getParameterList()
	 *		See also: ObjectBase::getParameter()
	 *
	 *
	 */
	void getParameter(const std::string &name, ParameterReturnValue parameter);


	//! sets up the camera sensor and put it into the scene
	void setup();

	// Set rendering order
	void setRenderOrder(int renderOrder) {view->SetRenderOrder(renderOrder);}

	int getRenderOrder() {return view->GetRenderOrder();}

protected:

	virtual ~CameraSensorBase();

	float minDistance, maxDistance;
	float frustumLeft, frustumRight, frustumUp, frustumDown;
	float flareAngle;
	osg::Vec4 clearColor;

	glType cameraType;

	dtCore::RefPtr<dtCore::Transformable> sensor;
	osg::Vec2 resolution;
	dtCore::DeltaWin* win;
	dtCore::View* view;
	osg::ref_ptr<osg::Camera> mOsgCamera;
	osg::ref_ptr<osg::Image> sensorImage;
	osg::ref_ptr<CameraScreenShotCallback> mScreenShotTaker;
};

}

#endif /*CAMERASENSORBASE_H_*/