| [13041] | 1 | |
|---|
| [5641] | 2 | |
|---|
| [13041] | 3 | |
|---|
| 4 | |
|---|
| [5641] | 5 | |
|---|
| 6 | |
|---|
| [13041] | 7 | |
|---|
| [5641] | 8 | |
|---|
| 9 | |
|---|
| [13041] | 10 | |
|---|
| [5641] | 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgShadow/ShadowMap> |
|---|
| [6233] | 15 | #include <osgShadow/ShadowedScene> |
|---|
| [5641] | 16 | #include <osg/Notify> |
|---|
| [6233] | 17 | #include <osg/ComputeBoundsVisitor> |
|---|
| 18 | #include <osg/PolygonOffset> |
|---|
| 19 | #include <osg/CullFace> |
|---|
| 20 | #include <osg/io_utils> |
|---|
| [5641] | 21 | |
|---|
| 22 | using namespace osgShadow; |
|---|
| 23 | |
|---|
| [7539] | 24 | #include <iostream> |
|---|
| 25 | |
|---|
| 26 | #include <osg/LightSource> |
|---|
| 27 | #include <osg/PolygonMode> |
|---|
| 28 | #include <osg/Geometry> |
|---|
| 29 | #include <osgDB/ReadFile> |
|---|
| 30 | #include <osgText/Text> |
|---|
| [6233] | 31 | |
|---|
| [7913] | 32 | #define IMPROVE_TEXGEN_PRECISION 1 |
|---|
| 33 | |
|---|
| [7692] | 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| [13041] | 37 | static const char fragmentShaderSource_noBaseTexture[] = |
|---|
| [7692] | 38 | "uniform sampler2DShadow osgShadow_shadowTexture; \n" |
|---|
| 39 | "uniform vec2 osgShadow_ambientBias; \n" |
|---|
| 40 | "\n" |
|---|
| 41 | "void main(void) \n" |
|---|
| 42 | "{ \n" |
|---|
| 43 | " gl_FragColor = gl_Color * (osgShadow_ambientBias.x + shadow2DProj( osgShadow_shadowTexture, gl_TexCoord[0] ) * osgShadow_ambientBias.y); \n" |
|---|
| 44 | "}\n"; |
|---|
| [6233] | 45 | |
|---|
| [7692] | 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| [13041] | 49 | static const char fragmentShaderSource_withBaseTexture[] = |
|---|
| [7692] | 50 | "uniform sampler2D osgShadow_baseTexture; \n" |
|---|
| 51 | "uniform sampler2DShadow osgShadow_shadowTexture; \n" |
|---|
| 52 | "uniform vec2 osgShadow_ambientBias; \n" |
|---|
| 53 | "\n" |
|---|
| 54 | "void main(void) \n" |
|---|
| 55 | "{ \n" |
|---|
| 56 | " vec4 color = gl_Color * texture2D( osgShadow_baseTexture, gl_TexCoord[0].xy ); \n" |
|---|
| 57 | " gl_FragColor = color * (osgShadow_ambientBias.x + shadow2DProj( osgShadow_shadowTexture, gl_TexCoord[1] ) * osgShadow_ambientBias.y); \n" |
|---|
| 58 | "}\n"; |
|---|
| [6233] | 59 | |
|---|
| [7692] | 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | static const char fragmentShaderSource_debugHUD_texcoord[] = |
|---|
| 64 | "uniform sampler2D osgShadow_shadowTexture; \n" |
|---|
| 65 | " \n" |
|---|
| 66 | "void main(void) \n" |
|---|
| 67 | "{ \n" |
|---|
| 68 | " vec4 texCoord = gl_TexCoord[1].xyzw; \n" |
|---|
| 69 | " float value = texCoord.z / texCoord.w; \n" |
|---|
| 70 | " gl_FragColor = vec4( value, value, value, 1.0 ); \n" |
|---|
| 71 | "} \n"; |
|---|
| [7545] | 72 | |
|---|
| [7692] | 73 | static const char fragmentShaderSource_debugHUD[] = |
|---|
| 74 | "uniform sampler2D osgShadow_shadowTexture; \n" |
|---|
| 75 | " \n" |
|---|
| 76 | "void main(void) \n" |
|---|
| 77 | "{ \n" |
|---|
| 78 | " vec4 texResult = texture2D(osgShadow_shadowTexture, gl_TexCoord[0].st ); \n" |
|---|
| [7913] | 79 | " float value = texResult.r; \n" |
|---|
| [7692] | 80 | " gl_FragColor = vec4( value, value, value, 0.8 ); \n" |
|---|
| 81 | "} \n"; |
|---|
| [7545] | 82 | |
|---|
| [7692] | 83 | ShadowMap::ShadowMap(): |
|---|
| [7974] | 84 | _baseTextureUnit(0), |
|---|
| [7692] | 85 | _shadowTextureUnit(1), |
|---|
| [9062] | 86 | _polyOffset(1.0,1.0), |
|---|
| [7692] | 87 | _ambientBias(0.5f,0.5f), |
|---|
| [9062] | 88 | _textureSize(1024,1024) |
|---|
| [7692] | 89 | { |
|---|
| 90 | } |
|---|
| [5641] | 91 | |
|---|
| [7692] | 92 | ShadowMap::ShadowMap(const ShadowMap& copy, const osg::CopyOp& copyop): |
|---|
| 93 | ShadowTechnique(copy,copyop), |
|---|
| 94 | _baseTextureUnit(copy._baseTextureUnit), |
|---|
| 95 | _shadowTextureUnit(copy._shadowTextureUnit), |
|---|
| [9062] | 96 | _polyOffset(copy._polyOffset), |
|---|
| [7692] | 97 | _ambientBias(copy._ambientBias), |
|---|
| [9062] | 98 | _textureSize(copy._textureSize) |
|---|
| [7692] | 99 | { |
|---|
| 100 | } |
|---|
| [6233] | 101 | |
|---|
| [7692] | 102 | void ShadowMap::setTextureUnit(unsigned int unit) |
|---|
| 103 | { |
|---|
| 104 | _shadowTextureUnit = unit; |
|---|
| 105 | } |
|---|
| [6233] | 106 | |
|---|
| [7974] | 107 | void ShadowMap::setPolygonOffset(const osg::Vec2& polyOffset) |
|---|
| 108 | { |
|---|
| 109 | _polyOffset = polyOffset; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| [7692] | 112 | void ShadowMap::setAmbientBias(const osg::Vec2& ambientBias) |
|---|
| 113 | { |
|---|
| 114 | _ambientBias = ambientBias; |
|---|
| 115 | if (_ambientBiasUniform.valid()) _ambientBiasUniform->set(_ambientBias); |
|---|
| 116 | } |
|---|
| [6991] | 117 | |
|---|
| [7692] | 118 | void ShadowMap::setTextureSize(const osg::Vec2s& textureSize) |
|---|
| 119 | { |
|---|
| 120 | _textureSize = textureSize; |
|---|
| 121 | dirty(); |
|---|
| 122 | } |
|---|
| [6233] | 123 | |
|---|
| [7692] | 124 | void ShadowMap::setLight(osg::Light* light) |
|---|
| 125 | { |
|---|
| 126 | _light = light; |
|---|
| 127 | } |
|---|
| [6233] | 128 | |
|---|
| 129 | |
|---|
| [7692] | 130 | void ShadowMap::setLight(osg::LightSource* ls) |
|---|
| 131 | { |
|---|
| 132 | _ls = ls; |
|---|
| 133 | _light = _ls->getLight(); |
|---|
| 134 | } |
|---|
| [6233] | 135 | |
|---|
| [7692] | 136 | void ShadowMap::createUniforms() |
|---|
| 137 | { |
|---|
| 138 | _uniformList.clear(); |
|---|
| [6233] | 139 | |
|---|
| [7692] | 140 | osg::Uniform* baseTextureSampler = new osg::Uniform("osgShadow_baseTexture",(int)_baseTextureUnit); |
|---|
| 141 | _uniformList.push_back(baseTextureSampler); |
|---|
| [6233] | 142 | |
|---|
| [7692] | 143 | osg::Uniform* shadowTextureSampler = new osg::Uniform("osgShadow_shadowTexture",(int)_shadowTextureUnit); |
|---|
| 144 | _uniformList.push_back(shadowTextureSampler); |
|---|
| [6233] | 145 | |
|---|
| [7692] | 146 | _ambientBiasUniform = new osg::Uniform("osgShadow_ambientBias",_ambientBias); |
|---|
| 147 | _uniformList.push_back(_ambientBiasUniform.get()); |
|---|
| [6233] | 148 | |
|---|
| [7692] | 149 | } |
|---|
| [6233] | 150 | |
|---|
| [7692] | 151 | void ShadowMap::createShaders() |
|---|
| 152 | { |
|---|
| 153 | |
|---|
| 154 | if( _shaderList.empty() ) |
|---|
| [7539] | 155 | { |
|---|
| [7692] | 156 | if (_shadowTextureUnit==0) |
|---|
| [7539] | 157 | { |
|---|
| [7692] | 158 | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_noBaseTexture); |
|---|
| 159 | _shaderList.push_back(fragment_shader); |
|---|
| 160 | } |
|---|
| 161 | else |
|---|
| 162 | { |
|---|
| 163 | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_withBaseTexture); |
|---|
| 164 | _shaderList.push_back(fragment_shader); |
|---|
| [6233] | 165 | |
|---|
| [7539] | 166 | } |
|---|
| 167 | } |
|---|
| [7692] | 168 | } |
|---|
| [6233] | 169 | |
|---|
| [7692] | 170 | void ShadowMap::init() |
|---|
| 171 | { |
|---|
| 172 | if (!_shadowedScene) return; |
|---|
| [6233] | 173 | |
|---|
| [7692] | 174 | _texture = new osg::Texture2D; |
|---|
| 175 | _texture->setTextureSize(_textureSize.x(), _textureSize.y()); |
|---|
| 176 | _texture->setInternalFormat(GL_DEPTH_COMPONENT); |
|---|
| 177 | _texture->setShadowComparison(true); |
|---|
| 178 | _texture->setShadowTextureMode(osg::Texture2D::LUMINANCE); |
|---|
| 179 | _texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 180 | _texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| [6233] | 181 | |
|---|
| [7692] | 182 | |
|---|
| 183 | _texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER); |
|---|
| 184 | _texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER); |
|---|
| 185 | _texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| [7545] | 186 | |
|---|
| [7692] | 187 | |
|---|
| 188 | { |
|---|
| 189 | |
|---|
| 190 | _camera = new osg::Camera; |
|---|
| [6233] | 191 | |
|---|
| [7913] | 192 | _camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF_INHERIT_VIEWPOINT); |
|---|
| 193 | |
|---|
| [7692] | 194 | _camera->setCullCallback(new CameraCullCallback(this)); |
|---|
| [7539] | 195 | |
|---|
| [7692] | 196 | _camera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 197 | |
|---|
| 198 | _camera->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 199 | _camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); |
|---|
| [7539] | 200 | |
|---|
| [7692] | 201 | |
|---|
| 202 | _camera->setViewport(0,0,_textureSize.x(),_textureSize.y()); |
|---|
| [7539] | 203 | |
|---|
| [7692] | 204 | |
|---|
| 205 | _camera->setRenderOrder(osg::Camera::PRE_RENDER); |
|---|
| [7539] | 206 | |
|---|
| [7692] | 207 | |
|---|
| 208 | _camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); |
|---|
| 209 | |
|---|
| [7539] | 210 | |
|---|
| [7692] | 211 | |
|---|
| 212 | _camera->attach(osg::Camera::DEPTH_BUFFER, _texture.get()); |
|---|
| [7539] | 213 | |
|---|
| [7692] | 214 | osg::StateSet* stateset = _camera->getOrCreateStateSet(); |
|---|
| [7539] | 215 | |
|---|
| 216 | |
|---|
| [7690] | 217 | #if 1 |
|---|
| [7692] | 218 | |
|---|
| [7690] | 219 | |
|---|
| [13041] | 220 | |
|---|
| [7692] | 221 | osg::ref_ptr<osg::CullFace> cull_face = new osg::CullFace; |
|---|
| 222 | cull_face->setMode(osg::CullFace::FRONT); |
|---|
| 223 | stateset->setAttribute(cull_face.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 224 | stateset->setMode(GL_CULL_FACE, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| [7690] | 225 | |
|---|
| [7692] | 226 | |
|---|
| 227 | |
|---|
| [7974] | 228 | float factor = -_polyOffset[0]; |
|---|
| 229 | float units = -_polyOffset[1]; |
|---|
| [7690] | 230 | |
|---|
| [7692] | 231 | osg::ref_ptr<osg::PolygonOffset> polygon_offset = new osg::PolygonOffset; |
|---|
| 232 | polygon_offset->setFactor(factor); |
|---|
| 233 | polygon_offset->setUnits(units); |
|---|
| 234 | stateset->setAttribute(polygon_offset.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 235 | stateset->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| [7690] | 236 | #else |
|---|
| [7692] | 237 | |
|---|
| 238 | stateset->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE); |
|---|
| [7539] | 239 | |
|---|
| [7692] | 240 | |
|---|
| 241 | |
|---|
| [7974] | 242 | float factor = _polyOffset[0]; |
|---|
| 243 | float units = _polyOffset[1]; |
|---|
| [7539] | 244 | |
|---|
| [7692] | 245 | osg::ref_ptr<osg::PolygonOffset> polygon_offset = new osg::PolygonOffset; |
|---|
| 246 | polygon_offset->setFactor(factor); |
|---|
| 247 | polygon_offset->setUnits(units); |
|---|
| 248 | stateset->setAttribute(polygon_offset.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 249 | stateset->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| [7690] | 250 | #endif |
|---|
| [7692] | 251 | } |
|---|
| [7539] | 252 | |
|---|
| [7692] | 253 | { |
|---|
| [13041] | 254 | _stateset = new osg::StateSet; |
|---|
| [7692] | 255 | _stateset->setTextureAttributeAndModes(_shadowTextureUnit,_texture.get(),osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 256 | _stateset->setTextureMode(_shadowTextureUnit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 257 | _stateset->setTextureMode(_shadowTextureUnit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 258 | _stateset->setTextureMode(_shadowTextureUnit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 259 | _stateset->setTextureMode(_shadowTextureUnit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON); |
|---|
| [6233] | 260 | |
|---|
| [7692] | 261 | _texgen = new osg::TexGen; |
|---|
| [6233] | 262 | |
|---|
| [7692] | 263 | |
|---|
| 264 | _program = new osg::Program; |
|---|
| 265 | _stateset->setAttribute(_program.get()); |
|---|
| [7539] | 266 | |
|---|
| [7692] | 267 | |
|---|
| 268 | createShaders(); |
|---|
| [7539] | 269 | |
|---|
| [7692] | 270 | |
|---|
| 271 | for(ShaderList::const_iterator itr=_shaderList.begin(); |
|---|
| 272 | itr!=_shaderList.end(); |
|---|
| 273 | ++itr) |
|---|
| 274 | { |
|---|
| 275 | _program->addShader(itr->get()); |
|---|
| 276 | } |
|---|
| [7539] | 277 | |
|---|
| [7692] | 278 | |
|---|
| 279 | createUniforms(); |
|---|
| [7539] | 280 | |
|---|
| [7692] | 281 | |
|---|
| 282 | for(UniformList::const_iterator itr=_uniformList.begin(); |
|---|
| 283 | itr!=_uniformList.end(); |
|---|
| 284 | ++itr) |
|---|
| 285 | { |
|---|
| 286 | _stateset->addUniform(itr->get()); |
|---|
| 287 | } |
|---|
| [7539] | 288 | |
|---|
| [7692] | 289 | { |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| [7635] | 294 | |
|---|
| [7692] | 295 | |
|---|
| 296 | |
|---|
| [7635] | 297 | |
|---|
| [7692] | 298 | osg::Image* image = new osg::Image; |
|---|
| 299 | |
|---|
| 300 | int noPixels = 1; |
|---|
| 301 | image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT); |
|---|
| 302 | image->setInternalTextureFormat(GL_RGBA); |
|---|
| 303 | |
|---|
| 304 | osg::Vec4* dataPtr = (osg::Vec4*)image->data(); |
|---|
| 305 | osg::Vec4 color(1,1,1,1); |
|---|
| 306 | *dataPtr = color; |
|---|
| 307 | |
|---|
| 308 | osg::Texture2D* fakeTex = new osg::Texture2D; |
|---|
| [7811] | 309 | fakeTex->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_EDGE); |
|---|
| 310 | fakeTex->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_EDGE); |
|---|
| [7692] | 311 | fakeTex->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 312 | fakeTex->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| 313 | fakeTex->setImage(image); |
|---|
| 314 | |
|---|
| 315 | _stateset->setTextureAttribute(_baseTextureUnit,fakeTex,osg::StateAttribute::ON); |
|---|
| 316 | _stateset->setTextureMode(_baseTextureUnit,GL_TEXTURE_2D,osg::StateAttribute::ON); |
|---|
| 317 | _stateset->setTextureMode(_baseTextureUnit,GL_TEXTURE_3D,osg::StateAttribute::OFF); |
|---|
| [10699] | 318 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) |
|---|
| 319 | _stateset->setTextureMode(_baseTextureUnit,GL_TEXTURE_1D,osg::StateAttribute::OFF); |
|---|
| 320 | #endif |
|---|
| [6233] | 321 | } |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| [7692] | 324 | _dirty = false; |
|---|
| 325 | } |
|---|
| [6233] | 326 | |
|---|
| 327 | |
|---|
| [7692] | 328 | void ShadowMap::update(osg::NodeVisitor& nv) |
|---|
| 329 | { |
|---|
| 330 | _shadowedScene->osg::Group::traverse(nv); |
|---|
| 331 | } |
|---|
| [6233] | 332 | |
|---|
| [7692] | 333 | void ShadowMap::cull(osgUtil::CullVisitor& cv) |
|---|
| 334 | { |
|---|
| 335 | |
|---|
| 336 | unsigned int traversalMask = cv.getTraversalMask(); |
|---|
| [6233] | 337 | |
|---|
| [7692] | 338 | osgUtil::RenderStage* orig_rs = cv.getRenderStage(); |
|---|
| [6233] | 339 | |
|---|
| [7974] | 340 | |
|---|
| [7692] | 341 | { |
|---|
| 342 | cv.pushStateSet(_stateset.get()); |
|---|
| [6233] | 343 | |
|---|
| [7692] | 344 | _shadowedScene->osg::Group::traverse(cv); |
|---|
| [6233] | 345 | |
|---|
| [7692] | 346 | cv.popStateSet(); |
|---|
| [7539] | 347 | |
|---|
| [7692] | 348 | } |
|---|
| [7539] | 349 | |
|---|
| [7692] | 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| [7539] | 353 | |
|---|
| [7692] | 354 | const osg::Light* selectLight = 0; |
|---|
| 355 | osg::Vec4 lightpos; |
|---|
| 356 | osg::Vec3 lightDir; |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | osgUtil::PositionalStateContainer::AttrMatrixList& aml = orig_rs->getPositionalStateContainer()->getAttrMatrixList(); |
|---|
| 360 | for(osgUtil::PositionalStateContainer::AttrMatrixList::iterator itr = aml.begin(); |
|---|
| 361 | itr != aml.end(); |
|---|
| 362 | ++itr) |
|---|
| 363 | { |
|---|
| 364 | const osg::Light* light = dynamic_cast<const osg::Light*>(itr->first.get()); |
|---|
| 365 | if (light) |
|---|
| [6233] | 366 | { |
|---|
| [7692] | 367 | if( _light.valid()) { |
|---|
| 368 | if( _light.get() == light ) |
|---|
| 369 | selectLight = light; |
|---|
| [7539] | 370 | else |
|---|
| [7692] | 371 | continue; |
|---|
| 372 | } |
|---|
| 373 | else |
|---|
| 374 | selectLight = light; |
|---|
| [6233] | 375 | |
|---|
| [7692] | 376 | osg::RefMatrix* matrix = itr->second.get(); |
|---|
| [13041] | 377 | if (matrix) |
|---|
| [7692] | 378 | { |
|---|
| 379 | lightpos = light->getPosition() * (*matrix); |
|---|
| [7913] | 380 | lightDir = osg::Matrix::transform3x3( light->getDirection(), *matrix ); |
|---|
| [7692] | 381 | } |
|---|
| [13041] | 382 | else |
|---|
| [7692] | 383 | { |
|---|
| 384 | lightpos = light->getPosition(); |
|---|
| 385 | lightDir = light->getDirection(); |
|---|
| 386 | } |
|---|
| [7539] | 387 | |
|---|
| [6233] | 388 | } |
|---|
| [7692] | 389 | } |
|---|
| [7539] | 390 | |
|---|
| [7692] | 391 | osg::Matrix eyeToWorld; |
|---|
| 392 | eyeToWorld.invert(*cv.getModelViewMatrix()); |
|---|
| [7539] | 393 | |
|---|
| [13041] | 394 | lightpos = lightpos * eyeToWorld; |
|---|
| [7913] | 395 | lightDir = osg::Matrix::transform3x3( lightDir, eyeToWorld ); |
|---|
| [7692] | 396 | lightDir.normalize(); |
|---|
| 397 | |
|---|
| 398 | if (selectLight) |
|---|
| 399 | { |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | const_cast<osg::Light*>(selectLight)->setAmbient(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| [8886] | 406 | float fov = selectLight->getSpotCutoff() * 2; |
|---|
| 407 | if(fov < 180.0f) |
|---|
| [7539] | 408 | { |
|---|
| [7692] | 409 | osg::Vec3 position(lightpos.x(), lightpos.y(), lightpos.z()); |
|---|
| [8886] | 410 | _camera->setProjectionMatrixAsPerspective(fov, 1.0, 0.1, 1000.0); |
|---|
| [11137] | 411 | _camera->setViewMatrixAsLookAt(position,position+lightDir,computeOrthogonalVector(lightDir)); |
|---|
| [7692] | 412 | } |
|---|
| 413 | else |
|---|
| 414 | { |
|---|
| [13041] | 415 | |
|---|
| [7692] | 416 | osg::ComputeBoundsVisitor cbbv(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN); |
|---|
| 417 | cbbv.setTraversalMask(getShadowedScene()->getCastsShadowTraversalMask()); |
|---|
| [7539] | 418 | |
|---|
| [7692] | 419 | _shadowedScene->osg::Group::traverse(cbbv); |
|---|
| [7690] | 420 | |
|---|
| [7692] | 421 | osg::BoundingBox bb = cbbv.getBoundingBox(); |
|---|
| [7539] | 422 | |
|---|
| [7692] | 423 | if (lightpos[3]!=0.0) |
|---|
| [7539] | 424 | { |
|---|
| 425 | osg::Vec3 position(lightpos.x(), lightpos.y(), lightpos.z()); |
|---|
| 426 | |
|---|
| [7692] | 427 | float centerDistance = (position-bb.center()).length(); |
|---|
| [7539] | 428 | |
|---|
| [7692] | 429 | float znear = centerDistance-bb.radius(); |
|---|
| 430 | float zfar = centerDistance+bb.radius(); |
|---|
| 431 | float zNearRatio = 0.001f; |
|---|
| 432 | if (znear<zfar*zNearRatio) znear = zfar*zNearRatio; |
|---|
| [7539] | 433 | |
|---|
| [7692] | 434 | float top = (bb.radius()/centerDistance)*znear; |
|---|
| 435 | float right = top; |
|---|
| [7539] | 436 | |
|---|
| [7692] | 437 | _camera->setProjectionMatrixAsFrustum(-right,right,-top,top,znear,zfar); |
|---|
| [11137] | 438 | _camera->setViewMatrixAsLookAt(position,bb.center(),computeOrthogonalVector(bb.center()-position)); |
|---|
| [7692] | 439 | } |
|---|
| 440 | else |
|---|
| 441 | { |
|---|
| 442 | |
|---|
| 443 | osg::Vec3 lightDir(lightpos.x(), lightpos.y(), lightpos.z()); |
|---|
| 444 | lightDir.normalize(); |
|---|
| [7539] | 445 | |
|---|
| [7692] | 446 | |
|---|
| [7703] | 447 | osg::Vec3 position = bb.center() + lightDir * bb.radius() * 2; |
|---|
| [7539] | 448 | |
|---|
| [7692] | 449 | float centerDistance = (position-bb.center()).length(); |
|---|
| [7539] | 450 | |
|---|
| [7692] | 451 | float znear = centerDistance-bb.radius(); |
|---|
| 452 | float zfar = centerDistance+bb.radius(); |
|---|
| 453 | float zNearRatio = 0.001f; |
|---|
| 454 | if (znear<zfar*zNearRatio) znear = zfar*zNearRatio; |
|---|
| [7539] | 455 | |
|---|
| [7692] | 456 | float top = bb.radius(); |
|---|
| 457 | float right = top; |
|---|
| [7539] | 458 | |
|---|
| [7692] | 459 | _camera->setProjectionMatrixAsOrtho(-right, right, -top, top, znear, zfar); |
|---|
| [11137] | 460 | _camera->setViewMatrixAsLookAt(position,bb.center(),computeOrthogonalVector(lightDir)); |
|---|
| [7692] | 461 | } |
|---|
| [7539] | 462 | |
|---|
| 463 | |
|---|
| [7692] | 464 | } |
|---|
| [7539] | 465 | |
|---|
| [13041] | 466 | cv.setTraversalMask( traversalMask & |
|---|
| [7692] | 467 | getShadowedScene()->getCastsShadowTraversalMask() ); |
|---|
| [7545] | 468 | |
|---|
| [7692] | 469 | |
|---|
| 470 | _camera->accept(cv); |
|---|
| [7539] | 471 | |
|---|
| [7913] | 472 | _texgen->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 473 | |
|---|
| 474 | #if IMPROVE_TEXGEN_PRECISION |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | _texgen->setPlanesFromMatrix( _camera->getProjectionMatrix() * |
|---|
| 479 | osg::Matrix::translate(1.0,1.0,1.0) * |
|---|
| 480 | osg::Matrix::scale(0.5f,0.5f,0.5f) ); |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | osg::RefMatrix * refMatrix = new osg::RefMatrix |
|---|
| 484 | ( _camera->getInverseViewMatrix() * *cv.getModelViewMatrix() ); |
|---|
| 485 | |
|---|
| 486 | cv.getRenderStage()->getPositionalStateContainer()-> |
|---|
| 487 | addPositionedTextureAttribute( _shadowTextureUnit, refMatrix, _texgen.get() ); |
|---|
| [13041] | 488 | #else |
|---|
| [7913] | 489 | |
|---|
| 490 | |
|---|
| [13041] | 491 | osg::Matrix MVPT = _camera->getViewMatrix() * |
|---|
| [7913] | 492 | _camera->getProjectionMatrix() * |
|---|
| 493 | osg::Matrix::translate(1.0,1.0,1.0) * |
|---|
| 494 | osg::Matrix::scale(0.5f,0.5f,0.5f); |
|---|
| 495 | |
|---|
| 496 | _texgen->setPlanesFromMatrix(MVPT); |
|---|
| 497 | |
|---|
| [7692] | 498 | orig_rs->getPositionalStateContainer()->addPositionedTextureAttribute(_shadowTextureUnit, cv.getModelViewMatrix(), _texgen.get()); |
|---|
| [7913] | 499 | #endif |
|---|
| [7692] | 500 | } |
|---|
| [7539] | 501 | |
|---|
| 502 | |
|---|
| [7692] | 503 | |
|---|
| 504 | cv.setTraversalMask( traversalMask ); |
|---|
| 505 | } |
|---|
| [7539] | 506 | |
|---|
| [7692] | 507 | void ShadowMap::cleanSceneGraph() |
|---|
| 508 | { |
|---|
| 509 | } |
|---|
| [7539] | 510 | |
|---|
| [7692] | 511 | |
|---|
| [7539] | 512 | |
|---|
| [7913] | 513 | |
|---|
| 514 | |
|---|
| [13041] | 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| [7913] | 518 | |
|---|
| [13041] | 519 | class ShadowMap::DrawableDrawWithDepthShadowComparisonOffCallback: |
|---|
| [7913] | 520 | public osg::Drawable::DrawCallback |
|---|
| 521 | { |
|---|
| 522 | public: |
|---|
| [13041] | 523 | |
|---|
| [7913] | 524 | DrawableDrawWithDepthShadowComparisonOffCallback |
|---|
| 525 | ( osg::Texture2D * texture, unsigned stage = 0 ) |
|---|
| 526 | : _texture( texture ), _stage( stage ) |
|---|
| 527 | { |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | virtual void drawImplementation |
|---|
| 531 | ( osg::RenderInfo & ri,const osg::Drawable* drawable ) const |
|---|
| 532 | { |
|---|
| 533 | if( _texture.valid() ) { |
|---|
| 534 | |
|---|
| 535 | ri.getState()->applyTextureAttribute( _stage, _texture.get() ); |
|---|
| 536 | |
|---|
| 537 | |
|---|
| [13041] | 538 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, |
|---|
| [7913] | 539 | GL_NONE ); |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | drawable->drawImplementation(ri); |
|---|
| 543 | |
|---|
| 544 | if( _texture.valid() ) { |
|---|
| 545 | |
|---|
| [13041] | 546 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, |
|---|
| [7913] | 547 | GL_COMPARE_R_TO_TEXTURE_ARB ); |
|---|
| 548 | } |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| [9062] | 551 | osg::ref_ptr< osg::Texture2D > _texture; |
|---|
| [7913] | 552 | unsigned _stage; |
|---|
| 553 | }; |
|---|
| [9540] | 554 | |
|---|
| [7913] | 555 | |
|---|
| [7692] | 556 | osg::ref_ptr<osg::Camera> ShadowMap::makeDebugHUD() |
|---|
| 557 | { |
|---|
| [7913] | 558 | |
|---|
| 559 | if( !_texture.valid() ) init(); |
|---|
| 560 | |
|---|
| [7692] | 561 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| [6233] | 562 | |
|---|
| [7692] | 563 | osg::Vec2 size(1280, 1024); |
|---|
| 564 | |
|---|
| 565 | camera->setProjectionMatrix(osg::Matrix::ortho2D(0,size.x(),0,size.y())); |
|---|
| [6233] | 566 | |
|---|
| [13041] | 567 | |
|---|
| [7692] | 568 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 569 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| [6233] | 570 | |
|---|
| [7692] | 571 | |
|---|
| 572 | camera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 573 | camera->setClearColor(osg::Vec4(0.2f, 0.3f, 0.5f, 0.2f)); |
|---|
| 574 | |
|---|
| [6233] | 575 | |
|---|
| [7692] | 576 | |
|---|
| 577 | camera->setRenderOrder(osg::Camera::POST_RENDER); |
|---|
| [6233] | 578 | |
|---|
| [7692] | 579 | |
|---|
| 580 | camera->setAllowEventFocus(false); |
|---|
| [6233] | 581 | |
|---|
| [7692] | 582 | osg::Geode* geode = new osg::Geode; |
|---|
| [6233] | 583 | |
|---|
| [7692] | 584 | osg::Vec3 position(10.0f,size.y()-100.0f,0.0f); |
|---|
| [13041] | 585 | osg::Vec3 delta(0.0f,-120.0f,0.0f); |
|---|
| [7913] | 586 | float length = 300.0f; |
|---|
| [6233] | 587 | |
|---|
| [7692] | 588 | |
|---|
| 589 | #if 0 |
|---|
| 590 | std::string timesFont("fonts/arial.ttf"); |
|---|
| [6233] | 591 | |
|---|
| [7692] | 592 | { |
|---|
| 593 | osgText::Text* text = new osgText::Text; |
|---|
| 594 | geode->addDrawable( text ); |
|---|
| [6289] | 595 | |
|---|
| [7692] | 596 | text->setFont(timesFont); |
|---|
| 597 | text->setPosition(position); |
|---|
| 598 | text->setText("Shadow Map HUD"); |
|---|
| [6289] | 599 | |
|---|
| [7692] | 600 | position += delta; |
|---|
| 601 | } |
|---|
| 602 | #endif |
|---|
| [6289] | 603 | |
|---|
| [7913] | 604 | osg::Vec3 widthVec(length, 0.0f, 0.0f); |
|---|
| 605 | osg::Vec3 depthVec(0.0f,length, 0.0f); |
|---|
| 606 | osg::Vec3 centerBase( 10.0f + length/2, size.y()-length/2, 0.0f); |
|---|
| [7692] | 607 | centerBase += delta; |
|---|
| [6289] | 608 | |
|---|
| [7913] | 609 | osg::Geometry *geometry = osg::createTexturedQuadGeometry |
|---|
| 610 | ( centerBase-widthVec*0.5f-depthVec*0.5f, widthVec, depthVec ); |
|---|
| [7539] | 611 | |
|---|
| [7913] | 612 | geode->addDrawable( geometry ); |
|---|
| 613 | |
|---|
| 614 | geometry->setDrawCallback |
|---|
| 615 | ( new DrawableDrawWithDepthShadowComparisonOffCallback( _texture.get() ) ); |
|---|
| 616 | |
|---|
| [7692] | 617 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| [6289] | 618 | |
|---|
| [7692] | 619 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 620 | stateset->setMode(GL_BLEND,osg::StateAttribute::ON); |
|---|
| 621 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| [6289] | 622 | |
|---|
| [7692] | 623 | |
|---|
| [7913] | 624 | |
|---|
| [6233] | 625 | |
|---|
| [7913] | 626 | stateset->setTextureAttributeAndModes(0,_texture.get(),osg::StateAttribute::ON); |
|---|
| [6233] | 627 | |
|---|
| [7692] | 628 | |
|---|
| [7545] | 629 | #if 0 |
|---|
| [7692] | 630 | stateset->setTextureMode(_shadowTextureUnit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 631 | stateset->setTextureMode(_shadowTextureUnit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 632 | stateset->setTextureMode(_shadowTextureUnit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 633 | stateset->setTextureMode(_shadowTextureUnit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON); |
|---|
| [7539] | 634 | |
|---|
| [7692] | 635 | |
|---|
| 636 | osg::ref_ptr<osg::TexGenNode> texGenNode = new osg::TexGenNode; |
|---|
| 637 | texGenNode->setTextureUnit(_shadowTextureUnit); |
|---|
| 638 | texGenNode->setTexGen(_texgen.get()); |
|---|
| 639 | camera->addChild(texGenNode.get()); |
|---|
| [7545] | 640 | #endif |
|---|
| [7692] | 641 | |
|---|
| [7545] | 642 | |
|---|
| [7692] | 643 | osg::ref_ptr<osg::Program> program = new osg::Program; |
|---|
| 644 | stateset->setAttribute(program.get()); |
|---|
| [7545] | 645 | |
|---|
| [7692] | 646 | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_debugHUD); |
|---|
| 647 | program->addShader(fragment_shader); |
|---|
| [7539] | 648 | |
|---|
| [7692] | 649 | camera->addChild(geode); |
|---|
| [6233] | 650 | |
|---|
| [7692] | 651 | return camera; |
|---|
| 652 | } |
|---|
| [6233] | 653 | |
|---|
| [7692] | 654 | |
|---|