From 3734c903cd9e5bea3265db8928300ad936a0e039 Mon Sep 17 00:00:00 2001 From: rvlander Date: Thu, 8 Aug 2013 10:24:26 +0000 Subject: [PATCH] OSG_GRAPH: ongoing process to support several EM methods git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@5663 30fe4595-0a0c-4342-8851-515496e4dcbd Former-commit-id: eb87263e15842bf04a6ad6737e3e5a5b13119002 Former-commit-id: a33966c6f090115fd54a4b65a3d725034f556f76 --- .../graphic/osggraph/OsgCar/OsgCar.cpp | 24 ++++++++++++++++--- src/modules/graphic/osggraph/OsgCar/OsgCar.h | 21 +++++++++------- .../osggraph/OsgFX/OsgReflectionMapping.cpp | 18 ++++++++++---- .../osggraph/OsgFX/OsgReflectionMapping.h | 15 ++++++++---- .../graphic/osggraph/OsgFX/OsgShader.cpp | 23 +++++++++++------- .../graphic/osggraph/OsgFX/OsgShader.h | 18 ++++++++++++-- .../graphic/osggraph/OsgView/OsgDebugHUD.cpp | 12 +--------- .../graphic/osggraph/OsgView/OsgDebugHUD.h | 3 +-- 8 files changed, 89 insertions(+), 45 deletions(-) diff --git a/src/modules/graphic/osggraph/OsgCar/OsgCar.cpp b/src/modules/graphic/osggraph/OsgCar/OsgCar.cpp index e17583bf9..2788c97c6 100755 --- a/src/modules/graphic/osggraph/OsgCar/OsgCar.cpp +++ b/src/modules/graphic/osggraph/OsgCar/OsgCar.cpp @@ -41,6 +41,8 @@ #include "OsgSky/OsgSky.h" +#include "OsgFX/OsgShader.h" +#include "OsgFX/OsgReflectionMapping.h" @@ -220,17 +222,33 @@ osg::Node *SDCar::loadCar(tCarElt *car) // car_root->setNodeMask(1); - this->shader = new SDCarShader(pCar.get()); + this->shader = new SDCarShader(pCar.get(),this); + this->reflectionMappingMethod = REFLECTIONMAPPING_STATIC; - this->reflectionMapping = new SDReflectionMapping(car); + this->reflectionMapping = new SDReflectionMapping(this); // this->setReflectionMap(reflectionMapping->getReflectionMap()); return this->car_root; } +bool SDCar::isCar(tCarElt*c){ + return c==car; +} +SDReflectionMapping * SDCar::getReflectionMap(){ + return reflectionMapping; +} + +int SDCar::getReflectionMappingMethod(){ + return reflectionMappingMethod; +} + +tCarElt * SDCar::getCar(){ + return car; +} + #define GR_SHADOW_POINTS 6 #define MULT 1.1 osg::ref_ptr SDCar::initOcclusionQuad(tCarElt *car){ @@ -371,7 +389,7 @@ void SDCar::updateShadingParameters(osg::Matrixf modelview){ shader->update(modelview); } -void SDCar::setReflectionMap(osg::ref_ptr map){ +void SDCar::setReflectionMap(osg::ref_ptr map){ car_branch->getOrCreateStateSet()->setTextureAttributeAndModes(2,map,osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); } diff --git a/src/modules/graphic/osggraph/OsgCar/OsgCar.h b/src/modules/graphic/osggraph/OsgCar/OsgCar.h index 23dbf9c73..675fa09e7 100755 --- a/src/modules/graphic/osggraph/OsgCar/OsgCar.h +++ b/src/modules/graphic/osggraph/OsgCar/OsgCar.h @@ -27,8 +27,9 @@ #include #include "OsgWheel.h" -#include "OsgFX/OsgShader.h" -#include "OsgFX/OsgReflectionMapping.h" + +class SDCarShader; +class SDReflectionMapping; class SDCar { @@ -41,22 +42,24 @@ class SDCar SDReflectionMapping * reflectionMapping; osg::ref_ptr shadowVertices; osg::ref_ptr quad; + int reflectionMappingMethod; //osg::ref_ptr wheels[4]; //osg::ref_ptr initWheel(int wheelIndec, const char *wheel_mod_name); osg::ref_ptr initOcclusionQuad(tCarElt *car); - void setReflectionMap(osg::ref_ptr map); + void setReflectionMap(osg::ref_ptr map); public : osg::Node *loadCar(tCarElt *car); void deactivateCar(tCarElt*car); - inline bool isCar(tCarElt*c){ - return c==car; - } - inline SDReflectionMapping * getReflectionMap(){ - return reflectionMapping; - } + bool isCar(tCarElt*c); + SDReflectionMapping * getReflectionMap(); + + int getReflectionMappingMethod(); + + tCarElt * getCar(); + void activateCar(tCarElt*car); void updateCar(); diff --git a/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.cpp b/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.cpp index bcaa75350..58fe3a84e 100755 --- a/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.cpp +++ b/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.cpp @@ -23,12 +23,15 @@ #include "OsgView/OsgScreens.h" -#include "OsgReflectionMapping.h" + #include "OsgCar/OsgCar.h" #include "OsgRender.h" #include "OsgMain.h" + +#include "OsgReflectionMapping.h" + #include @@ -77,13 +80,16 @@ CameraPreCallback * pre_cam = new CameraPreCallback; CameraPostCallback * post_cam = new CameraPostCallback; -SDReflectionMapping::SDReflectionMapping(tCarElt *c):car(c){ +SDReflectionMapping::SDReflectionMapping(SDCar *c):car(c){ SDRender * render = (SDRender *)getRender(); osg::ref_ptr m_sceneroot = render->getRoot(); + osg::ref_ptr reflectionMap; + reflectionMap = new osg::TextureCubeMap; + this->reflectionMap =reflectionMap; reflectionMap->setTextureSize( 256, 256 ); reflectionMap->setInternalFormat( GL_RGB); reflectionMap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); @@ -135,17 +141,19 @@ SDReflectionMapping::SDReflectionMapping(tCarElt *c):car(c){ void SDReflectionMapping::update(){ + //TODO support for multi screen + SDScreens * screens = (SDScreens*)getScreens(); osg::Camera * viewCam = screens->getActiveView()->getOsgCam(); + tCarElt * car = this->car->getCar(); + pre_cam->setCar(car); post_cam->setCar(car); - sgVec3 P, p; + sgVec3 p; osg::Vec3 eye,center,up; - float offset = 0; - p[0] = car->_drvPos_x; p[1] = car->_bonnetPos_y; p[2] = car->_drvPos_z; diff --git a/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.h b/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.h index 6ce6d970e..4ae741c0b 100755 --- a/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.h +++ b/src/modules/graphic/osggraph/OsgFX/OsgReflectionMapping.h @@ -24,22 +24,29 @@ #include +#define REFLECTIONMAPPING_OFF 0 +#define REFLECTIONMAPPING_STATIC 1 +#define REFLECTIONMAPPING_HYBRID 2 +#define REFLECTIONMAPPING_DYNAMIC 3 + +class SDCar; + class SDReflectionMapping { private: osg::ref_ptr camerasRoot; std::vector< osg::ref_ptr > cameras; - osg::ref_ptr reflectionMap; - tCarElt * car; + osg::ref_ptr reflectionMap; + SDCar *car; inline osg::ref_ptr getCamerasRoot(){ return camerasRoot; } public: - SDReflectionMapping(tCarElt *c); + SDReflectionMapping(SDCar *c); ~SDReflectionMapping(); - inline osg::ref_ptr getReflectionMap(){ + inline osg::ref_ptr getReflectionMap(){ return reflectionMap; } diff --git a/src/modules/graphic/osggraph/OsgFX/OsgShader.cpp b/src/modules/graphic/osggraph/OsgFX/OsgShader.cpp index 984de741a..3e19eb527 100755 --- a/src/modules/graphic/osggraph/OsgFX/OsgShader.cpp +++ b/src/modules/graphic/osggraph/OsgFX/OsgShader.cpp @@ -25,9 +25,10 @@ #include "OsgMain.h" #include "OsgShader.h" +#include "OsgCar/OsgCar.h" #include "OsgSky/OsgSky.h" -SDCarShader::SDCarShader(osg::Node *car){ +SDCarShader::SDCarShader(osg::Node *car, SDCar *c){ std::string TmpPath = GetDataDir(); osg::ref_ptr vertShader = @@ -41,26 +42,30 @@ SDCarShader::SDCarShader(osg::Node *car){ program->addShader( fragShader.get() ); pCar= car; + this->pSdCar = c; stateset = pCar->getOrCreateStateSet(); stateset->setAttributeAndModes(program); diffuseMap = new osg::Uniform("diffusemap", 0 ); stateset->addUniform(diffuseMap); - reflectionMap = new osg::Uniform("reflectionmap", 2 ); - stateset->addUniform(reflectionMap); specularColor = new osg::Uniform("specularColor", osg::Vec4(0.8f,0.8f,0.8f,1.0f)); stateset->addUniform(specularColor); - /*lightVector = new osg::Uniform("lightvector",osg::Vec3()); - stateset->addUniform(lightVector); - lightPower = new osg::Uniform("lightpower",osg::Vec4()); - stateset->addUniform(lightPower); - ambientColor =new osg::Uniform("ambientColor",osg::Vec4()); - stateset->addUniform(ambientColor);*/ + + lightVector = stateset->getOrCreateUniform("lightvector",osg::Uniform::FLOAT_VEC3); lightPower = stateset->getOrCreateUniform("lightpower",osg::Uniform::FLOAT_VEC4); ambientColor = stateset->getOrCreateUniform("ambientColor",osg::Uniform::FLOAT_VEC4); shininess = new osg::Uniform("smoothness", 300.0f); stateset->addUniform(shininess); + + reflectionMappingMethod = new osg::Uniform("reflectionMappingMethod",pSdCar->getReflectionMappingMethod()); + reflectionMapCube = new osg::Uniform("reflectionMapCube", 2 ); + reflectionMap2DSampler = new osg::Uniform("reflectionMap2DSampler", 2 ); + reflectionMapStaticOffsetCoords = stateset->getOrCreateUniform("reflectionMapStaticOffsetCoords",osg::Uniform::FLOAT_VEC3); + + stateset->addUniform(reflectionMappingMethod); + stateset->addUniform(reflectionMap2DSampler); + stateset->addUniform(reflectionMapCube); } void SDCarShader::update(osg::Matrixf view){ diff --git a/src/modules/graphic/osggraph/OsgFX/OsgShader.h b/src/modules/graphic/osggraph/OsgFX/OsgShader.h index fd50e55dd..344860ed7 100755 --- a/src/modules/graphic/osggraph/OsgFX/OsgShader.h +++ b/src/modules/graphic/osggraph/OsgFX/OsgShader.h @@ -16,23 +16,37 @@ * * ***************************************************************************/ +#ifndef _OSGSHADERS_H_ +#define _OSGSHADERS_H_ + +class SDCar; + class SDCarShader{ private : + SDCar * pSdCar; + osg::ref_ptr program ; osg::ref_ptr pCar; + osg::ref_ptr stateset; osg::ref_ptr diffuseMap; - osg::ref_ptr reflectionMap; osg::ref_ptr specularColor; osg::ref_ptr lightVector; osg::ref_ptr lightPower; osg::ref_ptr ambientColor; osg::ref_ptr shininess; + osg::ref_ptr reflectionMappingMethod; + osg::ref_ptr reflectionMapCube; + osg::ref_ptr reflectionMap2DSampler; + osg::ref_ptr reflectionMapStaticOffsetCoords; + public : - SDCarShader(osg::Node *car); + SDCarShader(osg::Node *car, SDCar *c); void update(osg::Matrixf view); }; + +#endif //_OSGSHADERS_H_ diff --git a/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.cpp b/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.cpp index 2054c31d8..ef12cf4f3 100755 --- a/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.cpp +++ b/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.cpp @@ -49,21 +49,11 @@ SDDebugHUD::SDDebugHUD(){ HUD_camera->setNodeMask(0); } -void SDDebugHUD::setTexture(osg::ref_ptr map){ +void SDDebugHUD::setTexture(osg::ref_ptr map){ osg::StateSet* stateset = HUD_camera->getOrCreateStateSet(); stateset->setTextureAttributeAndModes( 0,map); } -void SDDebugHUD::setTexture(osg::ref_ptr map){ - osg::StateSet* stateset = HUD_camera->getOrCreateStateSet(); - /* osg::ref_ptr lm = new osg::Texture2D; - lm->setTextureSize( 256, 256 ); - lm->setInternalFormat( GL_RGB ); - lm->setDataVariance(osg::Object::DYNAMIC); - lm->setImage(map->getImage(3));*/ - 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 69b296d35..e219c418c 100755 --- a/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.h +++ b/src/modules/graphic/osggraph/OsgView/OsgDebugHUD.h @@ -27,8 +27,7 @@ class SDDebugHUD public: void toggleHUD(); - void setTexture(osg::ref_ptr map); - void setTexture(osg::ref_ptr map); + void setTexture(osg::ref_ptr map); SDDebugHUD(); ~SDDebugHUD();