STKMesh: Support caustic material.

This commit is contained in:
Vincent Lejeune 2014-03-04 18:42:33 +01:00
parent ada19f828c
commit c93c921028
9 changed files with 93 additions and 53 deletions

View File

@ -3,17 +3,18 @@ uniform sampler2D caustictex;
uniform vec2 dir;
uniform vec2 dir2;
in vec2 uv;
out vec4 FragColor;
void main()
{
vec2 tc = gl_TexCoord[0].xy;
vec2 tc = uv;
vec3 col = texture(tex, tc).xyz;
float caustic = texture(caustictex, tc + dir).x;
float caustic2 = texture(caustictex, (tc.yx + dir2 * vec2(-0.6, 0.3)) * vec2(0.6)).x;
col += caustic * caustic2 * 10.0;
col += caustic * caustic2;
FragColor = vec4(col, 1.0);
}

View File

@ -541,43 +541,6 @@ void ShadowGenProvider::OnSetConstants(IMaterialRendererServices *srv, int)
//-------------------------------------
void CausticsProvider::OnSetConstants(IMaterialRendererServices *srv, int)
{
const float time = irr_driver->getDevice()->getTimer()->getTime() / 1000.0f;
const float speed = World::getWorld()->getTrack()->getCausticsSpeed();
float strength = time;
strength = fabsf(noise2d(strength / 10.0f)) * 0.006f + 0.001f;
vector3df wind = irr_driver->getWind() * strength * speed;
m_dir[0] += wind.X;
m_dir[1] += wind.Z;
strength = time * 0.56f + sinf(time);
strength = fabsf(noise2d(0.0, strength / 6.0f)) * 0.0095f + 0.001f;
wind = irr_driver->getWind() * strength * speed;
wind.rotateXZBy(cosf(time));
m_dir2[0] += wind.X;
m_dir2[1] += wind.Z;
srv->setVertexShaderConstant("dir", m_dir, 2);
srv->setVertexShaderConstant("dir2", m_dir2, 2);
if (!firstdone)
{
int tex = 0;
srv->setVertexShaderConstant("tex", &tex, 1);
tex = 1;
srv->setVertexShaderConstant("caustictex", &tex, 1);
firstdone = true;
}
}
//-------------------------------------
void DisplaceProvider::OnSetConstants(IMaterialRendererServices *srv, int)
{
core::matrix4 ProjectionMatrix = srv->getVideoDriver()->getTransform(ETS_PROJECTION);

View File

@ -542,19 +542,6 @@ public:
//
class CausticsProvider: public CallBase
{
public:
CausticsProvider() { m_dir[0] = m_dir[1] = m_dir2[0] = m_dir2[1] = 0; }
virtual void OnSetConstants(video::IMaterialRendererServices *srv, int);
private:
float m_dir[2], m_dir2[2];
};
//
class DisplaceProvider: public CallBase
{
public:

View File

@ -183,7 +183,7 @@ void Shaders::loadShaders()
m_callbacks[ES_SHADOWGEN], EMT_SOLID);
m_shaders[ES_CAUSTICS] = glslmat(dir + "pass.vert", dir + "pass.frag",
m_callbacks[ES_CAUSTICS], EMT_TRANSPARENT_ALPHA_CHANNEL);
m_callbacks[ES_CAUSTICS], EMT_SOLID);
m_shaders[ES_DISPLACE] = glsl(dir + "pass.vert", dir + "pass.frag",
m_callbacks[ES_DISPLACE]);
@ -249,6 +249,7 @@ void Shaders::loadShaders()
MeshShader::SplattingShader::init();
MeshShader::GrassPass1Shader::init();
MeshShader::GrassPass2Shader::init();
MeshShader::CausticsShader::init();
MeshShader::BubbleShader::init();
MeshShader::TransparentShader::init();
MeshShader::TransparentFogShader::init();
@ -746,6 +747,36 @@ namespace MeshShader
glUniform3f(uniform_ambient, s.r, s.g, s.b);
}
GLuint CausticsShader::Program;
GLuint CausticsShader::attrib_position;
GLuint CausticsShader::attrib_texcoord;
GLuint CausticsShader::uniform_MVP;
GLuint CausticsShader::uniform_dir;
GLuint CausticsShader::uniform_dir2;
GLuint CausticsShader::uniform_tex;
GLuint CausticsShader::uniform_caustictex;
void CausticsShader::init()
{
Program = LoadProgram(file_manager->getAsset("shaders/object_pass2.vert").c_str(), file_manager->getAsset("shaders/caustics.frag").c_str());
attrib_position = glGetAttribLocation(Program, "Position");
attrib_texcoord = glGetAttribLocation(Program, "Texcoord");
uniform_MVP = glGetUniformLocation(Program, "ModelViewProjectionMatrix");
uniform_dir = glGetUniformLocation(Program, "dir");
uniform_dir2 = glGetUniformLocation(Program, "dir2");
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_caustictex = glGetUniformLocation(Program, "caustictex");
}
void CausticsShader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::vector2df &dir, const core::vector2df &dir2, unsigned TU_tex, unsigned TU_caustictex)
{
glUniformMatrix4fv(uniform_MVP, 1, GL_FALSE, ModelViewProjectionMatrix.pointer());
glUniform2f(uniform_dir, dir.X, dir.Y);
glUniform2f(uniform_dir2, dir2.X, dir2.Y);
glUniform1i(uniform_tex, TU_tex);
glUniform1i(uniform_caustictex, TU_caustictex);
}
GLuint BubbleShader::Program;
GLuint BubbleShader::attrib_position;
GLuint BubbleShader::attrib_texcoord;

View File

@ -169,6 +169,17 @@ public:
static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, unsigned TU_tex_layout, unsigned TU_tex_detail0, unsigned TU_tex_detail1, unsigned TU_tex_detail2, unsigned TU_tex_detail3, unsigned TU_DiffuseMap, unsigned TU_SpecularMap, unsigned TU_SSAO);
};
class CausticsShader
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_texcoord;
static GLuint uniform_MVP, uniform_dir, uniform_dir2, uniform_tex, uniform_caustictex;
static void init();
static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::vector2df &dir, const core::vector2df &dir2, unsigned TU_tex, unsigned TU_caustictex);
};
class BubbleShader
{
public:

View File

@ -355,6 +355,23 @@ void drawObjectRefPass2(const GLMesh &mesh, const core::matrix4 &ModelViewProjec
glDrawElements(ptype, count, itype, 0);
}
void drawCaustics(const GLMesh &mesh, const core::matrix4 & ModelViewProjectionMatrix, core::vector2df dir, core::vector2df dir2)
{
GLenum ptype = mesh.PrimitiveType;
GLenum itype = mesh.IndexType;
size_t count = mesh.IndexCount;
setTexture(0, mesh.textures[0], GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true);
setTexture(1, mesh.textures[0], GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true);
glUseProgram(MeshShader::CausticsShader::Program);
MeshShader::CausticsShader::setUniforms(ModelViewProjectionMatrix, dir, dir2, 0, 1);
glBindVertexArray(mesh.vao_second_pass);
glDrawElements(ptype, count, itype, 0);
}
void drawGrassPass2(const GLMesh &mesh, const core::matrix4 & ModelViewProjectionMatrix, core::vector3df windDir)
{
GLenum ptype = mesh.PrimitiveType;
@ -625,6 +642,8 @@ bool isObject(video::E_MATERIAL_TYPE type)
return true;
if (type == irr_driver->getShader(ES_OBJECT_UNLIT))
return true;
if (type == irr_driver->getShader(ES_CAUSTICS))
return true;
if (type == video::EMT_TRANSPARENT_ALPHA_CHANNEL)
return true;
if (type == video::EMT_ONETEXTURE_BLEND)
@ -695,6 +714,11 @@ void initvaostate(GLMesh &mesh, video::E_MATERIAL_TYPE type)
mesh.vao_second_pass = createVAO(mesh.vertex_buffer, mesh.index_buffer,
MeshShader::ObjectUnlitShader::attrib_position, MeshShader::ObjectUnlitShader::attrib_texcoord, -1, -1, -1, -1, -1, mesh.Stride);
}
else if (type == irr_driver->getShader(ES_CAUSTICS))
{
mesh.vao_second_pass = createVAO(mesh.vertex_buffer, mesh.index_buffer,
MeshShader::CausticsShader::attrib_position, MeshShader::CausticsShader::attrib_texcoord, -1, -1, -1, -1, -1, mesh.Stride);
}
else if (mesh.textures[1])
{
mesh.vao_second_pass = createVAO(mesh.vertex_buffer, mesh.index_buffer,

View File

@ -44,6 +44,7 @@ void drawUntexturedObject(const GLMesh &mesh, const core::matrix4 &ModelViewProj
void drawObjectRefPass2(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &TextureMatrix);
void drawSphereMap(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &TransposeInverseModelView);
void drawSplatting(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix);
void drawCaustics(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix, core::vector2df dir, core::vector2df dir2);
void drawGrassPass2(const GLMesh &mesh, const core::matrix4 & ModelViewProjectionMatrix, core::vector3df windDir);
void drawObjectRimLimit(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &TransposeInverseModelView, const core::matrix4 &TextureMatrix);
void drawObjectUnlit(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix);

View File

@ -210,6 +210,27 @@ void STKMeshSceneNode::drawSolid(const GLMesh &mesh, video::E_MATERIAL_TYPE type
drawObjectRimLimit(mesh, ModelViewProjectionMatrix, TransposeInverseModelView, TextureMatrix);
else if (type == irr_driver->getShader(ES_OBJECT_UNLIT))
drawObjectUnlit(mesh, ModelViewProjectionMatrix);
else if (type == irr_driver->getShader(ES_CAUSTICS))
{
const float time = irr_driver->getDevice()->getTimer()->getTime() / 1000.0f;
const float speed = World::getWorld()->getTrack()->getCausticsSpeed();
float strength = time;
strength = fabsf(noise2d(strength / 10.0f)) * 0.006f + 0.001f;
vector3df wind = irr_driver->getWind() * strength * speed;
caustic_dir.X += wind.X;
caustic_dir.Y += wind.Z;
strength = time * 0.56f + sinf(time);
strength = fabsf(noise2d(0.0, strength / 6.0f)) * 0.0095f + 0.001f;
wind = irr_driver->getWind() * strength * speed;
wind.rotateXZBy(cosf(time));
caustic_dir2.X += wind.X;
caustic_dir2.Y += wind.Z;
drawCaustics(mesh, ModelViewProjectionMatrix, caustic_dir, caustic_dir2);
}
else if (mesh.textures[1] && type != irr_driver->getShader(ES_NORMAL_MAP))
drawDetailledObjectPass2(mesh, ModelViewProjectionMatrix);
else if (!mesh.textures[0])

View File

@ -9,6 +9,7 @@ protected:
std::vector<GLMesh> GLmeshes;
core::matrix4 ModelViewProjectionMatrix, TransposeInverseModelView, TextureMatrix;
core::vector3df windDir;
core::vector2df caustic_dir, caustic_dir2;
void drawSolid(const GLMesh &mesh, video::E_MATERIAL_TYPE type);
void drawTransparent(const GLMesh &mesh, video::E_MATERIAL_TYPE type);