decompose Light Struct

This commit is contained in:
songchengjiang 2014-08-27 19:12:49 +08:00
parent 731d1165c3
commit 72773a806c
4 changed files with 37 additions and 52 deletions

View File

@ -445,33 +445,18 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source
GLchar lightStruct[] = {
"#if CC_MAX_DIRECTIONAL_LIGHT_NUM \n"
"struct DirectionalLightSource \n"
"{ \n"
" vec3 color; \n"
" vec3 direction; \n"
"}; \n"
"uniform DirectionalLightSource CC_DirLightSource[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n"
"uniform int CC_EnabledDirLightNum; \n"
"uniform vec3 CC_DirLightSourceColor[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n"
"uniform vec3 CC_DirLightSourceDirection[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n"
"#endif \n"
"#if CC_MAX_POINT_LIGHT_NUM \n"
"struct PointLightSource \n"
"{ \n"
" vec3 color; \n"
" vec4 position; \n"
"}; \n"
"uniform PointLightSource CC_PointLightSource[CC_MAX_POINT_LIGHT_NUM]; \n"
"uniform int CC_EnabledPointLightNum; \n"
"uniform vec3 CC_PointLightSourceColor[CC_MAX_POINT_LIGHT_NUM]; \n"
"uniform vec4 CC_PointLightSourcePosition[CC_MAX_POINT_LIGHT_NUM]; \n"
"#endif \n"
"#if CC_MAX_SPOT_LIGHT_NUM \n"
"struct SpotLightSource \n"
"{ \n"
" vec3 color; \n"
" vec3 position; \n"
" vec3 direction; \n"
" vec3 params; \n"
"}; \n"
"uniform SpotLightSource CC_SpotLightSource[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform int CC_EnabledSpotLightNum; \n"
"uniform vec3 CC_SpotLightSourceColor[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform vec3 CC_SpotLightSourcePosition[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform vec3 CC_SpotLightSourceDirection[CC_MAX_SPOT_LIGHT_NUM]; \n"
"uniform vec3 CC_SpotLightSourceParams[CC_MAX_SPOT_LIGHT_NUM]; \n"
"#endif \n"
"uniform vec4 CC_AmbientColor; \n"
};
@ -1008,9 +993,9 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV)
CCASSERT(enabledDirLightNum < CC_MAX_DIRECTIONAL_LIGHT_NUM, "");
Vec3 dir = light->getWorldDirection();
dir.normalize();
sprintf(str, "CC_DirLightSource[%d].%s", enabledDirLightNum, "color");
sprintf(str, "CC_DirLightSourceColor[%d]", enabledDirLightNum);
setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f);
sprintf(str, "CC_DirLightSource[%d].%s", enabledDirLightNum, "direction");
sprintf(str, "CC_DirLightSourceDirection[%d]", enabledDirLightNum);
setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z);
++enabledDirLightNum;
}
@ -1019,9 +1004,9 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV)
{
CCASSERT(enabledPointLightNum < CC_MAX_POINT_LIGHT_NUM, "");
Mat4 mat= light->getNodeToWorldTransform();
sprintf(str, "CC_PointLightSource[%d].%s", enabledPointLightNum, "color");
sprintf(str, "CC_PointLightSourceColor[%d]", enabledPointLightNum);
setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f);
sprintf(str, "CC_PointLightSource[%d].%s", enabledPointLightNum, "position");
sprintf(str, "CC_PointLightSourcePosition[%d]", enabledPointLightNum);
setUniformLocationWith4f(glGetUniformLocation(_program, str), mat.m[12], mat.m[13], mat.m[14], 1.0f / light->getRange());
++enabledPointLightNum;
}
@ -1031,13 +1016,13 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV)
Vec3 dir = light->getWorldDirection();
dir.normalize();
Mat4 mat= light->getNodeToWorldTransform();
sprintf(str, "CC_SpotLightSource[%d].%s", enabledSpotLightNum, "color");
sprintf(str, "CC_SpotLightSourceColor[%d]", enabledSpotLightNum);
setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f);
sprintf(str, "CC_SpotLightSource[%d].%s", enabledSpotLightNum, "position");
sprintf(str, "CC_SpotLightSourcePosition[%d]", enabledSpotLightNum);
setUniformLocationWith3f(glGetUniformLocation(_program, str), mat.m[12], mat.m[13], mat.m[14]);
sprintf(str, "CC_SpotLightSource[%d].%s", enabledSpotLightNum, "direction");
sprintf(str, "CC_SpotLightSourceDirection[%d]", enabledSpotLightNum);
setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z);
sprintf(str, "CC_SpotLightSource[%d].%s", enabledSpotLightNum, "params");
sprintf(str, "CC_SpotLightSourceParams[%d]", enabledSpotLightNum);
setUniformLocationWith3f(glGetUniformLocation(_program, str), cosf(light->getInnerAngle()), cosf(light->getOuterAngle()), 1.0f / light->getRange());
++enabledSpotLightNum;
}

View File

@ -31,9 +31,9 @@ uniform vec4 u_color;
void PointLight(int n, vec3 normal, inout vec4 intensity)
{
vec3 dir = v_vertexToPointLightDirection[n];
vec3 ldir = dir * CC_PointLightSource[n].position.w;
vec3 ldir = dir * CC_PointLightSourcePosition[n].w;
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
intensity.xyz += CC_PointLightSource[n].color * max(0.0, dot(normalize(dir), normal)) * attenuation;
intensity.xyz += CC_PointLightSourceColor[n] * max(0.0, dot(normalize(dir), normal)) * attenuation;
intensity.w = 1.0;
}
\n#endif\n
@ -41,7 +41,7 @@ void PointLight(int n, vec3 normal, inout vec4 intensity)
\n#if CC_MAX_DIRECTIONAL_LIGHT_NUM\n
void DirectionalLight(int n, vec3 normal, inout vec4 intensity)
{
intensity.xyz += CC_DirLightSource[n].color * max(0.0, dot(-CC_DirLightSource[n].direction, normal));
intensity.xyz += CC_DirLightSourceColor[n] * max(0.0, dot(-CC_DirLightSourceDirection[n], normal));
intensity.w = 1.0;
}
\n#endif\n
@ -50,13 +50,13 @@ void DirectionalLight(int n, vec3 normal, inout vec4 intensity)
void SpotLight(int n, vec3 normal, inout vec4 intensity)
{
vec3 dir = v_vertexToSpotLightDirection[n];
vec3 ldir = dir * CC_SpotLightSource[n].params.z;
vec3 ldir = dir * CC_SpotLightSourceParams[n].z;
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
float spotDot = dot(normalize(dir), -CC_SpotLightSource[n].direction);
float innerCos = CC_SpotLightSource[n].params.x;
float outerCos = CC_SpotLightSource[n].params.y;
float spotDot = dot(normalize(dir), -CC_SpotLightSourceDirection[n]);
float innerCos = CC_SpotLightSourceParams[n].x;
float outerCos = CC_SpotLightSourceParams[n].y;
float factor = smoothstep(outerCos, innerCos, spotDot);
intensity.xyz += CC_SpotLightSource[n].color * max(0.0, dot(normalize(dir), normal)) * factor * attenuation;
intensity.xyz += CC_SpotLightSourceColor[n] * max(0.0, dot(normalize(dir), normal)) * factor * attenuation;
intensity.w = 1.0;
}
\n#endif\n

View File

@ -31,9 +31,9 @@ uniform vec4 u_color;
void PointLight(int n, vec3 normal, inout vec4 intensity)
{
vec3 dir = v_vertexToPointLightDirection[n];
vec3 ldir = dir * CC_PointLightSource[n].position.w;
vec3 ldir = dir * CC_PointLightSourcePosition[n].w;
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
intensity.xyz += CC_PointLightSource[n].color * max(0.0, dot(normalize(dir), normal)) * attenuation;
intensity.xyz += CC_PointLightSourceColor[n] * max(0.0, dot(normalize(dir), normal)) * attenuation;
intensity.w = 1.0;
}
\n#endif\n
@ -41,7 +41,7 @@ void PointLight(int n, vec3 normal, inout vec4 intensity)
\n#if CC_MAX_DIRECTIONAL_LIGHT_NUM\n
void DirectionalLight(int n, vec3 normal, inout vec4 intensity)
{
intensity.xyz += CC_DirLightSource[n].color * max(0.0, dot(-CC_DirLightSource[n].direction, normal));
intensity.xyz += CC_DirLightSourceColor[n] * max(0.0, dot(-CC_DirLightSourceDirection[n], normal));
intensity.w = 1.0;
}
\n#endif\n
@ -50,13 +50,13 @@ void DirectionalLight(int n, vec3 normal, inout vec4 intensity)
void SpotLight(int n, vec3 normal, inout vec4 intensity)
{
vec3 dir = v_vertexToSpotLightDirection[n];
vec3 ldir = dir * CC_SpotLightSource[n].params.z;
vec3 ldir = dir * CC_SpotLightSourceParams[n].z;
float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
float spotDot = dot(normalize(dir), -CC_SpotLightSource[n].direction);
float innerCos = CC_SpotLightSource[n].params.x;
float outerCos = CC_SpotLightSource[n].params.y;
float spotDot = dot(normalize(dir), -CC_SpotLightSourceDirection[n]);
float innerCos = CC_SpotLightSourceParams[n].x;
float outerCos = CC_SpotLightSourceParams[n].y;
float factor = smoothstep(outerCos, innerCos, spotDot);
intensity.xyz += CC_SpotLightSource[n].color * max(0.0, dot(normalize(dir), normal)) * factor * attenuation;
intensity.xyz += CC_SpotLightSourceColor[n] * max(0.0, dot(normalize(dir), normal)) * factor * attenuation;
intensity.w = 1.0;
}
\n#endif\n

View File

@ -20,14 +20,14 @@ void main(void)
\n#if (CC_MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = CC_PointLightSource[i].position.xyz - ePosition.xyz;
v_vertexToPointLightDirection[i] = CC_PointLightSourcePosition[i].xyz - ePosition.xyz;
}
\n#endif\n
\n#if (CC_MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = CC_SpotLightSource[i].position.xyz - ePosition.xyz;
v_vertexToSpotLightDirection[i] = CC_SpotLightSourcePosition[i] - ePosition.xyz;
}
\n#endif\n
@ -113,7 +113,7 @@ void getPositionAndNormal(out vec4 position, out vec3 normal)
position.w = p.w;
\n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM || CC_MAX_POINT_LIGHT_NUM || CC_MAX_SPOT_LIGHT_NUM)\n
vec3 n = vec4(a_normal, 0.0);
vec4 n = vec4(a_normal, 0.0);
normal.x = dot(n, matrixPalette1);
normal.y = dot(n, matrixPalette2);
normal.z = dot(n, matrixPalette3);
@ -130,14 +130,14 @@ void main()
\n#if (CC_MAX_POINT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = CC_PointLightSource[i].position.xyz- ePosition.xyz;
v_vertexToPointLightDirection[i] = CC_PointLightSourcePosition[i].xyz- ePosition.xyz;
}
\n#endif\n
\n#if (CC_MAX_SPOT_LIGHT_NUM > 0)\n
for (int i = 0; i < CC_MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = CC_SpotLightSource[i].position.xyz - ePosition.xyz;
v_vertexToSpotLightDirection[i] = CC_SpotLightSourcePosition[i] - ePosition.xyz;
}
\n#endif\n