Refactoring per vertex lighting shader
authorHeeyong Song <heeyong.song@samsung.com>
Mon, 2 Dec 2013 05:41:11 +0000 (14:41 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Mon, 2 Dec 2013 05:41:11 +0000 (14:41 +0900)
Change-Id: I45c7b04212a97b63898eb055a300ce4ce0884c27

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

index 6ce3128..07ec0d8 100644 (file)
@@ -422,13 +422,13 @@ _GlNode::SyncStatus(VisualElementSurface* pSurface, _VisualElementImpl& element)
                float* pPosition = pLightImpl->GetPosition();
 
                position[0] = (__transform.matrix[0][0] * pPosition[0]) + (__transform.matrix[1][0] * pPosition[1]) +
-                                       (__transform.matrix[2][0] * pPosition[2]) + (__transform.matrix[3][0] * pPosition[3]);
+                                       (__transform.matrix[2][0] * pPosition[2]) + __transform.matrix[3][0];
                position[1] = (__transform.matrix[0][1] * pPosition[0]) + (__transform.matrix[1][1] * pPosition[1]) +
-                                       (__transform.matrix[2][1] * pPosition[2]) + (__transform.matrix[3][1] * pPosition[3]);
+                                       (__transform.matrix[2][1] * pPosition[2]) + __transform.matrix[3][1];
                position[2] = (__transform.matrix[0][2] * pPosition[0]) + (__transform.matrix[1][2] * pPosition[1]) +
-                                       (__transform.matrix[2][2] * pPosition[2]) + (__transform.matrix[3][2] * pPosition[3]);
+                                       (__transform.matrix[2][2] * pPosition[2]) + __transform.matrix[3][2];
                position[3] = (__transform.matrix[0][3] * pPosition[0]) + (__transform.matrix[1][3] * pPosition[1]) +
-                                       (__transform.matrix[2][3] * pPosition[2]) + (__transform.matrix[3][3] * pPosition[3]);
+                                       (__transform.matrix[2][3] * pPosition[2]) + __transform.matrix[3][3] * pPosition[3];
 
                pLightImpl->SetWorldPosition(position);
        }
@@ -438,7 +438,7 @@ _GlNode::SyncStatus(VisualElementSurface* pSurface, _VisualElementImpl& element)
                __pLight = null;
        }
 
-       Material* pMaterial =element.GetSharedData().geometry.GetMaterial();
+       Material* pMaterial = element.GetSharedData().geometry.GetMaterial();
 
        if (pMaterial)
        {
index 25ea573..60b2bb6 100644 (file)
@@ -811,8 +811,6 @@ _GlContext::PrepareDefaultShaders(void)
                        "\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"
@@ -832,51 +830,76 @@ _GlContext::PrepareDefaultShaders(void)
                        "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"
@@ -885,7 +908,7 @@ _GlContext::PrepareDefaultShaders(void)
                        "               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"
@@ -900,6 +923,34 @@ _GlContext::PrepareDefaultShaders(void)
                        "       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"
@@ -907,7 +958,12 @@ _GlContext::PrepareDefaultShaders(void)
                        "\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"
@@ -927,7 +983,8 @@ _GlContext::PrepareDefaultShaders(void)
                        "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[] =
@@ -1055,7 +1112,8 @@ _GlContext::PrepareDefaultShaders(void)
                        "       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"
@@ -1073,9 +1131,9 @@ _GlContext::PrepareDefaultShaders(void)
                        "               {\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"