21 #include "CaelumPrecompiled.h" 22 #include "FlatCloudLayer.h" 23 #include "CaelumExceptions.h" 24 #include "InternalUtilities.h" 28 FlatCloudLayer::FlatCloudLayer(
29 Ogre::SceneManager *sceneMgr,
30 Ogre::SceneNode *cloudRoot)
38 mMaterial->getTechnique(0)->getPass(0)->getVertexProgramParameters(),
39 mMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters());
43 mNode.
reset(cloudRoot->createChildSceneNode());
44 mNode->setPosition(Ogre::Vector3(0, 0, 0));
47 mNoiseTextureNames.clear();
48 mNoiseTextureNames.push_back(
"noise1.dds");
49 mNoiseTextureNames.push_back(
"noise2.dds");
50 mNoiseTextureNames.push_back(
"noise3.dds");
51 mNoiseTextureNames.push_back(
"noise4.dds");
54 mCurrentTextureIndex = -1;
66 FlatCloudLayer::~FlatCloudLayer()
84 Ogre::String uniqueId = Ogre::StringConverter::toString((
size_t)
this);
85 Ogre::String planeMeshName =
"Caelum/FlatCloudLayer/Plane/" + uniqueId;
86 Ogre::String entityName =
"Caelum/FlatCloudLayer/Entity/" + uniqueId;
100 Ogre::Plane meshPlane(
101 Ogre::Vector3(1, 1, 0),
102 Ogre::Vector3(1, 1, 1),
103 Ogre::Vector3(0, 1, 1));
104 mMesh.
reset(Ogre::MeshManager::getSingleton().createPlane(
106 mMeshWidth, mMeshHeight,
107 mMeshWidthSegments, mMeshHeightSegments,
110 Ogre::Vector3::UNIT_X));
113 mEntity.
reset(mSceneMgr->createEntity(entityName, mMesh->getName()));
114 mEntity->setMaterialName(mMaterial->getName());
117 mNode->attachObject(mEntity.get());
124 Real meshWidth, Real meshHeight,
125 int meshWidthSegments,
int meshHeightSegments)
128 (mMeshWidthSegments != meshWidthSegments) ||
129 (mMeshHeightSegments != meshHeightSegments) ||
130 (abs(mMeshWidth - meshWidth) > 0.001) ||
131 (abs(mMeshHeight - meshHeight) > 0.001);
132 mMeshWidth = meshWidth;
133 mMeshHeight = meshHeight;
134 mMeshWidthSegments = meshWidthSegments;
135 mMeshHeightSegments = meshHeightSegments;
146 assert (mCloudCoverLookup.get() == 0);
151 setCloudMassOffset (Ogre::Vector2(0, 0));
152 setCloudDetailOffset (Ogre::Vector2(0, 0));
164 setSunDirection (Ogre::Vector3::UNIT_Y);
165 setFogColour (Ogre::ColourValue::Black);
166 setSunLightColour (Ogre::ColourValue::White);
167 setSunSphereColour (Ogre::ColourValue::White);
171 Ogre::Real timePassed,
172 const Ogre::Vector3 &sunDirection,
173 const Ogre::ColourValue &sunLightColour,
174 const Ogre::ColourValue &fogColour,
175 const Ogre::ColourValue &sunSphereColour)
181 setSunDirection (sunDirection);
182 setSunLightColour (sunLightColour);
183 setSunSphereColour (sunSphereColour);
184 setFogColour (fogColour);
201 setCloudMassOffset(mCloudMassOffset + timePassed * mCloudSpeed);
202 setCloudDetailOffset(mCloudDetailOffset - timePassed * mCloudSpeed);
208 void FlatCloudLayer::Params::setup(Ogre::GpuProgramParametersSharedPtr vpParams, Ogre::GpuProgramParametersSharedPtr fpParams)
210 this->vpParams = vpParams;
211 this->fpParams = fpParams;
212 cloudCoverageThreshold.bind(fpParams,
"cloudCoverageThreshold");
213 cloudMassOffset.bind(fpParams,
"cloudMassOffset");
214 cloudDetailOffset.bind(fpParams,
"cloudDetailOffset");
215 cloudMassBlend.bind(fpParams,
"cloudMassBlend");
216 vpSunDirection.bind(vpParams,
"sunDirection");
217 fpSunDirection.bind(fpParams,
"sunDirection");
218 sunLightColour.bind(fpParams,
"sunLightColour");
219 sunSphereColour.bind(fpParams,
"sunSphereColour");
220 fogColour.bind(fpParams,
"fogColour");
221 layerHeight.bind(fpParams,
"layerHeight");
222 cloudUVFactor.bind(fpParams,
"cloudUVFactor");
223 heightRedFactor.bind(fpParams,
"heightRedFactor");
224 nearFadeDist.bind(fpParams,
"nearFadeDist");
225 farFadeDist.bind(fpParams,
"farFadeDist");
226 fadeDistMeasurementVector.bind(fpParams,
"fadeDistMeasurementVector");
230 mCloudCoverLookup.reset(0);
231 mCloudCoverLookup.reset(
new Ogre::Image());
234 mCloudCoverLookupFileName = fileName;
238 mCloudCoverLookup.reset (0);
239 mCloudCoverLookupFileName.clear ();
243 return mCloudCoverLookupFileName;
247 mCloudCoverVisibilityThreshold = value;
252 mCloudCover = cloudCover;
253 float cloudCoverageThreshold = 0;
254 if (mCloudCoverLookup.get() != 0) {
257 cloudCoverageThreshold = 1 - cloudCover;
259 mParams.cloudCoverageThreshold.set(mParams.fpParams, cloudCoverageThreshold);
263 void FlatCloudLayer::setCloudMassOffset(
const Ogre::Vector2 &cloudMassOffset) {
264 mCloudMassOffset = cloudMassOffset;
265 mParams.cloudMassOffset.set(mParams.fpParams, Ogre::Vector3(cloudMassOffset.x,cloudMassOffset.y,0));
268 void FlatCloudLayer::setCloudDetailOffset(
const Ogre::Vector2 &cloudDetailOffset) {
269 mCloudDetailOffset = cloudDetailOffset;
270 mParams.cloudDetailOffset.set(mParams.fpParams, Ogre::Vector3(cloudDetailOffset.x,cloudDetailOffset.y,0));
274 mCloudBlendTime = value;
278 return mCloudBlendTime;
283 mCloudBlendPos = value;
284 int textureCount =
static_cast<int>(mNoiseTextureNames.size());
287 int currentTextureIndex =
static_cast<int>(floor(mCloudBlendPos));
288 currentTextureIndex = ((currentTextureIndex % textureCount) + textureCount) % textureCount;
289 assert(0 <= currentTextureIndex);
290 assert(currentTextureIndex < textureCount);
293 if (currentTextureIndex != mCurrentTextureIndex) {
294 String texture1 = mNoiseTextureNames[currentTextureIndex];
295 String texture2 = mNoiseTextureNames[(currentTextureIndex + 1) % textureCount];
298 Ogre::Pass* pass = mMaterial->getBestTechnique()->getPass(0);
299 pass->getTextureUnitState(0)->setTextureName(texture1);
300 pass->getTextureUnitState(1)->setTextureName(texture2);
301 mCurrentTextureIndex = currentTextureIndex;
304 Ogre::Real cloudMassBlend = fmod(mCloudBlendPos, 1);
305 mParams.cloudMassBlend.set(mParams.fpParams, cloudMassBlend);
309 return mCloudBlendPos;
313 mCloudSpeed = cloudSpeed;
316 void FlatCloudLayer::setSunDirection (
const Ogre::Vector3 &sunDirection) {
317 mSunDirection = sunDirection;
318 mParams.vpSunDirection.set(mParams.vpParams, sunDirection);
319 mParams.fpSunDirection.set(mParams.fpParams, sunDirection);
322 void FlatCloudLayer::setSunLightColour (
const Ogre::ColourValue &sunLightColour) {
323 mParams.sunLightColour.set(mParams.fpParams, mSunLightColour = sunLightColour);
326 void FlatCloudLayer::setSunSphereColour (
const Ogre::ColourValue &sunSphereColour) {
327 mParams.sunSphereColour.set(mParams.fpParams, mSunSphereColour = sunSphereColour);
330 void FlatCloudLayer::setFogColour (
const Ogre::ColourValue &fogColour) {
331 mParams.fogColour.set(mParams.fpParams, mFogColour = fogColour);
334 const Ogre::Vector3 FlatCloudLayer::getSunDirection ()
const {
335 return mSunDirection;
338 const Ogre::ColourValue FlatCloudLayer::getSunLightColour ()
const {
339 return mSunLightColour;
342 const Ogre::ColourValue FlatCloudLayer::getSunSphereColour ()
const {
343 return mSunSphereColour;
346 const Ogre::ColourValue FlatCloudLayer::getFogColour ()
const {
351 mNode->setPosition(Ogre::Vector3(0, height, 0));
353 mParams.layerHeight.set(mParams.fpParams, mHeight);
361 mParams.cloudUVFactor.set(mParams.fpParams, mCloudUVFactor = value);
365 mParams.heightRedFactor.set(mParams.fpParams, mHeightRedFactor = value);
374 mParams.nearFadeDist.set(mParams.fpParams, mNearFadeDist = value);
378 mParams.farFadeDist.set(mParams.fpParams, mFarFadeDist = value);
382 mParams.fadeDistMeasurementVector.set(mParams.fpParams, mFadeDistMeasurementVector = value);
void setCloudBlendPos(const Ogre::Real value)
Set the current blending position; between noise textures.
void setFadeDistMeasurementVector(const Ogre::Vector3 &value)
Set on which components is the fade distance measured.
Ogre::Real getCloudBlendPos() const
void setCloudCoverLookup(const Ogre::String &fileName)
Set the image used to lookup the cloud coverage threshold.
void update(Ogre::Real timePassed, const Ogre::Vector3 &sunDirection, const Ogre::ColourValue &sunLightColour, const Ogre::ColourValue &fogColour, const Ogre::ColourValue &sunSphereColour)
Update function called each frame from above.
void setFarFadeDist(const Ogre::Real value)
Set only far fade distance (see setFadeDistances).
Ogre::Real getCloudBlendTime() const
Gets the time it takes to blend two cloud shaped together, in seconds.
void setHeight(Ogre::Real height)
Set the height of the cloud layer.
static const Ogre::String pointerToString(void *pointer)
Quickly format a pointer as a string; in hex.
static Ogre::MaterialPtr checkLoadMaterialClone(const Ogre::String &originalName, const Ogre::String &cloneName)
Creates a private clone of a material from a script.
Ogre::Real getHeight() const
Get the height of the cloud layer.
void _updateVisibilityThreshold()
Enforce setCloudCoverVisibilityThreshold.
Ogre::Real getCloudCover() const
Gets the current cloud cover.
void disableCloudCoverLookup()
Disable any cloud cover lookup.
void setCloudBlendTime(const Ogre::Real value)
Sets the time it takes to blend two cloud shaped together, in seconds.
void setCloudCoverVisibilityThreshold(const Ogre::Real value)
Set cloud cover visiblity threshold.
static Ogre::ColourValue getInterpolatedColour(float fx, float fy, Ogre::Image *img, bool wrapX=true)
Gets the interpolated colour between two pixels from an image.
const Ogre::String getCloudCoverLookupFileName() const
Get the filename of the cloud cover lookup image.
bool isNull() const
Check if this is null.
void setCloudCover(const Ogre::Real cloudCover)
Sets cloud cover, between 0 (completely clear) and 1 (completely covered)
Ogre::Real getCloudCoverVisibilityThreshold() const
Get cloud cover visiblity threshold.
void advanceAnimation(Ogre::Real timePassed)
Advance cloud animation (the time part of FlatCloudLayer::update).
static const String RESOURCE_GROUP_NAME
Resource group name for caelum resources.
void setNearFadeDist(const Ogre::Real value)
Set only near fade distance (see setFadeDistances).
void _invalidateGeometry()
Regenerate the plane mesh and recreate entity.
void reset(const InnerPointerType &newInner=TraitsT::getNullValue())
Change the inner value.
void reset()
Reset most tweak settings to their default values.
void setCloudSpeed(const Ogre::Vector2 &cloudSpeed)
Sets cloud movement speed.
void setHeightRedFactor(const Ogre::Real value)
High-altitude clouds are tinted red in the evening.
void _ensureGeometry()
Regenerate the plane mesh and recreate entity.
void setCloudUVFactor(const Ogre::Real value)
Cloud texture coordinates are multiplied with this.
void setFadeDistances(Ogre::Real nearValue, Ogre::Real farValue)
Cloud fade distances.
void setMeshParameters(Real meshWidth, Real meshHeight, int meshWidthSegments, int meshHeightSegments)
Reset all mesh parameters.