diff --git a/src/basics/NoiseGenerator.cpp b/src/basics/NoiseGenerator.cpp index c8fb741..cff55b3 100644 --- a/src/basics/NoiseGenerator.cpp +++ b/src/basics/NoiseGenerator.cpp @@ -29,7 +29,7 @@ NoiseGenerator::~NoiseGenerator() { } -void NoiseGenerator::save(PackStream* stream) +void NoiseGenerator::save(PackStream* stream) const { int x; @@ -43,7 +43,7 @@ void NoiseGenerator::save(PackStream* stream) for (x = 0; x < level_count; x++) { - NoiseLevel* level = levels + x; + const NoiseLevel* level = levels + x; stream->write(&level->frequency); stream->write(&level->amplitude); @@ -84,7 +84,7 @@ void NoiseGenerator::load(PackStream* stream) validate(); } -void NoiseGenerator::copy(NoiseGenerator* destination) +void NoiseGenerator::copy(NoiseGenerator* destination) const { destination->function = function; destination->height_offset = height_offset; @@ -94,7 +94,7 @@ void NoiseGenerator::copy(NoiseGenerator* destination) state.copy(&destination->state); - validate(); + destination->validate(); } void NoiseGenerator::validate() @@ -189,13 +189,13 @@ void NoiseGenerator::forceValue(double value) addLevelSimple(1.0, 0.0, 0.0); /* FIXME Should not be needed */ } -void NoiseGenerator::getRange(double* minvalue, double* maxvalue) +void NoiseGenerator::getRange(double* minvalue, double* maxvalue) const { *minvalue = _min_value; *maxvalue = _max_value; } -int NoiseGenerator::getLevelCount() +int NoiseGenerator::getLevelCount() const { return level_count; } @@ -263,7 +263,7 @@ void NoiseGenerator::removeLevel(int level) } } -int NoiseGenerator::getLevel(int level, NoiseLevel* params) +int NoiseGenerator::getLevel(int level, NoiseLevel* params) const { if (level >= 0 && level < level_count) { @@ -374,12 +374,12 @@ static inline double _fixValue(double value, double ridge, double curve) -inline double NoiseGenerator::_get1DLevelValue(NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x) +inline double NoiseGenerator::_get1DLevelValue(const NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x) const { return level->minvalue + _fixValue(_func_noise_1d(x * level->frequency + offset.x), function.ridge_factor, function.curve_factor) * level->amplitude; } -double NoiseGenerator::get1DLevel(int level, double x) +double NoiseGenerator::get1DLevel(int level, double x) const { if (level >= 0 && level < level_count) { @@ -391,7 +391,7 @@ double NoiseGenerator::get1DLevel(int level, double x) } } -double NoiseGenerator::get1DTotal(double x) +double NoiseGenerator::get1DTotal(double x) const { int level; double result; @@ -404,7 +404,7 @@ double NoiseGenerator::get1DTotal(double x) return result + height_offset; } -double NoiseGenerator::get1DDetail(double x, double detail) +double NoiseGenerator::get1DDetail(double x, double detail) const { int level; double result, height, factor; @@ -431,12 +431,12 @@ double NoiseGenerator::get1DDetail(double x, double detail) -inline double NoiseGenerator::_get2DLevelValue(NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x, double y) +inline double NoiseGenerator::_get2DLevelValue(const NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x, double y) const { return level->minvalue + _fixValue(_func_noise_2d(x * level->frequency + offset.x, y * level->frequency + offset.y), function.ridge_factor, function.curve_factor) * level->amplitude; } -double NoiseGenerator::get2DLevel(int level, double x, double y) +double NoiseGenerator::get2DLevel(int level, double x, double y) const { if (level >= 0 && level < level_count) { @@ -448,7 +448,7 @@ double NoiseGenerator::get2DLevel(int level, double x, double y) } } -double NoiseGenerator::get2DTotal(double x, double y) +double NoiseGenerator::get2DTotal(double x, double y) const { int level; double result; @@ -461,7 +461,7 @@ double NoiseGenerator::get2DTotal(double x, double y) return result + height_offset; } -double NoiseGenerator::get2DDetail(double x, double y, double detail) +double NoiseGenerator::get2DDetail(double x, double y, double detail) const { int level; double result, height, factor; @@ -488,12 +488,12 @@ double NoiseGenerator::get2DDetail(double x, double y, double detail) -inline double NoiseGenerator::_get3DLevelValue(NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x, double y, double z) +inline double NoiseGenerator::_get3DLevelValue(const NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x, double y, double z) const { return level->minvalue + _fixValue(_func_noise_3d(x * level->frequency + offset.x, y * level->frequency + offset.y, z * level->frequency + offset.z), function.ridge_factor, function.curve_factor) * level->amplitude; } -double NoiseGenerator::get3DLevel(int level, double x, double y, double z) +double NoiseGenerator::get3DLevel(int level, double x, double y, double z) const { if (level >= 0 && level < level_count) { @@ -505,7 +505,7 @@ double NoiseGenerator::get3DLevel(int level, double x, double y, double z) } } -double NoiseGenerator::get3DTotal(double x, double y, double z) +double NoiseGenerator::get3DTotal(double x, double y, double z) const { int level; double result; @@ -518,7 +518,7 @@ double NoiseGenerator::get3DTotal(double x, double y, double z) return result + height_offset; } -double NoiseGenerator::get3DDetail(double x, double y, double z, double detail) +double NoiseGenerator::get3DDetail(double x, double y, double z, double detail) const { int level; double result, height, factor; diff --git a/src/basics/NoiseGenerator.h b/src/basics/NoiseGenerator.h index ca94f6f..20e3a6f 100644 --- a/src/basics/NoiseGenerator.h +++ b/src/basics/NoiseGenerator.h @@ -37,9 +37,9 @@ public: NoiseGenerator(); virtual ~NoiseGenerator(); - virtual void save(PackStream* stream); + virtual void save(PackStream* stream) const; virtual void load(PackStream* stream); - virtual void copy(NoiseGenerator* destination); + virtual void copy(NoiseGenerator* destination) const; virtual void validate(); inline const NoiseState &getState() const {return state;} @@ -51,32 +51,32 @@ public: void setFunction(NoiseFunction* function); void setFunctionParams(NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor); void forceValue(double value); - void getRange(double* minvalue, double* maxvalue); - int getLevelCount(); + void getRange(double* minvalue, double* maxvalue) const; + int getLevelCount() const; void clearLevels(); void addLevel(NoiseLevel level); void addLevelSimple(double scaling, double minvalue, double maxvalue); void addLevels(int level_count, NoiseLevel start_level, double scaling_factor, double amplitude_factor, double center_factor); void addLevelsSimple(int level_count, double scaling, double minvalue, double maxvalue, double center_factor); void removeLevel(int level); - int getLevel(int level, NoiseLevel* params); + int getLevel(int level, NoiseLevel* params) const; void setLevel(int index, NoiseLevel level); void setLevelSimple(int index, double scaling, double minvalue, double maxvalue); void normalizeAmplitude(double minvalue, double maxvalue, int adjust_scaling); - double get1DLevel(int level, double x); - double get1DTotal(double x); - double get1DDetail(double x, double detail); - double get2DLevel(int level, double x, double y); - double get2DTotal(double x, double y); - double get2DDetail(double x, double y, double detail); - double get3DLevel(int level, double x, double y, double z); - double get3DTotal(double x, double y, double z); - double get3DDetail(double x, double y, double z, double detail); + double get1DLevel(int level, double x) const; + double get1DTotal(double x) const; + double get1DDetail(double x, double detail) const; + double get2DLevel(int level, double x, double y) const; + double get2DTotal(double x, double y) const; + double get2DDetail(double x, double y, double detail) const; + double get3DLevel(int level, double x, double y, double z) const; + double get3DTotal(double x, double y, double z) const; + double get3DDetail(double x, double y, double z, double detail) const; private: - double _get1DLevelValue(NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x); - double _get2DLevelValue(NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x, double y); - double _get3DLevelValue(NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x, double y, double z); + double _get1DLevelValue(const NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x) const; + double _get2DLevelValue(const NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x, double y) const; + double _get3DLevelValue(const NoiseLevel* level, const NoiseState::NoiseOffset &offset, double x, double y, double z) const; NoiseFunction function; double height_offset; diff --git a/src/definition/NoiseNode.cpp b/src/definition/NoiseNode.cpp new file mode 100644 index 0000000..5430393 --- /dev/null +++ b/src/definition/NoiseNode.cpp @@ -0,0 +1,51 @@ +#include "NoiseNode.h" + +#include "NoiseGenerator.h" +#include "Logs.h" + +NoiseNode::NoiseNode(DefinitionNode *parent): + DefinitionNode(parent, "noise") +{ + noise = new NoiseGenerator(); +} + +NoiseNode::~NoiseNode() +{ + delete noise; +} + +void NoiseNode::setLevels(int levels, double min_value, double max_value) +{ + noise->clearLevels(); + noise->addLevelsSimple(levels, 1.0, -1.0, 1.0, 0.5); + noise->normalizeAmplitude(min_value, max_value, false); + noise->randomizeOffsets(); + noise->validate(); +} + +void NoiseNode::save(PackStream *stream) const +{ + noise->save(stream); +} + +void NoiseNode::load(PackStream *stream) +{ + noise->load(stream); +} + +void NoiseNode::copy(DefinitionNode *destination) const +{ + if (destination->getTypeName() == getTypeName()) + { + noise->copy(((NoiseNode *)destination)->noise); + } + else + { + Logs::error() << "Can't copy from " << getTypeName() << " to " << destination->getTypeName() << std::endl; + } +} + +void NoiseNode::validate() +{ + noise->validate(); +} diff --git a/src/definition/NoiseNode.h b/src/definition/NoiseNode.h new file mode 100644 index 0000000..f28340e --- /dev/null +++ b/src/definition/NoiseNode.h @@ -0,0 +1,41 @@ +#ifndef NOISENODE_H +#define NOISENODE_H + +#include "definition_global.h" + +#include "DefinitionNode.h" + +namespace paysages { +namespace definition { + +/** + * Definition node with noise parameters. + */ +class DEFINITIONSHARED_EXPORT NoiseNode: public DefinitionNode +{ +public: + NoiseNode(DefinitionNode *parent); + virtual ~NoiseNode(); + + inline const NoiseGenerator *getGenerator() {return noise;} + + /** + * Set the number of levels to use in the noise generator. + */ + void setLevels(int levels, double min_value=-1.0, double max_value=1.0); + +protected: + virtual void save(PackStream *stream) const override; + virtual void load(PackStream *stream) override; + virtual void copy(DefinitionNode *destination) const override; + virtual void validate() override; + +private: + NoiseGenerator *noise; + +}; + +} +} + +#endif // NOISENODE_H diff --git a/src/definition/definition.pro b/src/definition/definition.pro index 559d586..c85601f 100644 --- a/src/definition/definition.pro +++ b/src/definition/definition.pro @@ -42,6 +42,7 @@ SOURCES += \ DefinitionWatcher.cpp \ IntNode.cpp \ IntDiff.cpp \ + NoiseNode.cpp \ GodRaysDefinition.cpp \ TimeManager.cpp \ VegetationPresenceDefinition.cpp @@ -76,6 +77,7 @@ HEADERS +=\ DefinitionWatcher.h \ IntNode.h \ IntDiff.h \ + NoiseNode.h \ GodRaysDefinition.h \ TimeManager.h \ VegetationPresenceDefinition.h diff --git a/src/definition/definition_global.h b/src/definition/definition_global.h index 4565eee..489fa28 100644 --- a/src/definition/definition_global.h +++ b/src/definition/definition_global.h @@ -19,6 +19,7 @@ namespace definition { class FloatDiff; class IntNode; class IntDiff; + class NoiseNode; class DiffManager; class DefinitionWatcher; class Scenery;