00001
00007 #include "SO3ViewPort.h"
00008 #include "SO3Window.h"
00009 #include <scol.h>
00010 #include "../SO3Material/SO3Compositor.h"
00011 #include "../SO3Renderer/SO3Root.h"
00012 #include "../SO3SceneGraph/SO3Camera.h"
00013 #include "../SO3SceneGraph/SO3Entity.h"
00014 #include "../SO3SceneGraph/SO3Scene.h"
00015 #include "../SO3Utils/SO3ConversionTools.h"
00016 #include "../SO3SceneGraph/SO3SubEntitySelectorBuffer.h"
00017 #include "../SO3Utils/SO3RayCast.h"
00018
00019 namespace SO3
00020 {
00021
00022 SViewPort::SViewPort()
00023 {
00024 scolWindow = 0;
00025 }
00026
00027 SViewPort::SViewPort(SWindow* parent, SCamera* camera, int priority, float x, float y, float w, float h)
00028 {
00029 scolWindow = parent;
00030 O3ViewPort = const_cast <Ogre::RenderWindow*> (const_cast <SWindow*> (scolWindow)->GetOgreRenderWindowPointer())->addViewport(camera->GetOgreCameraPointer(), priority, x, y, w, h);
00031 bClearColor = true;
00032 bClearDepth = true;
00033
00034 currentWindow = parent;
00035 scolCamera = camera;
00036
00037
00038 O3ViewPort->setVisibilityMask(0xfff0);
00039
00040 if (scolCamera != 0)
00041 {
00042 camera->SetCurrentViewPort(this);
00043 }
00044 }
00045
00046 SViewPort::~SViewPort()
00047 {
00048
00049 ClearCompositors();
00050
00051
00052 #if OGRE_VERSION >= ((1 << 16) | (7 << 8) | 0)
00053 if(O3ViewPort->getZOrder() == 0)
00054 scolWindow->SetStereoMode(SWindow::SO3_NONE_STEREO, 0, 0);
00055 #endif
00056
00057 if (scolCamera != 0)
00058 scolCamera->SetCurrentViewPort(0);
00059
00060
00061 const_cast <Ogre::RenderWindow*> (const_cast <SWindow*> (scolWindow)->GetOgreRenderWindowPointer())->removeViewport(O3ViewPort->getZOrder());
00062 O3ViewPort = 0;
00063 }
00064
00065 Ogre::Viewport* SViewPort::GetOgreViewPortPointer()
00066 {
00067 return O3ViewPort;
00068 }
00069
00070 const SWindow* SViewPort::GetParentWindow() const
00071 {
00072 return scolWindow;
00073 }
00074
00075 std::string SViewPort::GetMaterialScheme()
00076 {
00077 return O3ViewPort->getMaterialScheme();
00078 }
00079
00080 void SViewPort::SetMaterialScheme(std::string existingScheme)
00081 {
00082 O3ViewPort->setMaterialScheme(existingScheme);
00083 }
00084
00085 void SViewPort::SetPositionAndSize(float x, float y, float w, float h)
00086 {
00087 O3ViewPort->setDimensions(static_cast <Ogre::Real> (x), static_cast <Ogre::Real> (y), static_cast <Ogre::Real> (w), static_cast <Ogre::Real> (h));
00088 scolWindow->RefreshSelector();
00089 }
00090
00091 void SViewPort::ResizeToWindow(float w, float h)
00092 {
00093 int rw = scolWindow->GetWidth();
00094 int rh = scolWindow->GetHeight();
00095
00096 if(rw <= 0)
00097 rw = 1;
00098
00099 if(rh <= 0)
00100 rh = 1;
00101
00102 MMechostr(MSKDEBUG,"SO3ViewportResize %i %i %f %f\n", rw, rh, (float)w / (float)rw, (float)h / (float)rh);
00103 SetPositionAndSize(O3ViewPort->getLeft(), O3ViewPort->getTop(), (float)w / (float)rw, (float)h / (float)rh);
00104 scolWindow->RefreshSelector();
00105 }
00106
00107 float SViewPort::GetLeft()
00108 {
00109 return O3ViewPort->getLeft();
00110 }
00111
00112 float SViewPort::GetTop()
00113 {
00114 return O3ViewPort->getTop();
00115 }
00116
00117 float SViewPort::GetWidth()
00118 {
00119 return O3ViewPort->getWidth();
00120 }
00121
00122 float SViewPort::GetHeight()
00123 {
00124 return O3ViewPort->getHeight();
00125 }
00126
00127 int SViewPort::GetLeftPixels()
00128 {
00129 return O3ViewPort->getActualLeft();
00130 }
00131
00132 int SViewPort::GetTopPixels()
00133 {
00134 return O3ViewPort->getActualTop();
00135 }
00136
00137 int SViewPort::GetWidthPixels()
00138 {
00139 return O3ViewPort->getActualWidth();
00140 }
00141
00142 int SViewPort::GetHeightPixels()
00143 {
00144 return O3ViewPort->getActualHeight();
00145 }
00146
00147 int SViewPort::GetPriority()
00148 {
00149 return O3ViewPort->getZOrder();
00150 }
00151
00152 bool SViewPort::GetShadowsEnabled()
00153 {
00154 return O3ViewPort->getShadowsEnabled();
00155 }
00156
00157 void SViewPort::SetShadowsEnabled(bool enableShadows)
00158 {
00159 O3ViewPort->setShadowsEnabled(enableShadows);
00160 }
00161
00162 void SViewPort::SetBackgroundColour(int backgroundColor)
00163 {
00164 O3ViewPort->setBackgroundColour(ConversionTools::ScolToOgreColorRGBA(backgroundColor));
00165 }
00166
00167 int SViewPort::GetBackgroundColor()
00168 {
00169 return ConversionTools::OgreToScolColorRGBA(O3ViewPort->getBackgroundColour());
00170 }
00171
00172 SCamera* SViewPort::GetCamera()
00173 {
00174 return scolCamera;
00175 }
00176
00177 void SViewPort::SetCamera(SCamera* camera)
00178 {
00179
00180 if (scolCamera)
00181 scolCamera->SetCurrentViewPort(0);
00182
00183
00184 scolCamera = camera;
00185
00186 if (camera == 0)
00187 {
00188 O3ViewPort->setCamera(0);
00189 }
00190 else
00191 {
00192 O3ViewPort->setCamera(camera->GetOgreCameraPointer());
00193 camera->SetCurrentViewPort(this);
00194
00195
00196 if ((O3ViewPort->getZOrder() == 0) && (scolWindow->GetStereoMode() != SWindow::SO3_NONE_STEREO))
00197 {
00198 float eyespacing = scolWindow->GetStereoEyesSpacing();
00199 float focallength = scolWindow->GetStereoFocalLength();
00200 SWindow::StereoMode stmode = scolWindow->GetStereoMode();
00201
00202 scolWindow->SetStereoMode(stmode, eyespacing, focallength);
00203 }
00204 }
00205
00206
00207 try
00208 {
00209 O3ViewPort->update();
00210 }
00211 catch (Ogre::Exception& e)
00212 {
00213 if (e.getNumber() != Ogre::Exception::ERR_INTERNAL_ERROR)
00214 throw e;
00215 }
00216
00217 scolWindow->RefreshSelector();
00218 }
00219
00220 bool SViewPort::GetClearDepth()
00221 {
00222 return bClearDepth;
00223 }
00224
00225 void SViewPort::SetClearDepth(bool value)
00226 {
00227
00228 bClearDepth = value;
00229 bool bClear = value;
00230
00231
00232 int mode = Ogre::FBT_DEPTH;
00233 if(bClearColor && !bClearDepth)
00234 {
00235 mode = Ogre::FBT_COLOUR;
00236 bClear = true;
00237 }
00238 else if(bClearColor && bClearDepth)
00239 {
00240 mode = mode|Ogre::FBT_COLOUR;
00241 bClear = true;
00242 }
00243 O3ViewPort->setClearEveryFrame(bClear, mode);
00244 scolWindow->RefreshSelector();
00245 }
00246
00247 bool SViewPort::GetClearColor()
00248 {
00249 return bClearColor;
00250 }
00251
00252 void SViewPort::SetClearColor(bool value)
00253 {
00254
00255 bClearColor = value;
00256 bool bClear = value;
00257
00258
00259 int mode = Ogre::FBT_COLOUR;
00260 if(bClearDepth && !bClearColor)
00261 {
00262 mode = Ogre::FBT_DEPTH;
00263 bClear = true;
00264 }
00265 else if(bClearDepth && bClearColor)
00266 {
00267 mode = mode|Ogre::FBT_DEPTH;
00268 bClear = true;
00269 }
00270 O3ViewPort->setClearEveryFrame(bClear, mode);
00271 scolWindow->RefreshSelector();
00272 }
00273
00274 void SViewPort::SetSkyEnable(bool value)
00275 {
00276 O3ViewPort->setSkiesEnabled(value);
00277 }
00278
00279 bool SViewPort::GetSkyEnable()
00280 {
00281 return O3ViewPort->getSkiesEnabled();
00282 }
00283
00284 void SViewPort::SetOverlayEnable(bool value)
00285 {
00286 O3ViewPort->setOverlaysEnabled(value);
00287 }
00288
00289 bool SViewPort::GetOverlayEnable()
00290 {
00291 return O3ViewPort->getOverlaysEnabled();
00292 }
00293
00294 void SViewPort::SetVisibilityMask(Ogre::uint32 mask)
00295 {
00296 O3ViewPort->setVisibilityMask(mask);
00297 }
00298
00299 Ogre::uint32 SViewPort::GetVisibilityMask()
00300 {
00301 return O3ViewPort->getVisibilityMask();
00302 }
00303
00304 void SViewPort::Update()
00305 {
00306 try
00307 {
00308 O3ViewPort->update();
00309 }
00310 catch (Ogre::Exception& e)
00311 {
00312 if (e.getNumber() != Ogre::Exception::ERR_INTERNAL_ERROR)
00313 throw e;
00314 }
00315 }
00316
00317 SCompositor* SViewPort::GetCompositor(std::string compositorName)
00318 {
00319 SCompositorMap::iterator iCompositorSearched = compList.find(compositorName);
00320 if(iCompositorSearched != compList.end())
00321 return iCompositorSearched->second;
00322 else
00323 return 0;
00324 }
00325
00326 const SCompositorMap& SViewPort::GetCompositorList() const
00327 {
00328 return compList;
00329 }
00330
00331 SCompositor* SViewPort::CreateCompositor(std::string newCompositorName, std::string newSchemeName)
00332 {
00333 SCompositor* newCompositor = GetCompositor(newCompositorName);
00334 if (newCompositor == 0)
00335 {
00336 newCompositor = new SCompositor(this, newCompositorName, newSchemeName);
00337 AddCompositor(newCompositor);
00338 }
00339 return newCompositor;
00340 }
00341
00342 void SViewPort::DeleteCompositor(std::string existingCompositorName)
00343 {
00344 DeleteCompositor(GetCompositor(existingCompositorName));
00345 }
00346
00347 void SViewPort::DeleteCompositor(SCompositor* existingCompositor)
00348 {
00349 RemoveCompositor(existingCompositor);
00350 SAFE_DELETE(existingCompositor);
00351 }
00352
00353 void SViewPort::ClearCompositors()
00354 {
00355
00356 SCompositorMap::iterator iCompositorList = compList.begin();
00357
00358
00359 while (iCompositorList != compList.end())
00360 {
00361 SAFE_DELETE(iCompositorList->second);
00362 iCompositorList++;
00363 }
00364 compList.clear();
00365 }
00366
00367 void SViewPort::AddCompositor(SCompositor* existingCompositor)
00368 {
00369 string name = existingCompositor->GetName();
00370 SCompositorMap::iterator iCompositorSearched = compList.find(name);
00371 if(iCompositorSearched == compList.end())
00372 {
00373 compList.insert(SCompositorMap::value_type(name, existingCompositor));
00374 }
00375 else
00376 {
00377
00378 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Can not add Compositor named \""+ name +"\", an element with the same name already exist!", "SViewPort::AddCompositor");
00379 }
00380 }
00381
00382 void SViewPort::RemoveCompositor(SCompositor* existingCompositor)
00383 {
00384 RemoveCompositor(existingCompositor->GetName());
00385 }
00386
00387 void SViewPort::RemoveCompositor(std::string compositorName)
00388 {
00389 SCompositorMap::iterator iCompositorSearched = compList.find(compositorName);
00390 if(iCompositorSearched != compList.end())
00391 {
00392 compList.erase(iCompositorSearched);
00393 }
00394 else
00395 {
00396
00397 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Can not remove Compositor named \""+ compositorName +"\", element not found!", "SViewPort::RemoveCompositor");
00398 }
00399 }
00400
00401 SRaycastResult SViewPort::RayCast(int pixelsX, int pixelsY, SSubEntitySelectorBuffer* subEntitySelector)
00402 {
00403 SRaycastResult raycastReturn;
00404 assert(subEntitySelector != 0);
00405
00406
00407 Ogre::SubEntity* subEntity = 0;
00408
00409 if (subEntitySelector != 0)
00410 subEntity = subEntitySelector->GetSelectedSubEntity(pixelsX, pixelsY);
00411
00412 if(subEntity != 0)
00413 {
00414 if(scolCamera != 0)
00415 {
00416 float xR = (static_cast <float> (pixelsX - GetLeftPixels()))/(static_cast <float> (GetWidthPixels()));
00417 float yR = (static_cast <float> (pixelsY - GetTopPixels()))/(static_cast <float> (GetHeightPixels()));
00418 raycastReturn = SRaycast::Cast(scolCamera, xR, yR, subEntity);
00419 }
00420 }
00421
00422 return raycastReturn;
00423 }
00424
00425 SRaycastResult SViewPort::RayCast(int pixelsX, int pixelsY)
00426 {
00427 return currentWindow->RayCast(O3ViewPort->getActualLeft() + pixelsX, O3ViewPort->getActualTop() + pixelsY);
00428 }
00429
00430
00431
00432 }