Revision: 1634
          http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=1634&view=rev
Author:   rorthomas
Date:     2011-02-02 15:06:12 +0000 (Wed, 02 Feb 2011)

Log Message:
-----------
added new DOF dir

Added Paths:
-----------
    trunk/source/main/dof/
    trunk/source/main/dof/DepthOfFieldEffect.cpp
    trunk/source/main/dof/DepthOfFieldEffect.h
    trunk/source/main/dof/Lens.cpp
    trunk/source/main/dof/Lens.h

Added: trunk/source/main/dof/DepthOfFieldEffect.cpp
===================================================================
--- trunk/source/main/dof/DepthOfFieldEffect.cpp                                
(rev 0)
+++ trunk/source/main/dof/DepthOfFieldEffect.cpp        2011-02-02 15:06:12 UTC 
(rev 1634)
@@ -0,0 +1,198 @@
+// "Depth of Field" demo for Ogre
+// Copyright (C) 2006  Christian Lindequist Larsen
+//
+// This code is in the public domain. You may do whatever you want with it.
+
+// changed by tdev, 02/02/2011
+
+#include "DepthOfFieldEffect.h"
+
+using namespace Ogre;
+
+const int DepthOfFieldEffect::BLUR_DIVISOR = 5;
+int DepthOfFieldEffect::DoF_ID = 0;
+
+DepthOfFieldEffect::DepthOfFieldEffect(Viewport* viewport)
+       : mViewport(viewport)
+       , mNearDepth(10.0)
+       , mFocalDepth(100.0)
+       , mFarDepth(190.0)
+       , mFarBlurCutoff(1.0)
+{
+       DoF_ID++;
+       createDepthRenderTexture();
+       addCompositor();
+}
+
+DepthOfFieldEffect::~DepthOfFieldEffect()
+{
+       removeCompositor();
+       destroyDepthRenderTexture();
+}
+
+void DepthOfFieldEffect::setFocalDepths(float nearDepth, float focalDepth, 
float farDepth)
+{
+       mNearDepth = nearDepth;
+       mFocalDepth = focalDepth;
+       mFarDepth = farDepth;
+}
+
+void DepthOfFieldEffect::setFarBlurCutoff(float cutoff)
+{
+       mFarBlurCutoff = cutoff;
+}
+
+bool DepthOfFieldEffect::getEnabled() const
+{
+       return mCompositor->getEnabled();
+}
+
+void DepthOfFieldEffect::setEnabled(bool value)
+{
+       mCompositor->setEnabled(value);
+}
+
+void DepthOfFieldEffect::createDepthRenderTexture()
+{
+       // Create the depth render texture
+       TexturePtr depthTexture = TextureManager::getSingleton().createManual(
+               "DoF_Depth"  + StringConverter::toString(DoF_ID),
+               ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
+               TEX_TYPE_2D, mViewport->getActualWidth(), 
mViewport->getActualHeight(),
+               0, PF_L8, TU_RENDERTARGET);
+
+       // Get its render target and add a viewport to it
+       RenderTexture* depthTarget = 
depthTexture->getBuffer()->getRenderTarget();
+       Viewport* depthViewport = 
depthTarget->addViewport(mViewport->getCamera());
+
+       // Register 'this' as a render target listener
+       depthTarget->addListener(this);
+
+       // Get the technique to use when rendering the depth render texture
+       mDepthMaterial = MaterialManager::getSingleton().getByName("DoF_Depth");
+       mDepthMaterial->load(); // needs to be loaded manually
+       mDepthTechnique = mDepthMaterial->getBestTechnique();
+       
+       try {
+               // Create a custom render queue invocation sequence for the 
depth render texture
+               RenderQueueInvocationSequence* invocationSequence =
+               
Root::getSingleton().createRenderQueueInvocationSequence("DoF_Depth");
+
+               // Add a render queue invocation to the sequence, and disable 
shadows for it
+               RenderQueueInvocation* invocation = 
invocationSequence->add(RENDER_QUEUE_MAIN, "main");
+               invocation->setSuppressShadows(true);
+       }
+       catch(Exception e)
+       {
+               // do nothing, RenderQueueInvocationSequence already created
+       }
+
+       // Set the render queue invocation sequence for the depth render 
texture viewport
+       depthViewport->setRenderQueueInvocationSequenceName("DoF_Depth");
+}
+
+void DepthOfFieldEffect::destroyDepthRenderTexture()
+{
+       // TODO
+}
+
+void DepthOfFieldEffect::addCompositor()
+{
+       // Add the compositor to the viewport
+       mCompositor = CompositorManager::getSingleton().addCompositor(
+               mViewport, "DoF_Compositor" + 
StringConverter::toString(DoF_ID));
+
+       // Register 'this' as a listener
+       mCompositor->addListener(this);
+}
+
+void DepthOfFieldEffect::removeCompositor()
+{
+       // TODO
+}
+
+void DepthOfFieldEffect::notifyMaterialSetup(uint32 passId, MaterialPtr& 
material)
+{
+       switch (passId)
+       {
+       case BlurPass:
+               {
+                       Vector3 pixelSize = Vector3(
+                               1.0f / (mViewport->getActualWidth() / 
BLUR_DIVISOR),
+                               1.0f / (mViewport->getActualHeight() / 
BLUR_DIVISOR),
+                               1.0);
+
+                       // Adjust fragment program parameters
+                       GpuProgramParametersSharedPtr fragParams =
+                               
material->getBestTechnique()->getPass(0)->getFragmentProgramParameters();
+                       
+                       
if((!fragParams.isNull())&&(fragParams->_findNamedConstantDefinition("pixelSize")))
+                               fragParams->setNamedConstant("pixelSize", 
pixelSize );
+
+                       break;
+               }
+
+       case OutputPass:
+               {
+                       Vector3 pixelSizeScene = Vector3(
+                               1.0f / (mViewport->getActualWidth()),
+                               1.0f / (mViewport->getActualHeight()),
+                               1.0);
+
+                       Vector3 pixelSizeBlur = Vector3(
+                               1.0f / (mViewport->getActualWidth() / 
BLUR_DIVISOR),
+                               1.0f / (mViewport->getActualHeight() / 
BLUR_DIVISOR),
+                               1.0);
+
+                       // Adjust fragment program parameters
+                       GpuProgramParametersSharedPtr fragParams =
+                               
material->getBestTechnique()->getPass(0)->getFragmentProgramParameters();
+                       
+                       
if((!fragParams.isNull())&&(fragParams->_findNamedConstantDefinition("pixelSizeScene")))
+                               fragParams->setNamedConstant("pixelSizeScene", 
pixelSizeScene );
+                       
+                       
if((!fragParams.isNull())&&(fragParams->_findNamedConstantDefinition("pixelSizeBlur")))
+                               fragParams->setNamedConstant("pixelSizeBlur", 
pixelSizeBlur );
+
+                       break;
+               }
+       }
+}
+
+void DepthOfFieldEffect::preViewportUpdate(const 
Ogre::RenderTargetViewportEvent& evt)
+{
+       Vector4 dofParams(mNearDepth, mFocalDepth, mFarDepth, mFarBlurCutoff);
+
+       // Adjust fragment program parameters for depth pass
+       GpuProgramParametersSharedPtr fragParams =
+               mDepthTechnique->getPass(0)->getFragmentProgramParameters();
+       
+       
if((!fragParams.isNull())&&(fragParams->_findNamedConstantDefinition("dofParams")))
+               fragParams->setNamedConstant("dofParams", dofParams);
+
+       /*
+       fragParams->setNamedConstant("pixelSizeScene", pixelSizeScene, 
sizeof(pixelSizeScene)/sizeof(pixelSizeScene[0]), 1.0f);
+       fragParams->setNamedConstant("pixelSizeBlur", pixelSizeBlur, 
sizeof(pixelSizeBlur)/sizeof(pixelSizeBlur[0]), 1.0f);
+       fragParams->setNamedConstant("dofParams", dofParams, 
sizeof(dofParams)/sizeof(dofParams[0]), 1.0f);
+       fragParams->setNamedConstant("pixelSize", 
pixelSize,sizeof(pixelSize)/sizeof(pixelSize[0]), 1.0f);
+       */
+
+       // Add 'this' as a RenderableListener to replace the technique for all 
renderables
+       RenderQueue* queue = 
evt.source->getCamera()->getSceneManager()->getRenderQueue();
+       queue->setRenderableListener(this);
+}
+
+void DepthOfFieldEffect::postViewportUpdate(const 
Ogre::RenderTargetViewportEvent& evt)
+{
+       // Reset the RenderableListener
+       RenderQueue* queue = 
evt.source->getCamera()->getSceneManager()->getRenderQueue();
+       queue->setRenderableListener(0);
+}
+
+bool DepthOfFieldEffect::renderableQueued(Renderable* rend, uint8 groupID, 
+                               ushort priority, Technique** ppTech, 
RenderQueue* pQueue)
+{
+       // Replace the technique of all renderables
+       *ppTech = mDepthTechnique;
+       return true;
+}

Added: trunk/source/main/dof/DepthOfFieldEffect.h
===================================================================
--- trunk/source/main/dof/DepthOfFieldEffect.h                          (rev 0)
+++ trunk/source/main/dof/DepthOfFieldEffect.h  2011-02-02 15:06:12 UTC (rev 
1634)
@@ -0,0 +1,66 @@
+// "Depth of Field" demo for Ogre
+// Copyright (C) 2006  Christian Lindequist Larsen
+//
+// This code is in the public domain. You may do whatever you want with it.
+
+#ifndef __DepthOfFieldEffect_H__
+#define __DepthOfFieldEffect_H__
+
+#include "RoRPrerequisites.h"
+#include <Ogre.h>
+#include <OgreCompositorInstance.h>
+
+class DepthOfFieldEffect : public Ogre::CompositorInstance::Listener,
+                                                  public 
Ogre::RenderTargetListener,
+                                                  public 
Ogre::RenderQueue::RenderableListener
+{
+public:
+       DepthOfFieldEffect(Ogre::Viewport* viewport);
+       ~DepthOfFieldEffect();
+
+       float getNearDepth() const {return mNearDepth; }
+       float getFocalDepth() const {return mFocalDepth; }
+       float getFarDepth() const {return mFarDepth; }
+       void setFocalDepths(float nearDepth, float focalDepth, float farDepth);
+       float getFarBlurCutoff() const {return mFarBlurCutoff; }
+       void setFarBlurCutoff(float cutoff);
+       bool getEnabled() const;
+       void setEnabled(bool value);
+
+private:
+       static const int BLUR_DIVISOR;
+       static int DoF_ID;
+
+       enum PassId
+       {
+               BlurPass,
+               OutputPass
+       };
+
+       Ogre::Viewport* mViewport;
+       Ogre::MaterialPtr mDepthMaterial;
+       Ogre::Technique* mDepthTechnique;
+       Ogre::CompositorInstance* mCompositor;
+       float mNearDepth;
+       float mFocalDepth;
+       float mFarDepth;
+       float mFarBlurCutoff;
+
+       void createDepthRenderTexture();
+       void destroyDepthRenderTexture();
+       void addCompositor();
+       void removeCompositor();
+
+       // Implementation of Ogre::CompositorInstance::Listener
+       virtual void notifyMaterialSetup(Ogre::uint32 passId, 
Ogre::MaterialPtr& material);
+
+       // Implementation of Ogre::RenderTargetListener
+       virtual void preViewportUpdate(const Ogre::RenderTargetViewportEvent& 
evt);
+       virtual void postViewportUpdate(const Ogre::RenderTargetViewportEvent& 
evt);
+
+       // Implementation of Ogre::RenderQueue::RenderableListener
+       virtual bool renderableQueued(Ogre::Renderable* rend, Ogre::uint8 
groupID, 
+                               Ogre::ushort priority, Ogre::Technique** 
ppTech, Ogre::RenderQueue* pQueue);
+};
+
+#endif // __DepthOfFieldEffect_H__

Added: trunk/source/main/dof/Lens.cpp
===================================================================
--- trunk/source/main/dof/Lens.cpp                              (rev 0)
+++ trunk/source/main/dof/Lens.cpp      2011-02-02 15:06:12 UTC (rev 1634)
@@ -0,0 +1,127 @@
+// "Depth of Field" demo for Ogre
+// Copyright (C) 2006  Christian Lindequist Larsen
+//
+// This code is in the public domain. You may do whatever you want with it.
+//
+// Author: Ryan Booker (eyevee99)
+
+#include "Lens.h"
+
+using namespace Ogre;
+
+#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
+#undef min
+#undef max
+#endif
+
+template<class T>
+const T& min(const T& a, const T& b) {
+  return (a < b) ? a : b;
+};
+
+
+template<class T>
+const T& max(const T& a, const T& b) {
+  return (a > b) ? a : b;
+};
+
+
+Lens::Lens(const Real& _focalLength, const Real& _fStop, const Real& 
_frameSize, const Real& _circleOfConfusion) {
+  init(_focalLength, _fStop, _frameSize, _circleOfConfusion);
+}
+
+
+Lens::Lens(const Radian& _fieldOfView, const Real& _fStop, const Real& 
_frameSize, const Real& _circleOfConfusion) {
+  init(_fieldOfView, _fStop, _frameSize, _circleOfConfusion);
+}
+
+
+void Lens::init(const Real& _focalLength, const Real& _fStop, const Real& 
_frameSize, const Real& _circleOfConfusion) {
+  mFocalLength = _focalLength;
+  mFStop = _fStop;
+  mFrameSize = _frameSize;
+  mCircleOfConfusion = _circleOfConfusion;
+  recalculateFieldOfView();
+}
+
+
+void Lens::init(const Radian& _fieldOfView, const Real& _fStop, const Real& 
_frameSize, const Real& _circleOfConfusion) {
+  mFieldOfView = _fieldOfView;
+  mFStop = _fStop;
+  mFrameSize = _frameSize;
+  mCircleOfConfusion = _circleOfConfusion;
+  recalculateFocalLength();
+}
+
+
+void Lens::setFrameSize(const Real& _frameSize) {
+  mFrameSize = _frameSize;
+  recalculateFieldOfView();
+}
+
+
+void Lens::setFocalDistance(const Real& _focalDistance) {
+  mFocalDistance = max(_focalDistance, Real(0));
+}
+
+
+void Lens::setFocalLength(const Real& _focalLength) {
+  mFocalLength = max(_focalLength, Real(0.3));
+  recalculateFieldOfView();
+}
+
+
+void Lens::setFieldOfView(const Radian& _fieldOfView) {
+  mFieldOfView = min(_fieldOfView, Radian(2.8));
+  recalculateFocalLength();
+}
+
+
+void Lens::setFStop(const Real& _fStop) {
+  mFStop = max(_fStop, Real(0));
+}
+
+
+void Lens::recalculateFocalLength(void) {
+  mFocalLength = (mFrameSize / Math::Tan(mFieldOfView / 2.0)) / 2.0;
+}
+
+
+void Lens::recalculateFieldOfView(void) {
+  mFieldOfView = 2.0 * Math::ATan(mFrameSize / (2.0 * mFocalLength));
+}
+
+
+void Lens::recalculateHyperfocalLength(void) {
+  mHyperfocalLength = (mFocalLength * mFocalLength) / (mFStop * 
mCircleOfConfusion) + mFocalLength;
+}
+
+
+void Lens::recalculateDepthOfField(Real& _nearDepth, Real& _focalDepth, Real& 
_farDepth) {
+  // Set focalDepth to the current focalDistance
+  _focalDepth = mFocalDistance;
+
+  // Recalculate the Hyperfocal length
+  recalculateHyperfocalLength();
+
+  // Calculate the numerator of the optics equations
+  Real numerator = (mFocalDistance * (mHyperfocalLength - mFocalLength));
+
+  Real nearClear = numerator / (mHyperfocalLength + mFocalDistance - (2.0 * 
mFocalLength));
+
+  // Adjust the nearDepth relative to the aperture. This is an approximation.
+  _nearDepth = min(nearClear - nearClear * mFStop, (Real)0);
+
+  if(mFocalDistance < mHyperfocalLength)
+  {
+    // Calculate the far clear plane
+    Real farClear = numerator / (mHyperfocalLength - mFocalDistance);
+
+    // Adjust the farDepth relative to the aperture. This is an approximation.
+    _farDepth = farClear + farClear * mFStop;
+  }
+
+  // Far depth of field should be infinite
+  else
+    _farDepth = Math::POS_INFINITY;
+}

Added: trunk/source/main/dof/Lens.h
===================================================================
--- trunk/source/main/dof/Lens.h                                (rev 0)
+++ trunk/source/main/dof/Lens.h        2011-02-02 15:06:12 UTC (rev 1634)
@@ -0,0 +1,60 @@
+// "Depth of Field" demo for Ogre
+// Copyright (C) 2006  Christian Lindequist Larsen
+//
+// This code is in the public domain. You may do whatever you want with it.
+//
+// Author: Ryan Booker (eyevee99)
+
+#ifndef __Lens_H__
+#define __Lens_H__
+
+#include "RoRPrerequisites.h"
+#include <Ogre.h>
+
+class Lens {
+public:
+  Lens(void) {};
+  Lens(const Ogre::Real& _focalLength, const Ogre::Real& _fStop, const 
Ogre::Real& _frameSize = 3.5, const Ogre::Real& _circleOfConfusion = 0.003);
+  Lens(const Ogre::Radian& _fieldOfView, const Ogre::Real& _fStop, const 
Ogre::Real& _frameSize = 3.5, const Ogre::Real& _circleOfConfusion = 0.003);
+  ~Lens(void) {};
+
+  void init(const Ogre::Real& _focalLength, const Ogre::Real& _fStop, const 
Ogre::Real& _frameSize = 3.5, const Ogre::Real& _circleOfConfusion = 0.003);
+  void init(const Ogre::Radian& _fieldOfView, const Ogre::Real& _fStop, const 
Ogre::Real& _frameSize = 3.5, const Ogre::Real& _circleOfConfusion = 0.003);
+
+  void setFrameSize(const Ogre::Real& _frameSize);
+  void setFocalDistance(const Ogre::Real& _focalDistance);
+  void setFocalLength(const Ogre::Real& _focalLength);
+  void setFieldOfView(const Ogre::Radian& _fieldOfView);
+  void setFStop(const Ogre::Real& _fStop);
+
+  const Ogre::Real& getFrameSize(void)            { return mFrameSize; }
+  const Ogre::Real& getFocalDistance(void)        { return mFocalDistance; }
+  const Ogre::Real& getFocalLength(void)          { return mFocalLength; }
+  const Ogre::Radian& getFieldOfView(void)        { return mFieldOfView; }
+  const Ogre::Real& getFStop(void)                { return mFStop; }
+  const Ogre::Real& getHyperfocalLength(void)     { return mHyperfocalLength; }
+
+  void recalculateDepthOfField(Ogre::Real& _nearDepth, Ogre::Real& 
_focalDepth, Ogre::Real& _farDepth);
+
+
+protected:
+  // NOTE: All units must be the same, eg mm, cm or m etc
+  // Primary attributes
+  Ogre::Real mFrameSize;                // Film stock/sensor size, arbitrarily 
selected to help mimic the properties of film, eg 35mm, 3.5cm, 0.035m etc
+  Ogre::Real mCircleOfConfusion;        // The area within which the depth of 
field is clear, it's tied to frame size, eg 0.03mm, 0.003cm, 0.0003m etc
+  Ogre::Real mFocalDistance;            // The distance to the object we are 
focusing on
+  Ogre::Real mFocalLength;              // Focal length of the lens, this 
directly effects field of view, we can do anything from wide angle to telephoto 
as we don't have the limitations of physical lenses.  Focal length is the 
distance from the optical centre of the lens to the film stock/sensor etc
+  Ogre::Real mFStop;                    // FStop number, ie aperture, changing 
the aperture of a lens has an effect of depth of field, the narrower the 
aperture/higher the fstop number, the greater the depth of field/clearer the 
picture is.
+
+  // Secondary attributes
+  Ogre::Real mHyperfocalLength;         // The hyperfocal length is the point 
at which far depth of field is infinite, ie if mFocalDistance is >= to this 
value everythig will be clear to infinity
+  Ogre::Radian mFieldOfView;            // Field of view of the lens, directly 
related to focal length
+
+
+private:
+  void recalculateHyperfocalLength(void);
+  void recalculateFieldOfView(void);
+  void recalculateFocalLength(void);
+};
+
+#endif


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Rigsofrods-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel

Reply via email to