aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2014-12-08 03:25:14 -0500
committerkwolekr <kwolekr@minetest.net>2014-12-08 03:26:29 -0500
commit2a7c6d27b32711e2439bf56715d813ef96b8f904 (patch)
tree4d10b13096a797a90284f9805844e03e0bb18af9 /src
parent0183c05ee0ed0c6566a860119ee93cf88ba7d9ee (diff)
Optimize noise implementations
Diffstat (limited to 'src')
-rw-r--r--src/noise.cpp197
-rw-r--r--src/noise.h16
2 files changed, 106 insertions, 107 deletions
diff --git a/src/noise.cpp b/src/noise.cpp
index ac84ab20a..3fedf357f 100644
--- a/src/noise.cpp
+++ b/src/noise.cpp
@@ -83,7 +83,7 @@ float noise3d(int x, int y, int z, int seed)
}
-float dotProduct(float vx, float vy, float wx, float wy)
+inline float dotProduct(float vx, float vy, float wx, float wy)
{
return vx * wx + vy * wy;
}
@@ -95,38 +95,35 @@ inline float linearInterpolation(float v0, float v1, float t)
}
-float biLinearInterpolation(
+inline float biLinearInterpolation(
float v00, float v10,
float v01, float v11,
float x, float y)
{
float tx = easeCurve(x);
float ty = easeCurve(y);
+#if 0
return (
v00 * (1 - tx) * (1 - ty) +
- v10 * tx * (1 - ty) +
- v01 * (1 - tx) * ty +
- v11 * tx * ty
+ v10 * tx * (1 - ty) +
+ v01 * (1 - tx) * ty +
+ v11 * tx * ty
);
- //float u = linearInterpolation(v00, v10, x);
- //float v = linearInterpolation(v01, v11, x);
- //return linearInterpolation(u, v, y);
+#endif
+ float u = linearInterpolation(v00, v10, tx);
+ float v = linearInterpolation(v01, v11, tx);
+ return linearInterpolation(u, v, ty);
}
-float biLinearInterpolationNoEase(
+inline float biLinearInterpolationNoEase(
float v00, float v10,
float v01, float v11,
float x, float y)
{
- float tx = x;
- float ty = y;
- return (
- v00 * (1 - tx) * (1 - ty) +
- v10 * tx * (1 - ty) +
- v01 * (1 - tx) * ty +
- v11 * tx * ty
- );
+ float u = linearInterpolation(v00, v10, x);
+ float v = linearInterpolation(v01, v11, x);
+ return linearInterpolation(u, v, y);
}
@@ -138,6 +135,7 @@ float triLinearInterpolation(
float tx = easeCurve(x);
float ty = easeCurve(y);
float tz = easeCurve(z);
+#if 0
return (
v000 * (1 - tx) * (1 - ty) * (1 - tz) +
v100 * tx * (1 - ty) * (1 - tz) +
@@ -148,9 +146,10 @@ float triLinearInterpolation(
v011 * (1 - tx) * ty * tz +
v111 * tx * ty * tz
);
- //float u = biLinearInterpolation(v000, v100, v010, v110, x, y);
- //float v = biLinearInterpolation(v001, v101, v011, v111, x, y);
- //return linearInterpolation(u, v, z);
+#endif
+ float u = biLinearInterpolationNoEase(v000, v100, v010, v110, tx, ty);
+ float v = biLinearInterpolationNoEase(v001, v101, v011, v111, tx, ty);
+ return linearInterpolation(u, v, tz);
}
float triLinearInterpolationNoEase(
@@ -158,19 +157,9 @@ float triLinearInterpolationNoEase(
float v001, float v101, float v011, float v111,
float x, float y, float z)
{
- float tx = x;
- float ty = y;
- float tz = z;
- return (
- v000 * (1 - tx) * (1 - ty) * (1 - tz) +
- v100 * tx * (1 - ty) * (1 - tz) +
- v010 * (1 - tx) * ty * (1 - tz) +
- v110 * tx * ty * (1 - tz) +
- v001 * (1 - tx) * (1 - ty) * tz +
- v101 * tx * (1 - ty) * tz +
- v011 * (1 - tx) * ty * tz +
- v111 * tx * ty * tz
- );
+ float u = biLinearInterpolationNoEase(v000, v100, v010, v110, x, y);
+ float v = biLinearInterpolationNoEase(v001, v101, v011, v111, x, y);
+ return linearInterpolation(u, v, z);
}
@@ -336,52 +325,59 @@ Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz)
this->sy = sy;
this->sz = sz;
- this->noisebuf = NULL;
+ this->persist_buf = NULL;
+ this->gradient_buf = NULL;
+ this->result = NULL;
if (np->flags & NOISE_FLAG_DEFAULTS) {
// By default, only 2d noise is eased.
- if (sz == 1)
+ if (sz <= 1)
np->flags |= NOISE_FLAG_EASED;
}
- resizeNoiseBuf(sz > 1);
- try {
- this->buf = new float[sx * sy * sz];
- this->result = new float[sx * sy * sz];
- } catch (std::bad_alloc &e) {
- throw InvalidNoiseParamsException();
- }
+ allocBuffers();
}
Noise::~Noise()
{
- delete[] buf;
+ delete[] gradient_buf;
/* Minetest Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com> Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr> This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "gameui.h" #include <irrlicht_changes/static_text.h> #include <gettext.h> #include "gui/mainmenumanager.h" #include "gui/guiChatConsole.h" #include "util/pointedthing.h" #include "client.h" #include "clientmap.h" #include "fontengine.h" #include "nodedef.h" #include "profiler.h" #include "renderingengine.h" #include "version.h" inline static const char *yawToDirectionString(int yaw) { static const char *direction[4] = {"North +Z", "West -X", "South -Z", "East +X"}; yaw = wrapDegrees_0_360(yaw); yaw = (yaw + 45) % 360 / 90; return direction[yaw]; } GameUI::GameUI() { if (guienv && guienv->getSkin()) m_statustext_initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT); else m_statustext_initial_color = video::SColor(255, 0, 0, 0); } void GameUI::init() { // First line of debug text m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(), core::rect<s32>(0, 0, 0, 0), false, false, guiroot); // Second line of debug text m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false, false, guiroot); // At the middle of the screen // Object infos are shown in this m_guitext_info = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5) + v2s32(100, 200), false, true, guiroot); // Status text (displays info when showing and hiding GUI stuff, etc.) m_guitext_status = gui::StaticText::add(guienv, L"<Status>", core::rect<s32>(0, 0, 0, 0), false, false, guiroot); m_guitext_status->setVisible(false); // Chat text m_guitext_chat = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), //false, false); // Disable word wrap as of now false, true, guiroot); // Profiler text (size is updated when text is updated) m_guitext_profiler = gui::StaticText::add(guienv, L"<Profiler>", core::rect<s32>(0, 0, 0, 0), false, false, guiroot); m_guitext_profiler->setOverrideFont(g_fontengine->getFont( g_fontengine->getDefaultFontSize() * 0.9f, FM_Mono)); m_guitext_profiler->setVisible(false); } void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control, const CameraOrientation &cam, const PointedThing &pointed_old, const GUIChatConsole *chat_console, float dtime) { v2u32 screensize = RenderingEngine::get_instance()->getWindowSize(); if (m_flags.show_debug) { static float drawtime_avg = 0; drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05; u16 fps = 1.0 / stats.dtime_jitter.avg; std::ostringstream os(std::ios_base::binary); os << std::fixed << PROJECT_NAME_C " " << g_version_hash << " | FPS: " << fps << std::setprecision(0) << " | drawtime: " << drawtime_avg << "ms" << std::setprecision(1) << " | dtime jitter: " << (stats.dtime_jitter.max_fraction * 100.0) << "%" << std::setprecision(