From eef5e2e3e382891aa659df01b30b2152105ba0c8 Mon Sep 17 00:00:00 2001
From: DarkGod <darkgod@net-core.org>
Date: Sun, 14 Jul 2013 17:28:31 +0200
Subject: [PATCH] text outline test

---
 game/engines/default/engine/LogDisplay.lua    |  7 ++--
 .../tome/data/gfx/shaders/textoutline.frag    | 32 +++++++++++++++++--
 .../tome/data/gfx/shaders/textoutline.lua     |  4 ++-
 src/core_lua.c                                | 14 ++++----
 4 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/game/engines/default/engine/LogDisplay.lua b/game/engines/default/engine/LogDisplay.lua
index 051920bbb3..75fc2407fa 100644
--- a/game/engines/default/engine/LogDisplay.lua
+++ b/game/engines/default/engine/LogDisplay.lua
@@ -26,8 +26,6 @@ local Slider = require "engine.ui.Slider"
 --- Module that handles message history in a mouse wheel scrollable zone
 module(..., package.seeall, class.inherit(engine.ui.Base))
 
-local shader = Shader.new("textoutline")
-
 --- Creates the log zone
 function _M:init(x, y, w, h, max, fontname, fontsize, color, bgcolor)
 	self.color = color or {255,255,255}
@@ -57,7 +55,7 @@ function _M:enableShadow(v)
 end
 
 function _M:enableFading(v)
---	self.fading = v
+	self.fading = v
 end
 
 --- Resize the display area
@@ -225,6 +223,7 @@ function _M:display()
 	return
 end
 
+local shader = Shader.new("textoutline")
 function _M:toScreen()
 	self:display()
 
@@ -248,8 +247,8 @@ function _M:toScreen()
 		self.dlist[i].dh = h
 		if self.shadow then
 			if shader.shad then
-				shader.shad:use(true)
 				shader.shad:paramNumber2("textSize", item._tex_w, item._tex_h)
+				shader.shad:use(true)
 			else
 				item._tex:toScreenFull(self.display_x+2, h+2, item.w, item.h, item._tex_w, item._tex_h, 0,0,0, self.shadow * fade)
 			end
diff --git a/game/modules/tome/data/gfx/shaders/textoutline.frag b/game/modules/tome/data/gfx/shaders/textoutline.frag
index 5caabdff38..865f8473bd 100644
--- a/game/modules/tome/data/gfx/shaders/textoutline.frag
+++ b/game/modules/tome/data/gfx/shaders/textoutline.frag
@@ -1,7 +1,35 @@
 uniform sampler2D tex;
+uniform vec2 textSize;
+uniform float intensity; 
+uniform vec2 outlineSize;
 uniform vec4 outlineColor;
 
 void main(void)
 {
-	gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);
-}
\ No newline at end of file
+	float xOffset = outlineSize.x / textSize.x;
+	float yOffset = outlineSize.y / textSize.y;
+
+	gl_FragColor = texture2D(tex, gl_TexCoord[0].xy); //default text color
+
+	//sobel distance to glyph base line approximation
+	vec4 col02 = texture2D(tex, gl_TexCoord[0].xy + vec2(xOffset,  -yOffset));
+	vec4 col12 = texture2D(tex, gl_TexCoord[0].xy + vec2(xOffset,   0.0     ));
+	vec4 col22 = texture2D(tex, gl_TexCoord[0].xy + vec2(xOffset,   yOffset));
+	vec4 col01 = texture2D(tex, gl_TexCoord[0].xy + vec2(0.0,        -yOffset));
+	vec4 col21 = texture2D(tex, gl_TexCoord[0].xy + vec2(0.0,         yOffset));
+	vec4 col00 = texture2D(tex, gl_TexCoord[0].xy + vec2(-xOffset, -yOffset));
+	vec4 col10 = texture2D(tex, gl_TexCoord[0].xy + vec2(-xOffset,  0.0     ));
+	vec4 col20 = texture2D(tex, gl_TexCoord[0].xy + vec2(-xOffset,  yOffset));
+	
+	
+	float xDist = 0.0 - col00.a - col01.a * 2.0 - col02.a + col20.a + col21.a * 2.0 + col22.a;
+	float yDist = 0.0 - col00.a + col02.a - col10.a * 2.0 + col12.a * 2.0 - col20.a + col22.a;
+	
+	//transperency of current outline pixel
+	float alpha = min(1.0, sqrt(xDist * xDist + yDist * yDist) * intensity); 
+	
+	//outlineColor = vec4(0, 0, 0, 1);
+	//blending character glyph over its outline
+	gl_FragColor = gl_FragColor * gl_FragColor.a + outlineColor * alpha * (1.0 - gl_FragColor.a);
+	gl_FragColor.a *= gl_Color.a;
+}
diff --git a/game/modules/tome/data/gfx/shaders/textoutline.lua b/game/modules/tome/data/gfx/shaders/textoutline.lua
index ed692199b2..2becfca846 100644
--- a/game/modules/tome/data/gfx/shaders/textoutline.lua
+++ b/game/modules/tome/data/gfx/shaders/textoutline.lua
@@ -23,7 +23,9 @@ return {
 	args = {
 		tex = { texture = 0 },
 		textSize = textSize or {1, 1},
-		outlineColor = outlineColor or {1,0,0,0.8},
+		intensity = intensity or 0.5,
+		outlineSize = outlineSize or {1.5, 1.5},
+		outlineColor = outlineColor or {0, 0, 0, 1},
 	},
 	clone = false,
 }
diff --git a/src/core_lua.c b/src/core_lua.c
index a45417c442..ba48e80993 100644
--- a/src/core_lua.c
+++ b/src/core_lua.c
@@ -264,10 +264,10 @@ static GLenum sdl_gl_texture_format(SDL_Surface *s) {
 
 // allocate memory for a texture without copying pixels in
 // caller binds texture
-void make_texture_for_surface(SDL_Surface *s, int *fw, int *fh) {
+void make_texture_for_surface(SDL_Surface *s, int *fw, int *fh, bool clamp) {
 	// Paramétrage de la texture.
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
 	// get the number of channels in the SDL surface
@@ -764,7 +764,7 @@ static font_make_texture_line(lua_State *L, SDL_Surface *s, int id, bool is_sepa
 	glGenTextures(1, t);
 	tfglBindTexture(GL_TEXTURE_2D, *t);
 	int fw, fh;
-	make_texture_for_surface(s, &fw, &fh);
+	make_texture_for_surface(s, &fw, &fh, true);
 	copy_surface_to_texture(s);
 
 	lua_pushliteral(L, "_tex_w");
@@ -1209,7 +1209,7 @@ int init_blank_surface()
 	glGenTextures(1, &gl_tex_white);
 	tfglBindTexture(GL_TEXTURE_2D, gl_tex_white);
 	int fw, fh;
-	make_texture_for_surface(s, &fw, &fh);
+	make_texture_for_surface(s, &fw, &fh, false);
 	copy_surface_to_texture(s);
 	return gl_tex_white;
 }
@@ -1516,7 +1516,7 @@ static int sdl_surface_toscreen(lua_State *L)
 	glGenTextures(1, &t);
 	tfglBindTexture(GL_TEXTURE_2D, t);
 
-	make_texture_for_surface(*s, NULL, NULL);
+	make_texture_for_surface(*s, NULL, NULL, false);
 	copy_surface_to_texture(*s);
 	draw_textured_quad(x,y,(*s)->w,(*s)->h);
 
@@ -1583,7 +1583,7 @@ static int sdl_surface_to_texture(lua_State *L)
 	tfglBindTexture(GL_TEXTURE_2D, *t);
 
 	int fw, fh;
-	make_texture_for_surface(*s, &fw, &fh);
+	make_texture_for_surface(*s, &fw, &fh, false);
 	copy_surface_to_texture(*s);
 
 	lua_pushnumber(L, fw);
-- 
GitLab