| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1215 人关注过本帖
标题:DirectX游戏编程的源代码,这段是真的不懂,求大神看看!!!!
只看楼主 加入收藏
jdwq333
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-2-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
DirectX游戏编程的源代码,这段是真的不懂,求大神看看!!!!
程序代码:
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就回到原点了吗?还有就是这么使它成为单位间距??
谢谢帮我解答!!!!!!!!!!
搜索更多相关主题的帖子: 游戏编程 源代码 DirectX 
2013-04-16 09:14
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:20 
这段函数是干吗的?
业务需求是什么?

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-04-16 09:20
jdwq333
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-2-20
收藏
得分:0 
这个代码是地形的那块。其中的一个函数,我在学习《3D游戏编程设计入门》这本书,但是这个地方卡住了。

现在真的很饿,所以要多吃。软件很好,我也很好。
2013-04-16 09:43
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
dafeisuowei

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-04-16 09:44
jdwq333
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-2-20
收藏
得分:0 
呵呵,这段函数是通过传人摄像机位置的X、Z坐标来找到所处的单元,返回摄像机正确设置在地形上的高度。
不好意思啊,呵呵!

现在真的很饿,所以要多吃。软件很好,我也很好。
2013-04-16 09:58
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
我对DirectX也不太熟,不过看注释
前半部分还好说,后半部分好像是个3D的算法?

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-04-16 13:28
jdwq333
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-2-20
收藏
得分:0 
嗯,是的,就是3D,我就是前半部分不懂。能否帮忙解释下啊,呵呵。

现在真的很饿,所以要多吃。软件很好,我也很好。
2013-04-17 08:09
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
注释不是很清楚了嘛...

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-04-19 10:53
快速回复:DirectX游戏编程的源代码,这段是真的不懂,求大神看看!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019551 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved