A piece of code from OpenGL game programming, based on the position of the current point of the coordinate system (the height must be computed, not by obtaining a coordinate point) to get the interpolation height, is a good idea:/** get the interpolation height of the ground current point */
Float getaveheight (float x,float z)
{
float Camerax, Cameraz;
Camerax = X/m_ncellwidth;
Cameraz = Z/m_ncellwidth;
/** Calculate elevation coordinates (Col0, Row0), (Col1, Row1) */
int col0 = int (Camerax);
int row0 = int (Cameraz);
unsigned int col1 = col0 + 1;
unsigned int row1 = row0 + 1;
/** ensure that the cell coordinates do not exceed the elevation */
if (col1 > m_nwidth) col1 = 0;
if (Row1 > m_nwidth) row1 = 0;
/** gets the height of the four corners of the unit */
float h00 = (float) (m_pheightmap[col0*m_ncellwidth + row0*m_ncellwidth*m_nwidth]);
float h01 = (float) (m_pheightmap[col1*m_ncellwidth + row0*m_ncellwidth*m_nwidth]);
float H11 = (float) (m_pheightmap[col1*m_ncellwidth + row1*m_ncellwidth*m_nwidth]);
float H10 = (float) (m_pheightmap[col0*m_ncellwidth + row1*m_ncellwidth*m_nwidth]);
/** the location of the computer camera relative to the cell */
float tx = Camerax-int (Camerax);
float ty = Cameraz-int (Cameraz);
/** bilinear interpolation is worth the ground height */
float txty = tx * TY;
float final_height = h00 * (1.0f-ty-tx + txty)
+ h01 * (tx-txty)
+ H11 * txty
+ H10 * (ty-txty);
return final_height;
}============= float tx = Camerax-int (Camerax);
float ty = Cameraz-int (Cameraz); in fact Camerax and Cameraz are two floating-point numbers, in which case the "computer camera's position relative to the cell" is obtained, and then the height of the ground relative to the coordinate system origin is obtained by using bilinear interpolation.
Good code (i)