[WIP] Added the simplex sampler to opengl water shader

This commit is contained in:
Michaël Lemaire 2014-01-05 20:37:51 +01:00
parent 98646efb99
commit c52eeac71d
8 changed files with 59 additions and 19 deletions

View file

@ -9,6 +9,8 @@
#include <cstdlib>
#include <cmath>
#include <cstring>
#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;
}

View file

@ -9,7 +9,7 @@ namespace basics {
class NoiseFunctionSimplex
{
public:
NoiseFunctionSimplex();
static const Texture2D *getSampleTexture();
};
}

View file

@ -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();
}

View file

@ -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);}

View file

@ -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;
}
}

View file

@ -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;
};

View file

@ -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()

View file

@ -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);