青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天行健 君子當自強而不息

D3D中的地形繪制基礎(4)

新建網頁 1 13

13.5在地形上“行走”

構造了一個地形以后,我們想要有移動攝像機的能力,以便模擬在地形上行走的效果。我們需要調整攝像機的高度,這依賴于地形部分的知識,好的,繼續往下看。我們首先需要找到照相機所在的方格的位置,并給出x軸和z軸坐標,cTerrain::get_height函數能做到這些,它能提供x軸、z軸坐標參數,返回攝像機需要被設置在地形上的高度值,現在看實現部分。

float cTerrain::get_height(float x, float z)
    {
        // Translate on xz-plane by the transformation that takes the terrain START point to the origin,
        // note that we negative z value so positive z-axis will be down in logical.
        x = m_width/2.0f + x;
        z = m_depth/2.0f - z;
   
        // Scale down by the transformation that makes the cell_spacing equal to one.
        x /= m_cell_spacing;
        z /= m_cell_spacing;


We first translate by the transformation that takes the start point of the terrain to the origin. Next, we scale by the inverse of the cell spacing variable; this scaling sets the cell spacing to 1. Then we switch to a new frame of reference where the positive z-axis points “down.” Of course, there is no code that changes the frame of reference, but it is now understood that +z goes down. Figure 13.9 shows these steps graphically.

We see that our changed coordinate system matches the ordering of a matrix. That is, the upper-left corner is at the origin, the column count increases in the right direction, and the row count increases in the down direction. Thus, by Figure 13.9 and knowing the cell spacing is equal to 1, we can immediately see that the row and column of the cell we are in is given by:

       // 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);
   
        // ensures row and col are valid
   
        if(row < 0)
            row = 0;
   
        if(row >= m_num_cells_per_col - 1)
            row = m_num_cells_per_col - 1;
   
        if(col < 0)
            col = 0;
   
        if(col >= m_num_cells_per_row)
            col = m_num_cells_per_row;

現在我們將取得方格的四個頂點的高度。

       // get the heights of the quad we're in:
        // 
        //  A   B
        //  *---*
        //  | / |
        //  *---*  
        //  C   D    
   
    float AHeight = get_height_map_entry(row,   col);
        float BHeight = get_height_map_entry(row,   col+1);
        float CHeight = get_height_map_entry(row+1, col);
        float DHeight = get_height_map_entry(row+1, col+1);

現在我們知道了方格的四個頂點的高度,我們需要找到照相機所在的位置的方格的高度,因為一個方格可能同時向幾個方向傾斜,這可能會稍微難一點,見圖 13.10:

為了找到高度,我們需要知道我們在方格中的哪個三角形里。方格是由二個三角形渲染成的,找到我們所在的三角形,我們要取得我們所在的方格并且轉換它,它的左上點是原點。

自從用行和列來描述我們所在的方格左上頂點的位置以來,我們必須沿x軸平移-col個單位,并沿z軸平移-row個單位。沿著x軸和z軸的平移過程用如下代碼表示。

  //
        // 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 cell_spacing 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;
   
        float height = 0.0f;


Then, if dx < 1.0 – dx we are in the “upper” triangle v0v1v2. Otherwise, we are in the “lower” triangle v0v2v3 (see Figure 13.10).

Now we explain how to find the height if we are in the “upper” triangle. The process is similar for the “lower” triangle, and of course the code for both follows shortly. To find the height if we are in the “upper”triangle, we construct two vectors, u = (cellSpacing, B – A, 0) and v = (0, C – A, – cellSpacing), on the sides of the triangle and originating at the terminal point of the vector q = (qx, A, qz) as Figure 13.12.a shows. Then we linearly interpolate along u by dx, and we linearly interpolate along v by dz. Figure 13.12.b illustrates these interpolations. The y-coordinate of the vector (q + dxu + dzv) gives the height based on the given x- and z-coordinates; recall the geometric interpretation of vector addition to see this.

注意:我們只關心改變的高度值,我們只修改y值,忽視其他部分,因此,Height=A + dxuy + dzvy

   float height = 0.0f;
   
        if(dz < 1.0f - dx)    // upper triangle ABC
        {
            float uy = BHeight - AHeight;    // A->B
            float vy = CHeight - AHeight;    // 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 = AHeight + lerp(0.0f, uy, dx) + lerp(0.0f, vy, dz);
        }
        else    // lower triangle DCB
        {
            float uy = CHeight - DHeight; // D->C
            float vy = BHeight - DHeight; // 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 = DHeight + lerp(0.0f, uy, 1.0f - dx) + lerp(0.0f, vy, 1.0f - dz);
        }
   
        return height;
    }

Lerp函數是一個沿著一維直線的基本線性插值算法,實現如下:

float lerp(float a, float b, float t)
    {
        return a * (1 - t) + (b * t);
    }


繪制函數:

    void cTerrain::draw(D3DXMATRIX* world_matrix, bool draw_triangle)
    {
        if(m_device == NULL)
            return;
   
        m_device->SetTransform(D3DTS_WORLD, world_matrix);
   
        m_device->SetStreamSource(0, m_vertex_buffer, 0, sizeof(cTerrainVertex));
        m_device->SetFVF(TERRAIN_VERTEX_FVF);
        m_device->SetIndices(m_index_buffer);
   
        m_device->SetTexture(0, m_texture);
   
        m_device->SetRenderState(D3DRS_LIGHTING, FALSE);    // trun off lighting since we're lighting it ourselves
            m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_num_vertices, 0, m_num_triangles);
        m_device->SetRenderState(D3DRS_LIGHTING, TRUE);
   
        if(draw_triangle)
        {
            m_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
            m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_num_vertices, 0, m_num_triangles);
            m_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
        }
    }

posted on 2008-04-02 21:21 lovedday 閱讀(1851) 評論(0)  編輯 收藏 引用

公告

導航

統計

常用鏈接

隨筆分類(178)

3D游戲編程相關鏈接

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美 日韩 国产在线| 国产精品毛片va一区二区三区| 国产精品多人| 国产主播一区二区三区| 在线中文字幕不卡| 欧美日韩亚洲综合| 亚洲精品人人| 亚洲综合国产激情另类一区| 亚洲免费在线看| 日韩亚洲成人av在线| 亚洲高清一区二| 国产精品天天看| 国产精品二区在线观看| 欧美久色视频| 久久精品人人| 久久久久这里只有精品| 亚洲欧美在线一区| 亚洲网站啪啪| 欧美亚洲午夜视频在线观看| 在线亚洲观看| 99精品欧美一区二区三区综合在线| 欧美va天堂| 免费观看在线综合色| 欧美**字幕| 久久久7777| 欧美成年人视频| 亚洲婷婷综合色高清在线| 午夜一区在线| 女仆av观看一区| 日韩视频中文字幕| 一区二区三区导航| 性欧美videos另类喷潮| 久久免费黄色| 一本色道久久综合亚洲精品小说 | 久久综合网络一区二区| 欧美精品国产| 国产欧美一区二区三区国产幕精品 | 久久久久青草大香线综合精品| 亚洲第一福利视频| 亚洲天堂av高清| 欧美国产高潮xxxx1819| 国产一区二区三区免费观看 | 国模精品娜娜一二三区| 亚洲欧美高清| 99国产精品久久久| 蜜臀a∨国产成人精品| 国产欧美精品| 亚洲欧美日韩另类精品一区二区三区| 欧美www在线| 久久国产精品久久久| 国产精品一区毛片| 亚洲永久免费av| 一本色道久久综合一区| 欧美日韩国产成人高清视频| 亚洲日本电影在线| 欧美 日韩 国产一区二区在线视频 | 午夜精品视频网站| 欧美亚日韩国产aⅴ精品中极品| 亚洲另类春色国产| 最新亚洲电影| 欧美日韩一区二区三区在线看| 亚洲精品视频中文字幕| 亚洲第一狼人社区| 欧美激情一区二区三区四区| 亚洲精品一区在线观看| 亚洲激情专区| 欧美日本三区| 一本色道久久加勒比88综合| 亚洲美女少妇无套啪啪呻吟| 欧美日韩国产一区精品一区| 亚洲色图综合久久| 亚洲午夜羞羞片| 国产日韩欧美一区在线| 久久精品视频免费观看| 小黄鸭精品密入口导航| 狠狠v欧美v日韩v亚洲ⅴ| 麻豆精品传媒视频| 免费在线视频一区| 亚洲一级二级在线| 午夜精品影院| 最新中文字幕亚洲| 中文一区二区| 韩国精品在线观看| 韩国精品在线观看| 亚洲精品乱码久久久久久黑人| 亚洲日本电影在线| 国产精品久久久一本精品| 久久精品成人| 你懂的网址国产 欧美| 洋洋av久久久久久久一区| 亚洲永久在线观看| 亚洲第一视频| 99国产精品一区| 精品成人a区在线观看| 亚洲精品色图| 狠狠色狠狠色综合| 日韩视频国产视频| 狠狠综合久久av一区二区老牛| 亚洲日本中文字幕区| 国产亚洲精品久久久| 亚洲激情精品| 狠狠色综合网站久久久久久久| 亚洲精品乱码久久久久久蜜桃麻豆 | 亚洲精品1区2区| 国产精品一区二区视频| 欧美激情精品久久久久久久变态 | 久久久亚洲高清| 亚洲一区二区三区免费观看 | 国产一区二区在线观看免费播放| 亚洲国产精品成人一区二区| 国产精品伊人日日| 亚洲国产精品一区二区www在线| 国产精品网站一区| 最新国产精品拍自在线播放| 伊人激情综合| 欧美在线啊v| 午夜一区不卡| 欧美日韩国内自拍| 欧美电影在线免费观看网站| 国产日韩欧美不卡| 亚洲图色在线| 亚洲一区免费| 欧美日韩一区二区三区在线 | 欧美一级精品大片| 亚洲欧美另类综合偷拍| 欧美精品 日韩| 欧美激情二区三区| 亚洲电影下载| 久久午夜色播影院免费高清| 欧美中文在线观看| 国产精品美女主播| 亚洲永久视频| 欧美一区二区精品| 国产精品一二三视频| 狂野欧美一区| 国产精品日本精品| 最近看过的日韩成人| 亚洲国产日韩欧美| 老妇喷水一区二区三区| 欧美承认网站| 在线观看视频一区二区欧美日韩| 欧美一区二区三区久久精品 | 日韩视频久久| 欧美精品亚洲| 日韩一区二区高清| 亚洲一区国产视频| 国产精品美女999| 午夜天堂精品久久久久| 久久久综合网| 亚洲激情另类| 欧美女人交a| 亚洲午夜在线观看视频在线| 欧美在线三区| 亚洲第一福利视频| 欧美va亚洲va日韩∨a综合色| 亚洲国产精品一区制服丝袜 | 亚洲精品一区久久久久久| 99这里只有精品| 国产精品久久97| 久久精品国产综合精品| 欧美 日韩 国产一区二区在线视频| 亚洲精品看片| 国产精品亚洲视频| 久久人人爽人人爽爽久久| 亚洲欧洲一区二区三区在线观看| 亚洲一区二区三区视频播放| 国产色综合天天综合网| 久久综合久久综合这里只有精品| 亚洲二区在线视频| 亚洲欧美国产日韩中文字幕| 国产一区二区三区丝袜| 欧美凹凸一区二区三区视频| 亚洲桃色在线一区| 欧美激情影音先锋| 欧美一二三区精品| 亚洲精品乱码| 国内精品一区二区三区| 欧美精品少妇一区二区三区| 亚洲综合日韩| 亚洲国产一区在线| 欧美在线1区| av成人免费在线观看| 国产日本欧美一区二区三区在线| 欧美国产91| 久久爱另类一区二区小说| 最新中文字幕亚洲| 久久婷婷人人澡人人喊人人爽| 夜夜嗨av一区二区三区四季av| 国产亚洲精品久久久| 欧美日韩一区二区三区在线视频| 久久精品一区二区三区不卡| 99热这里只有精品8| 欧美成人久久| 久久精品国产久精国产爱| 一区二区三区欧美在线| 亚洲韩日在线| 亚洲第一二三四五区| 国产一区二区三区久久悠悠色av| 欧美日韩精品免费观看视一区二区 | 99视频精品|