GLSL Fragment Shader Gridlines on heightmapped terrain

Started by
3 comments, last by mynameisnafe 11 years, 4 months ago
Hello all!

In short:

I have a heightmapped terrain, which has had its y value offset in the vertex shader.

Now I am looking to draw gridlines over the terrain, in the fragment shader.

I am able to sample colour from the heightmap texture in the fragment shader.

This what I've been attempting, but no luck. The idea is that the terrain is white, gridlines are black.

Terrain width is 1024. So is the texture.

Could somebody please suggest a method of testing whether the texture coord components, x, and y, are divisible by some number? and changing the pixel colour.

With this code I only get white terrain. :/



uniform sampler2D hmap_texture;
in vec2 texCoord;
layout (location=0) out vec4 fragColour;

void main(void) {
//vec4 colour = texture2D(hmap_texture, texCoord);
vec4 colour = vec4(1.0, 1.0, 1.0, 1.0);

if( mod(texCoord.x, 32) == 0) {
colour = vec4(0.0, 0.0, 0.0, 1.0)
}

if( mod(texCoord.y, 32) == 0) {
colour = vec4(0.0, 0.0, 0.0, 1.0)
}

fragColour = colour;
}


Many thanks.
Nathan
Advertisement
Texture coordinates in opengl are from 0 to 1, so you're going to need to divide texCoord by 1024 to get your texture2D to work.
Also, texCoord is likely a float, so you should try copying the data into an integer before using mod. There are debuggers for shaders provided by nvidia or amd that may help you solve this problem.
Thanks, I did try something along the lines of this before but it was a bit hashed - very good point about using intergers with modulo though! That's something I hadn't thought of :)

I'm away from a real GFX card at the mo but I'll try this in a few hours and get back to you :)
I took so long to reply! Many apologies my learned freind!

I got the Mod function working by multipling the vertexCoord * WIDTH, essentially. Thank you!

I now have a new question about this, but it's off this topic, so I'm off to start a new thread..

This topic is closed to new replies.

Advertisement