Commits

philiplb  committed 86a8051

Volume Rendering: implemented CSGNoiseSource and extended the CSG demo

  • Participants
  • Parent commits ff070d0
  • Branches v1-9

Comments (0)

Files changed (5)

File Components/Volume/include/OgreVolumeCSGSource.h

 #include "OgreVector3.h"
 #include "OgreAxisAlignedBox.h"
 #include "OgreVolumePrerequisites.h"
+#include "OgreVolumeSimplexNoise.h"
 
 namespace Ogre {
 namespace Volume {
         virtual Real getValue(const Vector3 &position) const;
     };
 
+    class _OgreVolumeExport CSGNoiseSource: public CSGUnarySource
+    {
+    protected:
+        
+        Real *mOctaves;
+        
+        size_t mNumOctaves;
+        
+        SimplexNoise mNoise;
+
+        Real mSmallestOctave;
+
+        void setSmallestOctave(void);
+
+        inline Real getInternalValue(const Vector3 &position) const
+        {
+            Real toAdd = (Real)0.0;
+            Real noise = mNoise.noise(position.x, position.y, position.z);
+            for (size_t i = 0; i < mNumOctaves; ++i)
+            {
+                toAdd += noise * mOctaves[i];
+            }
+            return mSrc->getValue(position) + toAdd;
+        }
+
+    public:
+        
+        CSGNoiseSource(const Source *src, Real *octaves, size_t numOctaves, long seed);
+        
+        CSGNoiseSource(const Source *src, Real *octaves, size_t numOctaves);
+        
+        /** Overridden from Source.
+        */
+        virtual Vector4 getValueAndGradient(const Vector3 &position) const;
+
+        /** Overridden from VolumeSource.
+        */
+        virtual Real getValue(const Vector3 &position) const;
+    };
+
 }
 }
 

File Components/Volume/include/OgreVolumeSimplexNoise.h

         @return
             The dot product.
         */
-        inline Real dot(const Vector3 &g, Real x, Real y, Real z)
+        inline Real dot(const Vector3 &g, Real x, Real y, Real z) const
         {
             return g.x * x + g.y * y + g.z * z;
         }
         @return
             The noise value.
         */
-        Real noise(Real xIn, Real yIn, Real zIn);
+        Real noise(Real xIn, Real yIn, Real zIn) const;
         
     };
 

File Components/Volume/src/OgreVolumeCSGSource.cpp

         return mSrc->getValue(position / mScale) * mScale;
     }
 
+    void CSGNoiseSource::setSmallestOctave(void)
+    {
+        mSmallestOctave = fabs(mOctaves[0]);
+        for (size_t i = 1; i < mNumOctaves; ++i)
+        {
+            if (fabs(mOctaves[i]) < mSmallestOctave)
+            {
+                mSmallestOctave = mOctaves[i];
+            }
+        }
+    }
+    
+    //-----------------------------------------------------------------------
+
+    CSGNoiseSource::CSGNoiseSource(const Source *src, Real *octaves, size_t numOctaves, long seed) :
+        CSGUnarySource(src), mOctaves(octaves), mNumOctaves(numOctaves), mNoise(seed)
+    {
+        setSmallestOctave();
+    }
+
+    //-----------------------------------------------------------------------
+
+    CSGNoiseSource::CSGNoiseSource(const Source *src, Real *octaves, size_t numOctaves) :
+        CSGUnarySource(src), mOctaves(octaves), mNumOctaves(numOctaves)
+    {
+        setSmallestOctave();
+    }
+        
+    //-----------------------------------------------------------------------
+
+    Vector4 CSGNoiseSource::getValueAndGradient(const Vector3 &position) const
+    {
+        return Vector4(
+            getInternalValue(Vector3(position.x + mSmallestOctave, position.y, position.z)) - getInternalValue(Vector3(position.x - mSmallestOctave, position.y, position.z)),
+            getInternalValue(Vector3(position.x, position.y + mSmallestOctave, position.z)) - getInternalValue(Vector3(position.x, position.y - mSmallestOctave, position.z)),
+            getInternalValue(Vector3(position.x, position.y, position.z + mSmallestOctave)) - getInternalValue(Vector3(position.x, position.y, position.z - mSmallestOctave)),
+            getInternalValue(position));
+    }
+    
+    //-----------------------------------------------------------------------
+
+    Real CSGNoiseSource::getValue(const Vector3 &position) const
+    {
+        return getInternalValue(position);
+    }
 }
 }

File Components/Volume/src/OgreVolumeSimplexNoise.cpp

     
     //-----------------------------------------------------------------------
     
-    Real SimplexNoise::noise(Real xIn, Real yIn, Real zIn)
+    Real SimplexNoise::noise(Real xIn, Real yIn, Real zIn) const
     {
         Real n0, n1, n2, n3; // Noise contributions from the four corners
         // Skew the input space to determine which simplex cell we're in

File Samples/VolumeCSG/src/VolumeCSG.cpp

     CSGSphereSource sphere5(innerHalfWidth + (Real)0.75, center);
     CSGIntersectionSource intersection1(&cube3, &sphere5);
 
-    // A plane
-    CSGPlaneSource plane1((Real)0.1, Vector3::UNIT_Y);
+    // A plane with noise
+    CSGPlaneSource plane1((Real)0.5, Vector3::UNIT_Y);
+    Real octaves[] = {(Real)0.1, (Real)0.4};
+    CSGNoiseSource noise1(&plane1, octaves, 2, 100);
 
     // Combine everything
     CSGUnionSource union1(&sphere1, &sphere2);
     CSGUnionSource union3(&union2, &sphere4);
     CSGUnionSource union4(&union3, &difference1);
     CSGUnionSource union5(&union4, &intersection1);
-    CSGUnionSource union6(&union5, &plane1);
+    CSGUnionSource union6(&union5, &noise1);
     Source *src = &union6;
 
     mVolumeRoot = OGRE_NEW Chunk();