Hello, I am trying to write a fragment shader that will black out anything a fixed distance around a position (like a maximum view range). The circle drawn by this shader is expected to get smaller when zoomed out as it should be of constant radius in world coordinates.
const std::string distanceFragShaderStr =
//Max dist from pos to draw
"uniform float maxDist;"
//Position to draw around
"uniform vec2 pos;"
"void main() {"
//How to calculate?
"vec2 fragpos = ?;"
"if (distance(pos,fragpos) > maxDist) {"
//Set as black (too far)
"gl_FragColor = vec4(0.0,0.0,0.0,1.0);"
"}"
"else {"
// Set as see through
"gl_FragColor = vec4(0.0,0.0,0.0,0.0);"
"}"
"}";
This shader is to be drawn to a rectangle covering the entire screen(which contains the point pos):
sf::Shader distanceShader;
if (!distanceShader.loadFromMemory(distanceFragShaderStr, sf::Shader::Fragment)) {
std::cout << "Shader did not compile!\n";
return -1;
}
sf::RectangleShape backgroundRect(view.getSize());
backgroundRect.setOrigin(backgroundRect.getSize() / 2.0f);
backgroundRect.setPosition(view.getCenter());
distanceShader.setUniform("pos", sf::Vector2f(window.getSize()) / 2.0f);
distanceShader.setUniform("maxDist", 400.0f);
//...
window.draw(backgroundRect, &distanceShader);
But I cannot seem to work out how to calculate fragpos, i.e. the world coordinates of the current fragment. I assume it needs some uniforms such as the current view size, position but I cant figure out how it is calculated, does anyone know how this could be achieved? I saw some posts about using gl_TexCoord[0] but couldn't get any results with it.