From: Heeyong Song Date: Mon, 30 Dec 2013 07:46:55 +0000 (+0900) Subject: Refactoring light shader X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a8f9b5c5360d9ec8cc134b557fb1c6fd1374fb0a;p=framework%2Fosp%2Fuifw.git Refactoring light shader Change-Id: Ibf690962cbf17cf5fd298f938255867f1c013df8 --- diff --git a/src/ui/animations/FUiAnim_GlRenderManager.cpp b/src/ui/animations/FUiAnim_GlRenderManager.cpp index 17b7298..651829c 100644 --- a/src/ui/animations/FUiAnim_GlRenderManager.cpp +++ b/src/ui/animations/FUiAnim_GlRenderManager.cpp @@ -969,10 +969,6 @@ _GlRenderManager::FlushRenderQueue(void) glUniform1i(pProgramImpl->__uniformLocation[UNIFORM_INT_ACTIVE_LIGHT_COUNT], __pRenderQueue[i].__activeLightCount); } - else - { - glUniform1i(pProgramImpl->__uniformLocation[UNIFORM_INT_ACTIVE_LIGHT_COUNT], 0); - } if (__pRenderQueue[i].__pMaterial) { @@ -1074,6 +1070,7 @@ _GlRenderManager::FlushRenderQueue(void) __pGlContext->BindTexture(0 , __pRenderQueue[i].__textureId); glUniform1i(pProgramImpl->__uniformLocation[UNIFORM_SAMPLER2D_TEXTURE], 0); + glUniform1i(pProgramImpl->__uniformLocation[UNIFORM_BOOL_USE_TEXTURE], 1); if (!__pRenderQueue[i].__isMesh || __pRenderQueue[i].__isTexUpdated) { @@ -1084,6 +1081,10 @@ _GlRenderManager::FlushRenderQueue(void) else { glUniform1i(pProgramImpl->__uniformLocation[UNIFORM_SAMPLER2D_TEXTURE], EXTRA_SURFACE_MAX + 2); + glUniform1i(pProgramImpl->__uniformLocation[UNIFORM_BOOL_USE_TEXTURE], 0); + + // temp + glVertexAttribPointer(__pRenderQueue[i].__aTexCoord, 2, GL_FLOAT, GL_FALSE, TEX_COORD_BUFFER_STRIDE * sizeof(float), __pRenderQueue[i].__pVertices); } for (int k = 0; k < EXTRA_SURFACE_MAX; k++) diff --git a/src/ui/animations/FUiAnim_ShaderProgramImpl.cpp b/src/ui/animations/FUiAnim_ShaderProgramImpl.cpp index 3d649a4..edbcfce 100644 --- a/src/ui/animations/FUiAnim_ShaderProgramImpl.cpp +++ b/src/ui/animations/FUiAnim_ShaderProgramImpl.cpp @@ -76,6 +76,7 @@ const wchar_t* VeUniformMaterialShininess = L"u_material.shininess"; const wchar_t* VeUniformActiveLightCount = L"u_lightCount"; const wchar_t* VeUniformEyePosition = L"u_eyePosition"; +const wchar_t* VeUniformUseTexture = L"u_useTexture"; const wchar_t* VeUniformTex2DAlphaMask = L"u_tex2dAlphaMask"; const wchar_t* VeUniformTex2DExtra = L"u_extraSurface%d.tex2DExtra";// "u_tex2dExtra%d";//u_extraSurface%d.tex2DExtra @@ -251,6 +252,7 @@ _ShaderProgramImpl::MakeUniformLocationTable(void) str.Format(MAX_STRING_LENGHT, VeUniformLightAttenuation, i); MoveUniformItem(UNIFORM_VEC3_LIGHT_ATTENUATION + offset, str.GetPointer()); } + for (int i = 0; i < EXTRA_SURFACE_MAX; i++) { // = i-1; @@ -261,6 +263,7 @@ _ShaderProgramImpl::MakeUniformLocationTable(void) str.Format(MAX_STRING_LENGHT, VeUniformTex2DExtraOpacity, i+1); MoveUniformItem(UNIFORM_FLOAT_EXTRA_SURFACE_OPACITY + offset, str.GetPointer()); } + MoveUniformItem(UNIFORM_VEC4_MATERIAL_AMBIENT, VeUniformMaterialAmbient); MoveUniformItem(UNIFORM_VEC4_MATERIAL_DIFFUSE, VeUniformMaterialDiffuse); MoveUniformItem(UNIFORM_VEC4_MATERIAL_SPECULAR, VeUniformMaterialSpecular); @@ -269,8 +272,9 @@ _ShaderProgramImpl::MakeUniformLocationTable(void) MoveUniformItem(UNIFORM_INT_ACTIVE_LIGHT_COUNT, VeUniformActiveLightCount); MoveUniformItem(UNIFORM_VEC3_EYE_POSITION, VeUniformEyePosition); - MoveUniformItem(UNIFORM_SAMPLER2D_ALPHA_MASK, VeUniformTex2DAlphaMask); + MoveUniformItem(UNIFORM_BOOL_USE_TEXTURE, VeUniformUseTexture); + MoveUniformItem(UNIFORM_SAMPLER2D_ALPHA_MASK, VeUniformTex2DAlphaMask); MoveUniformItem(UNIFORM_INT_EXTRA_SURFACE_COUNT, VeUniformExtraSurfaceCount); MoveUniformItem(UNOFORM_INT_ALPHA_MASK_COUNT, VeUniformAlphaMaskCount); } diff --git a/src/ui/animations/FUiAnim_ShaderProgramImpl.h b/src/ui/animations/FUiAnim_ShaderProgramImpl.h index 68b5491..a3dc74a 100644 --- a/src/ui/animations/FUiAnim_ShaderProgramImpl.h +++ b/src/ui/animations/FUiAnim_ShaderProgramImpl.h @@ -67,8 +67,9 @@ extern const wchar_t* VeUniformMaterialShininess; extern const wchar_t* VeUniformActiveLightCount; extern const wchar_t* VeUniformEyePosition; -extern const wchar_t* VeUniformTex2DAlphaMask; +extern const wchar_t* VeUniformUseTexture; +extern const wchar_t* VeUniformTex2DAlphaMask; extern const wchar_t* VeUniformTex2DExtra; extern const wchar_t* VeUniformTex2DExtraOpacity; extern const wchar_t* VeUniformExtraSurfaceCount; @@ -111,6 +112,8 @@ enum UniformLocation UNIFORM_INT_ACTIVE_LIGHT_COUNT, UNIFORM_VEC3_EYE_POSITION, + UNIFORM_BOOL_USE_TEXTURE, + UNIFORM_SAMPLER2D_ALPHA_MASK, /**< Uniform for alphaMask texture */ UNIFORM_INT_EXTRA_SURFACE_COUNT, UNOFORM_INT_ALPHA_MASK_COUNT, diff --git a/src/ui/animations/platform/FUiAnim_GlContext.cpp b/src/ui/animations/platform/FUiAnim_GlContext.cpp index 9be39f3..2c4c734 100644 --- a/src/ui/animations/platform/FUiAnim_GlContext.cpp +++ b/src/ui/animations/platform/FUiAnim_GlContext.cpp @@ -874,6 +874,7 @@ _GlContext::PrepareDefaultShaders(void) "//uniform mat3 u_inv_modelview;\n" "uniform mat4 u_inv_modelview;\n" "uniform vec3 u_eyePosition;\n" + "uniform bool u_useTexture;\n" "\n" "uniform material u_material;\n" "uniform light u_light[8];\n" @@ -881,13 +882,13 @@ _GlContext::PrepareDefaultShaders(void) "\n" "attribute vec4 a_position;\n" "attribute vec4 a_texcoord;\n" - "attribute vec4 a_color;\n" "attribute vec3 a_normal;\n" "\n" "varying vec2 v_texcoord;\n" "varying vec4 v_color;\n" "\n" - "vec4 pointLight(int i)\n" + "vec4\n" + "pointLight(int i)\n" "{\n" " vec3 lightdir, n, h, eye, distance, vertexPos;\n" " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n" @@ -1029,18 +1030,63 @@ _GlContext::PrepareDefaultShaders(void) " color.a = u_material.diffuse.a;\n" "\n" " v_color = color;\n" - " v_texcoord = a_texcoord.xy;\n" + " if (u_useTexture == true)\n" + " {\n" + " v_texcoord = a_texcoord.xy;\n" + " }\n" + " else\n" + " {\n" + " v_texcoord = vec2(0, 0);\n" + " }\n" " gl_Position = u_mvp * a_position;\n" "}\n"; static const char strFragmentShader_light[] = "precision highp float;\n" "uniform sampler2D u_tex2d;\n" + "uniform bool u_useTexture;\n" "varying vec2 v_texcoord;\n" "varying vec4 v_color;\n" "void main()\n" "{\n" - " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n" + " if (u_useTexture == true)\n" + " {\n" + " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n" + " }\n" + " else\n" + " {\n" + " gl_FragColor.rgba = v_color;\n" + " }\n" + "}\n"; + + static const char strVertexShader_PerPixellight[] = + "uniform mat4 u_mvp;\n" + "uniform mat4 u_modelview;\n" + "uniform mat4 u_inv_modelview;\n" + "uniform bool u_useTexture;\n" + "\n" + "attribute vec4 a_position;\n" + "attribute vec2 a_texcoord;\n" + "attribute vec3 a_normal;\n" + "\n" + "varying vec4 v_position;\n" + "varying vec2 v_texcoord;\n" + "varying vec3 v_normal;\n" + "\n" + "void\n" + "main()\n" + "{\n" + " v_position = u_modelview * a_position;\n" + " v_normal = (u_inv_modelview * vec4(a_normal, 0.0)).xyz;\n" + " if (u_useTexture == true)\n" + " {\n" + " v_texcoord = a_texcoord.xy;\n" + " }\n" + " else\n" + " {\n" + " v_texcoord = vec2(0, 0);\n" + " }\n" + " gl_Position = u_mvp * a_position;\n" "}\n"; static const char strFragmentShader_PerPixellight[] = @@ -1066,158 +1112,191 @@ _GlContext::PrepareDefaultShaders(void) " float shininess;\n" "};\n" "\n" + "const float c_zero = 0.0;\n" + "const float c_one = 1.0;\n" + "\n" "uniform vec3 u_eyePosition;\n" - "uniform int u_lightCount;\n" - "uniform sampler2D u_tex2d;\n" + "\n" "uniform material u_material;\n" "uniform light u_light[8];\n" + "uniform int u_lightCount;\n" + "\n" + "uniform sampler2D u_tex2d;\n" + "uniform bool u_useTexture;\n" "\n" "varying vec4 v_position;\n" "varying vec2 v_texcoord;\n" "varying vec3 v_normal;\n" "\n" - "vec4 pointLight(int i)\n" + "vec4\n" + "pointLight(int i)\n" "{\n" - " vec4 result = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec4 ambient = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec3 normal = normalize(v_normal);\n" - " vec3 lightdir;\n" - " vec3 v = normalize(u_eyePosition - v_position.xyz);\n" - " vec3 r = reflect(-v, normal);\n" + " vec3 lightdir, n, h, eye, distance;\n" + " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n" + " float ndotl, ndoth, attenuation;\n" "\n" - " float att = 0.0;\n" + " n = normalize(v_normal);\n" "\n" " lightdir = u_light[i].position.xyz - v_position.xyz;\n" - " lightdir = normalize(lightdir);\n" "\n" - " float denum = (u_light[i].attenuation.x + u_light[i].attenuation.y*u_light[i].attenuation.x + u_light[i].attenuation.z*u_light[i].attenuation.z );\n" + " // compute distance attenuation\n" + " distance.x = c_one;\n" + " distance.z = dot(lightdir, lightdir);\n" + " distance.y = sqrt(distance.z);\n" "\n" - " if (denum > 0.0)\n" - " att = 1.0 / denum;\n" + " attenuation = c_one / dot(distance, u_light[i].attenuation);" "\n" - " ambient = u_light[i].ambient * u_material.ambient;\n" - " diffuse += u_light[i].diffuse *u_material.diffuse *texture2D(u_tex2d, v_texcoord).bgra* max(0.0, dot(lightdir, normal));\n" - " specular = u_light[i].specular * u_material.specular * pow(max(dot(r, lightdir), 0.0), u_material.shininess );\n" + " if (attenuation > c_zero)\n" + " {\n" + " lightdir = normalize(lightdir);\n" + "\n" + " color = u_light[i].ambient * u_material.ambient;\n" + "\n" + " ndotl = max(c_zero, dot(n, lightdir));\n" + " if (u_useTexture == true)\n" + " {\n" + " color += ndotl * u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra;\n" + " }\n" + " else\n" + " {\n" + " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n" + " }\n" + "\n" + " eye = normalize(u_eyePosition - v_position.xyz);\n" + " h = normalize(lightdir + eye);\n" + " ndoth = dot(n, h);\n" "\n" - " result = ambient + diffuse + specular;\n" + " if (ndoth > c_zero)\n" + " {\n" + " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light[i].specular;\n" + " }\n" "\n" - "if (att > 0.0)\n" - " result*=att;\n" + " color *= attenuation;\n" + " }\n" "\n" - " return result;\n" - " \n" + " return color;\n" "}\n" "\n" - "vec4 spotLight(int i)\n" + "vec4\n" + "spotLight(int i)\n" "{\n" - " vec4 result = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec4 ambient = vec4(0.0, 0.0, 0.0, 0.0);\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, n, h, eye, distance;\n" + " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n" + " float ndotl, ndoth, attenuation, spot_factor;\n" "\n" - " vec3 v = normalize(u_eyePosition - v_position.xyz);\n" - " vec3 r = reflect(-v, normal);\n" - " vec3 d = normalize(u_light[i].direction.xyz);\n" + " n = normalize(v_normal);\n" "\n" - " float att = 0.0;\n" - " float spotEffect = 0.0;\n" + " distance = u_light[i].position.xyz - v_position.xyz;\n" "\n" - " lightdir = u_light[i].position.xyz - v_position.xyz;\n" - " lightdir = normalize(lightdir);\n" + " lightdir = normalize(distance);\n" "\n" - " spotEffect = dot( -lightdir, d);\n" + " // compute spot factor\n" + " spot_factor = dot(-lightdir, normalize(u_light[i].direction));\n" "\n" - " float denum = (u_light[i].attenuation.x + u_light[i].attenuation.y*u_light[i].attenuation.x + u_light[i].attenuation.z*u_light[i].attenuation.z );\n" - " if (denum > 0.0)\n" - " att = 1.0 / denum;\n" + " if (spot_factor >= cos(radians(u_light[i].cutoff)))\n" + " {\n" + " // compute distance attenuation\n" + " distance.z = dot(distance, distance);\n" + " distance.y = sqrt(distance.z);\n" + " distance.x = c_one;\n" "\n" - " if (spotEffect >= cos(radians(u_light[i].cutoff)))\n" - " spotEffect = pow(spotEffect, u_light[i].exponent);\n" + " attenuation = pow(spot_factor, u_light[i].exponent) / dot(distance, u_light[i].attenuation);\n" + " }\n" " else\n" - " spotEffect = 0.0;\n" + " attenuation = c_zero;\n" "\n" - " att*= spotEffect;\n" + " if (attenuation > c_zero)\n" + " {\n" + " color = u_light[i].ambient * u_material.ambient;\n" "\n" + " ndotl = max(c_zero, dot(n, lightdir));\n" + " if (u_useTexture == true)\n" + " {\n" + " color += ndotl * u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra;\n" + " }\n" + " else\n" + " {\n" + " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n" + " }\n" "\n" - " if (att > 0.0)\n" - " {\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" + " eye = normalize(u_eyePosition - v_position.xyz);\n" + " h = normalize(lightdir + eye);\n" + " ndoth = dot(n, h);\n" "\n" - " result = (ambient + diffuse + specular) * att;\n" + " if (ndoth > c_zero)\n" + " {\n" + " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light[i].specular;\n" + " }\n" "\n" - " return result;\n" + " color *= attenuation;\n" + " }\n" + "\n" + " return color;\n" "}\n" "\n" - "vec4 directionalLight(int i)\n" + "vec4\n" + "directionalLight(int i)\n" "{\n" - " vec4 result = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec4 ambient = vec4(0.0, 0.0, 0.0, 0.0);\n" - " vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);\n" - " \n" - " vec3 normal = normalize(v_normal);\n" - " vec3 lightdir = normalize(u_light[i].position.xyz);\n" - " 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" - " 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" - " result = ambient + diffuse + specular;\n" - " \n" - " return result;\n" + " vec3 lightdir, n, h, eye;\n" + " vec4 color;\n" + " float ndotl, ndoth;\n" + "\n" + " n = normalize(v_normal);\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" + " if (u_useTexture == true)\n" + " {\n" + " color += ndotl * u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra;\n" + " }\n" + " else\n" + " {\n" + " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n" + " }\n" + "\n" + " eye = normalize(u_eyePosition - v_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 main()\n" "{\n" - " vec4 res_color = vec4(0,0,0,1);\n" - " if (u_lightCount > 0 )\n" + " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n" + "\n" + " for (int i = 0; i < u_lightCount; i++)\n" " {\n" - " for (int i = 0; i < u_lightCount; i++)\n" - " {\n" - " if (u_light[i].type == 0)\n" - " res_color += pointLight(i);\n" - " else if (u_light[i].type == 1)\n" - " res_color += spotLight(i);\n" - " else if (u_light[i].type == 2)\n" - " res_color += directionalLight(i);\n" - " }\n" - " res_color += u_material.emissive;\n" - " res_color.a = u_material.diffuse.a;\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" - " gl_FragColor.rgba = res_color;\n" - "}"; - static const char strVertexShader_PerPixellight[] = - "uniform mat4 u_mvp;\n" - "uniform mat4 u_modelview;\n" - "//uniform mat3 u_inv_modelview;\n" - "uniform mat4 u_inv_modelview;\n" "\n" - "attribute vec4 a_position;\n" - "attribute vec2 a_texcoord;\n" - "attribute vec3 a_normal;\n" - "varying vec4 v_position;\n" - "varying vec2 v_texcoord;\n" - "varying vec3 v_normal;\n" + " color += u_material.emissive;\n" + "// color += u_material.emissive + u_material.ambient * u_scene_ambient; //todo\n" "\n" - "void\n" - "main()\n" - "{\n" - " v_position = u_modelview * a_position;\n" - " vec4 normal = u_inv_modelview * vec4(a_normal, 0.0);\n" - " v_normal = normal.xyz;\n" - " v_texcoord = a_texcoord.xy;\n" - " gl_Position = u_mvp * a_position;\n" - "}\n"; + " if (u_useTexture == true)\n" + " {\n" + " color.a = u_material.diffuse.a * texture2D(u_tex2d, v_texcoord).a;\n" + " }\n" + " else\n" + " {\n" + " color.a = u_material.diffuse.a;\n" + " }\n" + "\n" + " gl_FragColor = color;\n" + "}"; Shader vertexColor, fragmentColor; Shader vertexUniformColor, fragmentUniformColor; @@ -1427,7 +1506,6 @@ _GlContext::PrepareDefaultWithSkinningShaders(void) "\n" "attribute vec4 a_position;\n" "attribute vec4 a_texcoord;\n" - "attribute vec4 a_color;\n" "attribute vec3 a_normal;\n" "\n" "varying vec2 v_texcoord;\n" @@ -1532,6 +1610,7 @@ _GlContext::PrepareDefaultWithSkinningShaders(void) "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" "}\n"; Shader vertexColor, fragmentColor;