From 67f26977a5f2be82a50eddb031b4ef92a5205528 Mon Sep 17 00:00:00 2001 From: Kai Blaschke Date: Thu, 25 Nov 2021 14:51:55 +0100 Subject: [PATCH] Replaced Perlin noise textures with the original Milkdrop noise textures. --- presets/tests/260-compshader-noise_lq.milk | 13 + presets/tests/261-compshader-noisevol_lq.milk | 13 + src/libprojectM/Renderer/CMakeLists.txt | 6 +- src/libprojectM/Renderer/MilkdropNoise.cpp | 268 ++++++++++++++++++ src/libprojectM/Renderer/MilkdropNoise.hpp | 71 +++++ src/libprojectM/Renderer/PerlinNoise.cpp | 70 ----- src/libprojectM/Renderer/PerlinNoise.hpp | 170 ----------- .../Renderer/PerlinNoiseWithAlpha.cpp | 84 ------ .../Renderer/PerlinNoiseWithAlpha.hpp | 176 ------------ src/libprojectM/Renderer/TextureManager.cpp | 33 +-- 10 files changed, 375 insertions(+), 529 deletions(-) create mode 100644 presets/tests/260-compshader-noise_lq.milk create mode 100644 presets/tests/261-compshader-noisevol_lq.milk create mode 100644 src/libprojectM/Renderer/MilkdropNoise.cpp create mode 100644 src/libprojectM/Renderer/MilkdropNoise.hpp delete mode 100644 src/libprojectM/Renderer/PerlinNoise.cpp delete mode 100644 src/libprojectM/Renderer/PerlinNoise.hpp delete mode 100644 src/libprojectM/Renderer/PerlinNoiseWithAlpha.cpp delete mode 100644 src/libprojectM/Renderer/PerlinNoiseWithAlpha.hpp diff --git a/presets/tests/260-compshader-noise_lq.milk b/presets/tests/260-compshader-noise_lq.milk new file mode 100644 index 000000000..93e637e4d --- /dev/null +++ b/presets/tests/260-compshader-noise_lq.milk @@ -0,0 +1,13 @@ +[preset00] +per_frame_1000=// spectrum vs pcm + +fDecay=0 +warp=0.000000 +wave_a=0 + +// Display noise_lq texture. +// Alpha channel is not used, but present. +comp_1=`shader_body +comp_2=`{ +comp_3=`ret = tex2D(sampler_fw_noise_lq, uv).xyz; +comp_4=`} diff --git a/presets/tests/261-compshader-noisevol_lq.milk b/presets/tests/261-compshader-noisevol_lq.milk new file mode 100644 index 000000000..fec7c06d9 --- /dev/null +++ b/presets/tests/261-compshader-noisevol_lq.milk @@ -0,0 +1,13 @@ +[preset00] +per_frame_1000=// spectrum vs pcm + +fDecay=0 +warp=0.000000 +wave_a=0 + +// Display noisevol_lq texture and cycle through the cube's slices over time. +// Alpha channel is not used, but present. +comp_1=`shader_body +comp_2=`{ +comp_3=`ret = tex3D(sampler_fw_noisevol_lq, float3(uv, time / 10.0)).xyz; +comp_4=`} diff --git a/src/libprojectM/Renderer/CMakeLists.txt b/src/libprojectM/Renderer/CMakeLists.txt index 615a85bfc..d13282dd8 100644 --- a/src/libprojectM/Renderer/CMakeLists.txt +++ b/src/libprojectM/Renderer/CMakeLists.txt @@ -8,14 +8,12 @@ add_library(Renderer OBJECT Filters.hpp MenuText.cpp MenuText.h + MilkdropNoise.cpp + MilkdropNoise.hpp MilkdropWaveform.cpp MilkdropWaveform.hpp PerPixelMesh.cpp PerPixelMesh.hpp - PerlinNoise.cpp - PerlinNoise.hpp - PerlinNoiseWithAlpha.cpp - PerlinNoiseWithAlpha.hpp Pipeline.cpp Pipeline.hpp PipelineContext.cpp diff --git a/src/libprojectM/Renderer/MilkdropNoise.cpp b/src/libprojectM/Renderer/MilkdropNoise.cpp new file mode 100644 index 000000000..84da34051 --- /dev/null +++ b/src/libprojectM/Renderer/MilkdropNoise.cpp @@ -0,0 +1,268 @@ +#include "MilkdropNoise.hpp" + +#include +#include + +MilkdropNoise::MilkdropNoise() +{ + generate2D(256, 1, &noise_lq); + generate2D(32, 1, &noise_lq_lite); + generate2D(256, 4, &noise_mq); + generate2D(256, 8, &noise_hq); + + generate3D(32, 1, &noise_lq_vol); + generate3D(32, 4, &noise_hq_vol); +} + +MilkdropNoise::~MilkdropNoise() +{ + delete[] noise_lq; + delete[] noise_lq_lite; + delete[] noise_mq; + delete[] noise_hq; + delete[] noise_lq_vol; + delete[] noise_hq_vol; +} + +void MilkdropNoise::generate2D(int size, int zoomFactor, uint32_t** textureData) +{ + auto randomSeed = std::chrono::system_clock::now().time_since_epoch().count(); + std::default_random_engine randomGenerator(randomSeed); + std::uniform_int_distribution randomDistribution(0, INT32_MAX); + + *textureData = new uint32_t[size * size](); + + // write to the bits... + auto dst = *textureData; + auto RANGE = (zoomFactor > 1) ? 216 : 256; + for (auto y = 0; y < size; y++) + { + for (auto x = 0; x < size; x++) + { + dst[x] = (static_cast((randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 24) | + (static_cast((randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 16) | + (static_cast((randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 8) | + (static_cast((randomDistribution(randomGenerator) % RANGE) + RANGE / 2)); + } + // swap some pixels randomly, to improve 'randomness' + for (auto x = 0; x < size; x++) + { + auto x1 = randomDistribution(randomGenerator) % size; + auto x2 = randomDistribution(randomGenerator) % size; + auto temp = dst[x2]; + dst[x2] = dst[x1]; + dst[x1] = temp; + } + dst += size; + } + + // smoothing + if (zoomFactor > 1) + { + dst = *textureData; + + // first go ACROSS, blending cubically on X, but only on the main lines. + for (auto y = 0; y < size; y += zoomFactor) + { + for (auto x = 0; x < size; x++) + { + if (x % zoomFactor) + { + auto base_x = (x / zoomFactor) * zoomFactor + size; + auto base_y = y * size; + auto y0 = dst[base_y + ((base_x - zoomFactor) % size)]; + auto y1 = dst[base_y + ((base_x) % size)]; + auto y2 = dst[base_y + ((base_x + zoomFactor) % size)]; + auto y3 = dst[base_y + ((base_x + zoomFactor * 2) % size)]; + + auto t = static_cast(x % zoomFactor) / static_cast( zoomFactor); + + auto result = dwCubicInterpolate(y0, y1, y2, y3, t); + + dst[y * size + x] = result; + } + } + } + + // next go down, doing cubic interp along Y, on every line. + for (auto x = 0; x < size; x++) + { + for (auto y = 0; y < size; y++) + { + if (y % zoomFactor) + { + auto base_y = (y / zoomFactor) * zoomFactor + size; + auto y0 = dst[((base_y - zoomFactor) % size) * size + x]; + auto y1 = dst[((base_y) % size) * size + x]; + auto y2 = dst[((base_y + zoomFactor) % size) * size + x]; + auto y3 = dst[((base_y + zoomFactor * 2) % size) * size + x]; + + auto t = static_cast(y % zoomFactor) / static_cast(zoomFactor); + + auto result = dwCubicInterpolate(y0, y1, y2, y3, t); + + dst[y * size + x] = result; + } + } + } + + } +} + +void MilkdropNoise::generate3D(int size, int zoomFactor, uint32_t** textureData) +{ + auto randomSeed = std::chrono::system_clock::now().time_since_epoch().count(); + std::default_random_engine randomGenerator(randomSeed); + std::uniform_int_distribution randomDistribution(0, INT32_MAX); + + *textureData = new uint32_t[size * size * size](); + + // write to the bits... + int RANGE = (zoomFactor > 1) ? 216 : 256; + for (auto z = 0; z < size; z++) + { + auto dst = (*textureData) + z * size * size; + for (auto y = 0; y < size; y++) + { + for (auto x = 0; x < size; x++) + { + dst[x] = ((static_cast(randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 24) | + ((static_cast(randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 16) | + ((static_cast(randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 8) | + ((static_cast(randomDistribution(randomGenerator) % RANGE) + RANGE / 2)); + } + // swap some pixels randomly, to improve 'randomness' + for (auto x = 0; x < size; x++) + { + auto x1 = randomDistribution(randomGenerator) % size; + auto x2 = randomDistribution(randomGenerator) % size; + auto temp = dst[x2]; + dst[x2] = dst[x1]; + dst[x1] = temp; + } + dst += size; + } + } + + // smoothing + if (zoomFactor > 1) + { + // first go ACROSS, blending cubically on X, but only on the main lines. + auto dst = *textureData; + for (auto z = 0; z < size; z += zoomFactor) + { + for (auto y = 0; y < size; y += zoomFactor) + { + for (auto x = 0; x < size; x++) + { + if (x % zoomFactor) + { + auto base_x = (x / zoomFactor) * zoomFactor + size; + auto base_y = z * size + y * size; + auto y0 = dst[base_y + ((base_x - zoomFactor) % size)]; + auto y1 = dst[base_y + ((base_x) % size)]; + auto y2 = dst[base_y + ((base_x + zoomFactor) % size)]; + auto y3 = dst[base_y + ((base_x + zoomFactor * 2) % size)]; + + auto t = static_cast(x % zoomFactor) / static_cast(zoomFactor); + + auto result = dwCubicInterpolate(y0, y1, y2, y3, t); + + dst[z * size + y * size + x] = result; + } + } + } + } + + // next go down, doing cubic interp along Y, on the main slices. + for (auto z = 0; z < size; z += zoomFactor) + { + for (auto x = 0; x < size; x++) + { + for (auto y = 0; y < size; y++) + { + if (y % zoomFactor) + { + auto base_y = (y / zoomFactor) * zoomFactor + size; + auto base_z = z * size; + auto y0 = dst[((base_y - zoomFactor) % size) * size + base_z + x]; + auto y1 = dst[((base_y) % size) * size + base_z + x]; + auto y2 = dst[((base_y + zoomFactor) % size) * size + base_z + x]; + auto y3 = dst[((base_y + zoomFactor * 2) % size) * size + base_z + x]; + + auto t = static_cast(y % zoomFactor) / static_cast(zoomFactor); + + auto result = dwCubicInterpolate(y0, y1, y2, y3, t); + + dst[y * size + base_z + x] = result; + } + } + } + } + + // next go through, doing cubic interp along Z, everywhere. + for (auto x = 0; x < size; x++) + { + for (auto y = 0; y < size; y++) + { + for (auto z = 0; z < size; z++) + { + if (z % zoomFactor) + { + auto base_y = y * size; + auto base_z = (z / zoomFactor) * zoomFactor + size; + auto y0 = dst[((base_z - zoomFactor) % size) * size + base_y + x]; + auto y1 = dst[((base_z) % size) * size + base_y + x]; + auto y2 = dst[((base_z + zoomFactor) % size) * size + base_y + x]; + auto y3 = dst[((base_z + zoomFactor * 2) % size) * size + base_y + x]; + + auto t = static_cast(z % zoomFactor) / static_cast(zoomFactor); + + auto result = dwCubicInterpolate(y0, y1, y2, y3, t); + + dst[z * size + base_y + x] = result; + } + } + } + } + + } +} + +float MilkdropNoise::fCubicInterpolate(float y0, float y1, float y2, float y3, float t) +{ + auto t2 = t * t; + auto a0 = y3 - y2 - y0 + y1; + auto a1 = y0 - y1 - a0; + auto a2 = y2 - y0; + auto a3 = y1; + + return (a0 * t * t2 + a1 * t2 + a2 * t + a3); +} + +uint32_t MilkdropNoise::dwCubicInterpolate(uint32_t y0, uint32_t y1, uint32_t y2, uint32_t y3, float t) +{ + uint32_t ret = 0; + uint32_t shift = 0; + for (auto i = 0; i < 4; i++) + { + auto f = fCubicInterpolate( + static_cast((y0 >> shift) & 0xFF) / 255.0f, + static_cast((y1 >> shift) & 0xFF) / 255.0f, + static_cast((y2 >> shift) & 0xFF) / 255.0f, + static_cast((y3 >> shift) & 0xFF) / 255.0f, + t + ); + if (f < 0) + { + f = 0; + } + if (f > 1) + { + f = 1; + } + ret |= ((uint32_t) (f * 255)) << shift; + shift += 8; + } + return ret; +} diff --git a/src/libprojectM/Renderer/MilkdropNoise.hpp b/src/libprojectM/Renderer/MilkdropNoise.hpp new file mode 100644 index 000000000..0c8625708 --- /dev/null +++ b/src/libprojectM/Renderer/MilkdropNoise.hpp @@ -0,0 +1,71 @@ +#pragma once + +#include +#include + +/** + * @brief Implementation of Milkdrop's noise texture generator. + * + *

Uses the original Milkdrop noise code, generating a 8-bit RGBA 2D or 3D texture in different + * resolutions.

+ * + *

The preset authoring guide notes different resolutions (e.g. 32x32 for noise_hq) for the textures. These + * resolutions actually state the number of random data points inside the texture, which are interpolated, and not + * the actual texture size, which is always the size stated in the authoring guide time the zoom level used during + * generation.

. + * + *

projectM versions up to 3.x used Perlin noise, which looks quite similar, but the same noise value was used + * on all color channels. In addition to that, only the GLES version generated RGBA color channels, while the desktop + * version only used RGB channels and left alpha empty.

+ */ +class MilkdropNoise +{ +public: + /** + * Constructor. Generates all noise textures. + */ + MilkdropNoise(); + + /** + * Destructor. Deletes the allocated noise textures. + */ + ~MilkdropNoise(); + + uint32_t* noise_lq{ nullptr }; //!< Low-quality (high frequency) 2D noise texture, 256x256 with zoom level 1 + uint32_t* noise_lq_lite{ nullptr }; //!< Low-quality (high frequency) 2D noise texture, 32x32 with zoom level 1 + uint32_t* noise_mq{ nullptr }; //!< Medium-quality (medium frequency) 2D noise texture, 256x256 with zoom level 4 + uint32_t* noise_hq{ nullptr }; //!< High-quality (low frequency) 2D noise texture, 256x256 with zoom level 8 + + uint32_t* noise_lq_vol{ nullptr }; //!< Low-quality (high frequency) 3D noise texture, 32x32 with zoom level 1 + uint32_t* noise_hq_vol{ nullptr }; //!< High-quality (low frequency) 3D noise texture, 32x32 with zoom level 4 + +protected: + + /** + * @brief Milkdrop 2D noise algorithm + * + * Creates a different, smoothed noise texture in each of the four color channels. + * + * @param size Texture size in pixels. + * @param zoomFactor Zoom factor. Higher values give a more smoothed/interpolated look. + * @param textureData A pointer to the data structure that will receive the texture data. Must have size² elements. + */ + static void generate2D(int size, int zoomFactor, uint32_t** textureData); + + /** + * @brief Milkdrop §D noise algorithm + * + * Creates a different, smoothed noise texture in each of the four color channels. + * + * @param size Texture size in pixels. + * @param zoomFactor Zoom factor. Higher values give a more smoothed/interpolated look. + * @param textureData A pointer to the data structure that will receive the texture data. Must have size³ elements. + */ + static void generate3D(int size, int zoomFactor, uint32_t** textureData); + + static float fCubicInterpolate(float y0, float y1, float y2, float y3, float t); + + static uint32_t dwCubicInterpolate(uint32_t y0, uint32_t y1, uint32_t y2, uint32_t y3, float t); +}; + + diff --git a/src/libprojectM/Renderer/PerlinNoise.cpp b/src/libprojectM/Renderer/PerlinNoise.cpp deleted file mode 100644 index fac88da3d..000000000 --- a/src/libprojectM/Renderer/PerlinNoise.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * PerlinNoise.cpp - * - * Created on: Jul 11, 2008 - * Author: pete - */ - -#include "PerlinNoise.hpp" -#include -#include - -PerlinNoise::PerlinNoise() -{ - for (int x = 0; x < 256;x++) { - for (int y = 0; y < 256;y++) { - noise_lq[x][y][0] = noise(x , y); - noise_lq[x][y][1] = noise_lq[x][y][0]; - noise_lq[x][y][2] = noise_lq[x][y][0]; - } - } - - for (int x = 0; x < 32;x++) { - for (int y = 0; y < 32;y++) { - noise_lq_lite[x][y][0] = noise(4*x,16*y); - noise_lq_lite[x][y][1] = noise_lq_lite[x][y][0]; - noise_lq_lite[x][y][2] = noise_lq_lite[x][y][0]; - } - } - - for (int x = 0; x < 256;x++) { - for (int y = 0; y < 256;y++) { - noise_mq[x][y][0] = InterpolatedNoise((float)x/(float)2.0,(float)y/(float)2.0); - noise_mq[x][y][1] = noise_mq[x][y][0]; - noise_mq[x][y][2] = noise_mq[x][y][0]; - } - } - - for (int x = 0; x < 256;x++) { - for (int y = 0; y < 256;y++) { - noise_hq[x][y][0] = InterpolatedNoise((float)x/(float)3.0,(float)y/(float)3.0); - noise_hq[x][y][1] = noise_hq[x][y][0]; - noise_hq[x][y][2] = noise_hq[x][y][0]; - } - } - - for (int x = 0; x < 32;x++) { - for (int y = 0; y < 32;y++) { - for (int z = 0; z < 32;z++) { - noise_lq_vol[x][y][z][0] = noise(x,y,z); - noise_lq_vol[x][y][z][1] = noise_lq_vol[x][y][z][0]; - noise_lq_vol[x][y][z][2] = noise_lq_vol[x][y][z][0]; - } - } - } - - for (int x = 0; x < 32;x++) { - for (int y = 0; y < 32;y++) { - for (int z = 0; z < 32;z++) { - noise_hq_vol[x][y][z][0] = noise(x,y,z); - noise_hq_vol[x][y][z][1] = noise_hq_vol[x][y][z][0]; - noise_hq_vol[x][y][z][2] = noise_hq_vol[x][y][z][0]; - } - } - } -} - -PerlinNoise::~PerlinNoise() -{ - // TODO Auto-generated destructor stub -} diff --git a/src/libprojectM/Renderer/PerlinNoise.hpp b/src/libprojectM/Renderer/PerlinNoise.hpp deleted file mode 100644 index a999f899e..000000000 --- a/src/libprojectM/Renderer/PerlinNoise.hpp +++ /dev/null @@ -1,170 +0,0 @@ -/* - * PerlinNoise.hpp - * - * Created on: Jul 11, 2008 - * Author: pete - */ - -#ifndef PERLINNOISE_HPP_ -#define PERLINNOISE_HPP_ - -#include - -class PerlinNoise -{ -public: - - float noise_lq[256][256][3]; - float noise_lq_lite[32][32][3]; - float noise_mq[256][256][3]; - float noise_hq[256][256][3]; - float noise_lq_vol[32][32][32][3]; - float noise_hq_vol[32][32][32][3]; - - - PerlinNoise(); - virtual ~PerlinNoise(); - -private: - - static inline float noise( int x) - { - x = (x<<13)^x; - return (((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 2147483648.0); - } - - static inline float noise(int x, int y) - { - int n = x + y * 57; - return noise(n); - } - - static inline float noise(int x, int y, int z) - { - int n = x + y * 57 + z * 141; - return noise(n); - } - - static inline float cubic_interp(float v0, float v1, float v2, float v3, float x) - { - float P = (v3 - v2) - (v0 - v1); - float Q = (v0 - v1) - P; - float R = v2 - v0; - - return P*pow(x,3) + Q * pow(x,2) + R*x + v1; - } - - static inline float InterpolatedNoise(float x, float y) - { - int integer_X = int(x); - float fractional_X = x - integer_X; - - int integer_Y = int(y); - float fractional_Y = y - integer_Y; - - float a0 = noise(integer_X - 1, integer_Y - 1); - float a1 = noise(integer_X, integer_Y - 1); - float a2 = noise(integer_X + 1, integer_Y - 1); - float a3 = noise(integer_X + 2, integer_Y - 1); - - float x0 = noise(integer_X - 1, integer_Y); - float x1 = noise(integer_X, integer_Y); - float x2 = noise(integer_X + 1, integer_Y); - float x3 = noise(integer_X + 2, integer_Y); - - float y0 = noise(integer_X + 0, integer_Y + 1); - float y1 = noise(integer_X, integer_Y + 1); - float y2 = noise(integer_X + 1, integer_Y + 1); - float y3 = noise(integer_X + 2, integer_Y + 1); - - float b0 = noise(integer_X - 1, integer_Y + 2); - float b1 = noise(integer_X, integer_Y + 2); - float b2 = noise(integer_X + 1, integer_Y + 2); - float b3 = noise(integer_X + 2, integer_Y + 2); - - float i0 = cubic_interp(a0 , a1, a2, a3, fractional_X); - float i1 = cubic_interp(x0 , x1, x2, x3, fractional_X); - float i2 = cubic_interp(y0 , y1, y2, y3, fractional_X); - float i3 = cubic_interp(b0 , b1, b2, b3, fractional_X); - - return cubic_interp(i0, i1 , i2 , i3, fractional_Y); - - } - - static inline float perlin_octave_3d(float x,float y, float z,int width, int seed, float period) - { - float freq=1/(float)(period); - - int num=(int)(width*freq); - int step_x=(int)(x*freq); - int step_y=(int)(y*freq); - int step_z=(int)(z*freq); - float zone_x=x*freq-step_x; - float zone_y=y*freq-step_y; - float zone_z=z*freq-step_z; - - int boxB=step_x+step_y+step_z*num; - int boxC=step_x+step_y+step_z*(num+1); - int boxD=step_x+step_y+step_z*(num+2); - int boxA=step_x+step_y+step_z*(num-1); - - float u,a,b,v,noisedata,box; - - box = boxA; - noisedata=(box+seed); - u=cubic_interp(noise(noisedata-num-1),noise(noisedata-num),noise(noisedata-num+1),noise(noisedata-num+2),zone_x); - a=cubic_interp(noise(noisedata-1),noise(noisedata),noise(noisedata+1),noise(noisedata+2),zone_x); - b=cubic_interp(noise(noisedata+num -1),noise(noisedata+num),noise(noisedata+1+num),noise(noisedata+2+num),zone_x); - v=cubic_interp(noise(noisedata+2*num -1),noise(noisedata+2*num),noise(noisedata+1+2*num),noise(noisedata+2+2*num),zone_x); - float A=cubic_interp(u,a,b,v,zone_y); - - box = boxB; - noisedata=(box+seed); - u=cubic_interp(noise(noisedata-num-1),noise(noisedata-num),noise(noisedata-num+1),noise(noisedata-num+2),zone_x); - a=cubic_interp(noise(noisedata-1),noise(noisedata),noise(noisedata+1),noise(noisedata+2),zone_x); - b=cubic_interp(noise(noisedata+num -1),noise(noisedata+num),noise(noisedata+1+num),noise(noisedata+2+num),zone_x); - v=cubic_interp(noise(noisedata+2*num -1),noise(noisedata+2*num),noise(noisedata+1+2*num),noise(noisedata+2+2*num),zone_x); - float B=cubic_interp(u,a,b,v,zone_y); - - box = boxC; - noisedata=(box+seed); - u=cubic_interp(noise(noisedata-num-1),noise(noisedata-num),noise(noisedata-num+1),noise(noisedata-num+2),zone_x); - a=cubic_interp(noise(noisedata-1),noise(noisedata),noise(noisedata+1),noise(noisedata+2),zone_x); - b=cubic_interp(noise(noisedata+num -1),noise(noisedata+num),noise(noisedata+1+num),noise(noisedata+2+num),zone_x); - v=cubic_interp(noise(noisedata+2*num -1),noise(noisedata+2*num),noise(noisedata+1+2*num),noise(noisedata+2+2*num),zone_x); - float C=cubic_interp(u,a,b,v,zone_y); - - box = boxD; - noisedata=(box+seed); - u=cubic_interp(noise(noisedata-num-1),noise(noisedata-num),noise(noisedata-num+1),noise(noisedata-num+2),zone_x); - a=cubic_interp(noise(noisedata-1),noise(noisedata),noise(noisedata+1),noise(noisedata+2),zone_x); - b=cubic_interp(noise(noisedata+num -1),noise(noisedata+num),noise(noisedata+1+num),noise(noisedata+2+num),zone_x); - v=cubic_interp(noise(noisedata+2*num -1),noise(noisedata+2*num),noise(noisedata+1+2*num),noise(noisedata+2+2*num),zone_x); - float D=cubic_interp(u,a,b,v,zone_y); - - float value =cubic_interp(A,B,C,D,zone_z); - - return value; - } - - - static inline float perlin_noise_3d(int x, int y, int z, int width, int octaves, int seed, float persistance, float basePeriod) - { - float p = persistance; - float val = 0.0; - - for (int i = 0; i -#include -#include -#endif - -/* The reason for this cousin class is that in Open GLES 2.0 environments, -the glTexImage2d cannot convert from GL_RGB into GL_RGBA -so the TextureManager has to use something that is pre-RGBA -*/ -PerlinNoiseWithAlpha::PerlinNoiseWithAlpha() -{ - for (int x = 0; x < 256;x++) { - for (int y = 0; y < 256;y++) { - noise_lq[x][y][0] = noise(x , y); - noise_lq[x][y][1] = noise_lq[x][y][0]; - noise_lq[x][y][2] = noise_lq[x][y][0]; - noise_lq[x][y][3] = 1.f; - } - } - - for (int x = 0; x < 32;x++) { - for (int y = 0; y < 32;y++) { - noise_lq_lite[x][y][0] = noise(4*x,16*y); - noise_lq_lite[x][y][1] = noise_lq_lite[x][y][0]; - noise_lq_lite[x][y][2] = noise_lq_lite[x][y][0]; - noise_lq_lite[x][y][3] = 1.f; - } - } - - for (int x = 0; x < 256;x++) { - for (int y = 0; y < 256;y++) { - noise_mq[x][y][0] = InterpolatedNoise((float)x/(float)2.0,(float)y/(float)2.0); - noise_mq[x][y][1] = noise_mq[x][y][0]; - noise_mq[x][y][2] = noise_mq[x][y][0]; - noise_mq[x][y][3] = 1.f; - } - } - - for (int x = 0; x < 256;x++) { - for (int y = 0; y < 256;y++) { - noise_hq[x][y][0] = InterpolatedNoise((float)x/(float)3.0,(float)y/(float)3.0); - noise_hq[x][y][1] = noise_hq[x][y][0]; - noise_hq[x][y][2] = noise_hq[x][y][0]; - noise_hq[x][y][3] = 1.f; - } - } - - for (int x = 0; x < 32;x++) { - for (int y = 0; y < 32;y++) { - for (int z = 0; z < 32;z++) { - noise_lq_vol[x][y][z][0] = noise(x,y,z); - noise_lq_vol[x][y][z][1] = noise_lq_vol[x][y][z][0]; - noise_lq_vol[x][y][z][2] = noise_lq_vol[x][y][z][0]; - noise_lq_vol[x][y][z][3] = 1.f; - } - } - } - - for (int x = 0; x < 32;x++) { - for (int y = 0; y < 32;y++) { - for (int z = 0; z < 32;z++) { - noise_hq_vol[x][y][z][0] = noise(x,y,z); - noise_hq_vol[x][y][z][1] = noise_hq_vol[x][y][z][0]; - noise_hq_vol[x][y][z][2] = noise_hq_vol[x][y][z][0]; - noise_hq_vol[x][y][z][3] = 1.f; - } - } - } -} - -PerlinNoiseWithAlpha::~PerlinNoiseWithAlpha() -{ - // TODO Auto-generated destructor stub -} diff --git a/src/libprojectM/Renderer/PerlinNoiseWithAlpha.hpp b/src/libprojectM/Renderer/PerlinNoiseWithAlpha.hpp deleted file mode 100644 index 92f3b123b..000000000 --- a/src/libprojectM/Renderer/PerlinNoiseWithAlpha.hpp +++ /dev/null @@ -1,176 +0,0 @@ -/* - * PerlinNoise.hpp - * - * Created and based on PerlinNoise.hpp - * Created on: Sep 14, 2019 - * Author: hibengler - */ - - -/* The reason for this cousin class is that in Open GLES 2.0 environments, -the glTexImage2d cannot convert from GL_RGB into GL_RGBA -so the TextureManager has to use something that is pre-RGBA -*/ -#ifndef PERLINNOISEWITHALPHA_HPP_ -#define PERLINNOISEWITHALPHA_HPP_ - -#include - -class PerlinNoiseWithAlpha -{ -public: - - float noise_lq[256][256][4]; - float noise_lq_lite[32][32][4]; - float noise_mq[256][256][4]; - float noise_hq[256][256][4]; - float noise_lq_vol[32][32][32][4]; - float noise_hq_vol[32][32][32][4]; - - - PerlinNoiseWithAlpha(); - virtual ~PerlinNoiseWithAlpha(); - -private: - - static inline float noise( int x) - { - x = (x<<13)^x; - return (((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 2147483648.0); - } - - static inline float noise(int x, int y) - { - int n = x + y * 57; - return noise(n); - } - - static inline float noise(int x, int y, int z) - { - int n = x + y * 57 + z * 141; - return noise(n); - } - - static inline float cubic_interp(float v0, float v1, float v2, float v3, float x) - { - float P = (v3 - v2) - (v0 - v1); - float Q = (v0 - v1) - P; - float R = v2 - v0; - - return P*pow(x,3) + Q * pow(x,2) + R*x + v1; - } - - static inline float InterpolatedNoise(float x, float y) - { - int integer_X = int(x); - float fractional_X = x - integer_X; - - int integer_Y = int(y); - float fractional_Y = y - integer_Y; - - float a0 = noise(integer_X - 1, integer_Y - 1); - float a1 = noise(integer_X, integer_Y - 1); - float a2 = noise(integer_X + 1, integer_Y - 1); - float a3 = noise(integer_X + 2, integer_Y - 1); - - float x0 = noise(integer_X - 1, integer_Y); - float x1 = noise(integer_X, integer_Y); - float x2 = noise(integer_X + 1, integer_Y); - float x3 = noise(integer_X + 2, integer_Y); - - float y0 = noise(integer_X + 0, integer_Y + 1); - float y1 = noise(integer_X, integer_Y + 1); - float y2 = noise(integer_X + 1, integer_Y + 1); - float y3 = noise(integer_X + 2, integer_Y + 1); - - float b0 = noise(integer_X - 1, integer_Y + 2); - float b1 = noise(integer_X, integer_Y + 2); - float b2 = noise(integer_X + 1, integer_Y + 2); - float b3 = noise(integer_X + 2, integer_Y + 2); - - float i0 = cubic_interp(a0 , a1, a2, a3, fractional_X); - float i1 = cubic_interp(x0 , x1, x2, x3, fractional_X); - float i2 = cubic_interp(y0 , y1, y2, y3, fractional_X); - float i3 = cubic_interp(b0 , b1, b2, b3, fractional_X); - - return cubic_interp(i0, i1 , i2 , i3, fractional_Y); - - } - - static inline float perlin_octave_3d(float x,float y, float z,int width, int seed, float period) - { - float freq=1/(float)(period); - - int num=(int)(width*freq); - int step_x=(int)(x*freq); - int step_y=(int)(y*freq); - int step_z=(int)(z*freq); - float zone_x=x*freq-step_x; - float zone_y=y*freq-step_y; - float zone_z=z*freq-step_z; - - int boxB=step_x+step_y+step_z*num; - int boxC=step_x+step_y+step_z*(num+1); - int boxD=step_x+step_y+step_z*(num+2); - int boxA=step_x+step_y+step_z*(num-1); - - float u,a,b,v,noisedata,box; - - box = boxA; - noisedata=(box+seed); - u=cubic_interp(noise(noisedata-num-1),noise(noisedata-num),noise(noisedata-num+1),noise(noisedata-num+2),zone_x); - a=cubic_interp(noise(noisedata-1),noise(noisedata),noise(noisedata+1),noise(noisedata+2),zone_x); - b=cubic_interp(noise(noisedata+num -1),noise(noisedata+num),noise(noisedata+1+num),noise(noisedata+2+num),zone_x); - v=cubic_interp(noise(noisedata+2*num -1),noise(noisedata+2*num),noise(noisedata+1+2*num),noise(noisedata+2+2*num),zone_x); - float A=cubic_interp(u,a,b,v,zone_y); - - box = boxB; - noisedata=(box+seed); - u=cubic_interp(noise(noisedata-num-1),noise(noisedata-num),noise(noisedata-num+1),noise(noisedata-num+2),zone_x); - a=cubic_interp(noise(noisedata-1),noise(noisedata),noise(noisedata+1),noise(noisedata+2),zone_x); - b=cubic_interp(noise(noisedata+num -1),noise(noisedata+num),noise(noisedata+1+num),noise(noisedata+2+num),zone_x); - v=cubic_interp(noise(noisedata+2*num -1),noise(noisedata+2*num),noise(noisedata+1+2*num),noise(noisedata+2+2*num),zone_x); - float B=cubic_interp(u,a,b,v,zone_y); - - box = boxC; - noisedata=(box+seed); - u=cubic_interp(noise(noisedata-num-1),noise(noisedata-num),noise(noisedata-num+1),noise(noisedata-num+2),zone_x); - a=cubic_interp(noise(noisedata-1),noise(noisedata),noise(noisedata+1),noise(noisedata+2),zone_x); - b=cubic_interp(noise(noisedata+num -1),noise(noisedata+num),noise(noisedata+1+num),noise(noisedata+2+num),zone_x); - v=cubic_interp(noise(noisedata+2*num -1),noise(noisedata+2*num),noise(noisedata+1+2*num),noise(noisedata+2+2*num),zone_x); - float C=cubic_interp(u,a,b,v,zone_y); - - box = boxD; - noisedata=(box+seed); - u=cubic_interp(noise(noisedata-num-1),noise(noisedata-num),noise(noisedata-num+1),noise(noisedata-num+2),zone_x); - a=cubic_interp(noise(noisedata-1),noise(noisedata),noise(noisedata+1),noise(noisedata+2),zone_x); - b=cubic_interp(noise(noisedata+num -1),noise(noisedata+num),noise(noisedata+1+num),noise(noisedata+2+num),zone_x); - v=cubic_interp(noise(noisedata+2*num -1),noise(noisedata+2*num),noise(noisedata+1+2*num),noise(noisedata+2+2*num),zone_x); - float D=cubic_interp(u,a,b,v,zone_y); - - float value =cubic_interp(A,B,C,D,zone_z); - - return value; - } - - - static inline float perlin_noise_3d(int x, int y, int z, int width, int octaves, int seed, float persistance, float basePeriod) - { - float p = persistance; - float val = 0.0; - - for (int i = 0; i noise(new PerlinNoiseWithAlpha()); -#else - std::unique_ptr noise(new PerlinNoise()); -#endif + auto noise = std::make_unique(); GLuint noise_texture_lq_lite; glGenTextures(1, &noise_texture_lq_lite); glBindTexture(GL_TEXTURE_2D, noise_texture_lq_lite); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, NOISE_INTERNAL_DATA_FORMAT, GL_FLOAT, noise->noise_lq_lite); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, NOISE_INTERNAL_DATA_FORMAT, GL_UNSIGNED_INT_8_8_8_8, noise->noise_lq_lite); Texture * textureNoise_lq_lite = new Texture("noise_lq_lite", noise_texture_lq_lite, GL_TEXTURE_2D, 32, 32, false); textureNoise_lq_lite->getSampler(GL_REPEAT, GL_LINEAR); textures["noise_lq_lite"] = textureNoise_lq_lite; @@ -111,7 +94,7 @@ TextureManager::TextureManager(const std::string _presetsURL, const int texsizeX GLuint noise_texture_lq; glGenTextures(1, &noise_texture_lq); glBindTexture(GL_TEXTURE_2D, noise_texture_lq); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, NOISE_INTERNAL_DATA_FORMAT, GL_FLOAT, noise->noise_lq); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, NOISE_INTERNAL_DATA_FORMAT, GL_UNSIGNED_INT_8_8_8_8, noise->noise_lq); Texture * textureNoise_lq = new Texture("noise_lq", noise_texture_lq, GL_TEXTURE_2D, 256, 256, false); textureNoise_lq->getSampler(GL_REPEAT, GL_LINEAR); textures["noise_lq"] = textureNoise_lq; @@ -119,7 +102,7 @@ TextureManager::TextureManager(const std::string _presetsURL, const int texsizeX GLuint noise_texture_mq; glGenTextures(1, &noise_texture_mq); glBindTexture(GL_TEXTURE_2D, noise_texture_mq); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, NOISE_INTERNAL_DATA_FORMAT, GL_FLOAT, noise->noise_mq); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, NOISE_INTERNAL_DATA_FORMAT, GL_UNSIGNED_INT_8_8_8_8, noise->noise_mq); Texture * textureNoise_mq = new Texture("noise_mq", noise_texture_mq, GL_TEXTURE_2D, 256, 256, false); textureNoise_mq->getSampler(GL_REPEAT, GL_LINEAR); textures["noise_mq"] = textureNoise_mq; @@ -127,7 +110,7 @@ TextureManager::TextureManager(const std::string _presetsURL, const int texsizeX GLuint noise_texture_hq; glGenTextures(1, &noise_texture_hq); glBindTexture(GL_TEXTURE_2D, noise_texture_hq); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, NOISE_INTERNAL_DATA_FORMAT, GL_FLOAT, noise->noise_hq); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, NOISE_INTERNAL_DATA_FORMAT, GL_UNSIGNED_INT_8_8_8_8, noise->noise_hq); Texture * textureNoise_hq = new Texture("noise_hq", noise_texture_hq, GL_TEXTURE_2D, 256, 256, false); textureNoise_hq->getSampler(GL_REPEAT, GL_LINEAR); textures["noise_hq"] = textureNoise_hq; @@ -135,7 +118,7 @@ TextureManager::TextureManager(const std::string _presetsURL, const int texsizeX GLuint noise_texture_lq_vol; glGenTextures( 1, &noise_texture_lq_vol ); glBindTexture( GL_TEXTURE_3D, noise_texture_lq_vol ); - glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 32 ,32 ,32 ,0 ,NOISE_INTERNAL_DATA_FORMAT, GL_FLOAT ,noise->noise_lq_vol); + glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 32 ,32 ,32 ,0 ,NOISE_INTERNAL_DATA_FORMAT, GL_UNSIGNED_INT_8_8_8_8 ,noise->noise_lq_vol); Texture * textureNoise_lq_vol = new Texture("noisevol_lq", noise_texture_lq_vol, GL_TEXTURE_3D, 32, 32, false); textureNoise_lq_vol->getSampler(GL_REPEAT, GL_LINEAR); textures["noisevol_lq"] = textureNoise_lq_vol; @@ -143,7 +126,7 @@ TextureManager::TextureManager(const std::string _presetsURL, const int texsizeX GLuint noise_texture_hq_vol; glGenTextures( 1, &noise_texture_hq_vol ); glBindTexture( GL_TEXTURE_3D, noise_texture_hq_vol ); - glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 32, 32, 32, 0, NOISE_INTERNAL_DATA_FORMAT, GL_FLOAT, noise->noise_hq_vol); + glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 32, 32, 32, 0, NOISE_INTERNAL_DATA_FORMAT, GL_UNSIGNED_INT_8_8_8_8, noise->noise_hq_vol); Texture * textureNoise_hq_vol = new Texture("noisevol_hq", noise_texture_hq_vol, GL_TEXTURE_3D, 32, 32, false); textureNoise_hq_vol->getSampler(GL_REPEAT, GL_LINEAR);