OK so i have a really big square (grid) that I have made up using triangles, then applied a heightmap to bump it. What im trying to do now is get grid lines on it. I have figured out a way to do this but this results in 33 if statements in the fragment shader just for x and then another 33 for y. I have been told I can use what im doing now and implement it slightly differently (using some GLSL function) to only need 1 or 2 if statements. This is my current code (not all finished but gives you the idea of what im doing.)
#version 330
uniform sampler2D texture;
in vec2 texCoord;
layout (location=0) out vec4 fragColour;
void main(void) {
vec4 newColor;
vec2 line = texCoord * 32; // makes texCoords easier to number (as divided by 32 in the C++array)
if(line.x > 0 && line.x < 0.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 1 && line.x < 1.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 2 && line.x < 2.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 3 && line.x < 3.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 4 && line.x < 4.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 5 && line.x < 5.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 6 && line.x < 6.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 7 && line.x < 7.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 8 && line.x < 8.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 9 && line.x < 9.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 10 && line.x < 10.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 11 && line.x < 11.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 12 && line.x < 12.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 13 && line.x < 13.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 14 && line.x < 14.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 15 && line.x < 15.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 16 && line.x < 16.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 17 && line.x < 17.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 18 && line.x < 18.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 19 && line.x < 19.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 20 && line.x < 20.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 21 && line.x < 21.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 22 && line.x < 22.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 23 && line.x < 23.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else if(line.x > 24 && line.x < 24.9)
{
newColor = vec4(1.0,1.0,1.0,1.0);
}
else
{
newColor = vec4(0.0,0.0,0.0,1.0);
}
fragColour = newColor;
}
YOu shouldn’t use control flow (that depedns on something other than uniform) unless it is absolutely necessary (at least that was standard recommendation last time I wrote a glsl shader). In your case control flow isn’t needed. Use
step,smoothstep,mixand multiplication to avoid control flow.Your “if/else” segment of code could be implemented using something like this (untested code):
fragColor = mix(vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0), step(fract(line.x), 0.9));Or using texture lookup. Texture lookup might be faster than numerical calculation, depending on hardware (and texture size)
Please note that using “step” might produce jagged non-antialiased edges and noise/moire pattern on faraway surfaces. “OpenGL orange book” (“OpenGL shading language”) had some examples that should explain how to deal with it. However, using texture lookup might be easier. For a start, you could try using “smoothstep” instead of “step”.
Or, alternatively, you could simply redraw entire landscape in wireframe mode on top of solid rendered landscape.