"\n"
"const float c_zero = 0.0;\n"
"const float c_one = 1.0;\n"
- "const int indx_zero = 0;\n"
- "const int indx_one = 1;\n"
"\n"
"uniform mat4 u_mvp;\n"
"uniform mat4 u_modelview;\n"
"varying vec2 v_texcoord;\n"
"varying vec4 v_color;\n"
"\n"
- "vec4\n"
- "do_lighting(int i)\n"
+ "vec4 pointLight(int i)\n"
"{\n"
- " vec3 lightdir, n, h, eye;\n"
+ " vec3 lightdir, n, h, eye, distance, vertexPos;\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, 0.0)).xyz);\n"
"\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[i].position.xyz - (u_modelview * a_position).xyz;\n"
+ " vertexPos = (u_modelview * a_position).xyz;\n"
+ " lightdir = u_light[i].position.xyz - vertexPos;\n"
"\n"
- " // compute distance attenuation\n"
- " distance.x = c_one;\n"
- " distance.z = dot(lightdir, lightdir);\n"
- " distance.y = sqrt(distance.z);\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[i].attenuation);"
+ " attenuation = c_one / dot(distance, u_light[i].attenuation);"
"\n"
+ " if (attenuation > c_zero)\n"
+ " {\n"
" lightdir = normalize(lightdir);\n"
"\n"
- " if (u_light[i].cutoff < 180.0)\n"
- " {\n"
- " // compute spot factor\n"
- " spot_factor = dot(-lightdir, normalize(u_light[i].direction));\n"
+ " color = u_light[i].ambient * u_material.ambient;\n"
"\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"
+ " ndotl = max(c_zero, dot(n, lightdir));\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n"
"\n"
- " attenuation *= spot_factor;\n"
+ " eye = normalize(u_eyePosition - vertexPos);\n"
+ " h = normalize(lightdir + eye);\n"
+ " ndoth = dot(n, h);\n"
+ "\n"
+ " if (ndoth > c_zero)\n"
+ " {\n"
+ " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light[i].specular;\n"
" }\n"
+ "\n"
+ " color *= attenuation;\n"
" }\n"
- " else\n"
+ "\n"
+ " return color;\n"
+ "}\n"
+ "\n"
+ "vec4\n"
+ "spotLight(int i)\n"
+ "{\n"
+ " vec3 lightdir, n, h, eye, distance, vertexPos;\n"
+ " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n"
+ " float ndotl, ndoth, attenuation, spot_factor;\n"
+ "\n"
+ " n = normalize((u_inv_modelview * vec4(a_normal, 0.0)).xyz);\n"
+ "\n"
+ " vertexPos = (u_modelview * a_position).xyz;\n"
+ " distance = u_light[i].position.xyz - vertexPos;\n"
+ "\n"
+ " lightdir = normalize(distance);\n"
+ "\n"
+ " // compute spot factor\n"
+ " spot_factor = dot(-lightdir, normalize(u_light[i].direction));\n"
+ "\n"
+ " if (spot_factor >= cos(radians(u_light[i].cutoff)))\n"
" {\n"
- " // directional light\n"
- " lightdir = normalize(u_light[i].position.xyz);\n"
- " attenuation = c_one;\n"
+ " // compute distance attenuation\n"
+ " distance.z = dot(distance, distance);\n"
+ " distance.y = sqrt(distance.z);\n"
+ " distance.x = c_one;\n"
+ "\n"
+ " attenuation = pow(spot_factor, u_light[i].exponent) / dot(distance, u_light[i].attenuation);\n"
" }\n"
+ " else\n"
+ " attenuation = c_zero;\n"
"\n"
" if (attenuation > c_zero)\n"
" {\n"
" ndotl = max(c_zero, dot(n, lightdir));\n"
" color += ndotl * u_light[i].diffuse * u_material.diffuse;\n"
"\n"
- " eye = normalize(u_eyePosition - (u_modelview * a_position).xyz);\n"
+ " eye = normalize(u_eyePosition - vertexPos);\n"
" h = normalize(lightdir + eye);\n"
" ndoth = dot(n, h);\n"
"\n"
" return color;\n"
"}\n"
"\n"
+ "vec4\n"
+ "directionalLight(int i)\n"
+ "{\n"
+ " vec3 lightdir, n, h, eye;\n"
+ " vec4 color;\n"
+ " float ndotl, ndoth;\n"
+ "\n"
+ " n = normalize((u_inv_modelview * vec4(a_normal, 0.0)).xyz);\n"
+ "\n"
+ " lightdir = normalize(u_light[i].position.xyz);\n"
+ "\n"
+ " color = u_light[i].ambient * u_material.ambient;\n"
+ "\n"
+ " ndotl = max(c_zero, dot(n, lightdir));\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n"
+ "\n"
+ " eye = normalize(u_eyePosition - (u_modelview * a_position).xyz);\n"
+ " h = normalize(lightdir + eye);\n"
+ " ndoth = dot(n, h);\n"
+ "\n"
+ " if (ndoth > c_zero)\n"
+ " {\n"
+ " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light[i].specular;\n"
+ " }\n"
+ "\n"
+ " return color;\n"
+ "}\n"
+ "\n"
"void\n"
"main()\n"
"{\n"
"\n"
" for (int i = 0; i < u_lightCount; i++)\n"
" {\n"
- " color += do_lighting(i);\n"
+ " if (u_light[i].type == 0)\n"
+ " color += pointLight(i);\n"
+ " else if (u_light[i].type == 1)\n"
+ " color += spotLight(i);\n"
+ " else if (u_light[i].type == 2)\n"
+ " color += directionalLight(i);\n"
" }\n"
"\n"
" color += u_material.emissive;\n"
"varying vec4 v_color;\n"
"void main()\n"
"{\n"
- " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n"
+ "// gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n"
+ " gl_FragColor.rgba = v_color;\n"
"}\n";
static const char strFragmentShader_PerPixellight[] =
" vec3 v = normalize(u_eyePosition - v_position.xyz);\n"
" vec3 r = reflect(-v, normal);\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"
+ " diffuse = u_light[i].diffuse * u_material.diffuse * 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"
" {\n"
" if (u_light[i].type == 0)\n"
" res_color += pointLight(i);\n"
- " if (u_light[i].type == 1)\n"
+ " else if (u_light[i].type == 1)\n"
" res_color += spotLight(i);\n"
- " if (u_light[i].type == 2)\n"
+ " else if (u_light[i].type == 2)\n"
" res_color += directionalLight(i);\n"
" }\n"
" res_color += u_material.emissive;\n"