~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to inc/srCore/sensor/cameraSensorBase.h

  • Committer: Anne van Rossum
  • Date: 2010-08-10 15:58:55 UTC
  • Revision ID: anne@gamix-20100810155855-kve7x2vwouagdij9
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef CAMERASENSORBASE_H_
 
2
#define CAMERASENSORBASE_H_
 
3
 
 
4
// Open scene graph files
 
5
#include <osgDB/WriteFile>
 
6
#include <osg/Camera>
 
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>
 
15
 
 
16
// Delta3D files
 
17
#include <dtGame/gameapplication.h>
 
18
#include <dtGame/gamemanager.h>
 
19
#include <dtABC/application.h>
 
20
#include <dtCore/deltawin.h>
 
21
 
 
22
// Robot3D files
 
23
#include <srCore/sensor/sensorBase.h>
 
24
 
 
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.
 
26
 
 
27
namespace srCore {
 
28
 
 
29
//! screen shot call back class
 
30
class CameraScreenShotCallback : public osg::Camera::DrawCallback
 
31
{
 
32
public:
 
33
        CameraScreenShotCallback(glType type) :
 
34
                image (new osg::Image),
 
35
                mTakeScreenShotNextFrame(false),
 
36
                type (type)
 
37
 
 
38
                {}
 
39
 
 
40
        void setRequestDataToTrue()
 
41
        {
 
42
                mTakeScreenShotNextFrame  = true;
 
43
        }
 
44
 
 
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
 
50
        {
 
51
 
 
52
                if(mTakeScreenShotNextFrame)
 
53
                {
 
54
                        mTakeScreenShotNextFrame = false;
 
55
 
 
56
 
 
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());
 
61
 
 
62
                //      std::cout << "cam viewport: " << x << ", " << y << ", " << width << ", " << height << std::endl;
 
63
 
 
64
                        switch (type) {
 
65
 
 
66
                        case DEPTH_CAMERA:
 
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
 
69
                                break;
 
70
 
 
71
                        case RGB_CAMERA:
 
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
 
74
                                break;
 
75
 
 
76
                        default:
 
77
                                break;
 
78
 
 
79
                        }
 
80
//                      unsigned char *dst;
 
81
//                      glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, dst);
 
82
 
 
83
                }
 
84
        }
 
85
 
 
86
        osg::ref_ptr<osg::Image> getOSGImage() const
 
87
        {
 
88
                return image;
 
89
        }
 
90
 
 
91
        void requestData()
 
92
        {
 
93
                setRequestDataToTrue();
 
94
        }
 
95
 
 
96
protected:
 
97
 
 
98
        osg::ref_ptr<osg::Image> image;
 
99
 
 
100
 
 
101
        mutable bool  mTakeScreenShotNextFrame;
 
102
        glType type;
 
103
 
 
104
 
 
105
};
 
106
 
 
107
 
 
108
 
 
109
//! Camera sensor base class.
 
110
 
 
111
/*!
 
112
 *
 
113
 * Used for all sensor devices, which need to retrieve information via osg::camera.
 
114
 *
 
115
 * @author Lutz Winkler
 
116
 *
 
117
 */
 
118
class CameraSensorBase : public SensorBase
 
119
{
 
120
 
 
121
        friend class SimulationGUIComponent;
 
122
 
 
123
public:
 
124
 
 
125
        /**
 
126
         * \brief  Initializes the sensor
 
127
         *
 
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
 
135
         *
 
136
         *
 
137
         */
 
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),
 
141
                minDistance             (1),
 
142
                maxDistance             (1000),
 
143
                frustumLeft             (-1),
 
144
                frustumRight    (1),
 
145
                frustumUp               (-1),
 
146
                frustumDown             (1),
 
147
                flareAngle              (0.5),
 
148
                clearColor              (osg::Vec4(0,0,0,0)),
 
149
                sensorImage     (new osg::Image),
 
150
                mScreenShotTaker(new CameraScreenShotCallback(cameraType)){mScreenShotTaker->requestData();};
 
151
 
 
152
 
 
153
 
 
154
        //! sends command to retrieve data
 
155
        virtual void evaluate(ParameterReturnValue srv) = 0;
 
156
 
 
157
        //! returns the last image taken by mScreenShotTaker
 
158
        osg::ref_ptr<osg::Image> getRawData();
 
159
 
 
160
        //! Request to take a new image after the simulation step
 
161
        void requestData();
 
162
 
 
163
        //! Will be called in RobotActorBase::TickLocal()
 
164
        void update(float deltaSimTme);
 
165
 
 
166
        virtual void getNewSensorValue() = 0;
 
167
 
 
168
 
 
169
 
 
170
        /**
 
171
         * \brief  Sets a parameter
 
172
         *
 
173
         *              See also getParameter()
 
174
         *
 
175
         */
 
176
        virtual void setParameter(const std::string &name, ParameterReturnValue  parameter);
 
177
 
 
178
 
 
179
        /**
 
180
         * \brief  Writes the parameter value in parameter
 
181
         *
 
182
         *              The available parameters can be retrieved by calling getParameterList()
 
183
         *              See also: ObjectBase::getParameter()
 
184
         *
 
185
         *
 
186
         */
 
187
        void getParameter(const std::string &name, ParameterReturnValue parameter);
 
188
 
 
189
 
 
190
        //! sets up the camera sensor and put it into the scene
 
191
        void setup();
 
192
 
 
193
        // Set rendering order
 
194
        void setRenderOrder(int renderOrder) {view->SetRenderOrder(renderOrder);}
 
195
 
 
196
        int getRenderOrder() {return view->GetRenderOrder();}
 
197
 
 
198
protected:
 
199
 
 
200
        virtual ~CameraSensorBase();
 
201
 
 
202
        float minDistance, maxDistance;
 
203
        float frustumLeft, frustumRight, frustumUp, frustumDown;
 
204
        float flareAngle;
 
205
        osg::Vec4 clearColor;
 
206
 
 
207
        glType cameraType;
 
208
 
 
209
        dtCore::RefPtr<dtCore::Transformable> sensor;
 
210
        osg::Vec2 resolution;
 
211
        dtCore::DeltaWin* win;
 
212
        dtCore::View* view;
 
213
        osg::ref_ptr<osg::Camera> mOsgCamera;
 
214
        osg::ref_ptr<osg::Image> sensorImage;
 
215
        osg::ref_ptr<CameraScreenShotCallback> mScreenShotTaker;
 
216
};
 
217
 
 
218
}
 
219
 
 
220
#endif /*CAMERASENSORBASE_H_*/