I’ve a terrain mesh that I am making an attempt to attract a line on such that the road follows the contour of the terrain. To be able to accomplish this I am rendering my terrain mesh to a heightmap, then once I draw my line I pattern the heightmap for the peak. Evidently one thing is occurring as my traces aren’t utterly flat and do differ in top, however they are not following the terrain both. Evidently my sampling is working, however my math to calculate the UV coordinates into the heightmap is wrong.
The terrain mesh is generated from elevation knowledge as North, East, Down coordinates from the central location of an 18520×18520 meter sq.. The utmost and minimal elevations are 9987 and -9987 meters respectively.
When producing the terrain for the primary time or when the terrain must be up to date due to motion, I render the terrain to a framebuffer utilizing an orthographic projection matrix.
When rendering the heightmap I take advantage of the next shaders,
Heightmap Vertex:
#model 120
uniform mat4 Mannequin;
uniform mat4 View;
uniform mat4 Projection;
attribute vec3 Vertex; // place of the vertices
various float Altitude;
void predominant()
{
// Save off our vertex after mannequin is utilized
vec4 pos = Mannequin * vec4(Vertex, 1.0);
gl_Position = Projection * pos;
// Drive out depth so vertex shouldn't be behind digital camera
gl_Position.z = -1.0;
// Combine our top right into a 0 -> 1 vary
Altitude = (pos.z + 9987) / (2 * 9987);
}
Heightmap Fragment:
#model 120
various float Altitude;
void predominant()
{
// Save off top within the purple channel
gl_FragColor = vec4(Altitude, 0.0, 0.0, 1.0);
}
I then outline the road I wish to observe the terrain as a number of (x,z) coordinates in the identical -9260 -> 9260 coordinate vary as my terrain coordinates. Within the vertex shader for the road, I pattern the heightmap texture for the peak and apply the offset.
Line Vertex Shader:
#model 120
uniform mat4 Mannequin;
uniform mat4 View;
uniform mat4 Projection;
attribute vec2 Vertex;
uniform sampler2D terraintexture;
void predominant()
{
vec2 pos;
// Map vertex to -1 -> 1 vary
pos.x = (Vertex.x) / (9260);
pos.y = (Vertex.y) / (9260);
// Then convert to 0 -> 1 for texture
pos += 1.0;
pos /= 2.0;
vec4 tex = texture2D(terraintexture, pos);
// Convert 0 -> 1 again to -9987 -> 9987
float altitude = (tex.r * (2 * 9987)) - 9987;
// Apply mannequin with vertex at 0 top
vec4 x = Mannequin * vec4(Vertex.x, 0.0, Vertex.y, 1.0);
// Apply our offset
x.y += altitude;
gl_Position = Projection * View * x;
}
I’ve additionally tried producing a UV buffer on the identical time I generate the road buffer somewhat than utilizing the vertex knowledge to calculate a UV, however that wasn’t profitable both. Any assistance is significantly appreciated, thanks.