Factorise sun color/position

This commit is contained in:
Vincent Lejeune 2014-12-15 23:09:19 +01:00
parent 2995051f7c
commit 7aa7e2d148
6 changed files with 37 additions and 31 deletions

View File

@ -6,9 +6,13 @@ uniform mat4 InverseProjectionMatrix;
uniform mat4 ProjectionViewMatrix;
uniform vec2 screen;
uniform vec3 sun_direction;
uniform vec3 sun_col;
uniform float sun_angle;
uniform float blueLmn[9];
uniform float greenLmn[9];
uniform float redLmn[9];
#else
layout (std140) uniform MatrixesData
{
@ -24,6 +28,9 @@ layout (std140) uniform MatrixesData
// Expand because of catalyst (14.12) not correctly associating array in UBO
layout (std140) uniform LightingData
{
vec3 sun_direction;
vec3 sun_col;
float sun_angle;
float bL00;
float bL1m1;
float bL10;

View File

@ -1,10 +1,6 @@
uniform sampler2D ntex;
uniform sampler2D dtex;
uniform vec3 direction;
uniform vec3 col;
uniform float sunangle = .54;
out vec4 Diff;
out vec4 Spec;
@ -32,19 +28,18 @@ void main() {
float roughness = texture(ntex, uv).z;
vec3 eyedir = -normalize(xpos.xyz);
// Normalized on the cpu
vec3 L = direction;
vec3 L = normalize((transpose(InverseViewMatrix) * vec4(sun_direction, 0.)).xyz);
float NdotL = clamp(dot(norm, L), 0., 1.);
float angle = 3.14 * sunangle / 180.;
float angle = 3.14 * sun_angle / 180.;
vec3 R = reflect(-eyedir, norm);
vec3 Lightdir = getMostRepresentativePoint(direction, R, angle);
vec3 Lightdir = getMostRepresentativePoint(L, R, angle);
vec3 Specular = SpecularBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
vec3 Diffuse = DiffuseBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
Diff = vec4(NdotL * Diffuse * col, 1.);
Spec = vec4(NdotL * Specular * col, 1.);
Diff = vec4(NdotL * Diffuse * sun_col, 1.);
Spec = vec4(NdotL * Specular * sun_col, 1.);
/* if (hasclouds == 1)
{

View File

@ -7,10 +7,6 @@ uniform float split1;
uniform float split2;
uniform float splitmax;
uniform vec3 direction;
uniform vec3 col;
uniform float sunangle = .54;
in vec2 uv;
out vec4 Diff;
out vec4 Spec;
@ -50,13 +46,12 @@ void main() {
float roughness =texture(ntex, uv).z;
vec3 eyedir = -normalize(xpos.xyz);
// Normalized on the cpu
vec3 L = direction;
vec3 L = normalize((transpose(InverseViewMatrix) * vec4(sun_direction, 0.)).xyz);
float NdotL = clamp(dot(norm, L), 0., 1.);
float angle = 3.14 * sunangle / 180.;
float angle = 3.14 * sun_angle / 180.;
vec3 R = reflect(-eyedir, norm);
vec3 Lightdir = getMostRepresentativePoint(direction, R, angle);
vec3 Lightdir = getMostRepresentativePoint(L, R, angle);
vec3 Specular = SpecularBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
vec3 Diffuse = DiffuseBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
@ -74,6 +69,6 @@ void main() {
else
factor = 1.;
Diff = vec4(factor * NdotL * Diffuse * col, 1.);
Spec = vec4(factor * NdotL * Specular * col, 1.);
Diff = vec4(factor * NdotL * Diffuse * sun_col, 1.);
Spec = vec4(factor * NdotL * Specular * sun_col, 1.);
}

View File

@ -663,12 +663,7 @@ public:
// -----------------------------------------------------------------------
void setSunDirection(const core::vector3df &SunPos)
{
core::matrix4 m_view = getViewMatrix();
m_view.makeInverse();
m_view = m_view.getTransposed();
m_sundirection = SunPos;
m_view.transformVect(m_sundirection);
m_sundirection.normalize();
}
// -----------------------------------------------------------------------
video::SColorf getSunColor() const { return m_suncolor; }

View File

@ -963,13 +963,21 @@ void IrrDriver::computeCameraMatrix(scene::ICameraSceneNode * const camnode, siz
glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::ViewProjectionMatrixesUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, (16 * 9 + 2) * sizeof(float), tmp);
float Lighting[27];
memcpy(Lighting, blueSHCoeff, 9 * sizeof(float));
memcpy(&Lighting[9], greenSHCoeff, 9 * sizeof(float));
memcpy(&Lighting[18], redSHCoeff, 9 * sizeof(float));
float Lighting[36];
Lighting[0] = m_sundirection.X;
Lighting[1] = m_sundirection.Y;
Lighting[2] = m_sundirection.Z;
Lighting[4] = m_suncolor.getRed();
Lighting[5] = m_suncolor.getGreen();
Lighting[6] = m_suncolor.getBlue();
Lighting[7] = 0.54f;
memcpy(&Lighting[8], blueSHCoeff, 9 * sizeof(float));
memcpy(&Lighting[17], greenSHCoeff, 9 * sizeof(float));
memcpy(&Lighting[26], redSHCoeff, 9 * sizeof(float));
glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::LightingDataUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 27 * sizeof(float), Lighting);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 36 * sizeof(float), Lighting);
}

View File

@ -433,7 +433,7 @@ static void initLightingDataUBO()
{
glGenBuffers(1, &SharedObject::LightingDataUBO);
glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::LightingDataUBO);
glBufferData(GL_UNIFORM_BUFFER, 27 * sizeof(float), 0, GL_STREAM_DRAW);
glBufferData(GL_UNIFORM_BUFFER, 36 * sizeof(float), 0, GL_STREAM_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
@ -591,6 +591,12 @@ void bypassUBO(GLuint Program)
glUniform1fv(gLmn, 9, irr_driver->greenSHCoeff);
GLint rLmn = glGetUniformLocation(Program, "redLmn[0]");
glUniform1fv(rLmn, 9, irr_driver->redSHCoeff);
GLint sundir = glGetUniformLocation(Program, "sun_direction");
glUniform3f(sundir, irr_driver->getSunDirection().X, irr_driver->getSunDirection().Y, irr_driver->getSunDirection().Z);
GLint suncol = glGetUniformLocation(Program, "sun_col");
glUniform3f(suncol, irr_driver->getSunColor().getRed(), irr_driver->getSunColor().getGreen(), irr_driver->getSunColor().getBlue());
GLint sunangle = glGetUniformLocation(Program, "sun_angle");
glUniform1f(sunangle, 0.54f);
}
namespace UtilShader