From 50bb40be29b9eebce55b794ecac5513c0d80164a Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 28 Jan 2014 12:31:27 +0900 Subject: [PATCH] Use the new shader system for sprites. --- libs/video/renderer/glsl/Makefile.am | 6 +-- libs/video/renderer/glsl/glsl_sprite.c | 25 ++++++--- libs/video/renderer/glsl/quakeforge.glsl | 69 ++++++++++++++++++++++++ libs/video/renderer/glsl/quakespr.frag | 43 --------------- libs/video/renderer/glsl/quakespr.vert | 20 ------- 5 files changed, 88 insertions(+), 75 deletions(-) delete mode 100644 libs/video/renderer/glsl/quakespr.frag delete mode 100644 libs/video/renderer/glsl/quakespr.vert diff --git a/libs/video/renderer/glsl/Makefile.am b/libs/video/renderer/glsl/Makefile.am index 34108e9d4..1daa73c78 100644 --- a/libs/video/renderer/glsl/Makefile.am +++ b/libs/video/renderer/glsl/Makefile.am @@ -5,12 +5,10 @@ AM_CPPFLAGS= -I$(top_srcdir)/include $(GLX_CFLAGS) shader_src= quakeforge.glsl \ iqm.frag iqm.vert \ - quake2d.frag quakeico.vert \ - quakespr.frag quakespr.vert + quake2d.frag quakeico.vert shader_gen= quakeforge.slc \ iqm.fc iqm.vc \ - quake2d.fc quakeico.vc \ - quakespr.fc quakespr.vc + quake2d.fc quakeico.vc glsl_src = \ glsl_alias.c glsl_bsp.c glsl_draw.c glsl_fog.c glsl_iqm.c glsl_lightmap.c \ diff --git a/libs/video/renderer/glsl/glsl_sprite.c b/libs/video/renderer/glsl/glsl_sprite.c index ab96b137e..6603a7e69 100644 --- a/libs/video/renderer/glsl/glsl_sprite.c +++ b/libs/video/renderer/glsl/glsl_sprite.c @@ -55,13 +55,17 @@ #include "r_internal.h" -static const char quakesprite_vert[] = -#include "quakespr.vc" -; +static const char *sprite_vert_effects[] = +{ + "QuakeForge.Vertex.sprite", + 0 +}; -static const char quakesprite_frag[] = -#include "quakespr.fc" -; +static const char *sprite_frag_effects[] = +{ + "QuakeForge.Fragment.sprite", + 0 +}; //static float proj_matrix[16]; @@ -96,11 +100,14 @@ static struct { void glsl_R_InitSprites (void) { + shader_t *vert_shader, *frag_shader; int frag, vert; - vert = GLSL_CompileShaderS ("quakespr.vert", quakesprite_vert, + vert_shader = GLSL_BuildShader (sprite_vert_effects); + frag_shader = GLSL_BuildShader (sprite_frag_effects); + vert = GLSL_CompileShader ("quakespr.vert", vert_shader, GL_VERTEX_SHADER); - frag = GLSL_CompileShaderS ("quakespr.frag", quakesprite_frag, + frag = GLSL_CompileShader ("quakespr.frag", frag_shader, GL_FRAGMENT_SHADER); quake_sprite.program = GLSL_LinkProgram ("quakespr", vert, frag); GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.spritea); @@ -114,6 +121,8 @@ glsl_R_InitSprites (void) GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.uvab); GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.blend); GLSL_ResolveShaderParam (quake_sprite.program, &quake_sprite.fog); + GLSL_FreeShader (vert_shader); + GLSL_FreeShader (frag_shader); } static void diff --git a/libs/video/renderer/glsl/quakeforge.glsl b/libs/video/renderer/glsl/quakeforge.glsl index 097ef89ed..fc4954252 100644 --- a/libs/video/renderer/glsl/quakeforge.glsl +++ b/libs/video/renderer/glsl/quakeforge.glsl @@ -365,3 +365,72 @@ main (void) { gl_FragColor = fogBlend (texture2D (texture, st) * color); } + +-- Vertex.sprite + +uniform mat4 mvp_mat; +attribute float vblend; +attribute vec4 vcolora, vcolorb; +attribute vec4 uvab; ///< ua va ub vb +attribute vec3 vertexa, vertexb; + +varying float blend; +varying vec4 colora, colorb; +varying vec2 sta, stb; + +void +main (void) +{ + gl_Position = mvp_mat * vec4 (mix (vertexa, vertexb, vblend), 1.0); + blend = vblend; + colora = vcolora; + colorb = vcolorb; + sta = uvab.xy; + stb = uvab.zw; +} + +-- Fragment.sprite + +uniform sampler2D spritea; +uniform sampler2D spriteb; +uniform sampler2D palette; +uniform vec4 fog; + +varying float blend; +varying vec4 colora, colorb; +varying vec2 sta, stb; + +float +sqr (float x) +{ + return x * x; +} + +vec4 +fogBlend (vec4 color) +{ + float f; + vec4 fog_color = vec4 (fog.rgb, 1.0); + + f = exp (-sqr (fog.a * gl_FragCoord.z / gl_FragCoord.w)); + return vec4 (mix (fog_color.rgb, color.rgb, f), color.a); +} + +void +main (void) +{ + float pixa, pixb; + vec4 cola, colb; + vec4 col; + + pixa = texture2D (spritea, sta).r; + pixb = texture2D (spriteb, stb).r; + if (pixa == 1.0 && pixb == 1.0) + discard; + cola = texture2D (palette, vec2 (pixa, 0.0)) * colora; + colb = texture2D (palette, vec2 (pixb, 0.0)) * colorb; + col = mix (cola, colb, blend); + if (col.a == 0.0) + discard; + gl_FragColor = fogBlend (col); +} diff --git a/libs/video/renderer/glsl/quakespr.frag b/libs/video/renderer/glsl/quakespr.frag deleted file mode 100644 index fd20a4fa4..000000000 --- a/libs/video/renderer/glsl/quakespr.frag +++ /dev/null @@ -1,43 +0,0 @@ -uniform sampler2D spritea; -uniform sampler2D spriteb; -uniform sampler2D palette; -uniform vec4 fog; - -varying float blend; -varying vec4 colora, colorb; -varying vec2 sta, stb; - -float -sqr (float x) -{ - return x * x; -} - -vec4 -fogBlend (vec4 color) -{ - float f; - vec4 fog_color = vec4 (fog.rgb, 1.0); - - f = exp (-sqr (fog.a * gl_FragCoord.z / gl_FragCoord.w)); - return vec4 (mix (fog_color.rgb, color.rgb, f), color.a); -} - -void -main (void) -{ - float pixa, pixb; - vec4 cola, colb; - vec4 col; - - pixa = texture2D (spritea, sta).r; - pixb = texture2D (spriteb, stb).r; - if (pixa == 1.0 && pixb == 1.0) - discard; - cola = texture2D (palette, vec2 (pixa, 0.0)) * colora; - colb = texture2D (palette, vec2 (pixb, 0.0)) * colorb; - col = mix (cola, colb, blend); - if (col.a == 0.0) - discard; - gl_FragColor = fogBlend (col); -} diff --git a/libs/video/renderer/glsl/quakespr.vert b/libs/video/renderer/glsl/quakespr.vert deleted file mode 100644 index c248d292c..000000000 --- a/libs/video/renderer/glsl/quakespr.vert +++ /dev/null @@ -1,20 +0,0 @@ -uniform mat4 mvp_mat; -attribute float vblend; -attribute vec4 vcolora, vcolorb; -attribute vec4 uvab; ///< ua va ub vb -attribute vec3 vertexa, vertexb; - -varying float blend; -varying vec4 colora, colorb; -varying vec2 sta, stb; - -void -main (void) -{ - gl_Position = mvp_mat * vec4 (mix (vertexa, vertexb, vblend), 1.0); - blend = vblend; - colora = vcolora; - colorb = vcolorb; - sta = uvab.xy; - stb = uvab.zw; -}