Apply light count to "per vertex" light shader and fix position error of "per pixel...
authorHeeyong Song <heeyong.song@samsung.com>
Mon, 25 Nov 2013 08:09:08 +0000 (17:09 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Mon, 25 Nov 2013 08:09:08 +0000 (17:09 +0900)
Change-Id: I1f92750814286080a5e3e10a16ee834e7e83a9c3

src/ui/animations/FUiAnim_GlRenderManager.cpp
src/ui/animations/platform/FUiAnim_GlContext.cpp

index 017231f..0e89dcb 100644 (file)
@@ -781,7 +781,7 @@ _GlRenderManager::FlushRenderQueue(void)
                                glUniform4fv(pProgramImpl->__uniformLocation[UNIFORM_VEC4_LIGHT_AMBIENT + offset], 1, pLightImpl->GetAmbient());
                                glUniform4fv(pProgramImpl->__uniformLocation[UNIFORM_VEC4_LIGHT_DIFFUSE + offset], 1, pLightImpl->GetDiffuse());
                                glUniform4fv(pProgramImpl->__uniformLocation[UNIFORM_VEC4_LIGHT_SPECULAR + offset], 1, pLightImpl->GetSpecular());
-                               glUniform4fv(pProgramImpl->__uniformLocation[UNIFORM_VEC4_LIGHT_POSITION + offset], 1, pLightImpl->GetPosition());
+                               glUniform4fv(pProgramImpl->__uniformLocation[UNIFORM_VEC4_LIGHT_POSITION + offset], 1, pLightImpl->GetWorldPosition());
                                glUniform3fv(pProgramImpl->__uniformLocation[UNIFORM_VEC3_LIGHT_DIRECTION + offset], 1, pLightImpl->GetDirection());
                                glUniform1f(pProgramImpl->__uniformLocation[UNIFORM_FLOAT_LIGHT_EXPONENT + offset], pLightImpl->GetExponent());
                                glUniform1f(pProgramImpl->__uniformLocation[UNIFORM_FLOAT_LIGHT_CUTOFF + offset], pLightImpl->GetCutOff());
index 24f112b..babd55d 100644 (file)
@@ -808,7 +808,8 @@ _GlContext::PrepareDefaultShaders(void)
                        "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"
@@ -819,38 +820,38 @@ _GlContext::PrepareDefaultShaders(void)
                        "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"
@@ -860,40 +861,47 @@ _GlContext::PrepareDefaultShaders(void)
                        "       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";
@@ -908,8 +916,6 @@ _GlContext::PrepareDefaultShaders(void)
                        "       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"
@@ -1033,12 +1039,11 @@ _GlContext::PrepareDefaultShaders(void)
                        "       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"
@@ -1132,6 +1137,8 @@ _GlContext::PrepareDefaultShaders(void)
 
        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);
 }