From d82fc73531d6dd35910d7125ff6d5b53e6ce7ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 8 Nov 2015 23:32:52 +0100 Subject: [PATCH] Small optimizations (use reference to avoid object copy) --- src/definition/SurfaceMaterial.cpp | 7 +++++++ src/definition/SurfaceMaterial.h | 2 ++ src/render/opengl/OpenGLRenderer.cpp | 2 +- src/render/opengl/OpenGLRenderer.h | 2 +- src/render/software/AtmosphereRenderer.cpp | 16 ++++++++-------- src/render/software/AtmosphereRenderer.h | 8 ++++---- src/render/software/SoftwareRenderer.cpp | 11 ++++------- src/render/software/SoftwareRenderer.h | 2 +- 8 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/definition/SurfaceMaterial.cpp b/src/definition/SurfaceMaterial.cpp index f08971a..b00d6d9 100644 --- a/src/definition/SurfaceMaterial.cpp +++ b/src/definition/SurfaceMaterial.cpp @@ -3,6 +3,8 @@ #include "PackStream.h" #include "Color.h" +static SurfaceMaterial DEFAULT; + SurfaceMaterial::SurfaceMaterial(): SurfaceMaterial(COLOR_BLACK) { @@ -22,6 +24,11 @@ SurfaceMaterial::~SurfaceMaterial() delete base; } +const SurfaceMaterial &SurfaceMaterial::getDefault() +{ + return DEFAULT; +} + void SurfaceMaterial::setColor(double r, double g, double b, double a) { base->r = r; diff --git a/src/definition/SurfaceMaterial.h b/src/definition/SurfaceMaterial.h index 3a137be..aa6787e 100644 --- a/src/definition/SurfaceMaterial.h +++ b/src/definition/SurfaceMaterial.h @@ -13,6 +13,8 @@ public: SurfaceMaterial(const Color& color); ~SurfaceMaterial(); + static const SurfaceMaterial &getDefault(); + void setColor(double r, double g, double b, double a); void save(PackStream* stream) const; diff --git a/src/render/opengl/OpenGLRenderer.cpp b/src/render/opengl/OpenGLRenderer.cpp index 1a397a1..d960f28 100644 --- a/src/render/opengl/OpenGLRenderer.cpp +++ b/src/render/opengl/OpenGLRenderer.cpp @@ -235,7 +235,7 @@ double OpenGLRenderer::getPrecision(const Vector3 &) return 0.0000001; } -Color OpenGLRenderer::applyMediumTraversal(Vector3, Color color) +Color OpenGLRenderer::applyMediumTraversal(const Vector3&, const Color &color) { return color; } diff --git a/src/render/opengl/OpenGLRenderer.h b/src/render/opengl/OpenGLRenderer.h index d40a4b4..ceee92b 100644 --- a/src/render/opengl/OpenGLRenderer.h +++ b/src/render/opengl/OpenGLRenderer.h @@ -65,7 +65,7 @@ public: inline OpenGLSharedState* getSharedState() const {return shared_state;} virtual double getPrecision(const Vector3 &location) override; - virtual Color applyMediumTraversal(Vector3 location, Color color) override; + virtual Color applyMediumTraversal(const Vector3 &location, const Color &color) override; private: /** diff --git a/src/render/software/AtmosphereRenderer.cpp b/src/render/software/AtmosphereRenderer.cpp index 9ae2ff4..ac2e446 100644 --- a/src/render/software/AtmosphereRenderer.cpp +++ b/src/render/software/AtmosphereRenderer.cpp @@ -83,7 +83,7 @@ BaseAtmosphereRenderer::BaseAtmosphereRenderer(SoftwareRenderer* renderer): { } -AtmosphereResult BaseAtmosphereRenderer::applyAerialPerspective(Vector3, Color base) +AtmosphereResult BaseAtmosphereRenderer::applyAerialPerspective(const Vector3 &, const Color &base) { AtmosphereResult result; result.base = result.final = base; @@ -91,7 +91,7 @@ AtmosphereResult BaseAtmosphereRenderer::applyAerialPerspective(Vector3, Color b return result; } -AtmosphereResult BaseAtmosphereRenderer::getSkyColor(Vector3) +AtmosphereResult BaseAtmosphereRenderer::getSkyColor(const Vector3 &) { AtmosphereResult result; result.base = result.final = COLOR_WHITE; @@ -127,7 +127,7 @@ SoftwareBrunetonAtmosphereRenderer::~SoftwareBrunetonAtmosphereRenderer() delete model; } -AtmosphereResult SoftwareBrunetonAtmosphereRenderer::applyAerialPerspective(Vector3 location, Color base) +AtmosphereResult SoftwareBrunetonAtmosphereRenderer::applyAerialPerspective(const Vector3 &location, const Color &base) { AtmosphereDefinition* definition = getDefinition(); AtmosphereResult result; @@ -151,7 +151,7 @@ AtmosphereResult SoftwareBrunetonAtmosphereRenderer::applyAerialPerspective(Vect return result; } -AtmosphereResult SoftwareBrunetonAtmosphereRenderer::getSkyColor(Vector3 direction) +AtmosphereResult SoftwareBrunetonAtmosphereRenderer::getSkyColor(const Vector3 &direction) { AtmosphereDefinition* definition; Vector3 sun_direction, sun_position, camera_location; @@ -161,13 +161,13 @@ AtmosphereResult SoftwareBrunetonAtmosphereRenderer::getSkyColor(Vector3 directi camera_location = parent->getCameraLocation(VECTOR_ZERO); sun_direction = getSunDirection(); - direction = direction.normalize(); + Vector3 direction_norm = direction.normalize(); sun_position = sun_direction.scale(SUN_DISTANCE_SCALED); base = COLOR_BLACK; // Get night sky - base = base.add(parent->getNightSky()->getColor(camera_location.y, direction)); + base = base.add(parent->getNightSky()->getColor(camera_location.y, direction_norm)); // Get sun shape /*if (v3Dot(sun_direction, direction) >= 0) @@ -196,11 +196,11 @@ AtmosphereResult SoftwareBrunetonAtmosphereRenderer::getSkyColor(Vector3 directi // Get scattering AtmosphereResult result; - Vector3 location = camera_location.add(direction.scale(6421.0)); + Vector3 location = camera_location.add(direction_norm.scale(6421.0)); switch (definition->model) { case AtmosphereDefinition::ATMOSPHERE_MODEL_BRUNETON: - result = model->getSkyColor(camera_location, direction, sun_position, base); + result = model->getSkyColor(camera_location, direction_norm, sun_position, base); break; default: result = BaseAtmosphereRenderer::applyAerialPerspective(location, result.base); diff --git a/src/render/software/AtmosphereRenderer.h b/src/render/software/AtmosphereRenderer.h index 7f22aec..f29fa58 100644 --- a/src/render/software/AtmosphereRenderer.h +++ b/src/render/software/AtmosphereRenderer.h @@ -14,8 +14,8 @@ public: BaseAtmosphereRenderer(SoftwareRenderer* parent); virtual ~BaseAtmosphereRenderer() {} - virtual AtmosphereResult applyAerialPerspective(Vector3 location, Color base); - virtual AtmosphereResult getSkyColor(Vector3 direction); + virtual AtmosphereResult applyAerialPerspective(const Vector3 &location, const Color &base); + virtual AtmosphereResult getSkyColor(const Vector3 &direction); virtual Vector3 getSunDirection(bool cache=true) const; virtual bool getLightsAt(std::vector &result, const Vector3 &location) const override; @@ -31,8 +31,8 @@ public: SoftwareBrunetonAtmosphereRenderer(SoftwareRenderer* parent); virtual ~SoftwareBrunetonAtmosphereRenderer(); - virtual AtmosphereResult applyAerialPerspective(Vector3 location, Color base) override; - virtual AtmosphereResult getSkyColor(Vector3 direction) override; + virtual AtmosphereResult applyAerialPerspective(const Vector3 &location, const Color &base) override; + virtual AtmosphereResult getSkyColor(const Vector3 &direction) override; virtual bool getLightsAt(std::vector &result, const Vector3 &location) const override; diff --git a/src/render/software/SoftwareRenderer.cpp b/src/render/software/SoftwareRenderer.cpp index f34ee76..3daeed5 100644 --- a/src/render/software/SoftwareRenderer.cpp +++ b/src/render/software/SoftwareRenderer.cpp @@ -114,14 +114,11 @@ Color SoftwareRenderer::applyLightingToSurface(const Vector3 &location, const Ve return lighting->apply(getCameraLocation(location), location, normal, material); } -Color SoftwareRenderer::applyMediumTraversal(Vector3 location, Color color) +Color SoftwareRenderer::applyMediumTraversal(const Vector3 &location, const Color &color) { - color = atmosphere_renderer->applyAerialPerspective(location, color).final; - color = clouds_renderer->getColor(getCameraLocation(location), location, color); - return color; - - /*Vector3 eye = cameraGetLocation(scenery->getCamera()); - return fluid_medium->applyTraversal(eye, location, color);*/ + Color result = atmosphere_renderer->applyAerialPerspective(location, color).final; + result = clouds_renderer->getColor(getCameraLocation(location), location, result); + return result; } RayCastingResult SoftwareRenderer::rayWalking(const Vector3 &location, const Vector3 &direction, int, int, int, int) diff --git a/src/render/software/SoftwareRenderer.h b/src/render/software/SoftwareRenderer.h index 244921e..9786f26 100644 --- a/src/render/software/SoftwareRenderer.h +++ b/src/render/software/SoftwareRenderer.h @@ -58,7 +58,7 @@ public: inline GodRaysSampler* getGodRaysSampler() const {return godrays;} virtual Color applyLightingToSurface(const Vector3 &location, const Vector3 &normal, const SurfaceMaterial &material); - virtual Color applyMediumTraversal(Vector3 location, Color color); + virtual Color applyMediumTraversal(const Vector3 &location, const Color &color); virtual RayCastingResult rayWalking(const Vector3 &location, const Vector3 &direction, int terrain, int water, int sky, int clouds); private: