From afd23ff7974a9013b726a47abb822a501ae3b6cf Mon Sep 17 00:00:00 2001 From: darkrose Date: Sat, 5 Mar 2016 00:31:29 +1000 Subject: [PATCH] add pseudo signed distance field rendering --- data/shaders/model_fragment.glsl | 35 +++++++++++++++++++++++++---------- data/shaders/model_vertex.glsl | 4 ++-- data/shaders/ui_fragment.glsl | 20 +++++++++++++++++--- inc/content.h | 3 --- inc/graphics.h | 11 +++++++---- inc/wm.h | 1 + src/client/main.c | 2 +- src/content/content.c | 1 - src/content/content_block.c | 3 --- src/core/config_default.c | 1 + src/graphics/font_ttf.c | 16 ++++------------ src/graphics/material.c | 20 +++++++++++++++----- src/graphics/opengl.c | 16 +++++++++++++++- src/graphics/render2d.c | 2 +- src/server/dummy_commands.c | 4 ++++ 15 files changed, 93 insertions(+), 46 deletions(-) diff --git a/data/shaders/model_fragment.glsl b/data/shaders/model_fragment.glsl index bbb2626..b4222e4 100644 --- a/data/shaders/model_fragment.glsl +++ b/data/shaders/model_fragment.glsl @@ -1,7 +1,7 @@ #version 330 core in vec3 colour; -in vec2 textureCoords; +in vec2 uv; in vec3 surfaceNormal; in vec3 toLight; @@ -13,6 +13,7 @@ uniform sampler2D tex; uniform float shininess; uniform float reflectivity; uniform float bumpiness; +uniform float alphafunc; float intensity(vec3 color) { @@ -38,14 +39,14 @@ void main(void) float bump_factor = 1.0; if (bumpiness > 0.05) { - float tl = get_rgb_height(vec2(textureCoords.x - 0.0078125, textureCoords.y + 0.0078125)); - float t = get_rgb_height(vec2(textureCoords.x - 0.0078125, textureCoords.y - 0.0078125)); - float tr = get_rgb_height(vec2(textureCoords.x + 0.0078125, textureCoords.y + 0.0078125)); - float r = get_rgb_height(vec2(textureCoords.x + 0.0078125, textureCoords.y)); - float br = get_rgb_height(vec2(textureCoords.x + 0.0078125, textureCoords.y - 0.0078125)); - float b = get_rgb_height(vec2(textureCoords.x, textureCoords.y - 0.0078125)); - float bl = get_rgb_height(vec2(textureCoords.x -0.0078125, textureCoords.y - 0.0078125)); - float l = get_rgb_height(vec2(textureCoords.x - 0.0078125, textureCoords.y)); + float tl = get_rgb_height(vec2(uv.x - 0.0078125, uv.y + 0.0078125)); + float t = get_rgb_height(vec2(uv.x - 0.0078125, uv.y - 0.0078125)); + float tr = get_rgb_height(vec2(uv.x + 0.0078125, uv.y + 0.0078125)); + float r = get_rgb_height(vec2(uv.x + 0.0078125, uv.y)); + float br = get_rgb_height(vec2(uv.x + 0.0078125, uv.y - 0.0078125)); + float b = get_rgb_height(vec2(uv.x, uv.y - 0.0078125)); + float bl = get_rgb_height(vec2(uv.x -0.0078125, uv.y - 0.0078125)); + float l = get_rgb_height(vec2(uv.x - 0.0078125, uv.y)); float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl); float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr); vec3 bump = normalize(vec3(dX, dY, 1.0-bumpiness)); @@ -55,8 +56,22 @@ void main(void) bump_factor = (b_diff + 0.1 * b_spec); } - vec4 texdata = texture(tex,textureCoords); + vec4 texdata = texture(tex,uv); out_Colour = (vec4(diffuse,1.0)*texdata+vec4(specular,texdata.a+intensity(specular)))*bump_factor; + if (alphafunc > 1.5) { + float a0 = out_Colour.a; + float a1 = texture(tex,uv+vec2(-0.001,-0.001)).a; + float a2 = texture(tex,uv+vec2(-0.001,0.0)).a; + float a3 = texture(tex,uv+vec2(-0.001,0.001)).a; + float a4 = texture(tex,uv+vec2(0.0,-0.001)).a; + float a5 = texture(tex,uv+vec2(0.0,0.001)).a; + float a6 = texture(tex,uv+vec2(0.001,-0.001)).a; + float a7 = texture(tex,uv+vec2(0.001,0.0)).a; + float a8 = texture(tex,uv+vec2(0.001,0.001)).a; + out_Colour.a = smoothstep(0.45,0.55,(a0+a1+a2+a3+a4+a5+a6+a7+a8)/9.0); + }else if (alphafunc > 0.5) { + out_Colour.a = smoothstep(0.45,0.55,out_Colour.a); + } } diff --git a/data/shaders/model_vertex.glsl b/data/shaders/model_vertex.glsl index 6ffd180..015b8cd 100644 --- a/data/shaders/model_vertex.glsl +++ b/data/shaders/model_vertex.glsl @@ -4,7 +4,7 @@ in vec3 position; in vec3 normals; in vec2 texcoords; -out vec2 textureCoords; +out vec2 uv; out vec3 surfaceNormal; out vec3 toLight; @@ -21,7 +21,7 @@ void main(void) { gl_Position = projectionMatrix*viewMatrix*worldpos; - textureCoords = texcoords; + uv = texcoords; surfaceNormal = (transformationMatrix*vec4(normals,0.0)).xyz; toLight = lightpos - worldpos.xyz; diff --git a/data/shaders/ui_fragment.glsl b/data/shaders/ui_fragment.glsl index 5778706..0dc19e5 100644 --- a/data/shaders/ui_fragment.glsl +++ b/data/shaders/ui_fragment.glsl @@ -2,12 +2,26 @@ in vec2 uv; -out vec4 out_Color; +out vec4 out_Colour; uniform sampler2D tex; +uniform float alphafunc; void main(void) { - out_Color = texture(tex,uv); - + out_Colour = texture(tex,uv); + if (alphafunc > 1.5) { + float a0 = out_Colour.a; + float a1 = texture(tex,uv+vec2(-0.001,-0.001)).a; + float a2 = texture(tex,uv+vec2(-0.001,0.0)).a; + float a3 = texture(tex,uv+vec2(-0.001,0.001)).a; + float a4 = texture(tex,uv+vec2(0.0,-0.001)).a; + float a5 = texture(tex,uv+vec2(0.0,0.001)).a; + float a6 = texture(tex,uv+vec2(0.001,-0.001)).a; + float a7 = texture(tex,uv+vec2(0.001,0.0)).a; + float a8 = texture(tex,uv+vec2(0.001,0.001)).a; + out_Colour.a = smoothstep(0.45,0.55,(a0+a1+a2+a3+a4+a5+a6+a7+a8)/9.0); + }else if (alphafunc > 0.5) { + out_Colour.a = smoothstep(0.45,0.55,out_Colour.a); + } } diff --git a/inc/content.h b/inc/content.h index 8424efa..2fd5391 100644 --- a/inc/content.h +++ b/inc/content.h @@ -450,9 +450,6 @@ typedef struct contentfeatures_s { array_t collision_boxes; uint16_t collision_info; - /* if an item has a nodebox style wield, use this */ - array_t wield_boxes; - /* same info about how light reacts on this */ uint8_t light_data; uint8_t light_source; diff --git a/inc/graphics.h b/inc/graphics.h index 094ce93..ebaf6cb 100644 --- a/inc/graphics.h +++ b/inc/graphics.h @@ -30,10 +30,12 @@ #define MATOPT_NONE 0x00 #define MATOPT_GET 0x01 -#define MATOPT_BFCULL 0x02 -#define MATOPT_UPNORMAL 0x04 -#define MATOPT_ALPHA_BLEND 0x08 -#define MATOPT_ADDITIVE_BLEND 0x10 +#define MATOPT_BFCULL 0x02 /* back face culling */ +#define MATOPT_UPNORMAL 0x04 /* forces normals to up vector */ +#define MATOPT_ALPHA_BLEND 0x08 /* use alpha blending GL_ONE_MINUS_SRC_ALPHA */ +#define MATOPT_ADDITIVE_BLEND 0x10 /* use additive blending GL_ONE */ +#define MATOPT_ALPHA_TEST 0x20 /* semi-caps alpha values (signed distance field) */ +#define MATOPT_SDF_ALPHA 0x40 /* pseudo signed distance field */ #define RD2_NONE 0x00 #define RD2_LINE 0x01 @@ -373,6 +375,7 @@ int opengl_has_trilinear(void); int opengl_has_mipmap(void); int opengl_has_particles(void); int opengl_particles_max(void); +int opengl_has_psdf(void); char* opengl_error_string(GLenum e); /* defined in mesh.c */ diff --git a/inc/wm.h b/inc/wm.h index 3ca2f31..c9b10eb 100644 --- a/inc/wm.h +++ b/inc/wm.h @@ -261,6 +261,7 @@ int opengl_mipmap_setter(char* value); int opengl_particles_setter(char* value); int opengl_particles_max_setter(char* value); int opengl_bumpmap_setter(char* value); +int opengl_psdf_setter(char* value); /* defined in events.c and dummy.c */ void events_save(file_t *f); diff --git a/src/client/main.c b/src/client/main.c index a492d84..2edc19c 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -74,7 +74,7 @@ int main(int argc, char** argv) f = font_find("default"); textbuffer_init(&tb,f,14,20,20,0,0,0,0); - textbuffer_init(&tb2,f,14,20,40,0,0,0,0); + textbuffer_init(&tb2,f,40,20,40,0,0,0,0); textbuffer_addstr(&tb,"Hello world! - ößäндгя ygj"); glasscolour.r = 255; diff --git a/src/content/content.c b/src/content/content.c index f6f240d..47c6a6d 100644 --- a/src/content/content.c +++ b/src/content/content.c @@ -42,7 +42,6 @@ void content_defaults(contentfeatures_t *f) b = content_box(NULL,-0.5,-0.5,-0.5,0.5,0.5,0.5); array_push_ptr(&f->collision_boxes,b); f->collision_info = CCD_NONE; - array_init(&f->wield_boxes,ARRAY_TYPE_PTR); f->light_data = CLM_CLEAR; f->light_source = 0; content_item(&f->dug_item,CONTENT_IGNORE,0,0); diff --git a/src/content/content_block.c b/src/content/content_block.c index 509848d..d8f0f7f 100644 --- a/src/content/content_block.c +++ b/src/content/content_block.c @@ -24,13 +24,10 @@ static void content_block_defaults(contentfeatures_t *f) { int i; - aabox_t *b; content_defaults(f); /* TODO: set block materials unknown_block.png */ f->collision_info = CCD_WALKABLE | CCD_SELECTABLE; - b = content_box(NULL,-0.5,-0.5,-0.5,0.5,0.5,0.5); - array_push_ptr(&f->wield_boxes,b); f->data.block.vertex_alpha = 255; f->data.block.post_effect_colour.r = 0; f->data.block.post_effect_colour.g = 0; diff --git a/src/core/config_default.c b/src/core/config_default.c index c4a7301..2a1ddc5 100644 --- a/src/core/config_default.c +++ b/src/core/config_default.c @@ -48,6 +48,7 @@ void config_default_init() config_set_default("gl.particles.enabled","true",opengl_particles_setter); config_set_default("gl.particles.max","1000",opengl_particles_max_setter); config_set_default("gl.bumpmaps","true",opengl_bumpmap_setter); + config_set_default("gl.psdf","true",opengl_psdf_setter); config_set_default("ui.font.unifont","false",NULL); config_set_default("ui.scale","1.0",ui_scale_setter); diff --git a/src/graphics/font_ttf.c b/src/graphics/font_ttf.c index 3e7a882..8a00874 100644 --- a/src/graphics/font_ttf.c +++ b/src/graphics/font_ttf.c @@ -229,19 +229,11 @@ static int font_ttf_gen_fontpage(font_t *f, uint32_t i) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); } - if (t) { - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); - if (m) { - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); - }else{ - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - } - }else if (b) { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + if (m) { + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); }else{ - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } if (opengl_has_anisotropic()) { diff --git a/src/graphics/material.c b/src/graphics/material.c index 77eab97..6d6e067 100644 --- a/src/graphics/material.c +++ b/src/graphics/material.c @@ -194,6 +194,18 @@ int mat_bind_with_opts(GLuint tex, uint32_t options) mat_data.blend_mode = GL_ZERO; } + if ((options&MATOPT_SDF_ALPHA) == MATOPT_SDF_ALPHA) { + if (opengl_has_psdf()) { + shader_uniform_float(NULL,"alphafunc",2.0); + }else{ + shader_uniform_float(NULL,"alphafunc",1.0); + } + }else if ((options&MATOPT_ALPHA_TEST) == MATOPT_ALPHA_TEST) { + shader_uniform_float(NULL,"alphafunc",1.0); + }else{ + shader_uniform_float(NULL,"alphafunc",0.0); + } + return 0; } @@ -210,11 +222,9 @@ void mat_use(material_t *mat, shader_t *shader) } /* specular values */ - if (shader) { - shader_uniform_float(shader,"shininess",mat->shininess); - shader_uniform_float(shader,"reflectivity",mat->reflectivity); - shader_uniform_float(shader,"bumpiness",mat->bumpiness); - } + shader_uniform_float(shader,"shininess",mat->shininess); + shader_uniform_float(shader,"reflectivity",mat->reflectivity); + shader_uniform_float(shader,"bumpiness",mat->bumpiness); } /* set the shininess and reflectivity of a material */ diff --git a/src/graphics/opengl.c b/src/graphics/opengl.c index 87e55e9..6b4fa6c 100644 --- a/src/graphics/opengl.c +++ b/src/graphics/opengl.c @@ -32,6 +32,7 @@ static struct { int cfg_particles; int cfg_particles_max; int cfg_bumpmap; + int cfg_pseudosdf; } opengl_features = { -1, 1.0, @@ -41,6 +42,7 @@ static struct { 1, 1, 1000, + 1, 1 }; @@ -86,6 +88,12 @@ int opengl_bumpmap_setter(char* value) opengl_features.cfg_bumpmap = parse_bool(value); return 0; } +/* command psdf setter */ +int opengl_psdf_setter(char* value) +{ + opengl_features.cfg_pseudosdf = parse_bool(value); + return 0; +} /* check for anisotropic filtering support */ int opengl_has_anisotropic() @@ -139,7 +147,7 @@ int opengl_has_particles() return opengl_features.cfg_particles; } -/* check if backface culling is enabled */ +/* get the maximum number of particles allowed */ int opengl_particles_max() { if (!opengl_has_particles()) @@ -147,6 +155,12 @@ int opengl_particles_max() return opengl_features.cfg_particles_max; } +/* check if pseudo signed distance field rendering is enabled */ +int opengl_has_psdf() +{ + return opengl_features.cfg_pseudosdf; +} + /* converts an opengl error enum into a string */ char* opengl_error_string(GLenum e) { diff --git a/src/graphics/render2d.c b/src/graphics/render2d.c index af16c7e..2516f9b 100644 --- a/src/graphics/render2d.c +++ b/src/graphics/render2d.c @@ -256,7 +256,7 @@ void render2d() break; default:; } - mat_bind_with_opts(t->glid,MATOPT_ALPHA_BLEND); + mat_bind_with_opts(t->glid,MATOPT_ALPHA_BLEND|MATOPT_SDF_ALPHA); matrix_init(&m); matrix_scale(&m,render2d_data.current->box.w,render2d_data.current->box.h,1.0); matrix_translate(&m,render2d_data.current->box.x,render2d_data.current->box.y,1.0); diff --git a/src/server/dummy_commands.c b/src/server/dummy_commands.c index 0d6941f..669f876 100644 --- a/src/server/dummy_commands.c +++ b/src/server/dummy_commands.c @@ -68,6 +68,10 @@ int opengl_bumpmap_setter(char* value) { return 0; } +int opengl_psdf_setter(char* value) +{ + return 0; +} int ui_scale_setter(char* value) { return 0; -- 2.11.4.GIT