Grid following contour lines

I have a program that allows the user to build scenes and there is an option to display grid lines on the scene. Currently I just draw straight lines using GL_LINES. The problem is that the lines are hidden by the hills and float above the valleys. I would like the lines to follow the contour of the terrain.

I’ve come up with a brut force method which is to do my own rendering of the background model at fixed intervals and generate a point map along the grid lines. I could then create a series of GL_LINES based on these points. I already have the rendering code so this wouldn’t be a big deal.

However, I was wondering if there is a more direct and efficient way in OpenGL to do this.

Thank you.

If you have access to the terrain data you could sample that at regular intervals and use those vertex coords in a vertex array.
It will never be perfect as the lines will intercept the terrain due to the lower grid frequency.

Yes. There is. You can ‘project’ a striped texture onto the surface. This is a simple form of texture mapping in which OpenGL figures out what the texture coordinates should be. The Red Book refers to it as ‘Automatic Texture-Coordinate Generation’. The direct link to the discussion is -

http://glprogramming.com/red/chapter09.html#name7

Whilst projective texture mapping will get you your lines on the terrain the texture stretching which may occur will make the result look ugly. You may need to experiment with suitably large textures to keep the lines looking ‘sharp’

Thank you for your help. I will try both methods and see how they look.