Good code (i)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.