• Create Account

# Mathematical lerp between pixils of a heightmap

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1Hawkblood  Members   -  Reputation: 415

Like
0Likes
Like

Posted 06 September 2013 - 01:06 PM

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:

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:

It should be smooth.....

### #2Hawkblood  Members   -  Reputation: 415

Like
0Likes
Like

Posted 06 September 2013 - 04:14 PM

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;
}



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS