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

天行健 君子當自強而不息

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


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


公告

導航

統計

常用鏈接

隨筆分類(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>
            激情视频一区二区三区| 国产精品美女主播| 欧美日韩亚洲激情| 国产精品久久久久影院色老大| 在线观看精品| 亚洲最快最全在线视频| 久久福利资源站| 免费在线观看一区二区| 老色批av在线精品| 日韩一区二区福利| 美女主播视频一区| 国产精品va在线播放我和闺蜜| 国产欧美一区二区精品婷婷 | 日韩午夜视频在线观看| 欧美激情视频网站| 国外成人在线视频| 欧美一区二区女人| 亚洲视频中文字幕| 欧美精品一区二区在线观看| 亚洲大胆人体在线| 久久色在线观看| 亚洲欧美日韩国产综合| 国产精品久久久久久久久婷婷| 欧美一区观看| 久久野战av| 伊人婷婷欧美激情| 亚洲精品日韩久久| 欧美成人日本| 亚洲品质自拍| 亚洲日本欧美天堂| 美国十次了思思久久精品导航| 久久一区亚洲| 午夜精品国产更新| 亚洲欧洲99久久| 亚洲精品国产欧美| 亚洲精品影视| 影音先锋另类| 亚洲视屏一区| 日韩视频三区| 一区二区欧美日韩视频| 国产精品视频一二三| 欧美在线视频一区| 欧美精品在线网站| 美国三级日本三级久久99| 国产精品久久久久久久久久妞妞| 久久伊人一区二区| 免费影视亚洲| 一区二区三区视频在线看| 亚洲作爱视频| 国产婷婷97碰碰久久人人蜜臀| 久久久综合香蕉尹人综合网| 欧美日韩午夜精品| 欧美激情精品久久久久久久变态 | 亚洲毛片在线观看.| 国产综合婷婷| 欧美激情视频在线播放| 国产视频久久| 午夜精品一区二区三区在线播放| 国产在线精品自拍| 亚洲欧美韩国| 亚洲国产精品尤物yw在线观看| 亚洲国产福利在线| 国产精品v欧美精品v日韩精品 | 99精品国产在热久久婷婷| 99国内精品久久| 在线亚洲国产精品网站| 欧美金8天国| 亚洲美洲欧洲综合国产一区| 亚洲日本aⅴ片在线观看香蕉| 麻豆精品在线观看| 亚洲大胆视频| a4yy欧美一区二区三区| 欧美精品v国产精品v日韩精品 | 久久免费观看视频| 国模精品一区二区三区色天香| 午夜在线不卡| 久久久亚洲综合| 在线成人小视频| 裸体女人亚洲精品一区| 亚洲第一精品电影| 亚洲最新色图| 国产精品欧美久久久久无广告| 99国产麻豆精品| 欧美在线日韩在线| 国产一区二区日韩精品| 亚洲美女视频网| 亚洲欧美日韩一区二区| 欧美国产一区二区| 久久只有精品| 亚洲欧洲三级电影| 欧美日韩一区二区三区| 亚洲伊人一本大道中文字幕| 91久久久久| 欧美日韩岛国| 亚洲精品精选| 欧美一区二粉嫩精品国产一线天| 国模私拍视频一区| 麻豆成人91精品二区三区| 日韩亚洲欧美成人一区| 欧美一区二区三区免费观看视频| 国内一区二区三区| 欧美激情综合色综合啪啪| 亚洲影院色无极综合| 久色成人在线| 亚洲欧美国产视频| 激情一区二区三区| 欧美天堂亚洲电影院在线播放| 亚洲精品精选| 久久久精品免费视频| 国产一级揄自揄精品视频| 美女国产一区| 午夜精品久久久久久| 亚洲国产电影| 欧美综合第一页| 99视频精品在线| 一区视频在线播放| 国产精品视频精品| 欧美另类专区| 久久裸体视频| 欧美国产日韩亚洲一区| 欧美一级午夜免费电影| 国产日韩欧美另类| 欧美日韩性生活视频| 久久黄色影院| 欧美激情五月| 久久中文字幕导航| 亚洲欧美在线x视频| 日韩亚洲欧美在线观看| 在线观看视频欧美| 国产丝袜一区二区| 国产精品青草综合久久久久99| 欧美黄色小视频| 老鸭窝毛片一区二区三区| 亚洲精品乱码久久久久久黑人| 久久婷婷丁香| 久久久亚洲欧洲日产国码αv| 亚洲一区视频在线观看视频| 国产精品日本| 国产精品卡一卡二卡三| 欧美日韩国产丝袜另类| 欧美黄色免费网站| 欧美不卡视频一区| 欧美v日韩v国产v| 久久久久九九九| 久久精品国产免费看久久精品| 亚洲免费在线播放| 午夜性色一区二区三区免费视频| 亚洲毛片视频| 亚洲视频一区在线| 欧美成人69| 欧美成人资源| 久久国产精品黑丝| 久久成人这里只有精品| 欧美在线视频a| 久久久精品国产99久久精品芒果| 欧美在线视频一区二区三区| 香蕉av福利精品导航| 午夜欧美不卡精品aaaaa| 欧美一级成年大片在线观看| 欧美专区在线观看| 久久综合网络一区二区| 欧美激情精品久久久久久| 亚洲人成亚洲人成在线观看| 亚洲人成人一区二区三区| 一区二区三区不卡视频在线观看| 一区二区免费在线观看| 欧美制服丝袜| 亚洲欧美日韩一区在线| 久久成人综合网| 欧美国产极速在线| 国产精品高潮呻吟| 欧美日韩性生活视频| 国产精品一区二区女厕厕| 国产精品va| 很黄很黄激情成人| 亚洲黄页视频免费观看| 亚洲一区二区免费在线| 亚洲一级片在线观看| 久久精品国产精品 | 伊人久久成人| 99re6热在线精品视频播放速度| 亚洲一区二区动漫| 亚洲私拍自拍| 亚洲成人资源| 国产午夜精品全部视频播放 | 亚洲国产成人久久综合| 亚洲视频一二区| 欧美在线精品免播放器视频| 久久久久久一区二区| 欧美视频不卡| 国内外成人免费激情在线视频网站| 精品动漫一区| 欧美亚洲综合在线| 欧美成人免费小视频| 欧美va天堂| 亚洲资源在线观看| 米奇777在线欧美播放| 欧美区一区二| 国内精品福利| 亚洲视频在线看|