好代碼(一)

來源:互聯網
上載者:User

標籤: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 是兩個浮點數,通過此種方式即可獲得“電腦攝像機相對於儲存格的位置”,之後通過“雙線性插值”獲得地面相對於座標系原點的高度。

好代碼(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.