Sign in to follow this  

OpenGL Signed Distance Field issues

Recommended Posts


I've been experimenting with singed distance fields in OpenGL for a bit now and I've come across a few issues I need some help to solve.


I have put all the images here,1qXKq0d,8rYcS3J,U4VI01S,yYal9yV#0 as they are rather large

The first is that I have the basic fragment shader for performing the alpha testing

in vec2 VTexCoord;
in vec4 VColour;

uniform sampler2D Tex1;

out vec4 FragColour;

void main() {

	float distance = texture( Tex1, VTexCoord ).a;
	float smoothWidth = fwidth(distance);
	float alpha = smoothstep(0.5 - smoothWidth, 0.5 + smoothWidth, distance);
	FragColour = vec4(VColour.rgb, alpha);   

I use it on the following singed distance field generated via the tool that comes with libgtx ( which is the first image. The result gained from using the first image can be seen in the third image which looks ok but jagged around the edges and when zoomed in (second image) its doesn't seem to be working as i expected since i thought it would retain a smooth curve rather than pixelated edges.

I don't know if there are any opengl states that need to be set before applying the shader or if the texture needs have different parameters.

I cant really post all the code for rendering since its rather large but its all here: and the implementation at the moment is


#include <MoonLight.h>

using namespace std;

int main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    MLWindows::MLWindowsContext* WindowContext;

    WindowContext = new MLWindows::MLWindowsContext(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
    WindowContext->Create(L"MoonLight Test Bed", 1280, 720, 32, true);
    //set up the openGL stuff
    MLRenderer::MLGLContext* GLContext = new MLRenderer::MLGLContext();


    MLAudio::MLAudioManager::InitaliseAudio(false, 0);


    MLTrueTypeFont font;
    font.LoadFont("TestFont.ttf", 256);

    MLTexture* sdfTest = new MLTexture();

    MLSprite newSprite;
    MLTexture* text = font.GetFontTexture(0);
    text->ConverttoSignDistanceField(1, 4);

    newSprite.Texture = sdfTest;
    newSprite.Colour = Vector4f(1.0f);

    MLShader* sdfShader = new MLShader();
    sdfShader->LoadShaderFile("shaders\\SignedDistanceField", GL_VERTEX_SHADER);
    sdfShader->LoadShaderFile("shaders\\SignedDistanceField", GL_FRAGMENT_SHADER);
    sdfShader->BindAtt(MLRenderer::MLAttID::ML_ATT_VERTEXPOS,    BASE_SHADER_VERT_POS);
    sdfShader->BindAtt(MLRenderer::MLAttID::ML_ATT_VERTEXCOL,    BASE_SHADER_VERT_COL);
    sdfShader->BindAtt(MLRenderer::MLAttID::ML_ATT_TEXTURE0,    BASE_SHADER_VERT_UV);


            newSprite.Scale.x += 0.02f;
            newSprite.Scale.y += 0.02f;
            newSprite.Scale.x -= 0.02f;
            newSprite.Scale.y -= 0.02f;

        newSprite.Position = Vector2f(0.0f, 0.0f);
        newSprite.Shader = sdfShader;
        newSprite.Effect = MLSpriteEffect::None;




The second problem Im having is that I have implemented the Dead Reckoning method to create a signed distance field but I have no idea how to limit the spread of the distance values and I end up with a mess (images four and five). The code im using for calcualting the values are the same as ( My maths isn't amazing but I do understand the concept just dont really have a clue on how to limit the spread.


Share this post

Link to post
Share on other sites

Solved the issue with the field not looking correct as I loaded the texture with the GL_NEAREST parameters which does not work with the signed distance method.

Still need to solve the dead reckoning method as I would like to have a internal converter rather than having to load a pre made signed distance field in.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this