2
* Robot3D Physical realistic 3D simulator for robots
4
* @license GNU Lesser General Public License
6
* @author Adrian Friebel
7
* @author Anne van Rossum
11
#include <srCore/simulationCameraController.h>
12
#include <srCore/simulationEvents.h>
15
#include <dtUtil/log.h>
17
#define CAM_SCALE 0.6 //1=direct following, 0=no following
19
using namespace srCore;
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.
25
SimulationCameraController::SimulationCameraController(const std::string &name, osg::Vec3f camPos, bool camState)
26
: dtGame::GMComponent(name),
28
freeCamState(camState),
30
dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
31
mScreenShotTaker = new ScreenShotCallback(RGB_CAMERA, GA->GetCamera()->GetOSGCamera()->getViewport());
35
* Destroys the camera.
37
SimulationCameraController::~SimulationCameraController() {
38
mTrp->SetAttachToTransformable(NULL);
39
delete mScreenShotTaker;
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
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.
53
void SimulationCameraController::init(dtCore::Mouse *mouse, dtCore::Keyboard *keyboard)
55
bool noRobots = false;
56
SimulationArenaComponent *comp = static_cast<SimulationArenaComponent*>(
57
GetGameManager()->GetComponentByName(SimulationArenaComponent::DEFAULT_NAME));
58
int index = comp->getRobFocIndex();
60
mFMM = new dtCore::FlyMotionModel(keyboard, mouse);
62
mFMM->SetMaximumFlySpeed(1000 / GetGameManager()->GetTimeScale());
63
mFMM->SetMaximumTurnSpeed(10000 / GetGameManager()->GetTimeScale());
65
mFMM->SetMaximumFlySpeed(100);
66
mFMM->SetMaximumTurnSpeed(1000);
68
std::vector< dtGame::GameActorProxy* > allGameActors;
69
GetGameManager()->GetAllGameActors(allGameActors);
71
if (index < 0) noRobots = true;
72
if (index >= (int)allGameActors.size()) noRobots = true;
74
//Set Camera position and drop out of here if there are no robots
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);
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);
98
//void SimulationCameraController::setCamera() { };
101
mTrp->SetCamera(NULL);
102
mFMM->SetTarget(GetGameManager()->GetApplication().GetCamera());
106
//Set initial Tripod Offset
107
dtCore::Transform tx(-40.0f, 0.0f, 40.0f, 0.0f, 0.0f, 0.0f);
110
mTrp->SetOffset(camPos, hpr);
111
mTrp->SetLookAtTarget(actor.getBodies().at(0).get());
112
mTrp->SetCamera(GetGameManager()->GetApplication().GetCamera());
113
mFMM->SetTarget(NULL);
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.
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
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);
132
dtCore::Camera *camera = GetGameManager()->GetApplication().GetCamera();
133
dtCore::RefPtr<osg::Viewport> viewport = camera->GetOSGCamera()->getViewport();
135
float x = viewport->x();
136
float y = viewport->y();
137
float width = viewport->width() * 0.75;
138
float height = viewport->height() * 0.95;
140
std::ostringstream msg; msg.clear(); msg.str("");
141
msg.clear(); msg.str("");
142
msg << "Window [x,y,width,height] = [" << x << "," << y << "," << width << "," << height << "]";
144
camera->GetOSGCamera()->setViewport(new osg::Viewport(x, y, width, height));
148
//////////////////////////////////////////////////////////////////////////
149
void SimulationCameraController::sendMessageToAll(dtDAL::GameEvent &event)
151
dtCore::RefPtr<dtGame::GameEventMessage> eventMsg;
152
GetGameManager()->GetMessageFactory().CreateMessage(dtGame::MessageType::INFO_GAME_EVENT, eventMsg);
154
eventMsg->SetGameEvent(event);
155
GetGameManager()->SendMessage(*eventMsg);
159
//////////////////////////////////////////////////////////////////////////
160
void SimulationCameraController::ProcessMessage(const dtGame::Message& message)
163
if (message.GetMessageType() == dtGame::MessageType::TICK_LOCAL)
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();
172
std::vector< dtGame::GameActorProxy* > allGameActors;
173
GetGameManager()->GetAllGameActors(allGameActors);
174
// RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[index]->GetGameActor());
176
dtCore::RefPtr<RobotActorBase> selectedActor = comp->getSelectedRobot();
179
if(selectedActor->getBodies().size()>0)
180
mTrp->SetLookAtTarget(selectedActor->getBodies().at(0).get());
183
if (screenShotToggle) {
185
float simTime = counter; //GetGameManager()->GetSimTimeSinceStartup();
188
LOG_INFO("saving screenshot");
190
std::ostringstream t2;
191
t2 << "screenshots/srCore_";
194
for (int i=0; i<5; i++)
196
if (simTime < potenz) t2 << "0";
200
t2 << simTime << ".jpeg";
202
dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
204
//find the camera which will be drawn the latest
207
for (int i=0; i<(int)GA->GetNumberOfViews(); i++) {
208
if (GA->GetView(topView) < GA->GetView(i))
212
GA->GetView(topView)->GetCamera()->GetOSGCamera()->setFinalDrawCallback(mScreenShotTaker);
213
mScreenShotTaker->takeScreenShot(t2.str());
219
if (message.GetMessageType() == dtGame::MessageType::INFO_GAME_EVENT)
221
const dtGame::GameEventMessage &eventMsg = static_cast<const dtGame::GameEventMessage&>(message);
223
if(eventMsg.GetGameEvent() != NULL)
226
if(eventMsg.GetGameEvent()->GetName() == TIME_SCALE_CHANGED ) {
228
mFMM->SetMaximumFlySpeed(10 / GetGameManager()->GetTimeScale());
229
mFMM->SetMaximumTurnSpeed(10 / GetGameManager()->GetTimeScale());
232
if(eventMsg.GetGameEvent()->GetName() == MOUSE_IN_GUI )
234
mFMM->SetEnabled(false);
236
else if(eventMsg.GetGameEvent()->GetName() == MOUSE_IN_SIM )
238
mFMM->SetEnabled(true);
240
//else if(eventMsg.GetGameEvent()->GetName() == CHANGE_ROBOT_SELECTION )
241
else if(eventMsg.GetGameEvent()->GetName() == ROBOT_SELECTION_CHANGED )
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();
250
std::vector< dtGame::GameActorProxy* > allGameActors;
251
GetGameManager()->GetAllGameActors(allGameActors);
252
// RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[index]->GetGameActor());
254
dtCore::RefPtr<RobotActorBase> selectedActor = comp->getSelectedRobot();
257
if((!freeCamState) && (selectedActor->getBodies().size()>0))
259
dtCore::Object *body = selectedActor->getBodies().at(0).get();
260
mTrp->SetAttachToTransformable(body); //change the target of the tripod
261
mTrp->SetLookAtTarget(body);
266
mTrp->SetAttachToTransformable(NULL);
267
mTrp->SetLookAtTarget(NULL);
270
else if(eventMsg.GetGameEvent()->GetName() == CHANGE_CAMERA_MODE )
274
LOG_INFO("Switch from Tripod to FreeCam mode");
275
mTrp->SetCamera(NULL);
276
mFMM->SetTarget(GetGameManager()->GetApplication().GetCamera());
282
LOG_INFO("Switch from FreeCam to Tripod mode");
283
dtCore::Transform trfTrp;
284
dtCore::Transform trfAct;
289
mFMM->SetTarget(NULL);
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);
298
mTrp->SetCamera(GetGameManager()->GetApplication().GetCamera());
300
freeCamState = false;