Sign in to follow this  
Dekowta

OpenGL Signed Distance Field issues

Recommended Posts

Hi,

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();
    WindowContext->BindGLContext(GLContext);
    GLContext->Initalise();
    GLContext->SetClearColour(Vector4f(0.0f,0.5f,0.0f,1.0f));

    

    MLInput::MLInputManager::Initalise();
    MLInput::MLInputManager::CreateRawKeyboard();
    MLRenderer::MLText::MLFontCore::Initalise();
    MLRenderer::MLCameraManager::Initalise();
    MLAudio::MLAudioManager::InitaliseAudio(false, 0);
    WindowContext->SetResizeCallBack(&MLRenderer::ResizeGL);
    WindowContext->SetInputCallBack(&MLInput::MLInputManager::InputMSGHandler);

    MLGraphics::Initalise(5000);

    MLRenderer::MLText::MLFontCore::Initalise();
    MLTrueTypeFont font;
    font.LoadFont("TestFont.ttf", 256);
    font.SetPaddingX(15);
    font.SetPaddingY(15);
    font.SetTextureWidth(4096);
    font.SetTextureHeight(4096);
    font.LoadDefaultGlyps();


    MLTexture* sdfTest = new MLTexture();
    sdfTest->LoadTexture("TEST.png");


    MLSprite newSprite;
    MLTexture* text = font.GetFontTexture(0);
    /*text->StartEditing();
    text->ExportImage("TestBefore.png");
    text->ConverttoGrayScale();
    text->ExportImage("Test2.png");
    text->ConverttoSignDistanceField(1, 4);
    text->ExportImage("Test.png");
    text->EndEditing();*/

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


    MLShader* sdfShader = new MLShader();
    sdfShader->LoadShaderFile("shaders\\SignedDistanceField", GL_VERTEX_SHADER);
    sdfShader->LoadShaderFile("shaders\\SignedDistanceField", GL_FRAGMENT_SHADER);
    sdfShader->Lock();
    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);
    sdfShader->LinkShaders();
    sdfShader->Unlock();

    while(1)
    {

        if(MLInput::MLInputManager::IsKeyPressed(MLKeys::Up))
        {
            newSprite.Scale.x += 0.02f;
            newSprite.Scale.y += 0.02f;
        }
        if(MLInput::MLInputManager::IsKeyPressed(MLKeys::Down))
        {
            newSprite.Scale.x -= 0.02f;
            newSprite.Scale.y -= 0.02f;
        }
        GLContext->StartFrame();
        MLAudio::MLAudioManager::UpdateAudio();
        WindowContext->Update();
        MLGraphics::Begin();

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

        GLContext->EndFrame();
        

        
    }

}


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.

Thanks

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