Project

General

Profile

SO3Engine
SO3StereoManager.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source is part of the Stereoscopy manager for OGRE
4 (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6-----------------------------------------------------------------------------
7* Copyright (c) 2008, Mathieu Le Ber, AXYZ-IMAGES
8* All rights reserved.
9*
10* Redistribution and use in source and binary forms, with or without
11* modification, are permitted provided that the following conditions are met:
12* * Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* * Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* * Neither the name of the AXYZ-IMAGES nor the
18* names of its contributors may be used to endorse or promote products
19* derived from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED BY Mathieu Le Ber ''AS IS'' AND ANY
22* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL Mathieu Le Ber BE LIABLE FOR ANY
25* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31-----------------------------------------------------------------------------
32// Heavily modified by OS3D Team.
33*/
34
35#ifndef _SO3_STEREO_MANAGER_H_
36#define _SO3_STEREO_MANAGER_H_
37
39#include "SO3Utils/SO3Point.h"
40
48namespace SO3
49{
51 {
52 public:
72 protected:
73 private:
74 typedef std::unordered_map<std::string, bool> CompositorList;
75 typedef std::list<std::string> CompositorIndexList;
76 class StereoCameraListener; // Pre-declaration for StereoViewportData.
77 class StereoRTTListener;
78 class StereoViewportListener;
79 class StereoFrameListener;
83 struct StereoViewportData
84 {
85 CompositorList mCompList;
86 CompositorIndexList mCompIndexList;
87 bool mFlipEyes;
88 bool mShadowEnable;
89 bool mOverlayEnable;
90 bool mSkyEnable;
91 Ogre::String mMaterialScheme;
92 double frameTime;
93 Ogre::ColourValue mOldBackgroundColor;
94 Ogre::uint32 mMask;
95 Ogre::Vector3 mOldPos;
96 Ogre::Vector2 mOldOffset;
97 Ogre::Real mOldRatio;
98 Ogre::Radian mOldFOV;
99 Ogre::Matrix4 mOldMatrix;
100 Ogre::Matrix4 mBaseCameraMatrix;
101 bool mOldUseCustomMatrix;
102 Ogre::Camera* mCamera;
103 StereoCameraListener* leftCameraListener;
104 StereoCameraListener* rightCameraListener;
105 StereoRTTListener* rttListener;
106 StereoViewportListener* viewportListener;
107 StereoFrameListener* frameListener;
108 Ogre::Viewport* mOriginalViewport;
109 Ogre::Vector2 mOriginalViewportSize;
110 Ogre::Viewport* mLastLeftViewport;
111 Ogre::Viewport* mLastRightViewport;
112 Ogre::SceneManager* mScene;
113 Ogre::SceneManager* mStereoScene;
114 Ogre::Rectangle2D* mRect;
115 Ogre::SceneNode* mStereoCameraNode;
116 Ogre::Camera* mStereoCamera;
117 Ogre::ColourValue mBackgroundColor;
118 Ogre::TexturePtr mLeftRenderTexture;
119 Ogre::TexturePtr mRightRenderTexture;
120 Ogre::Entity* mLeftEntity;
121 Ogre::Entity* mRightEntity;
122 StereoViewportData() : mFlipEyes(false), mShadowEnable(false), mOverlayEnable(false), mSkyEnable(false), frameTime(0.0), mOldBackgroundColor(Ogre::ColourValue::Black), mMask(0xffffffff), mOldPos(Ogre::Vector3::ZERO), mOldOffset(Ogre::Vector2::ZERO), mOldRatio(0.0f), mOldFOV(Ogre::Radian()), mOldMatrix(Ogre::Matrix4::IDENTITY), mBaseCameraMatrix(Ogre::Matrix4::IDENTITY), mOldUseCustomMatrix(false), mCamera(0), leftCameraListener(0), rightCameraListener(0), rttListener(0), viewportListener(0), frameListener(0), mOriginalViewport(0), mLastLeftViewport(0), mLastRightViewport(0), mScene(0), mStereoScene(0), mRect(0), mStereoCameraNode(0), mStereoCamera(0), mBackgroundColor(Ogre::ColourValue::Black), mLeftRenderTexture(), mRightRenderTexture(), mLeftEntity(0), mRightEntity(0) {}
123 };
124
128 class StereoRTTListener : public Ogre::Resource::Listener
129 {
130 public:
131 protected:
132 private:
133 StereoManager* mStereoMgr;
134 StereoManager::StereoViewportData* viewportData;
135
136 public:
140 StereoRTTListener(StereoManager* stereoMgr, StereoManager::StereoViewportData* stereoViewportData);
141
145 virtual void loadingComplete(Ogre::Resource* resource);
146
150 virtual void unloadingComplete(Ogre::Resource* resource);
151
152 protected:
153 private:
154 };
155
159 class StereoCameraListener : public Ogre::RenderTargetListener
160 {
161 public:
162 protected:
163 private:
164 StereoManager* mStereoMgr;
165 StereoManager::StereoViewportData* viewportData;
166 Ogre::Viewport* mOriginalViewport;
167 bool mIsLeftEye;
168
169 public:
173 StereoCameraListener(StereoManager* stereoMgr, bool isLeftCamera, Ogre::Viewport* originalViewport, StereoManager::StereoViewportData* stereoViewportData);
174
178 virtual void preRenderTargetUpdate(const Ogre::RenderTargetEvent& evt);
179
183 virtual void postRenderTargetUpdate(const Ogre::RenderTargetEvent& evt);
184
185 protected:
186 private:
187 };
188
192 class StereoViewportListener : public Ogre::RenderTargetListener
193 {
194 public:
195 protected:
196 private:
197 StereoManager* mStereoMgr;
198 StereoManager::StereoViewportData* viewportData;
199
200 public:
204 StereoViewportListener(StereoManager* stereoMgr, StereoManager::StereoViewportData* stereoViewportData);
205
209 virtual void preRenderTargetUpdate(const Ogre::RenderTargetEvent& evt);
210
214 virtual void postRenderTargetUpdate(const Ogre::RenderTargetEvent& evt);
215
216 protected:
217 private:
218 };
219
223 class StereoFrameListener : public Ogre::FrameListener
224 {
225 public:
226 protected:
227 private:
228 StereoManager* mStereoMgr;
229 StereoManager::StereoViewportData* viewportData;
230
231 public:
235 StereoFrameListener(StereoManager* stereoMgr, StereoManager::StereoViewportData* stereoViewportData);
236
240 virtual bool frameStarted(const Ogre::FrameEvent& evt);
241
245 virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
246
250 virtual bool frameEnded(const Ogre::FrameEvent& evt);
251
252 protected:
253 private:
254 };
255
259 class DeviceLostListener : public Ogre::RenderSystem::Listener
260 {
261 public:
262 protected:
263 private:
264 StereoManager* mStereoMgr;
265
266 public:
270 DeviceLostListener(StereoManager* stereoMgr);
271
275 virtual void eventOccurred(const Ogre::String &eventName, const Ogre::NameValuePairList *parameters);
276 protected:
277 private:
278 };
279 typedef std::map<StereoMode, Ogre::String> StereoModeList;
280 StereoModeList mAvailableModes;
281 typedef std::map<Ogre::Viewport*, StereoViewportData*> StereoViewports;
282 StereoViewports stereoViewports;
283 DeviceLostListener* mDeviceLostListener;
284 StereoMode mStereoMode;
285 Ogre::Real mEyesSpacing;
286 Ogre::uint32 mLeftMask, mRightMask;
287 friend class DeviceLostListener;
291
292 SWindow* mBuffer;
293 bool mIsCustomProjection;
294 bool mIsCustomOffset;
295 bool mIsCustomRatio;
296 bool mIsCustomFOV;
297 bool mIsCustomDistortion;
298 bool mIsCustomChromAbCorrection;
299 bool mRotateView;
300 Ogre::Vector4 mDistortion;
301 Ogre::Vector4 mChromAbCorrection;
302 Ogre::Matrix4 mLeftCustomProjection, mRightCustomProjection;
303 Ogre::Matrix4 mLeftStartWarpMatrix, mLeftEndWarpMatrix;
304 Ogre::Matrix4 mRightStartWarpMatrix, mRightEndWarpMatrix;
305 Ogre::Real mCenterOffset;
306 Ogre::Real mRatio;
307 Ogre::Radian mFOV;
308 Ogre::Vector2 mLeftUVScale;
309 Ogre::Vector2 mLeftUVOffset;
310 Ogre::Vector2 mRightUVScale;
311 Ogre::Vector2 mRightUVOffset;
312 SO3::SPoint<int> mLeftRenderTextureSize;
313 SO3::SPoint<int> mRightRenderTextureSize;
314 Ogre::MeshPtr mLeftMesh;
315 Ogre::MeshPtr mRightMesh;
316 bool mHasExternalWindow;
317 SO3::SPoint<int> mWindowPos;
318 SO3::SPoint<int> mWindowSise;
319 int mWindowMonitorIndex;
320 Ogre::RenderWindow* mExternalWindow;
321 Ogre::Viewport* mExternalViewport;
322 public:
326 StereoManager(SWindow* buffer);
327
332
335 void ReloadStereo();
336
340 void RestoreStereoCamera(Ogre::Viewport* ogreViewportInstance);
341
345 void UpdateStereoCamera(Ogre::Viewport* ogreViewportInstance, Ogre::Camera* camera);
346
350 void UpdateStereoCameraMatrix(Ogre::Viewport* ogreViewportInstance, Ogre::Camera* camera);
351
352 /*
353 */
354 bool UpdateStereoCompositorState(Ogre::Viewport* ogreViewportInstance, const Ogre::String compname, bool state);
355
359 void RegisterViewport(SViewPort* viewportInstance);
360
364 void UnregisterViewport(SViewPort* viewportInstance);
365
366 void SynchViewportSetup(SViewPort* viewportInstance, bool overlay = false, bool shadow = false);
367
370 bool IsViewportRegistered(Ogre::Viewport* ogreViewportInstance);
371
376
380 void SetStereoMode(const StereoMode& newStereoMode);
381
384 Ogre::Real GetEyesSpacing() const;
385
388 void SetEyesSpacing(Ogre::Real l);
389
392 void SetStereoProjectionOffset(Ogre::Real offset);
393
396 void SetStereoAspectRatio(Ogre::Real ratio);
397
400 Ogre::Real GetStereoAspectRatio();
401
404 void SetStereoFOVy(Ogre::Real fov);
405
408 void SetRotateView(bool state);
409
412 bool GetRotateView();
413
416 void SetStereoChromaticAbCorrection(bool enable, Ogre::Vector4 chromAbC);
417
420 void SetStereoDistortion(bool enable, Ogre::Vector4 dist);
421
423 void SetCustomProjectonMatrices(bool enable, const Ogre::Matrix4 &leftMatrix, const Ogre::Matrix4 &rightMatrix);
424 void GetCustomProjectonMatrices(bool &enabled, Ogre::Matrix4 &leftMatrix, Ogre::Matrix4 &rightMatrix) const;
425 void SetTimeWarpMatrix(const Ogre::Matrix4 &leftStartMatrix, const Ogre::Matrix4 &leftEndMatrix, const Ogre::Matrix4 &rightStartMatrix, const Ogre::Matrix4 &rightEndMatrix);
426
427 void SetStereoMeshUVConfig(Ogre::Vector2 leftuvscale, Ogre::Vector2 leftuvoffset, Ogre::Vector2 rightuvscale, Ogre::Vector2 rightuvoffset);
429 void SetStereoMeshLeft(std::vector<Ogre::Vector3> vertices, std::vector<std::vector<Ogre::Vector2> > uvs, std::vector<Ogre::Real> vignetteColor, std::vector<Ogre::Real> warp, std::vector<int> indexs);
430 void SetStereoMeshRight(std::vector<Ogre::Vector3> vertices, std::vector<std::vector<Ogre::Vector2> > uvs, std::vector<Ogre::Real> vignetteColor, std::vector<Ogre::Real> warp, std::vector<int> indexs);
431 void SetStereoWindow(SO3::SPoint<int> pos, SO3::SPoint<int> size, int index, bool state);
432 std::array<SCOL_PTR_TYPE, 2> GetStereoTextures();
433 std::array<SO3::SPoint<int>, 2> GetStereoViewportSize();
434
435 protected:
438 void ReleaseRttTarget(StereoViewportData* viewportData);
439
442 void ConstructRttTarget(StereoViewportData* viewportData);
443 private:
446 void InitializeViewport(Ogre::Viewport* viewport, StereoViewportData* viewportData);
447
450 void ShutdownViewport(Ogre::Viewport* viewport, StereoViewportData* viewportData);
451
454 void InitializeStereo(Ogre::Viewport* viewport, StereoViewportData* viewportData);
455
458 void ShutdownStereo(Ogre::Viewport* viewport, StereoViewportData* viewportData);
459
462 void InitializeListeners(Ogre::Viewport* viewport, StereoViewportData* viewportData);
463
466 void ShutdownListeners(Ogre::Viewport* viewport, StereoViewportData* viewportData);
467 };
468}
469
470#endif
librairies include
void SetStereoMeshLeft(std::vector< Ogre::Vector3 > vertices, std::vector< std::vector< Ogre::Vector2 > > uvs, std::vector< Ogre::Real > vignetteColor, std::vector< Ogre::Real > warp, std::vector< int > indexs)
StereoMode GetStereoMode() const
void SetStereoFOVy(Ogre::Real fov)
std::array< SCOL_PTR_TYPE, 2 > GetStereoTextures()
void SetCustomProjectonMatrices(bool enable, const Ogre::Matrix4 &leftMatrix, const Ogre::Matrix4 &rightMatrix)
bool UpdateStereoCompositorState(Ogre::Viewport *ogreViewportInstance, const Ogre::String compname, bool state)
void SetStereoProjectionOffset(Ogre::Real offset)
friend class StereoCameraListener
void SetStereoDistortion(bool enable, Ogre::Vector4 dist)
void GetCustomProjectonMatrices(bool &enabled, Ogre::Matrix4 &leftMatrix, Ogre::Matrix4 &rightMatrix) const
Ogre::Real GetStereoAspectRatio()
void SetStereoMeshRttSize(SO3::SPoint< int > leftsize, SO3::SPoint< int > rightsize)
bool IsViewportRegistered(Ogre::Viewport *ogreViewportInstance)
void SetStereoChromaticAbCorrection(bool enable, Ogre::Vector4 chromAbC)
void SetStereoMode(const StereoMode &newStereoMode)
void SetStereoWindow(SO3::SPoint< int > pos, SO3::SPoint< int > size, int index, bool state)
friend class StereoFrameListener
Ogre::Real GetEyesSpacing() const
void UnregisterViewport(SViewPort *viewportInstance)
void ConstructRttTarget(StereoViewportData *viewportData)
void SetTimeWarpMatrix(const Ogre::Matrix4 &leftStartMatrix, const Ogre::Matrix4 &leftEndMatrix, const Ogre::Matrix4 &rightStartMatrix, const Ogre::Matrix4 &rightEndMatrix)
std::array< SO3::SPoint< int >, 2 > GetStereoViewportSize()
void ReleaseRttTarget(StereoViewportData *viewportData)
void SetStereoMeshRight(std::vector< Ogre::Vector3 > vertices, std::vector< std::vector< Ogre::Vector2 > > uvs, std::vector< Ogre::Real > vignetteColor, std::vector< Ogre::Real > warp, std::vector< int > indexs)
friend class StereoViewportListener
@ SO3_CARDBOARD_STEREO
Google card board.
@ SO3_QUAD_BUFFER_STEREO
OpenGL QuadBuffer.
@ SO3_INTERLACED_V_STEREO
Verticaly interlaced mode.
@ SO3_SIDE_BY_SIDE_STEREO
Side by side image.
@ SO3_ANAGLYPH_YB_STEREO
Anaglyph yellow/blue.
@ SO3_ANAGLYPH_RC_STEREO
Anaglyph red/cyan.
@ SO3_INTERLACED_CB_STEREO
Interlaced mode with a checkerboard pattern.
@ SO3_OCULUS2_STEREO
Oculus rift DK2.
@ SO3_OCULUS_STEREO
Oculus rift.
@ SO3_INTERLACED_H_STEREO
Horizontaly interlaced mode.
@ SO3_UP_DOWN_STEREO
Up/down image.
friend class DeviceLostListener
void SetStereoMeshUVConfig(Ogre::Vector2 leftuvscale, Ogre::Vector2 leftuvoffset, Ogre::Vector2 rightuvscale, Ogre::Vector2 rightuvoffset)
void UpdateStereoCamera(Ogre::Viewport *ogreViewportInstance, Ogre::Camera *camera)
void RestoreStereoCamera(Ogre::Viewport *ogreViewportInstance)
void RegisterViewport(SViewPort *viewportInstance)
void SynchViewportSetup(SViewPort *viewportInstance, bool overlay=false, bool shadow=false)
void SetEyesSpacing(Ogre::Real l)
void SetStereoAspectRatio(Ogre::Real ratio)
void UpdateStereoCameraMatrix(Ogre::Viewport *ogreViewportInstance, Ogre::Camera *camera)
void SetRotateView(bool state)