Project

General

Profile

SO3Engine
SO3ViewPort.cpp
Go to the documentation of this file.
1
14#include "SO3Utils/SO3RayCast.h"
18
19namespace SO3
20{
21
22 SViewPort::SViewPort()
23 {
24 scolWindow = 0;
25 }
26
27 SViewPort::SViewPort(SWindow* parent, SCamera* camera, int priority, const float& x, const float& y, const float& w, const float& h)
28 {
29 scolWindow = parent;
30
31 Ogre::RenderWindow* renderWindow = const_cast <Ogre::RenderWindow*> (const_cast <SWindow*> (scolWindow)->GetOgreRenderWindowPointer());
32 priority = renderWindow->getNumViewports();
33 O3ViewPort = renderWindow->addViewport(camera->GetOgreCameraPointer(), priority, x, y, w, h);
34 scolCamera = camera;
35 bClearColor = true;
36 bClearDepth = true;
37 bSSAOEnable = false;
38
39#ifdef SO3_BUILD_DEFERRED
40 bDeferredEnable = false;
41 bDeferredAntiAliasingMode = SDeferredShading::SO3_DEFERRED_AA_NONE;
42#endif
43
44 // set default visibility mask to 0xfffe keep the last bit to be invisible by default in viewports
45 O3ViewPort->setVisibilityMask(SO3_VISIBILITY_MASK_DEFAULT);
46
47 try
48 {
49 ssaoHandler = new SSsaoHandler(this);
50 }
51 catch(Ogre::Exception& e)
52 {
53 ssaoHandler = 0;
54 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage("An exception has occurred: " + e.getDescription());
55 }
56
57 try
58 {
59 hdrHandler = new SHdrHandler(this);
60 }
61 catch (Ogre::Exception& e)
62 {
63 hdrHandler = 0;
64 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage("An exception has occurred: " + e.getDescription());
65 }
66
67 if (scolCamera != 0)
68 {
69 camera->SetCurrentViewPort(this);
70 }
71
72 O3ViewPort->addListener(this);
73 O3ViewPort->setMaterialScheme(Ogre::MSN_SHADERGEN);
74 }
75
77 {
78 O3ViewPort->removeListener(this);
79
80#ifdef SO3_BUILD_DEFERRED
81 SetDeferredEnable(false);
82#endif
83
84 SAFE_DELETE(ssaoHandler);
85 SAFE_DELETE(hdrHandler);
86
87 // Clear all the compositors
89
90 if (scolCamera != 0)
91 scolCamera->SetCurrentViewPort(0);
92
93 // removing this viewport from the parent window.
94 const_cast <Ogre::RenderWindow*> (const_cast <SWindow*> (scolWindow)->GetOgreRenderWindowPointer())->removeViewport(O3ViewPort->getZOrder());
95 O3ViewPort = 0;
96 }
97
99 {
100 return O3ViewPort;
101 }
102
104 {
105 return scolWindow;
106 }
107
109 {
110 std::string schemeName = O3ViewPort->getMaterialScheme();
111
112 if (schemeName.find("ShaderGenerator") != std::string::npos)
113 schemeName = schemeName.erase(0, 15);
114
115 return schemeName;
116 }
117
118 void SViewPort::SetMaterialScheme(const std::string& existingScheme)
119 {
120 if (existingScheme.empty() || existingScheme == "Default" || existingScheme == "DefaultScheme")
121 O3ViewPort->setMaterialScheme(Ogre::MSN_SHADERGEN);
122 else
123 {
124 std::string newScheme = existingScheme;
125 O3ViewPort->setMaterialScheme(newScheme);
126 }
127
128 scolWindow->SynchStereoViewportSetup(this);
129 }
130
131 void SViewPort::SetPositionAndSize(const float& x, const float& y, const float& w, const float& h)
132 {
133 O3ViewPort->setDimensions(static_cast <Ogre::Real> (x), static_cast <Ogre::Real> (y), static_cast <Ogre::Real> (w), static_cast <Ogre::Real> (h));
134 scolWindow->RefreshSelector();
135 }
136
137 void SViewPort::ResizeToWindow(const float& w, const float& h)
138 {
139 int rw = scolWindow->GetWidth();
140 int rh = scolWindow->GetHeight();
141
142 if (rw <= 0)
143 rw = 1;
144
145 if (rh <= 0)
146 rh = 1;
147
148 MMechostr(MSKDEBUG, "SO3ViewportResize %i %i %f %f\n", rw, rh, (float)w / (float)rw, (float)h / (float)rh);
149 SetPositionAndSize(O3ViewPort->getLeft(), O3ViewPort->getTop(), (float)w / (float)rw, (float)h / (float)rh);
150 scolWindow->RefreshSelector();
151 }
152
154 {
155 return O3ViewPort->getLeft();
156 }
157
159 {
160 return O3ViewPort->getTop();
161 }
162
164 {
165 return O3ViewPort->getWidth();
166 }
167
169 {
170 return O3ViewPort->getHeight();
171 }
172
174 {
175 return O3ViewPort->getActualLeft();
176 }
177
179 {
180 return O3ViewPort->getActualTop();
181 }
182
184 {
185 return O3ViewPort->getActualWidth();
186 }
187
189 {
190 return O3ViewPort->getActualHeight();
191 }
192
194 {
195 return O3ViewPort->getZOrder();
196 }
197
199 {
200 return O3ViewPort->getShadowsEnabled();
201 }
202
203 void SViewPort::SetShadowsEnabled(const bool& enableShadows)
204 {
205 O3ViewPort->setShadowsEnabled(enableShadows);
206 scolWindow->SynchStereoViewportSetup(this, false, true);
207 }
208
209 void SViewPort::SetBackgroundColour(const int& backgroundColor)
210 {
211 O3ViewPort->setBackgroundColour(ConversionTools::ScolToOgreColorRGBA(backgroundColor));
212 scolWindow->SynchStereoViewportSetup(this);
213 }
214
216 {
217 return ConversionTools::OgreToScolColorRGBA(O3ViewPort->getBackgroundColour());
218 }
219
221 {
222 return scolCamera;
223 }
224
226 {
227 if (camera)
228 {
229 // disable viewport on previous camera
230 if (scolCamera)
231 {
232 //restore Stereo camera
233 scolWindow->RestoreStereoCamera(this);
234
235 //remove the shadow viewport
236 //scolCamera->GetParentScene()->GetShadowManager()->RemoveViewport(this);
237 scolCamera->SetCurrentViewPort(0);
238 }
239
240 O3ViewPort->setCamera(camera->GetOgreCameraPointer());
241
242 // last render target set the camera ratio, we need this to restore to the main viewport ratio
243 float cRatio = camera->GetAspectRatio();
244 if (camera->GetOgreCameraPointer()->getAutoAspectRatio())
245 cRatio = (float)O3ViewPort->getActualWidth() / (float)O3ViewPort->getActualHeight();
246
247 camera->SetCurrentViewPort(this);
248
249 // restore the correct ratio
250 camera->GetOgreCameraPointer()->setAspectRatio(cRatio);
251
252 //update Stereo camera
253 scolWindow->UpdateStereoCamera(this, camera->GetOgreCameraPointer());
254
255 //force RTSS update
256 Ogre::RTShader::ShaderGenerator::getSingletonPtr()->invalidateScheme(O3ViewPort->getMaterialScheme());
257
258 //hack for SSAO not handling camera correctly on app start
259 if (ssaoHandler && ssaoHandler->GetEnable())
260 {
261 ssaoHandler->SetEnable(false);
262 ssaoHandler->SetEnable(true);
263 }
264 }
265 else
266 {
267 // disable viewport on previous camera
268 if (scolCamera)
269 {
270 //restore Stereo camera
271 scolWindow->RestoreStereoCamera(this);
272 scolCamera->SetCurrentViewPort(0);
273 }
274
275 O3ViewPort->setCamera(0);
276 scolWindow->UpdateStereoCamera(this, 0);
277 }
278
279 // Now we can change our camera using the "normal way".
280 scolCamera = camera;
281
282 if (scolCamera)
283 scolWindow->RefreshSelector(true);
284
285 // Inform the camera
287
288 // Update viewport
289 /*try
290 {
291 O3ViewPort->update();
292 }
293 catch (Ogre::Exception& e)
294 {
295 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage("An exception has occurred: "+ e.getDescription());
296 }*/
297 }
298
299 bool SViewPort::UpdateStereoCompositorState(const Ogre::String compname, bool state)
300 {
301 return scolWindow->UpdateStereoCompositorState(this, compname, state);
302 }
303
304 void SViewPort::UpdateStereoCamera(Ogre::Camera* camera)
305 {
306 scolWindow->UpdateStereoCamera(this, camera);
307 }
308
310 {
311 scolWindow->RestoreStereoCamera(this);
312 }
313
314 void SViewPort::UpdateStereoCameraMatrix(Ogre::Camera* camera)
315 {
316 scolWindow->UpdateStereoCameraMatrix(this, camera);
317 }
318
320 {
321 scolWindow->UnregisterStereoViewport(this);
322 }
323
325 {
326 scolWindow->RegisterStereoViewport(this);
327 }
328
330 {
331 return scolWindow->IsStereoViewportRegistered(this);
332 }
333
335 {
336 return bClearDepth;
337 }
338
339 void SViewPort::SetClearDepth(const bool& value)
340 {
341 // keep the new depth state
342 bClearDepth = value;
343 bool bClear = value;
344
345 // Update viewport clearing settings.
346 int mode = Ogre::FBT_DEPTH;
347 if (bClearColor && !bClearDepth)
348 {
349 mode = Ogre::FBT_COLOUR;
350 bClear = true;
351 }
352 else if (bClearColor && bClearDepth)
353 {
354 mode = mode | Ogre::FBT_COLOUR;
355 bClear = true;
356 }
357 O3ViewPort->setClearEveryFrame(bClear, mode);
358 scolWindow->RefreshSelector();
359 }
360
362 {
363 return bClearColor;
364 }
365
366 void SViewPort::SetClearColor(const bool& value)
367 {
368 // keep the new color state
369 bClearColor = value;
370 bool bClear = value;
371
372 // Update viewport clearing settings.
373 int mode = Ogre::FBT_COLOUR;
374 if (bClearDepth && !bClearColor)
375 {
376 mode = Ogre::FBT_DEPTH;
377 bClear = true;
378 }
379 else if (bClearDepth && bClearColor)
380 {
381 mode = mode | Ogre::FBT_DEPTH;
382 bClear = true;
383 }
384 O3ViewPort->setClearEveryFrame(bClear, mode);
385 scolWindow->RefreshSelector();
386 }
387
388 void SViewPort::SetSkyEnable(const bool& value)
389 {
390 O3ViewPort->setSkiesEnabled(value);
391 scolWindow->SynchStereoViewportSetup(this);
392 }
393
395 {
396 return O3ViewPort->getSkiesEnabled();
397 }
398
399 void SViewPort::SetOverlayEnable(const bool& value)
400 {
401 O3ViewPort->setOverlaysEnabled(value);
402 scolWindow->SynchStereoViewportSetup(this, true);
403 }
404
406 {
407 return O3ViewPort->getOverlaysEnabled();
408 }
409
410#ifdef SO3_BUILD_DEFERRED
411 void SViewPort::SetDeferredEnable(const bool& value)
412 {
414 if (deferredManager != 0)
415 {
416 if (value)
417 {
418 try
419 {
420 deferredManager->RegisterViewport(this);
421 deferredManager->SetSSAO(this, bSSAOEnable);
422 deferredManager->SetAntiAliasing(this, bDeferredAntiAliasingMode);
423 }
424 catch (SException&)
425 {
426 }
427 }
428 else
429 {
430 try
431 {
432 deferredManager->UnregisterViewport(this);
433 }
434 catch (SException&)
435 {
436 }
437 }
438
439 bDeferredEnable = value;
440 }
441 }
442
444 {
445 return bDeferredEnable;
446 }
447
449 {
451 if (deferredManager != 0)
452 {
453 try
454 {
455 deferredManager->SetAntiAliasing(this, value);
456 }
457 catch (SException&)
458 {
459 }
460 }
461 bDeferredAntiAliasingMode = value;
462 }
463
468
469 void SViewPort::SetDeferredDebugMode(const int& value)
470 {
472 if (deferredManager != 0)
473 {
474 try
475 {
476 deferredManager->SetMode(static_cast<SDeferredShading::DeferredShadingMode>(value));
477 }
478 catch (Ogre::Exception&)
479 {
480 }
481 }
482 }
483
485 {
486 int currentDebugMode = 0;
487
489 if (deferredManager != 0)
490 {
491 currentDebugMode = static_cast<int>(deferredManager->GetMode());
492 }
493 return currentDebugMode;
494 }
495#endif
496
497 void SViewPort::SetSSAOEnable(const bool& value)
498 {
499 if (!ssaoHandler)
500 return;
501
502#ifdef SO3_BUILD_DEFERRED
504 if ((deferredManager != 0) && bDeferredEnable)
505 {
506 // Deferred rendering ssao.
507 try
508 {
509 deferredManager->SetSSAO(this, value);
510 }
511 catch (SException&)
512 {
513 }
514 }
515 else
516#endif
517 {
518 // Enable forward rendering SSAO
519 ssaoHandler->SetEnable(value);
520 }
521 bSSAOEnable = value;
522 }
523
525 {
526 return bSSAOEnable;
527 }
528
530 {
531 return ssaoHandler;
532 }
533
534 void SViewPort::SetHDREnable(const bool& value)
535 {
536 if (hdrHandler)
537 hdrHandler->SetEnable(value);
538 }
539
541 {
542 if (!hdrHandler)
543 return false;
544
545 return hdrHandler->GetEnable();
546 }
547
549 {
550 return hdrHandler;
551 }
552
553 void SViewPort::SetHDRBlurAmount(const float& value)
554 {
555 if (hdrHandler)
556 hdrHandler->SetBlurAmount(value);
557 }
558
559 void SViewPort::SetHDREffectAmount(const float& value)
560 {
561 if (hdrHandler)
562 hdrHandler->SetEffectAmount(value);
563 }
564
565 void SViewPort::SetHDRGamma(const float& value)
566 {
567 if (hdrHandler)
568 hdrHandler->SetGamma(value);
569 }
570
571 void SViewPort::SetVisibilityMask(const Ogre::uint32& mask)
572 {
573 O3ViewPort->setVisibilityMask(mask);
574 scolWindow->RefreshSelector();
575 scolWindow->SynchStereoViewportSetup(this);
576 }
577
579 {
580 return O3ViewPort->getVisibilityMask();
581 }
582
583 void SViewPort::SetVisibilityMaskIndexEnable(const Ogre::uint32 flagIndex, const bool enable)
584 {
585 assert((flagIndex > 0) && (flagIndex < 32));
586 Ogre::uint32 flag = 1 << flagIndex;
587 Ogre::uint32 flags = O3ViewPort->getVisibilityMask();
588 if (enable)
589 O3ViewPort->setVisibilityMask(flags | flag);
590 else
591 O3ViewPort->setVisibilityMask(flags & ~flag);
592
593 scolWindow->RefreshSelector();
594 scolWindow->SynchStereoViewportSetup(this);
595 }
596
597 bool SViewPort::GetVisibilityMaskIndexEnable(const Ogre::uint32 flagIndex)
598 {
599 assert((flagIndex > 0) && (flagIndex < 32));
600 Ogre::uint32 flag = 1 << flagIndex;
601 if ((flag & O3ViewPort->getVisibilityMask()) > 0)
602 return true;
603 else
604 return false;
605 }
606
608 {
609 try
610 {
611 O3ViewPort->update();
612 }
613 catch (Ogre::Exception& e)
614 {
615 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage("An exception has occurred: " + e.getDescription());
616 }
617 }
618
619 SCompositor* SViewPort::GetCompositor(const std::string& compositorName)
620 {
621 SCompositorMap::iterator iCompositorSearched = compList.find(compositorName);
622 if (iCompositorSearched != compList.end())
623 return iCompositorSearched->second;
624 else
625 return 0;
626 }
627
629 {
630 return compList;
631 }
632
633 SCompositor* SViewPort::CreateCompositor(const std::string& newCompositorName, const std::string& newSchemeName)
634 {
635 SCompositor* newCompositor = GetCompositor(newCompositorName);
636 if (newCompositor == 0)
637 {
638 newCompositor = new SCompositor(this, newCompositorName, newSchemeName);
639 AddCompositor(newCompositor);
640 }
641 return newCompositor;
642 }
643
644 void SViewPort::DeleteCompositor(const std::string& existingCompositorName)
645 {
646 DeleteCompositor(GetCompositor(existingCompositorName));
647 }
648
649 void SViewPort::DeleteCompositor(SCompositor* existingCompositor)
650 {
651 RemoveCompositor(existingCompositor);
652 SO3_SAFE_DELETE(existingCompositor);
653 }
654
656 {
657 // clear all compositors.
658 SCompositorMap::iterator iCompositorList = compList.begin();
659
660 // Do not use DeleteCompositor function here, cause this one use "RemoveCompositor" function, and it breaks the iterator!
661 while (iCompositorList != compList.end())
662 {
663 SO3_SAFE_DELETE(iCompositorList->second);
664 iCompositorList++;
665 }
666 compList.clear();
667 }
668
669 void SViewPort::AddCompositor(SCompositor* existingCompositor)
670 {
671 string name = existingCompositor->GetName();
672 SCompositorMap::iterator iCompositorSearched = compList.find(name);
673 if (iCompositorSearched == compList.end())
674 {
675 compList.insert(SCompositorMap::value_type(name, existingCompositor));
676 }
677 else
678 {
679 // Compositor already exist in the handled compositors list.
680 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Can not add Compositor named \"" + name + "\", an element with the same name already exist!", "SViewPort::AddCompositor");
681 }
682 }
683
684 void SViewPort::RemoveCompositor(SCompositor* existingCompositor)
685 {
686 RemoveCompositor(existingCompositor->GetName());
687 }
688
689 void SViewPort::RemoveCompositor(const std::string& compositorName)
690 {
691 SCompositorMap::iterator iCompositorSearched = compList.find(compositorName);
692 if (iCompositorSearched != compList.end())
693 {
694 compList.erase(iCompositorSearched);
695 }
696 else
697 {
698 // Compositor not found in the handled Compositor list
699 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Can not remove Compositor named \"" + compositorName + "\", element not found!", "SViewPort::RemoveCompositor");
700 }
701 }
702
703 bool SViewPort::GetScreenPosFromWorldPos(Ogre::Vector3 worldPosition, Ogre::Vector2 &screenPos)
704 {
705 if (!scolCamera)
706 return false;
707
708 Ogre::Camera* cam = scolCamera->GetOgreCameraPointer();
709
710 // Is the camera facing that point? If not, return false
711 Ogre::Plane cameraPlane = Ogre::Plane(Ogre::Vector3(cam->getDerivedOrientation().zAxis()), cam->getDerivedPosition());
712 if (cameraPlane.getSide(worldPosition) != Ogre::Plane::NEGATIVE_SIDE)
713 return false;
714
715 Ogre::Vector3 screenPoint = cam->getProjectionMatrix() * cam->getViewMatrix() * worldPosition;
716 float x = (screenPoint.x * 0.5f) + 0.5f;
717 float y = 1.0f - ((screenPoint.y * 0.5f) + 0.5f);
718
719 //out of screen
720 if ((x < 0.0f) || (x > 1.0f) || (y < 0.0f) || (y > 1.0f))
721 return false;
722
723 screenPos = Ogre::Vector2(O3ViewPort->getActualWidth() * x, O3ViewPort->getActualHeight() * y);
724
725 //position in viewport size not in screen
726 /*
727 #if defined(ANDROID) || defined(APPLE_IOS)
728 ToWindowPos(screenPos.x, screenPos.y);
729 #endif
730 */
731 return true;
732 }
734 SRaycastResult SViewPort::RayCast(const int& pixelsX, const int& pixelsY, SSubEntitySelectorBuffer* subEntitySelector)
735 {
736 SRaycastResult raycastReturn;
737
738 float xR = (static_cast <float> (pixelsX - GetLeftPixels())) / (static_cast <float> (GetWidthPixels()));
739 float yR = (static_cast <float> (pixelsY - GetTopPixels())) / (static_cast <float> (GetHeightPixels()));
740
741 //subentity raycast
742 if (subEntitySelector)
743 {
744 // First, use our selection buffer to find which sub-entity was hit
745 Ogre::SubEntity* subEntity = 0;
746
747 Ogre::Vector3 pos(0.0f, 0.0f, 0.0f);
748 float rayLenght = 0.0f;
749 if (subEntitySelector != 0)
750 {
751 subEntity = subEntitySelector->GetSelectedSubEntity(pixelsX, pixelsY);
752
753 if (subEntity && subEntitySelector->UseDepthBuffer())
754 {
755 float zdepth = subEntitySelector->GetSelectedZdepth(pixelsX, pixelsY);
756
757 Ogre::Camera* camera = O3ViewPort->getCamera();
758 if (camera)
759 {
760 // x & y are in range [-1; 1];
761 Ogre::Real nx = (2.0f * xR) - 1.0f;
762 Ogre::Real ny = 1.0f - (2.0f * yR);
763 Ogre::Vector3 nearPoint(nx, ny, -1.0f);
764 Ogre::Vector3 midPoint(nx, ny, 0.0f);
765
766 //get ray world position and direction
767 Ogre::Matrix4 invViewMat = (camera->getProjectionMatrix() * camera->getViewMatrix(true)).inverse();
768 Ogre::Vector3 rayOrigin = invViewMat * nearPoint;
769 Ogre::Vector3 rayDest = invViewMat * midPoint;
770 Ogre::Vector3 rayDirection = rayDest - rayOrigin;
771 rayDirection.normalise();
772
773 float farclip = camera->getFarClipDistance();
774 float nearclip = camera->getNearClipDistance();
775
776 // correct perspective
777 if (camera->getProjectionType() == Ogre::PT_PERSPECTIVE)
778 {
779 // non linear depth
780 Ogre::Vector4 viewpos = camera->getProjectionMatrixWithRSDepth().inverse() * Ogre::Vector4(nx, ny, zdepth, 1.0f);
781 zdepth = viewpos.z / viewpos.w;
782
783 //restore view distance from linear depth
784 //zdepth *= farclip - nearclip;
785
786 if (SRoot::getSingleton().GetRenderSystem() == SRoot::SO3_OPENGL3_RENDERER ||
787 SRoot::getSingleton().GetRenderSystem() == SRoot::SO3_OPENGL_RENDERER ||
788 SRoot::getSingleton().GetRenderSystem() == SRoot::SO3_METAL_RENDERER ||
789 SRoot::getSingleton().GetRenderSystem() == SRoot::SO3_VULKAN_RENDERER)
790 zdepth += nearclip;
791 else
792 zdepth -= nearclip;
793
794 // get new distance for ray direction
795 // A B C triangle : tan(fov / 2) gives AB : AC is depth : then use pythagore to get hypotenuse BC
796 double ylenght = Ogre::Math::Tan(camera->getFOVy().valueRadians() / 2.0f);
797 double xlenght = ylenght * camera->getAspectRatio();
798 double opx = zdepth * abs(xlenght * nx);
799 double opy = zdepth * abs(ylenght * ny);
800 rayLenght = Ogre::Math::Sqrt((Ogre::Real)((zdepth * zdepth) + (opx * opx) + (opy * opy)));
801 }
802 else
803 {
804 zdepth *= (farclip - nearclip);
805
810 zdepth *= 0.5f;
811
812 rayLenght = zdepth;
813 }
814
815 pos = rayOrigin + (rayDirection * rayLenght);
816 }
817 }
818 }
819 //MMechostr(MSKDEBUG, ">>>>>> RayCast : %i:%i\n", pixelsX, pixelsY);
820
821 if (subEntity != 0)
822 {
823 //MMechostr(MSKDEBUG, ">>>>>> RayCast : Entity found > %s\n", subEntity->getParent()->getName().c_str());
824
825 int subid = -1;
826 for (unsigned int i = 0; i < subEntity->getParent()->getNumSubEntities() && (subid == -1); i++)
827 {
828 if (subEntity->getParent()->getSubEntity(i) == subEntity)
829 subid = i;
830 }
831
832 //Only on widgets when the depth is available
833 if (scolCamera != 0)
834 {
835 if (subEntitySelector->UseDepthBuffer())
836 {
837 Ogre::Any bindedCustomEntity = subEntity->getUserObjectBindings().getUserAny("SEntity");
838 SEntity* entity = Ogre::any_cast<SEntity*> (bindedCustomEntity);
839 SScene* scene = entity->GetParentScene();
840 SMaterial* mat = scene->GetMaterial(entity->GetGroupName(), subEntity->getMaterial()->getName());
841
842 raycastReturn.entity = entity;
843 raycastReturn.entityName = entity->GetName();
844 raycastReturn.scene = scene;
845 raycastReturn.sceneName = scene->GetName();
846 raycastReturn.viewport = this;
847 raycastReturn.material = mat;
848
849 // Should we force uv coordonate retrieving too?
850 if (mat && (mat->GetAssociatedWidget() != 0))
851 raycastReturn = SRaycast::Cast(scolCamera, xR, yR, subEntity, true);
852
853 raycastReturn.point = pos;
854 raycastReturn.closestDistance = rayLenght;
855 }
856 else if (scolWindow->GetSelectorMode() == SWindow::SO3_SELECTOR_LIMITED) // position only on widgets
857 {
858 Ogre::Any bindedCustomEntity = subEntity->getUserObjectBindings().getUserAny("SEntity");
859 SEntity* entity = Ogre::any_cast<SEntity*> (bindedCustomEntity);
860 SScene* scene = entity->GetParentScene();
861 SMaterial* mat = scene->GetMaterial(entity->GetGroupName(), subEntity->getMaterial()->getName());
862
863 // Should we force uv coordonate retrieving too?
864 if (mat && (mat->GetAssociatedWidget() != 0))
865 {
866 raycastReturn.entity = entity;
867 raycastReturn.entityName = entity->GetName();
868 raycastReturn.scene = scene;
869 raycastReturn.sceneName = scene->GetName();
870 raycastReturn.material = mat;
871 raycastReturn.viewport = this;
872 raycastReturn = SRaycast::Cast(scolCamera, xR, yR, subEntity, true);
873 }
874 else
875 {
876 float xR = (static_cast <float> (pixelsX - GetLeftPixels())) / (static_cast <float> (GetWidthPixels()));
877 float yR = (static_cast <float> (pixelsY - GetTopPixels())) / (static_cast <float> (GetHeightPixels()));
878 Ogre::Ray ray = scolCamera->GetOgreCameraPointer()->getCameraToViewportRay(fabs(xR), fabs(yR));
879 ray.setOrigin(scolCamera->GetGlobalPosition());
880 scolCamera->GetParentScene()->GetSimpleRayCast(ray, entity, raycastReturn);
881
882 if (raycastReturn.scene == 0)
883 {
884 raycastReturn.entity = entity;
885 raycastReturn.entityName = entity->GetName();
886 raycastReturn.scene = scene;
887 raycastReturn.sceneName = scene->GetName();
888 }
889 raycastReturn.material = mat;
890 raycastReturn.viewport = this;
891 }
892 }
893 else
894 {
895 raycastReturn = SRaycast::Cast(scolCamera, xR, yR, subEntity);
896 }
897 }
898
899 raycastReturn.indexSubEntity = subid;
900 }
901 }
902 else
903 {
904 //simple raycast
905 if (scolCamera != 0)
906 {
907 float xR = (static_cast <float> (pixelsX - GetLeftPixels())) / (static_cast <float> (GetWidthPixels()));
908 float yR = (static_cast <float> (pixelsY - GetTopPixels())) / (static_cast <float> (GetHeightPixels()));
909 Ogre::Ray ray = scolCamera->GetOgreCameraPointer()->getCameraToViewportRay(fabs(xR), fabs(yR));
910 ray.setOrigin(scolCamera->GetGlobalPosition());
911 raycastReturn.viewport = this;
912
913 scolCamera->GetParentScene()->GetSimpleRayCast(ray, raycastReturn);
914 }
915 }
916
917 return raycastReturn;
918 }
920 SRaycastResult SViewPort::RayCast(const int& pixelsX, const int& pixelsY)
921 {
922 int x = pixelsX;
923 int y = pixelsY;
924 ToPixelPos(x, y);
925
926 return scolWindow->RayCast(O3ViewPort->getActualLeft() + x, O3ViewPort->getActualTop() + y);
927 }
929 Ogre::Vector3 SViewPort::RayCastCamera(const int& pixelsX, const int& pixelsY, const float distance, Ogre::Vector3 &outDirection)
930 {
931 if (!scolCamera || !scolCamera->GetOgreCameraPointer())
932 OGRE_EXCEPT(Ogre::Exception::ERR_INVALID_STATE, "There is now active camera on viewport", "SViewPort::RayCastCamera");
933
934 float xR = (static_cast <float> (pixelsX - GetLeftPixels())) / (static_cast <float> (GetWidthPixels()));
935 float yR = (static_cast <float> (pixelsY - GetTopPixels())) / (static_cast <float> (GetHeightPixels()));
936 Ogre::Ray ray = scolCamera->GetOgreCameraPointer()->getCameraToViewportRay(xR, yR);
937 outDirection = ray.getDirection();
938 return ray.getOrigin() + ray.getDirection() * distance;
939 }
941 void SViewPort::viewportDimensionsChanged(Ogre::Viewport* viewport)
942 {
943 //hack to solve a bug in opengl renderer, this is not needed in DirectX
944 //reset all compositors
945 if (SRoot::getSingletonPtr()->GetRenderSystem() == SRoot::SO3_OPENGL_RENDERER)
946 {
947 Ogre::CompositorManager &compositorManager = Ogre::CompositorManager::getSingleton();
948 if (compositorManager.hasCompositorChain(O3ViewPort))
949 {
950 Ogre::CompositorChain* chain = compositorManager.getCompositorChain(O3ViewPort);
951
952 if (chain)
953 {
954 Ogre::CompositorChain::Instances instances = chain->getCompositorInstances();
955 for (unsigned int i = 0; i < instances.size(); i++)
956 {
957 Ogre::CompositorInstance *compositorInstance = instances.at(i);
958 Ogre::Compositor *compositor = compositorInstance->getCompositor();
959 bool state = compositorInstance->getEnabled();
960 if (state)
961 {
962 compositorInstance->setEnabled(false);
963 compositorInstance->setEnabled(true);
964 }
965 }
966 }
967 }
968 }
969 }
971 Ogre::Technique* SViewPort::handleSchemeNotFound(unsigned short schemeIndex, const Ogre::String& schemeName, Ogre::Material* originalMaterial, unsigned short lodIndex, const Ogre::Renderable* rend)
972 {
973 MMechostr(MSKDEBUG, ">>>>>>>>> Material scheme missed : %s for material : %s\n", schemeName.c_str(), originalMaterial->getName().c_str());
974 Ogre::MaterialPtr smat = static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().getByName("SO3/Internal/Default", SO3_INTERNAL_RESOURCE_GROUP));
975 if (smat)
976 return smat->getTechnique(0);
977 else
978 return 0;
979 }
981 unsigned int SViewPort::GetNumRenderedFaces()
982 {
983 if (O3ViewPort)
984 {
985 unsigned int numFaces = 0;
986
987 Ogre::CompositorManager &compositorManager = Ogre::CompositorManager::getSingleton();
988 if (compositorManager.hasCompositorChain(O3ViewPort))
989 {
990 Ogre::CompositorChain* chain = compositorManager.getCompositorChain(O3ViewPort);
991
992 if (chain)
993 {
994 Ogre::CompositorChain::Instances instances = chain->getCompositorInstances();
995 for (unsigned int i = 0; i < instances.size(); i++)
996 {
997 Ogre::CompositorInstance *compositorInstance = instances.at(i);
998 if (compositorInstance->getEnabled())
999 {
1000 Ogre::CompositionTechnique::TargetPasses passList = compositorInstance->getTechnique()->getTargetPasses();
1001 for (unsigned int j = 0; j < passList.size(); ++j)
1002 {
1003 Ogre::RenderTarget* rendTarget = 0;
1004
1005 try
1006 {
1007 rendTarget = compositorInstance->getRenderTarget(passList[j]->getOutputName());
1008 }
1009 catch (Ogre::Exception &)
1010 {
1011 rendTarget = 0;
1012 }
1013
1014 if (rendTarget)
1015 numFaces += rendTarget->getStatistics().triangleCount;
1016 }
1017 }
1018 }
1019 }
1020 }
1021
1022 numFaces += O3ViewPort->_getNumRenderedFaces();
1023 return numFaces;
1024 }
1025
1026 return 0;
1027 }
1030 {
1031 if (O3ViewPort)
1032 {
1033 unsigned int numBatches = 0;
1034
1035 Ogre::CompositorManager &compositorManager = Ogre::CompositorManager::getSingleton();
1036 if (compositorManager.hasCompositorChain(O3ViewPort))
1037 {
1038 Ogre::CompositorChain* chain = compositorManager.getCompositorChain(O3ViewPort);
1039
1040 if (chain)
1041 {
1042 Ogre::CompositorChain::Instances instances = chain->getCompositorInstances();
1043 for (unsigned int i = 0; i < instances.size(); i++)
1044 {
1045 Ogre::CompositorInstance *compositorInstance = instances.at(i);
1046 if (compositorInstance->getEnabled())
1047 {
1048 Ogre::CompositionTechnique::TargetPasses passList = compositorInstance->getTechnique()->getTargetPasses();
1049 for (unsigned int j = 0; j < passList.size(); ++j)
1050 {
1051 Ogre::RenderTarget* rendTarget = 0;
1052
1053 try
1054 {
1055 rendTarget = compositorInstance->getRenderTarget(passList[j]->getOutputName());
1056 }
1057 catch (Ogre::Exception &)
1058 {
1059 rendTarget = 0;
1060 }
1061
1062 if (rendTarget)
1063 numBatches += rendTarget->getStatistics().batchCount;
1064 }
1065 }
1066 }
1067 }
1068 }
1069 numBatches += O3ViewPort->_getNumRenderedBatches();
1070 return numBatches;
1071 }
1072
1073 return 0;
1074 }
1076 void SViewPort::ToPixelPos(int& px, int& py)
1077 {
1078#if defined(ANDROID)
1079 ScolWindowHandle awindow = (ScolWindowHandle)SCgetExtra("hscol");
1080 int nwidth = (int)ANativeWindow_getWidth(awindow);
1081 int nheight = (int)ANativeWindow_getHeight(awindow);
1082
1083 px = (int)(((float)GetWidthPixels() / (float)nwidth) * (float)px);
1084 py = (int)(((float)GetHeightPixels() / (float)nheight) * (float)py);
1085
1086#elif defined(APPLE_IOS)
1087 float ratio = GetParentWindow()->GetOgreRenderWindowPointer()->getViewPointToPixelScale();
1088
1089 px *= ratio;
1090 py *= ratio;
1091#endif
1092 }
1094 void SViewPort::ToPixelPos(float& px, float& py)
1095 {
1096#if defined(ANDROID)
1097 ScolWindowHandle awindow = (ScolWindowHandle)SCgetExtra("hscol");
1098 int nwidth = (int)ANativeWindow_getWidth(awindow);
1099 int nheight = (int)ANativeWindow_getHeight(awindow);
1100
1101 px = ((float)GetWidthPixels() / (float)nwidth) * px;
1102 py = ((float)GetHeightPixels() / (float)nheight) * py;
1103
1104#elif defined(APPLE_IOS)
1105 float ratio = GetParentWindow()->GetOgreRenderWindowPointer()->getViewPointToPixelScale();
1106
1107 px *= ratio;
1108 py *= ratio;
1109#endif
1110 }
1112 void SViewPort::ToWindowPos(int& px, int& py)
1113 {
1114#if defined(ANDROID)
1115 ScolWindowHandle awindow = (ScolWindowHandle)SCgetExtra("hscol");
1116 int nwidth = (int)ANativeWindow_getWidth(awindow);
1117 int nheight = (int)ANativeWindow_getHeight(awindow);
1118
1119 px = (int)(((float)nwidth / (float)GetWidthPixels()) * (float)px);
1120 py = (int)(((float)nheight / (float)GetHeightPixels()) * (float)py);
1121
1122#elif defined(APPLE_IOS)
1123 float ratio = GetParentWindow()->GetOgreRenderWindowPointer()->getViewPointToPixelScale();
1124
1125 px = (int)((float)px / ratio);
1126 py = (int)((float)py / ratio);
1127#endif
1128 }
1130 void SViewPort::ToWindowPos(float& px, float& py)
1131 {
1132#if defined(ANDROID)
1133 ScolWindowHandle awindow = (ScolWindowHandle)SCgetExtra("hscol");
1134 int nwidth = (int)ANativeWindow_getWidth(awindow);
1135 int nheight = (int)ANativeWindow_getHeight(awindow);
1136
1137 px = ((float)nwidth / (float)GetWidthPixels()) * px;
1138 py = ((float)nheight / (float)GetHeightPixels()) * py;
1139
1140#elif defined(APPLE_IOS)
1141 float ratio = GetParentWindow()->GetOgreRenderWindowPointer()->getViewPointToPixelScale();
1142
1143 px /= ratio;
1144 py /= ratio;
1145#endif
1146 }
1147
1148
1149}
MMechostr(MSKDEBUG, " > Start loading Plugin SO3Engine dll\n")
int nheight
Definition SO3SCOL.cpp:5095
SCOL_EXPORT int cbmachine w
Definition SO3SCOL.cpp:5150
int nwidth
Definition SO3SCOL.cpp:5094
static Ogre::ColourValue ScolToOgreColorRGBA(const int &scolColor)
static int OgreToScolColorRGBA(const Ogre::ColourValue &ogreColor)
void SetCurrentViewPort(SViewPort *mViewPort)
Definition SO3Camera.cpp:60
float GetAspectRatio()
Definition SO3Camera.cpp:84
Ogre::Camera * GetOgreCameraPointer()
Definition SO3Camera.cpp:50
std::string GetName() const
void SetAntiAliasing(SViewPort *existingViewport, DeferredShadingAntiAliasingMode newMode)
void SetSSAO(SViewPort *existingViewport, bool ssao)
void UnregisterViewport(SViewPort *existingViewport)
void RegisterViewport(SViewPort *targetViewport)
DeferredShadingMode GetMode() const
void SetMode(DeferredShadingMode mode)
std::string GetGroupName()
Base class for SO3 custom exception.
void SetEnable(bool enable)
void SetEffectAmount(float amount)
void SetBlurAmount(float amount)
void SetGamma(float gamma)
SWidget * GetAssociatedWidget()
SScene * GetParentScene()
virtual Ogre::Vector3 GetGlobalPosition()
static SRaycastResult Cast(SCamera *camera, const float &relativePosX, const float &relativePosY, Ogre::SubEntity *subEntity, bool getUvCoordonate=false)
Ogre::Vector3 point
Definition SO3RayCast.h:50
std::string sceneName
Definition SO3RayCast.h:44
std::string entityName
Definition SO3RayCast.h:47
SMaterial * material
Definition SO3RayCast.h:48
SViewPort * viewport
Definition SO3RayCast.h:45
SDeferredShading * GetDeferredManager()
Definition SO3Root.cpp:312
RenderSystem GetRenderSystem()
Definition SO3Root.cpp:860
@ SO3_METAL_RENDERER
Definition SO3Root.h:83
@ SO3_OPENGL3_RENDERER
Definition SO3Root.h:81
@ SO3_OPENGL_RENDERER
Definition SO3Root.h:80
@ SO3_VULKAN_RENDERER
Definition SO3Root.h:82
void UpdateCamera(SViewPort *viewport)
Definition SO3Root.cpp:1591
static SRoot & getSingleton()
Definition SO3Root.cpp:116
static SRoot * getSingletonPtr()
Definition SO3Root.cpp:111
SMaterial * GetMaterial(const std::string &groupName, const std::string &materialName, bool searchOtherGroups=true)
void GetSimpleRayCast(Ogre::Ray cameraRay, SRaycastResult &result)
void SetEnable(bool enable)
Ogre::SubEntity * GetSelectedSubEntity(int posX, int posY)
void SetVisibilityMaskIndexEnable(const Ogre::uint32 flagIndex, const bool enable)
SHdrHandler * GetHDRhandler()
void SetSkyEnable(const bool &value)
void SetVisibilityMask(const Ogre::uint32 &mask)
bool GetShadowsEnabled()
void AddCompositor(SCompositor *existingCompositor)
void ToWindowPos(int &px, int &py)
void SetDeferredEnable(const bool &value)
bool GetVisibilityMaskIndexEnable(const Ogre::uint32 flagIndex)
void SetHDREnable(const bool &value)
void RegisterStereoViewport()
SCompositor * GetCompositor(const std::string &compositorName)
SCamera * GetCamera()
void SetPositionAndSize(const float &x, const float &y, const float &w, const float &h)
void ToPixelPos(int &px, int &py)
void UnregisterStereoViewport()
Ogre::Vector3 RayCastCamera(const int &pixelsX, const int &pixelsY, const float distance, Ogre::Vector3 &outDirection)
void SetShadowsEnabled(const bool &enableShadows)
void SetMaterialScheme(const std::string &existingScheme)
void ClearCompositors()
void SetOverlayEnable(const bool &value)
void SetDeferredDebugMode(const int &value)
SDeferredShading::DeferredShadingAntiAliasingMode GetDeferredAntiAliasingMode()
Ogre::uint32 GetVisibilityMask()
void SetSSAOEnable(const bool &value)
void SetHDREffectAmount(const float &value)
void RestoreStereoCamera()
bool IsStereoViewportRegistered()
void ResizeToWindow(const float &w, const float &h)
unsigned int GetNumRenderedBatches()
SRaycastResult RayCast(const int &pixelsX, const int &pixelsY, SSubEntitySelectorBuffer *subEntitySelector)
unsigned int GetNumRenderedFaces()
bool GetDeferredEnable()
int GetDeferredDebugMode()
void SetBackgroundColour(const int &backgroundColor)
SWindow * GetParentWindow() const
void UpdateStereoCamera(Ogre::Camera *camera)
const SCompositorMap & GetCompositorList() const
void RemoveCompositor(SCompositor *existingCompositor)
Ogre::Viewport * GetOgreViewPortPointer()
SCompositor * CreateCompositor(const std::string &newCompositorName, const std::string &newSchemeName)
void SetClearDepth(const bool &state)
void SetDeferredAntiAliasingMode(const SDeferredShading::DeferredShadingAntiAliasingMode &value)
void UpdateStereoCameraMatrix(Ogre::Camera *camera)
void SetHDRBlurAmount(const float &value)
bool GetOverlayEnable()
std::string GetMaterialScheme()
SSsaoHandler * GetSSAOhandler()
void SetCamera(SCamera *camera)
virtual Ogre::Technique * handleSchemeNotFound(unsigned short schemeIndex, const Ogre::String &schemeName, Ogre::Material *originalMaterial, unsigned short lodIndex, const Ogre::Renderable *rend)
bool GetScreenPosFromWorldPos(Ogre::Vector3 worldPosition, Ogre::Vector2 &screenPos)
void SetHDRGamma(const float &value)
bool UpdateStereoCompositorState(const Ogre::String compname, bool state)
void SetClearColor(const bool &value)
virtual void viewportDimensionsChanged(Ogre::Viewport *viewport)
void DeleteCompositor(SCompositor *existingCompositor)
void RegisterStereoViewport(SViewPort *viewport)
bool UpdateStereoCompositorState(SViewPort *viewport, const Ogre::String compname, bool state)
void UpdateStereoCameraMatrix(SViewPort *viewport, Ogre::Camera *camera)
void UpdateStereoCamera(SViewPort *viewport, Ogre::Camera *camera)
Ogre::RenderWindow * GetOgreRenderWindowPointer()
void RestoreStereoCamera(SViewPort *viewport)
void SynchStereoViewportSetup(SViewPort *viewport, bool overlay=false, bool shadow=false)
SelectorMode GetSelectorMode()
void UnregisterStereoViewport(SViewPort *viewport)
bool IsStereoViewportRegistered(SViewPort *viewport)
unsigned int GetHeight() const
@ SO3_SELECTOR_LIMITED
Definition SO3Window.h:50
SRaycastResult RayCast(const int &pixelsX, const int &pixelsY)
unsigned int GetWidth() const
void RefreshSelector(const bool &forceRefresh=false)
std::unordered_map< std::string, SCompositor * > SCompositorMap
STBI_EXTERN unsigned long flags
Definition stb_image.h:1182