Home Game Development opengl – Render Line Following Terrain Using Heightmap

opengl – Render Line Following Terrain Using Heightmap

0
opengl – Render Line Following Terrain Using Heightmap

[ad_1]

I’ve a terrain mesh that I’m making an attempt to attract a line on such that the road follows the contour of the terrain. In order to perform this I’m rendering my terrain mesh to a heightmap, then once I draw my line I pattern the heightmap for the peak. It appears that one thing is going on as my traces aren’t utterly flat and do fluctuate in top, however they don’t seem to be following the terrain both. It appears that my sampling is working, however my math to calculate the UV coordinates into the heightmap is inaccurate.

The terrain mesh is generated from elevation information as North, East, Down coordinates from the central location of an 18520×18520 meter sq.. The most 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 Model;
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 = Model * vec4(Vertex, 1.0);

  gl_Position =  Projection * pos;

  // Force out depth so vertex shouldn't be behind digicam
  gl_Position.z = -1.0;

  // Mix 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 pink channel
  gl_FragColor = vec4(Altitude, 0.0, 0.0, 1.0);
}

I then outline the road I need to observe the terrain as a number of (x,z) coordinates in the identical -9260 -> 9260 coordinate vary as my terrain coordinates. In 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 Model;
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 = Model * 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 similar time I generate the road buffer quite than utilizing the vertex information to calculate a UV, however that wasn’t profitable both. Any assistance is significantly appreciated, thanks.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here