"uniform mat4 u_inv_modelview;\n"
"\n"
"uniform material u_material;\n"
- "uniform light u_light;\n"
+ "uniform light u_light[8];\n"
+ "uniform int u_lightCount;\n"
"\n"
"attribute vec4 a_position;\n"
"attribute vec4 a_texcoord;\n"
"varying vec4 v_color;\n"
"\n"
"vec4\n"
- "do_lighting()\n"
+ "do_lighting(int i)\n"
"{\n"
" vec3 lightdir, n, h;\n"
" vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n"
" float ndotl, ndoth, attenuation;\n"
"\n"
- " n = normalize((u_inv_modelview * vec4(a_normal, 1.0)).xyz);\n"
+ " n = normalize((u_inv_modelview * vec4(a_normal, 0.0)).xyz);\n"
"\n"
- " if (u_light.type < 2)\n"
+ " if (u_light[i].type < 2)\n"
" {\n"
" vec3 distance;\n"
" float spot_factor;\n"
"\n"
" // point or spot case\n"
- " lightdir = u_light.position.xyz - (u_modelview * a_position).xyz;\n"
+ " lightdir = u_light[i].position.xyz - (u_modelview * a_position).xyz;\n"
"\n"
" // compute distance attenuation\n"
" distance.x = c_one;\n"
" distance.z = dot(lightdir, lightdir);\n"
" distance.y = sqrt(distance.z);\n"
"\n"
- " attenuation = c_one / dot(distance, u_light.attenuation);"
+ " attenuation = c_one / dot(distance, u_light[i].attenuation);"
"\n"
" lightdir = normalize(lightdir);\n"
"\n"
- " if (u_light.cutoff < 180.0)\n"
+ " if (u_light[i].cutoff < 180.0)\n"
" {\n"
" // compute spot factor\n"
- " spot_factor = dot(-lightdir, normalize(u_light.direction));\n"
+ " spot_factor = dot(-lightdir, normalize(u_light[i].direction));\n"
"\n"
- " if (spot_factor >= cos(radians(u_light.cutoff)))\n"
- " spot_factor = pow(spot_factor, u_light.exponent);\n"
+ " if (spot_factor >= cos(radians(u_light[i].cutoff)))\n"
+ " spot_factor = pow(spot_factor, u_light[i].exponent);\n"
" else\n"
" spot_factor = c_zero;\n"
"\n"
" else\n"
" {\n"
" // directional light\n"
- " lightdir = normalize(u_light.position.xyz);\n"
+ " lightdir = normalize(u_light[i].position.xyz);\n"
" attenuation = c_one;\n"
" }\n"
"\n"
" if (attenuation > c_zero)\n"
" {\n"
- " color = u_light.ambient * u_material.ambient;\n"
+ " color = u_light[i].ambient * u_material.ambient;\n"
"\n"
" ndotl = max(c_zero, dot(n, lightdir));\n"
- " color += ndotl * u_light.diffuse * u_material.diffuse;\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n"
"\n"
" h = normalize(lightdir + vec3(c_zero, c_zero, c_one));\n"
" ndoth = dot(n, h);\n"
"\n"
" if (ndoth > c_zero)\n"
" {\n"
- " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light.specular;\n"
+ " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light[i].specular;\n"
" }\n"
"\n"
" color *= attenuation;\n"
" }\n"
"\n"
- " color += u_material.emissive;\n"
- "// color += u_material.emissive + u_material.ambient * u_scene_ambient; //todo\n"
- "\n"
- " color.a = u_material.diffuse.a;\n"
- "\n"
" return color;\n"
"}\n"
"\n"
"void\n"
"main()\n"
"{\n"
- " v_color = do_lighting();\n"
+ " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n"
+ "\n"
+ " for (int i = 0; i < u_lightCount; i++)\n"
+ " {\n"
+ " color += do_lighting(i);\n"
+ " }\n"
+ "\n"
+ " color += u_material.emissive;\n"
+ "// color += u_material.emissive + u_material.ambient * u_scene_ambient; //todo\n"
+ "\n"
+ " color.a = u_material.diffuse.a;\n"
+ "\n"
+ " v_color = color;\n"
" v_texcoord = a_texcoord.xy;\n"
" gl_Position = u_mvp * a_position;\n"
"}\n";
" gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n"
"}\n";
-
-
static const char strFragmentShader_PerPixellight[] =
"precision highp float;\n"
"struct light\n"
" vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);\n"
" \n"
" vec3 normal = normalize(v_normal);\n"
- " vec3 lightdir;\n"
+ " vec3 lightdir = normalize(u_light[i].position.xyz);\n"
" vec3 v = normalize(eyePosition - v_position.xyz);\n"
" vec3 r = reflect(-v, normal);\n"
- " lightdir = normalize(u_light[i].direction);\n"
" \n"
- " diffuse += u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra* max(0.0, dot(lightdir, normal));\n"
+ " diffuse = u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra * max(0.0, dot(lightdir, normal));\n"
" ambient = u_light[i].ambient * u_material.ambient;\n"
" specular = u_light[i].specular * u_material.specular * pow(max(dot(r, lightdir), 0.0), u_material.shininess );\n"
" \n"
vertexLight.Construct(Shader::SHADER_VERTEX, strVertexShader_PerPixellight);
fragmentLight.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_PerPixellight);
+// vertexLight.Construct(Shader::SHADER_VERTEX, strVertexShader_light);
+// fragmentLight.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_light);
__pLightShader->Construct(vertexLight, fragmentLight);
}