diff --git a/src/modules/graphic/osggraph/CMakeLists.txt b/src/modules/graphic/osggraph/CMakeLists.txt index 48adff95..a6c17d8c 100644 --- a/src/modules/graphic/osggraph/CMakeLists.txt +++ b/src/modules/graphic/osggraph/CMakeLists.txt @@ -24,6 +24,7 @@ SET(OSGGRAPH_HEADERS OsgUtil/OsgVectorArrayAdapter.h OsgWorld/OsgScenery.h OsgFX/OsgRender.h + OsgFX/OsgReflectionMapping.h OsgView/OsgView.h OsgView/OsgScreens.h @@ -58,6 +59,7 @@ SET(OSGGRAPH_SOURCES OsgUtil/OsgSphere.cpp OsgWorld/OsgScenery.cpp OsgFX/OsgRender.cpp + OsgFX/OsgReflectionMapping.cpp OsgView/OsgView.cpp OsgView/OsgScreens.cpp diff --git a/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.cpp b/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.cpp new file mode 100755 index 00000000..b47d3a64 --- /dev/null +++ b/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.cpp @@ -0,0 +1,103 @@ +/*************************************************************************** + + file : OsgScreens.h + created : Sat Feb 2013 15:52:19 CEST 2013 + copyright : (C) 2013 by Gaëtan André + email : gaetan.andre@gmail.com + version : $Id$ + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ****************************************************************************/ + +#include +#include +#include +#include + +#include "OsgView/OsgScreens.h" + +#include "OsgReflectionMapping.h" + +#include + + + + +SDReflectionMapping::SDReflectionMapping(SDScreens *s, osg::ref_ptr m_sceneroot){ + + screens=s; + + map = new osg::Texture2D; + map->setTextureSize( 256, 256 ); + map->setInternalFormat( GL_RGB ); + + camera = new osg::Camera; + camera->setViewport( 0, 0, 256, 256 ); + camera->setClearColor( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); + camera->setClearMask( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); + + camera->setRenderOrder( osg::Camera::PRE_RENDER ); + camera->setRenderTargetImplementation( + osg::Camera::FRAME_BUFFER_OBJECT ); + camera->attach( osg::Camera::COLOR_BUFFER, map.get() ); + + camera->setReferenceFrame( osg::Camera::ABSOLUTE_RF ); + camera->addChild( m_sceneroot ); + + + //camera->setProjectionMatrixAsOrtho(-1,1,-1,1,0,1000000); + + camera->setProjectionMatrixAsPerspective(45.0,1.0,1.0,1000000.0); + + cameras = new osg::Group; + + cameras->addChild(camera); +} + + +void SDReflectionMapping::update(){ + + tCarElt * car = screens->getActiveView()->getCurrentCar(); + sgVec3 P, p; + osg::Vec3 eye,center,up; + + float offset = 0; + + p[0] = car->_drvPos_x; + p[1] = car->_bonnetPos_y; + p[2] = car->_drvPos_z; + sgXformPnt3(p, car->_posMat); + + eye[0] = p[0]; + eye[1] = p[1]; + eye[2] = p[2]; + + + P[0] = car->_drvPos_x + 30.0 * cos(2*PI/3 * car->_glance + offset); + P[1] = car->_bonnetPos_y - 30.0 * sin(2*PI/3 * car->_glance + offset); + P[2] = car->_drvPos_z; + sgXformPnt3(P, car->_posMat); + + center[0] = P[0]; + center[1] = P[1]; + center[2] = P[2]; + + up[0] = car->_posMat[2][0]; + up[1] = car->_posMat[2][1]; + up[2] = car->_posMat[2][2]; + + + + camera->setViewMatrixAsLookAt(eye,center,up); + +} + +SDReflectionMapping::~SDReflectionMapping(){ +} diff --git a/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.h b/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.h new file mode 100755 index 00000000..0107ffda --- /dev/null +++ b/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.h @@ -0,0 +1,47 @@ +/*************************************************************************** + + file : OsgScreens.h + created : Sat Feb 2013 15:52:19 CEST 2013 + copyright : (C) 2013 by Gaëtan André + email : gaetan.andre@gmail.com + version : $Id$ + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef _OSGREFLECTIONMAPPING_H_ +#define _OSGREFLECTIONMAPPING_H_ + +#include + +class SDReflectionMapping +{ + private: + osg::ref_ptr cameras; + osg::ref_ptr camera; + osg::ref_ptr map; + SDScreens * screens; + + public: + SDReflectionMapping(SDScreens *s, osg::ref_ptr m_sceneroot); + inline osg::ref_ptr getCameras(){ + return cameras; + } + inline osg::ref_ptr getReflectionMap(){ + return map; + } + + void update(); + + ~SDReflectionMapping(); + +}; + +#endif //_OSGREFLECTIONMAPPING_H_ diff --git a/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.cpp b/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.cpp index 092e928c..da1b70d9 100755 --- a/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.cpp +++ b/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.cpp @@ -1,14 +1,18 @@ +#include #include #include #include +#include #include "OsgDebugHUD.h" + + SDDebugHUD::SDDebugHUD(){ osg::Geometry* geom = osg::createTexturedQuadGeometry( osg::Vec3(), osg::Vec3(0.5f,0.0f,0.0f), osg::Vec3(0.0f,0.7f,0.0f), - 0.0f, 0.0f, -1.0f, 1.0f ); + 0.0f,0.0f,1.0f,1.0f); osg::ref_ptr quad = new osg::Geode; quad->addDrawable( geom ); @@ -21,6 +25,11 @@ SDDebugHUD::SDDebugHUD(){ HUD_camera->setNodeMask(0); } +void SDDebugHUD::setTexture(osg::ref_ptr map){ + osg::StateSet* stateset = HUD_camera->getOrCreateStateSet(); + stateset->setTextureAttributeAndModes( 0,map); +} + void SDDebugHUD::toggleHUD(){ HUD_camera->setNodeMask(1-HUD_camera->getNodeMask()); } diff --git a/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.h b/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.h index 1dff916b..ecfcb60d 100755 --- a/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.h +++ b/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.h @@ -19,8 +19,6 @@ #ifndef _OSGDEBUGHUD_H_ #define _OSGDEBUGHUD_H_ -#include -#include class SDDebugHUD { @@ -29,6 +27,7 @@ class SDDebugHUD public: void toggleHUD(); + void setTexture(osg::ref_ptr map); SDDebugHUD(); ~SDDebugHUD(); @@ -36,6 +35,7 @@ class SDDebugHUD return HUD_camera; } + }; #endif //_OSGDEBUGHUD_H_ diff --git a/src/modules/graphic/osggraph/OsgView/OsgScreens.cpp b/src/modules/graphic/osggraph/OsgView/OsgScreens.cpp index db5e19ca..ccee2080 100755 --- a/src/modules/graphic/osggraph/OsgView/OsgScreens.cpp +++ b/src/modules/graphic/osggraph/OsgView/OsgScreens.cpp @@ -26,6 +26,9 @@ #include #include "OsgScreens.h" + +#include "OsgDebugHUD.h" +#include "OsgFX/OsgReflectionMapping.h" #include "OsgMain.h" #include "OsgCar/OsgCar.h" @@ -51,6 +54,9 @@ public: void SDScreens::Init(int x,int y, int width, int height, osg::ref_ptr m_sceneroot) { + + reflectionMapping = new SDReflectionMapping(this,m_sceneroot); + m_Winx = x; m_Winy = y; m_Winw = width; @@ -109,6 +115,9 @@ void SDScreens::Init(int x,int y, int width, int height, osg::ref_ptr root->addChild(mirrorCam.get()); } + + root->addChild(reflectionMapping->getCameras()); + debugHUD->setTexture(reflectionMapping->getReflectionMap()); root->addChild(debugHUD->getRootCamera()); viewer->setSceneData(root.get()); @@ -190,6 +199,9 @@ void SDScreens::update(tSituation * s,SDFrameInfo* fi) Screens[i]->update(s,fi); } + reflectionMapping->update(); + + if (!viewer->done()) viewer->frame(); } diff --git a/src/modules/graphic/osggraph/OsgView/OsgScreens.h b/src/modules/graphic/osggraph/OsgView/OsgScreens.h index 6c710dc4..40c1de36 100755 --- a/src/modules/graphic/osggraph/OsgView/OsgScreens.h +++ b/src/modules/graphic/osggraph/OsgView/OsgScreens.h @@ -24,7 +24,8 @@ #include #include "OsgView.h" -#include "OsgDebugHUD.h" +class SDDebugHUD; +class SDReflectionMapping; #define SD_SPLIT_ADD 0 #define SD_SPLIT_REM 1 @@ -42,6 +43,7 @@ class SDScreens std::vector Screens; osg::ref_ptr root; SDDebugHUD * debugHUD; + SDReflectionMapping * reflectionMapping; int m_Winx; int m_Winy;