注册 登录
编程论坛 C++教室

DirectX游戏编程的源代码,这段是真的不懂,求大神看看!!!!

jdwq333 发布于 2013-04-16 09:14, 1215 次点击
程序代码:
float Terrain::getHeight(float x, float z)
{
    // Translate on xz-plane by the transformation that takes
   
// the terrain START point to the origin.
    x = ((float)_width / 2.0f) + x;
    z = ((float)_depth / 2.0f) - z;

    // Scale down by the transformation that makes the
   
// cellspacing equal to one.  This is given by
   
// 1 / cellspacing since; cellspacing * 1 / cellspacing = 1.
    x /= (float)_cellSpacing;
    z /= (float)_cellSpacing;

    // From now on, we will interpret our positive z-axis as
   
// going in the 'down' direction, rather than the 'up' direction.
   
// This allows to extract the row and column simply by 'flooring'
   
// x and z:

    float col = ::floorf(x);
    float row = ::floorf(z);

    // get the heights of the quad we're in:
   
//
   
//  A   B
   
//  *---*
   
//  | / |
   
//  *---*
   
//  C   D

    float A = getHeightmapEntry(row,   col);
    float B = getHeightmapEntry(row,   col+1);
    float C = getHeightmapEntry(row+1, col);
    float D = getHeightmapEntry(row+1, col+1);

    //
   
// Find the triangle we are in:
   
//

   
// Translate by the transformation that takes the upper-left
   
// corner of the cell we are in to the origin.  Recall that our
   
// cellspacing was nomalized to 1.  Thus we have a unit square
   
// at the origin of our +x -> 'right' and +z -> 'down' system.
    float dx = x - col;
    float dz = z - row;

    // Note the below compuations of u and v are unneccessary, we really
   
// only need the height, but we compute the entire vector to emphasis
   
// the books discussion.
    float height = 0.0f;
    if(dz < 1.0f - dx)  // upper triangle ABC
    {
        float uy = B - A; // A->B
        float vy = C - A; // A->C

        
// Linearly interpolate on each vector.  The height is the vertex
        
// height the vectors u and v originate from {A}, plus the heights
        
// found by interpolating on each vector u and v.
        height = A + d3d::Lerp(0.0f, uy, dx) + d3d::Lerp(0.0f, vy, dz);
    }
    else // lower triangle DCB
    {
        float uy = C - D; // D->C
        float vy = B - D; // D->B

        
// Linearly interpolate on each vector.  The height is the vertex
        
// height the vectors u and v originate from {D}, plus the heights
        
// found by interpolating on each vector u and v.
        height = D + d3d::Lerp(0.0f, uy, 1.0f - dx) + d3d::Lerp(0.0f, vy, 1.0f - dz);
    }

    return height;
}
这个函数看了很久还是不懂,开始的时候怎么x、z怎么回到原点的呢?难道除2就回到原点了吗?还有就是这么使它成为单位间距??
谢谢帮我解答!!!!!!!!!!
7 回复
#2
peach54602013-04-16 09:20
这段函数是干吗的?
业务需求是什么?
#3
jdwq3332013-04-16 09:43
这个代码是地形的那块。其中的一个函数,我在学习《3D游戏编程设计入门》这本书,但是这个地方卡住了。
#4
peach54602013-04-16 09:44
dafeisuowei
#5
jdwq3332013-04-16 09:58
呵呵,这段函数是通过传人摄像机位置的X、Z坐标来找到所处的单元,返回摄像机正确设置在地形上的高度。
不好意思啊,呵呵!
#6
peach54602013-04-16 13:28
我对DirectX也不太熟,不过看注释
前半部分还好说,后半部分好像是个3D的算法?
#7
jdwq3332013-04-17 08:09
嗯,是的,就是3D,我就是前半部分不懂。能否帮忙解释下啊,呵呵。
#8
peach54602013-04-19 10:53
注释不是很清楚了嘛...
1