# 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.SetTextureWidth(4096);
font.SetTextureHeight(4096);

MLTexture* sdfTest = new MLTexture();

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);

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.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 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.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627686
• Total Posts
2978635
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 11
• 14
• 12
• 10
• 12