標籤:sp div bs 代碼 nbsp htm br ht 編程
摘自《OpenGL遊戲編程》中的一段代碼,根據座標系當前點的位置(高度必須由計算得來,不是通過擷取座標點就能做到的)獲得插值高度,是一種比較好的一種思想:/** 獲得地面當前點的插值高度 */
float getAveHeight(float x,float z)
{
float CameraX, CameraZ;
CameraX = x / m_nCellWidth;
CameraZ = z / m_nCellWidth;
/** 計算高程座標(Col0, Row0),(Col1, Row1) */
int col0 = int(CameraX);
int row0 = int(CameraZ);
unsigned int col1 = col0 + 1;
unsigned int row1 = row0 + 1;
/** 確保單元座標不超過高程以外 */
if (col1 > m_nWidth) col1 = 0;
if (row1 > m_nWidth) row1 = 0;
/** 擷取單元的四個角的高度 */
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]);
/** 電腦攝像機相對於儲存格的位置 */
float tx = CameraX - int(CameraX);
float ty = CameraZ - int(CameraZ);
/** 進行雙線性插值得到地面高度 */
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);實際上CameraX 和CameraZ 是兩個浮點數,通過此種方式即可獲得“電腦攝像機相對於儲存格的位置”,之後通過“雙線性插值”獲得地面相對於座標系原點的高度。
好代碼(一)