29 #define _def_GodRays_Projector_Camera_Name "_Hydrax_GodRays_Projector_Camera"
30 #define _def_GodRays_ManualObject_Name "_Hydrax_GodRays_ManualObject"
31 #define _def_GodRays_Depth_Map "_Hydrax_GodRays_Depth_Map"
33 #define _def_GodRays_Material_Name "_Hydrax_GodRays_Material"
34 #define _def_GodRays_Shader_VP_Name "_Hydrax_GodRays_VP"
35 #define _def_GodRays_Shader_FP_Name "_Hydrax_GodRays_FP"
37 #define _def_GodRaysDepth_Material_Name "_Hydrax_GodRaysDepth_Material"
38 #define _def_GodRaysDepth_Shader_VP_Name "_Hydrax_GodRaysDepth_VP"
39 #define _def_GodRaysDepth_Shader_FP_Name "_Hydrax_GodRaysDepth_FP"
57 , mNoisePositionMultiplier(50)
58 , mNoiseYNormalMultiplier(10)
59 , mNoiseNormalMultiplier(0.175)
60 , mSimulationSpeed(5.0f)
63 , mObjectsIntersections(false)
66 for (
int k = 0; k < 2; k++)
109 std::vector<Ogre::Technique*>::iterator TechIt;
140 for (
int k = 0; k < 12; k++)
184 for (
int k = 0; k < 2; k++)
191 Ogre::RenderTarget* RT =
mProjectorRTT->getBuffer()->getRenderTarget();
192 RT->removeAllListeners();
193 RT->removeAllViewports();
195 Ogre::TextureManager::getSingleton().remove(
mProjectorRTT->getName());
231 const Ogre::Vector3 *FrustumCorners =
mProjectorCamera->getWorldSpaceCorners();
233 float FarWidth = (FrustumCorners[4] - FrustumCorners[5]).length();
239 Ogre::Real Dis, RayLength;
257 RayLength = RaysLength*(0.95+Pos.length());
297 Ogre::Real sqrt_NumberOfRays = Ogre::Math::Sqrt(
mNumberOfRays);
298 Ogre::Real XCoord = RayNumber;
300 while (XCoord >= sqrt_NumberOfRays)
302 XCoord -= sqrt_NumberOfRays;
305 Ogre::Vector2 RayPos =
307 static_cast<int>(XCoord),
309 static_cast<int>((RayNumber+sqrt_NumberOfRays)/sqrt_NumberOfRays)-1);
311 RayPos /= sqrt_NumberOfRays;
312 RayPos -= Ogre::Vector2(0.5, 0.5);
326 Ogre::Vector3 Normal = (p_x-m_x).crossProduct((p_y-m_y));
330 return RayPos + Ogre::Vector2(Normal.x, Normal.z);
366 Ogre::GpuProgramParametersSharedPtr VP_Parameters, FP_Parameters;
369 VP_Parameters =
mMaterials[0]->getTechnique(0)->getPass(0)->getVertexProgramParameters();
370 FP_Parameters =
mMaterials[0]->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
372 Ogre::Matrix4 TexViewProj =
377 VP_Parameters->setNamedConstant(
"uTexViewProj", TexViewProj);
379 FP_Parameters->setNamedConstant(
"uLightPosition",
mProjectorSN->getPosition());
380 FP_Parameters->setNamedConstant(
"uLightFarClipDistance",
mProjectorCamera->getFarClipDistance());
383 FP_Parameters =
mMaterials[1]->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
385 FP_Parameters->setNamedConstant(
"uLightPosition",
mProjectorSN->getPosition());
386 FP_Parameters->setNamedConstant(
"uLightFarClipDistance",
mProjectorCamera->getFarClipDistance());
388 std::vector<Ogre::Technique*>::iterator TechIt;
399 (*TechIt)->getPass(0)->getFragmentProgramParameters()->setNamedConstant(
"uLightPosition",
mProjectorSN->getPosition());
400 (*TechIt)->getPass(0)->getFragmentProgramParameters()->setNamedConstant(
"uLightFarClipDistance",
mProjectorCamera->getFarClipDistance());
407 const Ogre::Vector3& CameraPosition =
mHydrax->
getCamera()->getDerivedPosition();
410 Ogre::Ray SunToCameraRay = Ogre::Ray(SunPosition, CameraPosition-SunPosition);
412 Ogre::Vector3 WaterProjectionPoint = SunToCameraRay.getPoint(SunToCameraRay.intersects(WaterPlane).second);
415 mProjectorCamera->setFarClipDistance((WaterProjectionPoint-CameraPosition).length());
416 mProjectorSN->setDirection(-(WaterProjectionPoint-CameraPosition).normalisedCopy(), Ogre::Node::TS_WORLD);
421 Ogre::String VertexProgramData, FragmentProgramData;
422 Ogre::GpuProgramParametersSharedPtr VP_Parameters, FP_Parameters;
423 Ogre::String EntryPoints[2];
426 EntryPoints[0] = Ogre::String(
"main");
427 EntryPoints[1] = Ogre::String(
"main");
431 EntryPoints[0] = Ogre::String(
"main_vp");
432 EntryPoints[1] = Ogre::String(
"main_fp");
434 Ogre::String GpuProgramsData[2]; Ogre::String GpuProgramNames[2];
437 int NumberOfDepthChannels = 0;
438 Ogre::String GB[2] = {
"0, 1, 0",
"0, 0, 1"};
442 NumberOfDepthChannels++;
447 VertexProgramData =
"";
448 FragmentProgramData =
"";
460 "float4 iPosition : POSITION,\n" +
462 "out float4 oPosition : POSITION,\n";
465 VertexProgramData += Ogre::String(
466 "out float3 oPosition_ : TEXCOORD0,\n") +
467 "out float4 oProjUV : TEXCOORD1,\n" +
469 "uniform float4x4 uWorld,\n" +
470 "uniform float4x4 uTexViewProj,\n";
472 VertexProgramData += Ogre::String(
473 "uniform float4x4 uWorldViewProj)\n") +
475 "oPosition = mul(uWorldViewProj, iPosition);\n";
478 VertexProgramData += Ogre::String(
479 "float4 wPos = mul(uWorld, iPosition);\n")+
480 "oPosition_ = wPos.xyz;\n"+
481 "oProjUV = mul(uTexViewProj, wPos);\n";
490 VertexProgramData += Ogre::String(
"\n" );
494 VertexProgramData += Ogre::String(
495 "uniform mat4 uWorld;\n") +
496 "uniform mat4 uTexViewProj;\n";
502 VertexProgramData += Ogre::String(
503 "varying vec3 Position_;\n") +
504 "varying vec4 ProjUV;\n";
507 VertexProgramData += Ogre::String(
510 "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n";
513 VertexProgramData += Ogre::String(
514 "vec4 wPos = uWorld * gl_Vertex);\n")+
515 "Position_ = wPos.xyz;\n"+
516 "ProjUV = uTexViewProj * wPos;\n";
531 FragmentProgramData +=
535 "float3 iPosition : TEXCOORD0,\n" +
536 "float4 iProjUV : TEXCOORD1,\n" +
538 "out float4 oColor : COLOR,\n" +
540 "uniform float3 uLightPosition,\n"+
541 "uniform float uLightFarClipDistance,\n" +
542 "uniform sampler2D uDepthMap : register(s0))\n" +
544 "iProjUV = iProjUV / iProjUV.w;\n"+
545 "float Depth = tex2D(uDepthMap, iProjUV.xy).r;\n"+
546 "if (Depth < saturate( length(iPosition-uLightPosition) / uLightFarClipDistance ))\n"+
548 "oColor = float4(0,0,0,1);\n"+
552 "oColor = float4(float3(" + GB[NumberOfDepthChannels] +
") * 0.1, 1);\n"+
556 FragmentProgramData +=
560 "out float4 oColor : COLOR)\n" +
562 "oColor = float4(float3(" + GB[NumberOfDepthChannels] +
") * 0.1, 1);\n"+
570 FragmentProgramData += Ogre::String(
"\n" ) +
572 "uniform vec3 uLightPosition;\n"+
573 "uniform float uLightFarClipDistance;\n" +
574 "uniform sampler2D uDepthMap;\n" +
576 "varying vec3 Position_;\n" +
577 "varying vec4 ProjUV;\n" +
582 "ProjUV /= ProjUV.w;\n" +
583 "float Depth = texture2D(uDepthMap, ProjUV.xy).x;\n" +
584 "if(Depth < clamp( length(Position_-uLightPosition) / uLightFarClipDistance ), 0.0, 1.0)\n" +
586 "gl_FragColor = vec4(0.0,0.0,0.0,1.0);\n"+
590 "gl_FragColor = vec4(vec3(" + GB[NumberOfDepthChannels] +
") * 0.1, 1.0);\n"+
594 FragmentProgramData += Ogre::String(
"\n" ) +
596 "uniform vec3 uLightPosition;\n"+
597 "uniform float uLightFarClipDistance;\n" +
598 "uniform sampler2D uDepthMap;\n" +
600 "varying vec3 Position_;\n" +
601 "varying vec4 ProjUV;\n" +
606 "gl_FragColor = vec4(vec3(" + GB[NumberOfDepthChannels] +
") * 0.1, 1.0);\n"+
613 mMaterials[0] = Ogre::MaterialManager::getSingleton().
615 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
617 Ogre::Pass *GR_Technique0_Pass0 =
mMaterials[0]->getTechnique(0)->getPass(0);
619 GR_Technique0_Pass0->setLightingEnabled(
false);
620 GR_Technique0_Pass0->setCullingMode(Ogre::CULL_NONE);
621 GR_Technique0_Pass0->setDepthWriteEnabled(
false);
623 GR_Technique0_Pass0->setSceneBlending(Ogre::SBT_ADD);
625 GpuProgramsData[0] = VertexProgramData; GpuProgramsData[1] = FragmentProgramData;
630 VP_Parameters = GR_Technique0_Pass0->getVertexProgramParameters();
631 FP_Parameters = GR_Technique0_Pass0->getFragmentProgramParameters();
635 VP_Parameters->setNamedAutoConstant(
"uWorldViewProj", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
643 Ogre::Matrix4 TexViewProj =
648 VP_Parameters->setNamedAutoConstant(
"uWorld", Ogre::GpuProgramParameters::ACT_WORLD_MATRIX);
649 VP_Parameters->setNamedConstant(
"uTexViewProj", TexViewProj);
651 FP_Parameters->setNamedConstant(
"uLightPosition",
mProjectorSN->getPosition());
652 FP_Parameters->setNamedConstant(
"uLightFarClipDistance",
mProjectorCamera->getFarClipDistance());
654 int GLSLTextUnit = 0;
657 FP_Parameters->setNamedConstant(
"uDepthMap", GLSLTextUnit);
660 GR_Technique0_Pass0->createTextureUnitState(
_def_GodRays_Depth_Map)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
665 VertexProgramData =
"";
666 FragmentProgramData =
"";
678 "float4 iPosition : POSITION,\n" +
679 "float2 iUV : TEXCOORD0,\n" +
681 "out float4 oPosition : POSITION,\n" +
682 "out float3 oPosition_ : TEXCOORD0,\n" +
684 "uniform float4x4 uWorld,\n" +
685 "uniform float4x4 uWorldViewProj)\n" +
687 "oPosition = mul(uWorldViewProj, iPosition);\n"+
688 "float4 wPos = mul(uWorld, iPosition);\n"+
689 "oPosition_ = wPos.xyz;\n"+
696 VertexProgramData += Ogre::String(
"\n" ) +
698 "uniform mat4 uWorld;\n" +
701 "varying vec3 Position_;\n" +
705 "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"+
706 "vec4 wPos = uWorld * gl_Vertex;\n"+
707 "Position_ = wPos.xyz;\n"+
719 FragmentProgramData +=
723 "float3 iPosition : TEXCOORD0,\n" +
725 "out float4 oColor : COLOR,\n" +
727 "uniform float3 uLightPosition,\n" +
728 "uniform float uLightFarClipDistance)\n" +
730 "float depth = saturate( length(iPosition-uLightPosition) / uLightFarClipDistance );\n"+
731 "oColor = float4(depth, 0, 0, 0);\n"+
738 VertexProgramData += Ogre::String(
"\n" ) +
740 "uniform vec3 uLightPosition;\n" +
741 "uniform float uLightFarClipDistance;\n" +
743 "varying vec3 Position_;\n" +
748 "float depth = clamp( length(Position_-uLightPosition) / uLightFarClipDistance , 0.0, 1.0);\n"+
749 "gl_FragColor = vec4(depth, 0.0, 0.0, 0.0);\n"+
756 mMaterials[1] = Ogre::MaterialManager::getSingleton().
758 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
760 Ogre::Pass *GRD_Technique0_Pass0 =
mMaterials[1]->getTechnique(0)->getPass(0);
762 mMaterials[1]->getTechnique(0)->setSchemeName(
"HydraxGodRaysDepth");
764 GRD_Technique0_Pass0->setLightingEnabled(
false);
765 GRD_Technique0_Pass0->setCullingMode(Ogre::CULL_NONE);
767 GpuProgramsData[0] = VertexProgramData; GpuProgramsData[1] = FragmentProgramData;
772 VP_Parameters = GRD_Technique0_Pass0->getVertexProgramParameters();
773 FP_Parameters = GRD_Technique0_Pass0->getFragmentProgramParameters();
777 VP_Parameters->setNamedAutoConstant(
"uWorldViewProj", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
779 VP_Parameters->setNamedAutoConstant(
"uWorld", Ogre::GpuProgramParameters::ACT_WORLD_MATRIX);
781 FP_Parameters->setNamedConstant(
"uLightPosition",
mProjectorSN->getPosition());
782 FP_Parameters->setNamedConstant(
"uLightFarClipDistance",
mProjectorCamera->getFarClipDistance());
789 HydraxLOG(
"GodRaysManager::addDepthTechnique(...) Objects intersection must be enabled and Hydrax::create() already called, skipping...");
794 Technique->removeAllPasses();
795 Technique->createPass();
796 Technique->setSchemeName(
"HydraxGodRaysDepth");
798 Ogre::Pass *DM_Technique_Pass0 = Technique->getPass(0);
803 Ogre::GpuProgramParametersSharedPtr VP_Parameters = DM_Technique_Pass0->getVertexProgramParameters();
804 Ogre::GpuProgramParametersSharedPtr FP_Parameters = DM_Technique_Pass0->getFragmentProgramParameters();
808 VP_Parameters->setNamedAutoConstant(
"uWorldViewProj", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
810 VP_Parameters->setNamedAutoConstant(
"uWorld", Ogre::GpuProgramParameters::ACT_WORLD_MATRIX);
812 FP_Parameters->setNamedConstant(
"uLightPosition",
mProjectorSN->getPosition());
813 FP_Parameters->setNamedConstant(
"uLightFarClipDistance",
mProjectorCamera->getFarClipDistance());
845 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
853 Ogre::TU_RENDERTARGET);
855 Ogre::RenderTarget* RT_Texture =
mProjectorRTT->getBuffer()->getRenderTarget();
856 RT_Texture->setAutoUpdated(
false);
858 Ogre::Viewport *RT_Texture_Viewport = RT_Texture->addViewport(
mProjectorCamera);
859 RT_Texture_Viewport->setClearEveryFrame(
true);
860 RT_Texture_Viewport->setMaterialScheme(
"HydraxGodRaysDepth");
861 RT_Texture_Viewport->setBackgroundColour(Ogre::ColourValue::White);
862 RT_Texture_Viewport->setOverlaysEnabled(
false);
863 RT_Texture_Viewport->setSkiesEnabled(
false);
864 RT_Texture_Viewport->setShadowsEnabled(
false);
872 Ogre::Entity * CurrentEntity = NULL;
879 while( EntityIterator.hasMoreElements() )
881 CurrentEntity =
static_cast<Ogre::Entity *
>(EntityIterator.peekNextValue());
883 for( k = 0; k < CurrentEntity->getNumSubEntities(); k++ )
885 mMaterials.push(CurrentEntity->getSubEntity(k)->getMaterialName());
889 EntityIterator.moveNext();
895 Ogre::SceneManager::MovableObjectIterator EntityIterator = mGodRaysManager->mHydrax->getSceneManager()->getMovableObjectIterator(
"Entity");
896 Ogre::Entity * CurrentEntity = NULL;
899 mGodRaysManager->mHydrax->getMesh()->getEntity()->setVisible(
true);
901 while( EntityIterator.hasMoreElements() )
903 CurrentEntity =
static_cast<Ogre::Entity *
>(EntityIterator.peekNextValue());
905 for( k = 0; k < CurrentEntity->getNumSubEntities(); k++ )
907 CurrentEntity->getSubEntity(k)->setMaterialName(
mMaterials.front());
911 EntityIterator.moveNext();