• <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>

            天行健 君子當(dāng)自強(qiáng)而不息

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

            新建網(wǎng)頁 1 13

            13.5在地形上“行走”

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

            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;

            現(xiàn)在我們將取得方格的四個(gè)頂點(diǎn)的高度。

                   // 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);

            現(xiàn)在我們知道了方格的四個(gè)頂點(diǎn)的高度,我們需要找到照相機(jī)所在的位置的方格的高度,因?yàn)橐粋€(gè)方格可能同時(shí)向幾個(gè)方向傾斜,這可能會稍微難一點(diǎn),見圖 13.10:

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

            自從用行和列來描述我們所在的方格左上頂點(diǎn)的位置以來,我們必須沿x軸平移-col個(gè)單位,并沿z軸平移-row個(gè)單位。沿著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.

            注意:我們只關(guān)心改變的高度值,我們只修改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函數(shù)是一個(gè)沿著一維直線的基本線性插值算法,實(shí)現(xiàn)如下:

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


            繪制函數(shù):

                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 閱讀(1850) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評論

            久久亚洲AV成人无码国产| 久久se精品一区二区| 伊人色综合久久天天网| 亚洲人成无码久久电影网站| 久久狠狠爱亚洲综合影院| 久久精品国产亚洲AV高清热| 国产精品综合久久第一页| 久久国产精品无| 99久久国产综合精品麻豆| 久久久精品人妻无码专区不卡| 7777精品久久久大香线蕉| 中文字幕一区二区三区久久网站| 精品国产日韩久久亚洲| 国内精品伊人久久久久影院对白| 婷婷久久精品国产| 99久久精品国产一区二区蜜芽 | 国产69精品久久久久APP下载| 97久久国产露脸精品国产| 国产精品无码久久四虎| 久久久久无码精品国产| 欧美成人免费观看久久| 国产精品VIDEOSSEX久久发布| 久久99精品久久只有精品| 色狠狠久久综合网| 久久天天躁狠狠躁夜夜2020老熟妇| 国产亚洲综合久久系列| 亚洲国产精品无码久久久不卡| 亚洲精品久久久www| 国产精品成人无码久久久久久| 久久精品人人槡人妻人人玩AV| 久久久国产精华液| 久久午夜夜伦鲁鲁片免费无码影视 | 国产精品伦理久久久久久| AV狠狠色丁香婷婷综合久久| 囯产精品久久久久久久久蜜桃| 伊人久久五月天| 亚洲一区精品伊人久久伊人| 香港aa三级久久三级老师2021国产三级精品三级在 | 麻豆成人久久精品二区三区免费| 污污内射久久一区二区欧美日韩| 四虎影视久久久免费观看|