OSG : working on Reflection Mapping

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@5496 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: 34c574252f9847f835f6d2199b46d87bc84bc2c1
Former-commit-id: b30e1ea001438a8a8b1e06c2767cac101da0c08d
This commit is contained in:
rvlander 2013-06-09 11:58:26 +00:00
parent a311e61420
commit 671facfa33
7 changed files with 179 additions and 4 deletions

View file

@ -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

View file

@ -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 <osg/Group>
#include <osg/Texture2D>
#include <osg/Camera>
#include <osgViewer/Viewer>
#include "OsgView/OsgScreens.h"
#include "OsgReflectionMapping.h"
#include <car.h>
SDReflectionMapping::SDReflectionMapping(SDScreens *s, osg::ref_ptr<osg::Node> 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(){
}

View file

@ -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 <osg/Texture2D>
class SDReflectionMapping
{
private:
osg::ref_ptr<osg::Group> cameras;
osg::ref_ptr<osg::Camera> camera;
osg::ref_ptr<osg::Texture2D> map;
SDScreens * screens;
public:
SDReflectionMapping(SDScreens *s, osg::ref_ptr<osg::Node> m_sceneroot);
inline osg::ref_ptr<osg::Group> getCameras(){
return cameras;
}
inline osg::ref_ptr<osg::Texture2D> getReflectionMap(){
return map;
}
void update();
~SDReflectionMapping();
};
#endif //_OSGREFLECTIONMAPPING_H_

View file

@ -1,14 +1,18 @@
#include<osg/Camera>
#include <osgDB/ReadFile>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Texture2D>
#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<osg::Geode> quad = new osg::Geode;
quad->addDrawable( geom );
@ -21,6 +25,11 @@ SDDebugHUD::SDDebugHUD(){
HUD_camera->setNodeMask(0);
}
void SDDebugHUD::setTexture(osg::ref_ptr<osg::Texture2D> map){
osg::StateSet* stateset = HUD_camera->getOrCreateStateSet();
stateset->setTextureAttributeAndModes( 0,map);
}
void SDDebugHUD::toggleHUD(){
HUD_camera->setNodeMask(1-HUD_camera->getNodeMask());
}

View file

@ -19,8 +19,6 @@
#ifndef _OSGDEBUGHUD_H_
#define _OSGDEBUGHUD_H_
#include<osg/ref_ptr>
#include<osg/Camera>
class SDDebugHUD
{
@ -29,6 +27,7 @@ class SDDebugHUD
public:
void toggleHUD();
void setTexture(osg::ref_ptr<osg::Texture2D> map);
SDDebugHUD();
~SDDebugHUD();
@ -36,6 +35,7 @@ class SDDebugHUD
return HUD_camera;
}
};
#endif //_OSGDEBUGHUD_H_

View file

@ -26,6 +26,9 @@
#include <osg/ValueObject>
#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<osg::Node> 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<osg::Node>
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();
}

View file

@ -24,7 +24,8 @@
#include <vector>
#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<SDView *> Screens;
osg::ref_ptr<osg::Group> root;
SDDebugHUD * debugHUD;
SDReflectionMapping * reflectionMapping;
int m_Winx;
int m_Winy;