Added refraction to the skybox shader

This commit is contained in:
Hugo Willaume 2019-04-08 13:49:26 +09:00
parent 91ccda04e4
commit 0ca64e14cc
6 changed files with 15 additions and 16 deletions

View File

@ -110,9 +110,9 @@ void MyGlWindow::shaderSetup()
shaders["Toon"]->uniformFlags &= ~ShaderFlags::SHINE_FLAG;
shaders["Skybox"] = new Shader("skybox.vert", "skybox.frag");
shaders["Skybox"]->uniformFlags = MVP_FLAG | KA_FLAG | MODEL_MATRIX_FLAG | SKYBOX_TEX_FLAG;
shaders["Skybox"]->mat.enabled = true;
shaders["Skybox"]->mat.ka = glm::vec3(0.4, 0.4, 0.4);
shaders["Skybox"]->uniformFlags = MVP_FLAG | MODEL_MATRIX_FLAG | SKYBOX_TEX_FLAG;
shaders["Skybox"]->addUniform("ReflectFactor", (float)0.1);
shaders["Skybox"]->addUniform("RefractionIndex", (float)0.95);
}

View File

@ -23,7 +23,7 @@ enum ShaderFlags
MODELVIEW_FLAG = 64,
LIGHTS_FLAG = 128,
MODEL_MATRIX_FLAG = 256,
SKYBOX_TEX_FLAG = 512
SKYBOX_TEX_FLAG = 512,
};
class Shader

View File

@ -98,8 +98,7 @@ void Skybox::initialize(std::string skybox_dir, Shader *sky)
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
sky->enable();
sky->addUniform("DrawSkyBox", (int)GL_FALSE);
sky->addUniform("CubeMapTex", (int)0);
sky->addUniform("ReflectFactor", (float)0.85);
sky->addUniform("CubeMapTex", (int)0);
sky->disable();
}

View File

@ -46,5 +46,5 @@ Collapsed=0
[Window][First Window]
Pos=20,20
Size=357,188
Collapsed=0
Collapsed=1

View File

@ -3,21 +3,18 @@
out vec4 FragColors;
in vec3 reflectDir;
in vec3 refractDir;
uniform vec3 Ka;
uniform float ReflectFactor;
uniform bool DrawSkyBox;
uniform samplerCube CubeMapTex;
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
void main()
{
vec4 texColor = texture(CubeMapTex, reflectDir);
vec4 reflectColor = texture(CubeMapTex, reflectDir);
vec4 refractColor = texture(CubeMapTex, refractDir);
if (DrawSkyBox == true)
FragColors = texColor;
FragColors = reflectColor;
else
FragColors = vec4(mix(Ka, texColor.xyz, ReflectFactor), 1);
FragColors = vec4(mix(refractColor, reflectColor, ReflectFactor));
}

View File

@ -6,7 +6,9 @@ layout(location=2) in vec3 v_color;
layout(location=3) in vec2 v_texmap;
out vec3 reflectDir;
out vec3 refractDir;
uniform float RefractionIndex;
uniform mat4 ModelMatrix;
uniform mat4 mvp;
uniform bool DrawSkyBox;
@ -17,7 +19,7 @@ void main(void)
{
//For drawing a mesh with reflected skybox
if (DrawSkyBox == true)
if (DrawSkyBox == true)
reflectDir = coord3d;
else
{
@ -25,6 +27,7 @@ void main(void)
vec3 N = normalize(ModelMatrix * vec4(v_normal, 1)).xyz;
vec3 V = normalize(WorldCamPos - coord3d);
reflectDir = reflect(-V, N);
refractDir = refract(-V, N, RefractionIndex);
}
gl_Position = mvp * vec4(coord3d, 1.0f);