Index: ps/trunk/binaries/data/mods/mod/shaders/arb/canvas2d.fp =================================================================== --- ps/trunk/binaries/data/mods/mod/shaders/arb/canvas2d.fp +++ ps/trunk/binaries/data/mods/mod/shaders/arb/canvas2d.fp @@ -3,11 +3,23 @@ TEMP colorTex; TEX colorTex, fragment.texcoord[0], texture[0], 2D; +TEMP grayscale; +MOV grayscale.r, 0.3; +MOV grayscale.g, 0.59; +MOV grayscale.b, 0.11; +MOV grayscale.a, 0.0; + PARAM colorAdd = program.local[1]; PARAM colorMul = program.local[2]; +PARAM grayscaleFactor = program.local[3]; + +TEMP colorGray; +DP3 colorGray.rgb, colorTex, grayscale; +MOV colorGray.a, colorTex.a; TEMP color; -MUL color, colorTex, colorMul; +LRP color, grayscaleFactor, colorGray, colorTex; +MUL color, color, colorMul; ADD color, color, colorAdd; MOV result.color, color; Index: ps/trunk/binaries/data/mods/mod/shaders/arb/canvas2d.xml =================================================================== --- ps/trunk/binaries/data/mods/mod/shaders/arb/canvas2d.xml +++ ps/trunk/binaries/data/mods/mod/shaders/arb/canvas2d.xml @@ -11,6 +11,7 @@ + Index: ps/trunk/binaries/data/mods/mod/shaders/glsl/canvas2d.fs =================================================================== --- ps/trunk/binaries/data/mods/mod/shaders/glsl/canvas2d.fs +++ ps/trunk/binaries/data/mods/mod/shaders/glsl/canvas2d.fs @@ -1,12 +1,15 @@ #version 110 +uniform sampler2D tex; uniform vec4 colorAdd; uniform vec4 colorMul; -uniform sampler2D tex; +uniform float grayscaleFactor; varying vec2 v_uv; void main() { - gl_FragColor = clamp(texture2D(tex, v_uv) * colorMul + colorAdd, 0.0, 1.0); + vec4 colorTex = texture2D(tex, v_uv); + vec3 grayColor = vec3(dot(vec3(0.3, 0.59, 0.11), colorTex.rgb)); + gl_FragColor = clamp(mix(colorTex, vec4(grayColor, colorTex.a), grayscaleFactor) * colorMul + colorAdd, 0.0, 1.0); } Index: ps/trunk/source/graphics/Canvas2D.h =================================================================== --- ps/trunk/source/graphics/Canvas2D.h +++ ps/trunk/source/graphics/Canvas2D.h @@ -48,10 +48,12 @@ * Draws a piece of the texture from the source rect into the * destination rect. The result color is set by the following formula: * TEXTURE_COLOR * COLOR_MULTIPLY + COLOR_ADD + * The texture color is blended with its own grayscale version according to + * the grayscale factor. */ void DrawTexture(CTexturePtr texture, const CRect& destination, const CRect& source, - const CColor& multiply, const CColor& add); + const CColor& multiply, const CColor& add, const float grayscaleFactor); /** * A simpler version of the previous one, draws the texture into the Index: ps/trunk/source/graphics/Canvas2D.cpp =================================================================== --- ps/trunk/source/graphics/Canvas2D.cpp +++ ps/trunk/source/graphics/Canvas2D.cpp @@ -38,7 +38,7 @@ inline void DrawTextureImpl(CTexturePtr texture, const PlaneArray2D& vertices, PlaneArray2D uvs, - const CColor& multiply, const CColor& add) + const CColor& multiply, const CColor& add, const float grayscaleFactor) { CShaderDefines defines; CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect( @@ -58,6 +58,7 @@ shader->Uniform(str_transform, GetDefaultGuiMatrix()); shader->Uniform(str_colorAdd, add); shader->Uniform(str_colorMul, multiply); + shader->Uniform(str_grayscaleFactor, grayscaleFactor); shader->VertexPointer(2, GL_FLOAT, 0, vertices.data()); shader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, 0, uvs.data()); shader->AssertPointersBound(); @@ -121,19 +122,19 @@ DrawTextureImpl( g_Renderer.GetTextureManager().GetTransparentTexture(), - vertices, uvs, CColor(0.0f, 0.0f, 0.0f, 0.0f), color); + vertices, uvs, CColor(0.0f, 0.0f, 0.0f, 0.0f), color, 0.0f); } void CCanvas2D::DrawTexture(CTexturePtr texture, const CRect& destination) { DrawTexture(texture, destination, CRect(0, 0, texture->GetWidth(), texture->GetHeight()), - CColor(1.0f, 1.0f, 1.0f, 1.0f), CColor(0.0f, 0.0f, 0.0f, 0.0f)); + CColor(1.0f, 1.0f, 1.0f, 1.0f), CColor(0.0f, 0.0f, 0.0f, 0.0f), 0.0f); } void CCanvas2D::DrawTexture( CTexturePtr texture, const CRect& destination, const CRect& source, - const CColor& multiply, const CColor& add) + const CColor& multiply, const CColor& add, const float grayscaleFactor) { const PlaneArray2D uvs = { source.left, source.bottom, @@ -148,5 +149,5 @@ destination.left, destination.top }; - DrawTextureImpl(texture, vertices, uvs, multiply, add); + DrawTextureImpl(texture, vertices, uvs, multiply, add, grayscaleFactor); } Index: ps/trunk/source/gui/GUIRenderer.h =================================================================== --- ps/trunk/source/gui/GUIRenderer.h +++ ps/trunk/source/gui/GUIRenderer.h @@ -44,23 +44,16 @@ const SGUIImage* m_Image; - bool m_HasTexture; CTexturePtr m_Texture; CRect m_ObjectSize; - - CShaderTechniquePtr m_Shader; - CColor m_ShaderColorParameter; - CRect m_Vertices; CGUIColor* m_BackColor; - // Temporary type to make a soft transition to canvas rendering. - CStrIntern m_Material; - CColor m_ColorAdd; CColor m_ColorMultiply; + float m_GrayscaleFactor; }; class DrawCalls : public std::vector Index: ps/trunk/source/gui/GUIRenderer.cpp =================================================================== --- ps/trunk/source/gui/GUIRenderer.cpp +++ ps/trunk/source/gui/GUIRenderer.cpp @@ -20,7 +20,6 @@ #include "GUIRenderer.h" #include "graphics/Canvas2D.h" -#include "graphics/ShaderManager.h" #include "graphics/TextureManager.h" #include "gui/CGUI.h" #include "gui/CGUISprite.h" @@ -206,25 +205,22 @@ Call.m_Vertices.bottom = (int)(Call.m_Vertices.bottom + 0.5f); } + bool hasTexture = false; if (!(*cit)->m_TextureName.empty()) { CTextureProperties textureProps(g_L10n.LocalizePath((*cit)->m_TextureName)); textureProps.SetWrap((*cit)->m_WrapMode); CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps); texture->Prefetch(); - Call.m_HasTexture = true; + hasTexture = true; Call.m_Texture = texture; Call.m_ObjectSize = ObjectSize; } - else - { - Call.m_HasTexture = false; - } Call.m_BackColor = &(*cit)->m_BackColor; - if (!Call.m_HasTexture) + Call.m_GrayscaleFactor = 0.0f; + if (!hasTexture) { - Call.m_Material = str_gui_solid; Call.m_ColorAdd = *Call.m_BackColor; Call.m_ColorMultiply = CColor(0.0f, 0.0f, 0.0f, 0.0f); Call.m_Texture = g_Renderer.GetTextureManager().GetTransparentTexture(); @@ -233,33 +229,30 @@ { if ((*cit)->m_Effects->m_AddColor != CGUIColor()) { - Call.m_Material = str_gui_add; const CColor color = (*cit)->m_Effects->m_AddColor; Call.m_ColorAdd = CColor(color.r, color.g, color.b, 0.0f); Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f); } else if ((*cit)->m_Effects->m_Greyscale) { - Call.m_Shader = g_Renderer.GetShaderManager().LoadEffect(str_gui_grayscale); - Call.m_Material = str_gui_grayscale; + Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f); + Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f); + Call.m_GrayscaleFactor = 1.0f; } else if ((*cit)->m_Effects->m_SolidColor != CGUIColor()) { - Call.m_Material = str_gui_solid_mask; const CColor color = (*cit)->m_Effects->m_SolidColor; Call.m_ColorAdd = CColor(color.r, color.g, color.b, 0.0f); Call.m_ColorMultiply = CColor(0.0f, 0.0f, 0.0f, color.a); } else /* Slight confusion - why no effects? */ { - Call.m_Material = str_gui_basic; Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f); Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f); } } else { - Call.m_Material = str_gui_basic; Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f); Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f); } @@ -334,91 +327,34 @@ // TODO: batching by shader/texture/etc would be nice - CMatrix3D matrix = GetDefaultGuiMatrix(); - glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Iterate through each DrawCall, and execute whatever drawing code is being called for (DrawCalls::const_iterator cit = Calls.begin(); cit != Calls.end(); ++cit) { - if (cit->m_HasTexture && (cit->m_Material == str_gui_basic || cit->m_Material == str_gui_solid_mask || cit->m_Material == str_gui_add)) - { - // A hack to preload the handle to get a correct texture size. - GLuint h; - ogl_tex_get_texture_id(cit->m_Texture->GetHandle(), &h); - - CRect texCoords = cit->ComputeTexCoords().Scale( - cit->m_Texture->GetWidth(), cit->m_Texture->GetHeight()); - - // Ensure the quad has the correct winding order - CRect rect = cit->m_Vertices; - if (rect.right < rect.left) - { - std::swap(rect.right, rect.left); - std::swap(texCoords.right, texCoords.left); - } - if (rect.bottom < rect.top) - { - std::swap(rect.bottom, rect.top); - std::swap(texCoords.bottom, texCoords.top); - } + // A hack to preload the handle to get a correct texture size. + GLuint h; + ogl_tex_get_texture_id(cit->m_Texture->GetHandle(), &h); - canvas.DrawTexture(cit->m_Texture, - rect, texCoords, cit->m_ColorMultiply, cit->m_ColorAdd); - } - else if (cit->m_HasTexture) - { - cit->m_Shader->BeginPass(); - CShaderProgramPtr shader = cit->m_Shader->GetShader(); - shader->Uniform(str_transform, matrix); - shader->Uniform(str_color, cit->m_ShaderColorParameter); - shader->BindTexture(str_tex, cit->m_Texture); - - CRect TexCoords = cit->ComputeTexCoords(); - - // Ensure the quad has the correct winding order, and update texcoords to match - CRect Verts = cit->m_Vertices; - if (Verts.right < Verts.left) - { - std::swap(Verts.right, Verts.left); - std::swap(TexCoords.right, TexCoords.left); - } - if (Verts.bottom < Verts.top) - { - std::swap(Verts.bottom, Verts.top); - std::swap(TexCoords.bottom, TexCoords.top); - } - - std::vector data; -#define ADD(u, v, x, y, z) STMT(data.push_back(u); data.push_back(v); data.push_back(x); data.push_back(y); data.push_back(z)) - ADD(TexCoords.left, TexCoords.bottom, Verts.left, Verts.bottom, 0.0f); - ADD(TexCoords.right, TexCoords.bottom, Verts.right, Verts.bottom, 0.0f); - ADD(TexCoords.right, TexCoords.top, Verts.right, Verts.top, 0.0f); - - ADD(TexCoords.right, TexCoords.top, Verts.right, Verts.top, 0.0f); - ADD(TexCoords.left, TexCoords.top, Verts.left, Verts.top, 0.0f); - ADD(TexCoords.left, TexCoords.bottom, Verts.left, Verts.bottom, 0.0f); -#undef ADD - - shader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, 5*sizeof(float), &data[0]); - shader->VertexPointer(3, GL_FLOAT, 5*sizeof(float), &data[2]); - glDrawArrays(GL_TRIANGLES, 0, 6); + CRect texCoords = cit->ComputeTexCoords().Scale( + cit->m_Texture->GetWidth(), cit->m_Texture->GetHeight()); - cit->m_Shader->EndPass(); + // Ensure the quad has the correct winding order + CRect rect = cit->m_Vertices; + if (rect.right < rect.left) + { + std::swap(rect.right, rect.left); + std::swap(texCoords.right, texCoords.left); } - else + if (rect.bottom < rect.top) { - // Ensure the quad has the correct winding order - CRect rect = cit->m_Vertices; - if (rect.right < rect.left) - std::swap(rect.right, rect.left); - if (rect.bottom < rect.top) - std::swap(rect.bottom, rect.top); - canvas.DrawTexture(cit->m_Texture, - rect, CRect(0, 0, cit->m_Texture->GetWidth(), cit->m_Texture->GetHeight()), - cit->m_ColorMultiply, cit->m_ColorAdd); + std::swap(rect.bottom, rect.top); + std::swap(texCoords.bottom, texCoords.top); } + + canvas.DrawTexture(cit->m_Texture, + rect, texCoords, cit->m_ColorMultiply, cit->m_ColorAdd, cit->m_GrayscaleFactor); } glDisable(GL_BLEND); Index: ps/trunk/source/ps/CStrInternStatic.h =================================================================== --- ps/trunk/source/ps/CStrInternStatic.h +++ ps/trunk/source/ps/CStrInternStatic.h @@ -102,6 +102,7 @@ X(fogColor) X(fogParams) X(foreground_overlay) +X(grayscaleFactor) X(gui_add) X(gui_basic) X(gui_grayscale)