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

天行健 君子當自強而不息

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>
            久久九九热re6这里有精品| 性久久久久久久久久久久| 久久综合国产精品台湾中文娱乐网| 精品1区2区| 欧美激情一区二区三级高清视频 | av不卡在线| 亚洲精品在线免费观看视频| 欧美午夜精品一区| 亚洲欧洲一二三| 亚洲精品欧美专区| 国产欧美日本| 免费中文字幕日韩欧美| 欧美激情乱人伦| 亚洲午夜精品久久| 久久精品国产99国产精品澳门| 一区二区视频欧美| 亚洲最新视频在线播放| 韩国精品一区二区三区| 亚洲欧洲日产国码二区| 国产精品网站一区| 欧美风情在线观看| 国产精品五月天| 亚洲国内欧美| 国产午夜精品一区二区三区视频 | 夜夜嗨av一区二区三区网页| 亚洲一区国产精品| 亚洲精品视频一区| 久久成人免费电影| 亚洲一区黄色| 你懂的成人av| 久久爱www.| 欧美调教vk| 亚洲高清不卡av| 国产一区二区三区黄视频| 日韩一区二区高清| 亚洲国产高清在线| 欧美在线黄色| 午夜一区不卡| 欧美日韩一区在线观看| 亚洲成色777777在线观看影院| 欧美激情一区在线观看| 国产欧美日韩精品专区| 亚洲人体1000| 亚洲乱码精品一二三四区日韩在线| 欧美亚洲尤物久久| 亚洲欧美日韩专区| 欧美色精品天天在线观看视频| 欧美不卡在线视频| 影音欧美亚洲| 久久精品国产99精品国产亚洲性色| 欧美亚洲日本网站| 国产精品久久久久9999| 亚洲精品少妇| 日韩午夜精品| 欧美久久99| 亚洲精品久久久一区二区三区| 亚洲人被黑人高潮完整版| 麻豆精品传媒视频| 欧美成人国产| 亚洲精品国产精品国自产观看| 欧美a一区二区| 欧美韩日一区二区三区| 亚洲欧洲日本专区| 欧美韩日一区二区| 亚洲伦伦在线| 亚洲一区免费观看| 国产精品夜夜夜| 欧美中文日韩| 欧美二区在线看| 亚洲精选视频免费看| 欧美人成在线| 亚洲无线观看| 久久狠狠一本精品综合网| 国产主播一区二区三区| 久久精品国产免费| 亚洲国产精品小视频| 亚洲免费大片| 国产精品久久二区二区| 亚洲欧美成人一区二区三区| 久久精品国产精品亚洲| 亚洲福利视频一区二区| 欧美剧在线观看| 亚洲女同性videos| 免费看黄裸体一级大秀欧美| 亚洲精品一区二区三区樱花 | 久久www成人_看片免费不卡| 久久美女性网| 99视频有精品| 国产热re99久久6国产精品| 久热精品视频在线观看一区| 亚洲精品一区中文| 久久精品一区四区| 99这里只有久久精品视频| 国产精品呻吟| 欧美国产欧美综合| 午夜精品免费| 亚洲日本va午夜在线电影| 久久国产欧美| 在线一区观看| 在线看欧美视频| 国产精品国产三级国产专播品爱网 | 99国产精品国产精品毛片| 国产精品理论片| 麻豆av一区二区三区久久| 亚洲一区二区免费| 亚洲福利视频二区| 亚洲免费高清| 欧美丝袜一区二区三区| 欧美在线视频导航| 日韩视频一区二区在线观看 | 亚洲欧美国产精品va在线观看| 韩国成人精品a∨在线观看| 欧美午夜无遮挡| 久久婷婷激情| 欧美亚洲日本一区| 亚洲激情综合| 免费视频一区| 久久久久久成人| 亚洲欧美日韩精品在线| 99视频一区二区| 亚洲精品久久久蜜桃| 极品尤物久久久av免费看| 国产精品羞羞答答| 欧美日本韩国一区| 欧美v日韩v国产v| 久久精品人人| 欧美中日韩免费视频| 亚洲欧美在线免费| 亚洲自拍另类| 亚洲在线免费| 亚洲欧美精品在线观看| 亚洲午夜精品网| 亚洲视频一区二区| 一本一道久久综合狠狠老精东影业| 亚洲国产另类久久精品| 欧美激情中文字幕在线| 亚洲高清激情| 亚洲日本中文字幕区| 亚洲欧洲在线播放| 亚洲精品乱码久久久久久蜜桃91 | 欧美国产免费| 欧美日韩国产影院| 欧美日韩亚洲免费| 国产精品久久久久久久午夜| 国产精品女主播在线观看| 国产精品色网| 国产日韩亚洲欧美精品| 国模大胆一区二区三区| 国模私拍视频一区| 亚洲大片av| 夜夜嗨av一区二区三区四区| 亚洲一区二区三区色| 欧美一级片在线播放| 久久久www| 欧美激情视频一区二区三区在线播放| 亚洲第一在线综合网站| 亚洲精品国产欧美| 亚洲一区网站| 久久久久99| 欧美精品自拍偷拍动漫精品| 国产精品久久久999| 国产亚洲综合在线| 亚洲人成在线影院| 午夜天堂精品久久久久 | 亚洲欧美网站| 老鸭窝亚洲一区二区三区| 亚洲黄色视屏| 亚洲一区中文字幕在线观看| 久久久久久久一区| 欧美日韩在线精品一区二区三区| 国产精品区二区三区日本| 在线精品观看| 亚洲欧美国产制服动漫| 欧美成人亚洲成人日韩成人| 亚洲免费观看| 久久久久网址| 国产精品麻豆va在线播放| 亚洲国产91色在线| 性欧美暴力猛交另类hd| 欧美黄色一级视频| 午夜精品久久久久久久蜜桃app| 蜜桃精品一区二区三区| 国产精品普通话对白| 亚洲精品小视频| 久久久久久穴| 中文欧美在线视频| 欧美大片在线看| 国内精品国产成人| 亚洲欧美卡通另类91av| 欧美国产91| 久久成人精品无人区| 国产精品激情| 亚洲视频1区2区| 亚洲二区在线观看| 久久久噜噜噜久噜久久| 国产情人综合久久777777| 亚洲视频网站在线观看| 最新国产成人av网站网址麻豆 | 亚洲精品国产精品国产自| 久久精品国产成人|