Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Signed Distance Field issues

This topic is 2034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 http://imgur.com/DxGdGe9,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 (https://github.com/libgdx/libgdx/wiki/Distance-field-fonts) 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: https://bitbucket.org/Chris_Millin/moonlight-engine/src/87ccdb3302c5082a1ab065ec3da37923205f7197/Moonlight/Moonlight/?at=master 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 (http://people.sju.edu/~ggrevera/software/distance/html/class_dead_reckoning__3x3.html). 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!