From c52eeac71dbdd22f547672e8f6026de82ec770c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 5 Jan 2014 20:37:51 +0100 Subject: [PATCH] [WIP] Added the simplex sampler to opengl water shader --- src/basics/NoiseFunctionSimplex.cpp | 25 +++++++++++++++++++++ src/basics/NoiseFunctionSimplex.h | 2 +- src/render/opengl/ExplorerChunkTerrain.cpp | 4 ++-- src/render/opengl/OpenGLSharedState.h | 6 ++--- src/render/opengl/OpenGLVariable.cpp | 26 ++++++++++++++-------- src/render/opengl/OpenGLVariable.h | 8 ++++--- src/render/opengl/OpenGLWater.cpp | 3 +++ src/render/opengl/shaders/water.frag | 4 +++- 8 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/basics/NoiseFunctionSimplex.cpp b/src/basics/NoiseFunctionSimplex.cpp index 9390297..eac4da4 100644 --- a/src/basics/NoiseFunctionSimplex.cpp +++ b/src/basics/NoiseFunctionSimplex.cpp @@ -9,6 +9,8 @@ #include #include #include +#include "Texture2D.h" +#include "Color.h" typedef struct { @@ -475,3 +477,26 @@ double noiseSimplexGet4DValue(double x, double y, double z, double w) /* Sum up and scale the result to cover the range [-0.5,0.5] */ return 13.5 * (n0 + n1 + n2 + n3 + n4) + 0.5; } + + +static Texture2D *_sampleTexture = NULL; + +const Texture2D *NoiseFunctionSimplex::getSampleTexture() +{ + const int width = 1024; + const int height = 1024; + if (!_sampleTexture) + { + _sampleTexture = new Texture2D(width, height); + for (int x = 0; x < width; x++) + { + for (int z = 0; z < height; z++) + { + double val = noiseSimplexGet2DValue((double)x, (double)z); + _sampleTexture->setPixel(x, z, Color(val, val, val)); + } + } + } + + return _sampleTexture; +} diff --git a/src/basics/NoiseFunctionSimplex.h b/src/basics/NoiseFunctionSimplex.h index 9b71afd..076b547 100644 --- a/src/basics/NoiseFunctionSimplex.h +++ b/src/basics/NoiseFunctionSimplex.h @@ -9,7 +9,7 @@ namespace basics { class NoiseFunctionSimplex { public: - NoiseFunctionSimplex(); + static const Texture2D *getSampleTexture(); }; } diff --git a/src/render/opengl/ExplorerChunkTerrain.cpp b/src/render/opengl/ExplorerChunkTerrain.cpp index 4586b60..3310814 100644 --- a/src/render/opengl/ExplorerChunkTerrain.cpp +++ b/src/render/opengl/ExplorerChunkTerrain.cpp @@ -272,9 +272,9 @@ void ExplorerChunkTerrain::render(QOpenGLShaderProgram* program, OpenGLFunctions _lock_data.lock(); // TEMP - functions->glActiveTexture(GL_TEXTURE0 + 3); + functions->glActiveTexture(GL_TEXTURE0 + 7); functions->glBindTexture(GL_TEXTURE_2D, texture_id); - program->setUniformValue("groundTexture", 3); + program->setUniformValue("groundTexture", 7); tessellated->render(program, functions); _lock_data.unlock(); } diff --git a/src/render/opengl/OpenGLSharedState.h b/src/render/opengl/OpenGLSharedState.h index afe8817..4cdd8e6 100644 --- a/src/render/opengl/OpenGLSharedState.h +++ b/src/render/opengl/OpenGLSharedState.h @@ -28,9 +28,9 @@ public: OpenGLVariable *get(const std::string &name); // Shortcuts - inline void set(const std::string &name, const Texture2D *texture) {get(name)->set(texture);} - inline void set(const std::string &name, const Texture3D *texture) {get(name)->set(texture);} - inline void set(const std::string &name, const Texture4D *texture) {get(name)->set(texture);} + inline void set(const std::string &name, const Texture2D *texture, bool repeat=false, bool color=true) {get(name)->set(texture, repeat, color);} + inline void set(const std::string &name, const Texture3D *texture, bool repeat=false, bool color=true) {get(name)->set(texture, repeat, color);} + inline void set(const std::string &name, const Texture4D *texture, bool repeat=false, bool color=true) {get(name)->set(texture, repeat, color);} inline void set(const std::string &name, float value) {get(name)->set(value);} inline void set(const std::string &name, const Vector3 &vector) {get(name)->set(vector);} inline void set(const std::string &name, const QVector3D &vector) {get(name)->set(vector);} diff --git a/src/render/opengl/OpenGLVariable.cpp b/src/render/opengl/OpenGLVariable.cpp index be33aff..1b7e422 100644 --- a/src/render/opengl/OpenGLVariable.cpp +++ b/src/render/opengl/OpenGLVariable.cpp @@ -62,31 +62,37 @@ void OpenGLVariable::apply(OpenGLShaderProgram *program, int &texture_unit) } } -void OpenGLVariable::set(const Texture2D *texture) +void OpenGLVariable::set(const Texture2D *texture, bool repeat, bool color) { assert(type == TYPE_NONE or type == TYPE_TEXTURE_2D); type = TYPE_TEXTURE_2D; value_tex2d = texture; texture_toupload = true; + texture_repeat = repeat; + texture_color = color; } -void OpenGLVariable::set(const Texture3D *texture) +void OpenGLVariable::set(const Texture3D *texture, bool repeat, bool color) { assert(type == TYPE_NONE or type == TYPE_TEXTURE_3D); type = TYPE_TEXTURE_3D; value_tex3d = texture; texture_toupload = true; + texture_repeat = repeat; + texture_color = color; } -void OpenGLVariable::set(const Texture4D *texture) +void OpenGLVariable::set(const Texture4D *texture, bool repeat, bool color) { assert(type == TYPE_NONE or type == TYPE_TEXTURE_4D); type = TYPE_TEXTURE_4D; value_tex4d = texture; texture_toupload = true; + texture_repeat = repeat; + texture_color = color; } void OpenGLVariable::set(float value) @@ -149,13 +155,15 @@ void OpenGLVariable::uploadTexture(OpenGLRenderer* renderer) functions->glBindTexture(textype, texture_id); functions->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER, GL_LINEAR); functions->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - functions->glTexParameteri(textype, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - functions->glTexParameteri(textype, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + functions->glTexParameteri(textype, GL_TEXTURE_WRAP_S, texture_repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE); + functions->glTexParameteri(textype, GL_TEXTURE_WRAP_T, texture_repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE); if (textype == GL_TEXTURE_3D) { - functions->glTexParameteri(textype, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + functions->glTexParameteri(textype, GL_TEXTURE_WRAP_R, texture_repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE); } + int dest_format = texture_color ? GL_RGBA : GL_RED; + if (type == TYPE_TEXTURE_2D) { int sx, sy; @@ -174,7 +182,7 @@ void OpenGLVariable::uploadTexture(OpenGLRenderer* renderer) } } - functions->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sx, sy, 0, GL_RGBA, GL_FLOAT, pixels); + functions->glTexImage2D(GL_TEXTURE_2D, 0, dest_format, sx, sy, 0, GL_RGBA, GL_FLOAT, pixels); delete[] pixels; } else if (type == TYPE_TEXTURE_3D) @@ -198,7 +206,7 @@ void OpenGLVariable::uploadTexture(OpenGLRenderer* renderer) } } - functions->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz, 0, GL_RGBA, GL_FLOAT, pixels); + functions->glTexImage3D(GL_TEXTURE_3D, 0, dest_format, sx, sy, sz, 0, GL_RGBA, GL_FLOAT, pixels); delete[] pixels; } else @@ -225,7 +233,7 @@ void OpenGLVariable::uploadTexture(OpenGLRenderer* renderer) } } - functions->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz * sw, 0, GL_RGBA, GL_FLOAT, pixels); + functions->glTexImage3D(GL_TEXTURE_3D, 0, dest_format, sx, sy, sz * sw, 0, GL_RGBA, GL_FLOAT, pixels); delete[] pixels; } } diff --git a/src/render/opengl/OpenGLVariable.h b/src/render/opengl/OpenGLVariable.h index 07a7f29..418c4f0 100644 --- a/src/render/opengl/OpenGLVariable.h +++ b/src/render/opengl/OpenGLVariable.h @@ -32,9 +32,9 @@ public: void apply(OpenGLShaderProgram *program, int &texture_unit); - void set(const Texture2D *texture); - void set(const Texture3D *texture); - void set(const Texture4D *texture); + void set(const Texture2D *texture, bool repeat=false, bool color=true); + void set(const Texture3D *texture, bool repeat=false, bool color=true); + void set(const Texture4D *texture, bool repeat=false, bool color=true); void set(float value); void set(const Vector3 &vector); void set(const QVector3D &vector); @@ -58,6 +58,8 @@ private: const Texture4D* value_tex4d; bool texture_toupload; + bool texture_repeat; + bool texture_color; unsigned int texture_id; }; diff --git a/src/render/opengl/OpenGLWater.cpp b/src/render/opengl/OpenGLWater.cpp index 7de134b..9a316fe 100644 --- a/src/render/opengl/OpenGLWater.cpp +++ b/src/render/opengl/OpenGLWater.cpp @@ -7,6 +7,7 @@ #include "Scenery.h" #include "WaterDefinition.h" #include "SurfaceMaterial.h" +#include "NoiseFunctionSimplex.h" OpenGLWater::OpenGLWater(OpenGLRenderer *renderer): OpenGLPart(renderer) @@ -41,6 +42,8 @@ void OpenGLWater::update() Color water_color = renderer->getScenery()->getWater()->material->_rgb; renderer->getSharedState()->set("waterColor", water_color); + + renderer->getSharedState()->set("simplexSampler", NoiseFunctionSimplex::getSampleTexture(), true, false); } void OpenGLWater::render() diff --git a/src/render/opengl/shaders/water.frag b/src/render/opengl/shaders/water.frag index 06c68b6..591eadd 100644 --- a/src/render/opengl/shaders/water.frag +++ b/src/render/opengl/shaders/water.frag @@ -1,8 +1,10 @@ uniform vec4 waterColor; +uniform sampler2D simplexSampler; void main(void) { - gl_FragColor = waterColor; + //gl_FragColor = waterColor; + gl_FragColor = texture2D(simplexSampler, unprojected.xz * 0.01); gl_FragColor = applyAerialPerspective(gl_FragColor);