# Mathematical lerp between pixils of a heightmap

This topic is 2145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok. Here's the basic idea behind what I am trying to do:

I have a rectangular heightmap (not actually an image, but just an array of float values).

I have a quad-tree going along this map. The vertices don't fall exactly on an integer value, so I have to get the closest heightmap value. This makes the mesh "blocky" where I will have areas that are flat and transition sharply to another elevation area that is also flat.

To fix this, I should lerp the values..... Here's the problem-- I'm not doing it right!

Here is the applicable code:

lat*=Top->planetSizeX;
lon*=Top->planetSizeY;
float xFraction=lat-int(lat);
float yFraction=lon-int(lon);
float elev;

if (xFraction+yFraction>1.0f){
lat++;
lon++;
xFraction=1.0f-xFraction;
yFraction=1.0f-yFraction;
elev=((SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)]*(1.0f-xFraction)
+SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)-1][int(lon)]*xFraction)
+(SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)]*(1.0f-yFraction)
+SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)-1]*yFraction))*0.5f;
}
else{
elev=((SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)]*(1.0f-xFraction)
+SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)+1][int(lon)]*xFraction)
+(SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)]*(1.0f-yFraction)
+SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)+1]*yFraction))*0.5f;
}



Before this code is "lat" and "lon" which are floats from 0.0f-1.0f.

"Top->planetSizeX" and "Top->planetSizeY" are also floats that represent the size values of the rectangular heightmap.

Here is an illustration of what I think I'm trying to accomplish:

[attachment=17828:Untitled.png]

The black dots represent height values somewhere within the heightmap and the red dot is the vertex in question. Each of the 4 values of the heightmap can be any value, but 3 of them together are considered a plane (basic stuff I know)-- ABC and DBC. So I know I need to determine which plane the vertex is in, so I add the fractions of X and Z and if the result is greater than 1.0f then it's in the "other" plane (that's why I have 2 conditions).

PLEASE tell me what I'm doing wrong........ Is there a better (working) way?

Here is an image showing the problem:

[attachment=17829:Untitled.png]

It should be smooth.....

##### Share on other sites

Never mind guys. I figured it out. If anyone is interested, here is the fix:

lat*=Top->planetSizeX;
lon*=Top->planetSizeY;
float xFraction=lat-int(lat);
float yFraction=lon-int(lon);
float elev;

if (xFraction+yFraction>1.0f){
lat++;
lon++;
if (lat>Top->planetSizeX) lat-=Top->planetSizeX;
xFraction=1.0f-xFraction;
yFraction=1.0f-yFraction;
float leg=xFraction+yFraction;
elev=(SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)-1][int(lon)]*xFraction
+SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)-1]*yFraction);
elev+=SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)]*(1.0f-leg);
elev*=0.5f;
}
else{
float leg=xFraction+yFraction;
elev=(SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)+1][int(lon)]*xFraction
+SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)+1]*yFraction);
elev+=SolarSystem.Planet[Top->Planet].PlanetTopography[int(lat)][int(lon)]*(1.0f-leg);
elev*=0.5f;
}



• 13
• 18
• 29
• 11
• 27