~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to src/srCore/simulationCameraController.cpp

  • 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
/*
 
2
 * Robot3D Physical realistic 3D simulator for robots
 
3
 *
 
4
 * @license GNU Lesser General Public License
 
5
 *
 
6
 * @author Adrian Friebel
 
7
 * @author Anne van Rossum
 
8
 */
 
9
 
 
10
// Robot3D files
 
11
#include <srCore/simulationCameraController.h>
 
12
#include <srCore/simulationEvents.h>
 
13
 
 
14
// Delta3D files
 
15
#include <dtUtil/log.h>
 
16
 
 
17
#define CAM_SCALE                                                               0.6                     //1=direct following, 0=no following
 
18
 
 
19
using namespace srCore;
 
20
 
 
21
/**
 
22
 * The camera controller is a Delta3D Game Component. It concerns the "user" camera, through which the
 
23
 * scene is seen. It is not the camera on the robot.
 
24
 */
 
25
SimulationCameraController::SimulationCameraController(const std::string &name, osg::Vec3f camPos, bool camState)
 
26
:       dtGame::GMComponent(name),
 
27
        camPos(camPos),
 
28
        freeCamState(camState),
 
29
        counter(0) {
 
30
        dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
 
31
        mScreenShotTaker = new ScreenShotCallback(RGB_CAMERA, GA->GetCamera()->GetOSGCamera()->getViewport());
 
32
}
 
33
 
 
34
/**
 
35
 * Destroys the camera.
 
36
 */
 
37
SimulationCameraController::~SimulationCameraController() {
 
38
        mTrp->SetAttachToTransformable(NULL);
 
39
        delete mScreenShotTaker;
 
40
}
 
41
 
 
42
/**
 
43
 * Initialises the camera and where it looks at. If there are no actors at all, it actually
 
44
 * should look at the position indicated in the XML configuration file. It is possible to
 
45
 * set the viewport on the generic camera, like this:
 
46
 *   camera->GetOSGCamera()->setViewport(new osg::Viewport(0.0, 10.0, 480, 480));
 
47
 * This has the effect that the entire thing plus the GUI elements becomes limited to this
 
48
 * part of the screen.
 
49
 * The function:
 
50
 *   GetWindow()->GetOsgViewerGraphicsWindow()->getWindowRectangle(x, y, width, height);
 
51
 * does not return the proper x and y, it starts at x=35 and y=50 instead of 0,0.
 
52
 */
 
53
void SimulationCameraController::init(dtCore::Mouse *mouse, dtCore::Keyboard *keyboard)
 
54
{
 
55
        bool noRobots = false;
 
56
        SimulationArenaComponent *comp = static_cast<SimulationArenaComponent*>(
 
57
                        GetGameManager()->GetComponentByName(SimulationArenaComponent::DEFAULT_NAME));
 
58
        int index = comp->getRobFocIndex();
 
59
 
 
60
        mFMM = new dtCore::FlyMotionModel(keyboard, mouse);
 
61
        /*
 
62
        mFMM->SetMaximumFlySpeed(1000 / GetGameManager()->GetTimeScale());
 
63
        mFMM->SetMaximumTurnSpeed(10000 / GetGameManager()->GetTimeScale());
 
64
         */
 
65
        mFMM->SetMaximumFlySpeed(100);
 
66
        mFMM->SetMaximumTurnSpeed(1000);
 
67
 
 
68
        std::vector< dtGame::GameActorProxy* > allGameActors;
 
69
        GetGameManager()->GetAllGameActors(allGameActors);
 
70
 
 
71
        if (index < 0) noRobots = true;
 
72
        if (index >= (int)allGameActors.size()) noRobots = true;
 
73
 
 
74
        //Set Camera position and drop out of here if there are no robots
 
75
        if (noRobots) {
 
76
                dtCore::Transform xform;
 
77
                osg::Vec3 origin = osg::Vec3(0,0,0);
 
78
                osg::Vec3 up = osg::Vec3(0,0,1);
 
79
                xform.Set(camPos, origin, up);
 
80
                GetGameManager()->GetApplication().GetCamera()->SetTransform(xform);
 
81
                mFMM->SetTarget(GetGameManager()->GetApplication().GetCamera());
 
82
                mTrp = new dtCore::Tripod(GetGameManager()->GetApplication().GetCamera());
 
83
                mTrp->SetTetherMode(dtCore::Tripod::TETHER_WORLD_REL);
 
84
                mTrp->SetScale(CAM_SCALE,CAM_SCALE,CAM_SCALE,CAM_SCALE,CAM_SCALE,CAM_SCALE);
 
85
                mTrp->SetCamera(NULL);
 
86
                return;
 
87
        }
 
88
 
 
89
        //Create a Tripod and attach it to the robot which is specified
 
90
        RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[index]->GetGameActor());
 
91
        mTrp = new dtCore::Tripod(GetGameManager()->GetApplication().GetCamera(), actor.getBodies().at(0).get());
 
92
        mTrp->SetTetherMode(dtCore::Tripod::TETHER_WORLD_REL);
 
93
        mTrp->SetScale(CAM_SCALE,CAM_SCALE,CAM_SCALE,CAM_SCALE,CAM_SCALE,CAM_SCALE);
 
94
 
 
95
        //      setCamera();
 
96
        //}
 
97
 
 
98
        //void SimulationCameraController::setCamera() { };
 
99
        if(freeCamState)
 
100
        {
 
101
                mTrp->SetCamera(NULL);
 
102
                mFMM->SetTarget(GetGameManager()->GetApplication().GetCamera());
 
103
        }
 
104
        else
 
105
        {
 
106
                //Set initial Tripod Offset
 
107
                dtCore::Transform tx(-40.0f, 0.0f, 40.0f, 0.0f, 0.0f, 0.0f);
 
108
                osg::Vec3 hpr;
 
109
                tx.GetRotation(hpr);
 
110
                mTrp->SetOffset(camPos, hpr);
 
111
                mTrp->SetLookAtTarget(actor.getBodies().at(0).get());
 
112
                mTrp->SetCamera(GetGameManager()->GetApplication().GetCamera());
 
113
                mFMM->SetTarget(NULL);
 
114
        }
 
115
}
 
116
 
 
117
/**
 
118
 * The size of the viewport depends on the existence of the GUI. If there is a GUI then
 
119
 * the viewport becomes smaller. It starts then namely at (0,0) and it ends at 3/4th of
 
120
 * the width, and 95% of the height.
 
121
 *
 
122
 * @param fullwindow true indicates that there is no GUI and the viewport fill the
 
123
 * window, false means that it will be limited to 3/4 of the width and 95% of the height
 
124
 */
 
125
void SimulationCameraController::setViewport(bool fullwindow=true) {
 
126
        dtCore::Transform xform;
 
127
        osg::Vec3 robotPos = osg::Vec3(0,0,0); //actor.getTranslation();
 
128
        osg::Vec3 up            = osg::Vec3(0,0,1);
 
129
        xform.Set(camPos, robotPos, up);
 
130
        GetGameManager()->GetApplication().GetCamera()->SetTransform(xform);
 
131
        if (!fullwindow) {
 
132
                dtCore::Camera *camera = GetGameManager()->GetApplication().GetCamera();
 
133
                dtCore::RefPtr<osg::Viewport> viewport = camera->GetOSGCamera()->getViewport();
 
134
 
 
135
                float x = viewport->x();
 
136
                float y = viewport->y();
 
137
                float width = viewport->width() * 0.75;
 
138
                float height = viewport->height() * 0.95;
 
139
 
 
140
                std::ostringstream msg; msg.clear(); msg.str("");
 
141
                msg.clear(); msg.str("");
 
142
                msg << "Window [x,y,width,height] = [" << x << "," << y << "," << width << "," << height << "]";
 
143
                LOG_INFO(msg.str());
 
144
                camera->GetOSGCamera()->setViewport(new osg::Viewport(x, y, width, height));
 
145
        }
 
146
}
 
147
 
 
148
//////////////////////////////////////////////////////////////////////////
 
149
void SimulationCameraController::sendMessageToAll(dtDAL::GameEvent &event)
 
150
{
 
151
        dtCore::RefPtr<dtGame::GameEventMessage> eventMsg;
 
152
        GetGameManager()->GetMessageFactory().CreateMessage(dtGame::MessageType::INFO_GAME_EVENT, eventMsg);
 
153
 
 
154
        eventMsg->SetGameEvent(event);
 
155
        GetGameManager()->SendMessage(*eventMsg);
 
156
}
 
157
 
 
158
 
 
159
//////////////////////////////////////////////////////////////////////////
 
160
void SimulationCameraController::ProcessMessage(const dtGame::Message& message)
 
161
{
 
162
 
 
163
        if (message.GetMessageType() == dtGame::MessageType::TICK_LOCAL)
 
164
        {
 
165
                //               SimulationRobotSelectionComponent *comp = static_cast<SimulationRobotSelectionComponent*>(GetGameManager()->GetComponentByName("SimulationRobotSelectionComponent"));
 
166
                //               int index = comp->getRobFocIndex();
 
167
                SimulationArenaComponent *comp = static_cast<SimulationArenaComponent*>(GetGameManager()->GetComponentByName(SimulationArenaComponent::DEFAULT_NAME));
 
168
                int index = comp->getRobFocIndex();
 
169
 
 
170
 
 
171
                if(index!=-1) {
 
172
                        std::vector< dtGame::GameActorProxy* > allGameActors;
 
173
                        GetGameManager()->GetAllGameActors(allGameActors);
 
174
                        // RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[index]->GetGameActor());
 
175
 
 
176
                        dtCore::RefPtr<RobotActorBase> selectedActor = comp->getSelectedRobot();
 
177
 
 
178
                        if (selectedActor)
 
179
                                if(selectedActor->getBodies().size()>0)
 
180
                                        mTrp->SetLookAtTarget(selectedActor->getBodies().at(0).get());
 
181
                }
 
182
 
 
183
                if (screenShotToggle) {
 
184
 
 
185
                        float simTime = counter; //GetGameManager()->GetSimTimeSinceStartup();
 
186
                        counter++;
 
187
 
 
188
                        LOG_INFO("saving screenshot");
 
189
 
 
190
                        std::ostringstream t2;
 
191
                        t2 << "screenshots/srCore_";
 
192
 
 
193
                        int potenz = 1;
 
194
                        for (int i=0; i<5; i++)
 
195
                        {
 
196
                                if (simTime < potenz) t2 << "0";
 
197
                                potenz *=10;
 
198
                        }
 
199
 
 
200
                        t2 << simTime << ".jpeg";
 
201
 
 
202
                        dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
 
203
 
 
204
                        //find the camera which will be drawn the latest
 
205
                        int topView = 0;
 
206
 
 
207
                        for (int i=0; i<(int)GA->GetNumberOfViews(); i++) {
 
208
                                if (GA->GetView(topView) < GA->GetView(i))
 
209
                                        topView = i;
 
210
                        }
 
211
 
 
212
                        GA->GetView(topView)->GetCamera()->GetOSGCamera()->setFinalDrawCallback(mScreenShotTaker);
 
213
                        mScreenShotTaker->takeScreenShot(t2.str());
 
214
 
 
215
                }
 
216
 
 
217
        }
 
218
 
 
219
        if (message.GetMessageType() == dtGame::MessageType::INFO_GAME_EVENT)
 
220
        {
 
221
                const dtGame::GameEventMessage &eventMsg = static_cast<const dtGame::GameEventMessage&>(message);
 
222
 
 
223
                if(eventMsg.GetGameEvent() != NULL)
 
224
                {
 
225
 
 
226
                        if(eventMsg.GetGameEvent()->GetName() == TIME_SCALE_CHANGED ) {
 
227
 
 
228
                                mFMM->SetMaximumFlySpeed(10 / GetGameManager()->GetTimeScale());
 
229
                                mFMM->SetMaximumTurnSpeed(10 / GetGameManager()->GetTimeScale());
 
230
 
 
231
                        }
 
232
                        if(eventMsg.GetGameEvent()->GetName() == MOUSE_IN_GUI )
 
233
                        {
 
234
                                mFMM->SetEnabled(false);
 
235
                        }
 
236
                        else if(eventMsg.GetGameEvent()->GetName() == MOUSE_IN_SIM )
 
237
                        {
 
238
                                mFMM->SetEnabled(true);
 
239
                        }
 
240
                        //else if(eventMsg.GetGameEvent()->GetName() == CHANGE_ROBOT_SELECTION )
 
241
                        else if(eventMsg.GetGameEvent()->GetName() == ROBOT_SELECTION_CHANGED )
 
242
                        {
 
243
                                //                         SimulationRobotSelectionComponent *comp = static_cast<SimulationRobotSelectionComponent*>(GetGameManager()->GetComponentByName("SimulationRobotSelectionComponent"));
 
244
                                //                         int index = comp->getRobFocIndex();
 
245
                                SimulationArenaComponent *comp = static_cast<SimulationArenaComponent*>(GetGameManager()->GetComponentByName(SimulationArenaComponent::DEFAULT_NAME));
 
246
                                int index = comp->getRobFocIndex();
 
247
 
 
248
                                if(index!=-1)
 
249
                                {
 
250
                                        std::vector< dtGame::GameActorProxy* > allGameActors;
 
251
                                        GetGameManager()->GetAllGameActors(allGameActors);
 
252
                                        // RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[index]->GetGameActor());
 
253
 
 
254
                                        dtCore::RefPtr<RobotActorBase> selectedActor = comp->getSelectedRobot();
 
255
 
 
256
                                        if (selectedActor)
 
257
                                                if((!freeCamState) && (selectedActor->getBodies().size()>0))
 
258
                                                {
 
259
                                                        dtCore::Object *body = selectedActor->getBodies().at(0).get();
 
260
                                                        mTrp->SetAttachToTransformable(body);                                           //change the target of the tripod
 
261
                                                        mTrp->SetLookAtTarget(body);
 
262
                                                }
 
263
                                }
 
264
                                else
 
265
                                {
 
266
                                        mTrp->SetAttachToTransformable(NULL);
 
267
                                        mTrp->SetLookAtTarget(NULL);
 
268
                                }
 
269
                        }
 
270
                        else if(eventMsg.GetGameEvent()->GetName() == CHANGE_CAMERA_MODE )
 
271
                        {
 
272
                                if(!freeCamState)
 
273
                                {
 
274
                                        LOG_INFO("Switch from Tripod to FreeCam mode");
 
275
                                        mTrp->SetCamera(NULL);
 
276
                                        mFMM->SetTarget(GetGameManager()->GetApplication().GetCamera());
 
277
                                        freeCamState = true;
 
278
                                        //                                      setCamera();
 
279
                                }
 
280
                                else
 
281
                                {
 
282
                                        LOG_INFO("Switch from FreeCam to Tripod mode");
 
283
                                        dtCore::Transform trfTrp;
 
284
                                        dtCore::Transform trfAct;
 
285
                                        osg::Vec3 xyzTrp;
 
286
                                        osg::Vec3 xyzAct;
 
287
                                        osg::Vec3 hpr;
 
288
 
 
289
                                        mFMM->SetTarget(NULL);
 
290
 
 
291
                                        if (mTrp->GetAttachedTransformable() != NULL) {
 
292
                                                GetGameManager()->GetApplication().GetCamera()->GetTransform(trfTrp, dtCore::Transformable::ABS_CS);
 
293
                                                trfTrp.Get(xyzTrp, hpr);
 
294
                                                mTrp->GetAttachedTransformable()->GetTransform(trfAct, dtCore::Transformable::ABS_CS);
 
295
                                                trfAct.Get(xyzAct, hpr);
 
296
                                                mTrp->SetOffset(xyzTrp.x() - xyzAct.x(), xyzTrp.y() - xyzAct.y(), xyzTrp.z() - xyzAct.z(), 0, 0, 0);
 
297
                                        }
 
298
                                        mTrp->SetCamera(GetGameManager()->GetApplication().GetCamera());
 
299
 
 
300
                                        freeCamState = false;
 
301
                                        //                                      setCamera();
 
302
                                }
 
303
                        }
 
304
                }
 
305
        }
 
306
}
 
307