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

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

創(chuàng)建3D圖形引擎(3)


本篇是創(chuàng)建3D圖形引擎(2)的續(xù)篇,3D圖形引擎的代碼以創(chuàng)建游戲內(nèi)核中編寫(xiě)的代碼為基礎(chǔ)進(jìn)行開(kāi)發(fā)。


高級(jí)三維引擎的開(kāi)發(fā)

在每幀中繪制所有的多邊形是非常低效的,為了提高處理的速度,可以僅渲染那些位于視野內(nèi)的多邊形,同時(shí)應(yīng)避免掃描場(chǎng)景中的每個(gè)多邊形來(lái)確定哪些多邊形是可見(jiàn)的。 如果不每幀進(jìn)行搜索,又如何知道哪些多邊形是位于視野內(nèi)呢?解決的方法就是將一個(gè)三維模型分解為一些較小的塊(稱為節(jié)點(diǎn)nodes),其容納較少的多邊形。然后將節(jié)點(diǎn)排列到一個(gè)特定的結(jié)構(gòu)中(一棵樹(shù)),以便進(jìn)行快速搜 索,并確定哪個(gè)節(jié)點(diǎn)是可見(jiàn)的,然后渲染這些可見(jiàn)的節(jié)點(diǎn), 通過(guò)使用視錐,可以找出哪些節(jié)點(diǎn)是可見(jiàn)的。取代搜索數(shù)千個(gè)多邊形,通過(guò)搜索一個(gè)小小的節(jié)點(diǎn)集合,就能決定怎樣進(jìn)行繪制。節(jié)點(diǎn)樹(shù)引擎適用于任何的網(wǎng)格模型,并將它拆分為節(jié)點(diǎn),以便快速渲染網(wǎng)格模型(網(wǎng)格模型代表了游戲的層次)。

 

NodeTree引擎的介紹

NodeTree引擎非常通用,因?yàn)樗梢栽趦煞N不同的模式下操作(對(duì)于節(jié)點(diǎn)的拆分):四叉樹(shù)(quadtree)和八叉樹(shù)(octree)模式。四叉樹(shù)模式將世界(及隨后的節(jié)點(diǎn))一次拆分為4個(gè)節(jié)點(diǎn),這種模式最適合y軸變化不大的層次網(wǎng)格模型(即觀察點(diǎn)的高度并沒(méi)有太大的變化)。而八叉樹(shù)將世界(及隨后的節(jié)點(diǎn))一次拆分為8個(gè)節(jié)點(diǎn),使用這種模式,大型三維網(wǎng)格模型中的觀察點(diǎn)可以被移動(dòng)到世界中的任意位置。如下圖所示:

使用哪一種拆分模式由自己決定,考慮自己的網(wǎng)格模型,是否要搜索一座城堡,或者進(jìn)入一個(gè)洞穴,或在風(fēng)景中漫游?如果網(wǎng)格模型在高度上并沒(méi)有太大的變化(例如風(fēng)景),四叉樹(shù)模式最好。如果網(wǎng)格模型的每一條軸線都要擴(kuò)展開(kāi)來(lái)(例如一個(gè)擁有許多層次的城堡),則適合使用八叉樹(shù)模式。

世界(它被表示為包含了所有多邊形的一個(gè)立方體)可以不斷地被拆分為更小的、尺寸大小相同的節(jié)點(diǎn)。四叉樹(shù)在二維空間中將節(jié)點(diǎn)進(jìn)行拆分(使用x軸和z軸),而八叉樹(shù)在三維空間中將節(jié)點(diǎn)進(jìn)行拆分(使用所有的軸線)。一個(gè)節(jié)點(diǎn)代表了一組多邊形,同時(shí)也代表了三維空間中的一個(gè)區(qū)域。每個(gè)節(jié)點(diǎn)可以包含另外的節(jié)點(diǎn),而每個(gè)其后的節(jié)點(diǎn)也可以成為一個(gè)更小節(jié)點(diǎn)的父節(jié)點(diǎn)。通常,三維世界被認(rèn)為是根節(jié)點(diǎn)(root node,最頂層的節(jié)點(diǎn),其他所有的節(jié)點(diǎn)都屬于它)。對(duì)于節(jié)點(diǎn)和樹(shù),有一些技巧,通過(guò)確定哪些多邊形被包含在一個(gè)節(jié)點(diǎn)的三維空間里,可以將它們進(jìn)行分組,然后從根節(jié)點(diǎn)開(kāi)始,可以快速遍歷樹(shù)中的每個(gè)節(jié)點(diǎn)。

 

創(chuàng)建節(jié)點(diǎn)和樹(shù)

創(chuàng)建節(jié)點(diǎn)并構(gòu)造樹(shù)形結(jié)構(gòu),需要先對(duì)網(wǎng)格模型中的每個(gè)多邊形進(jìn)行檢查,只需做一次檢查而已,所以它不會(huì)成為影響渲染速度的一個(gè)因素。這樣做的目的是決定如何對(duì)樹(shù)中的節(jié)點(diǎn)進(jìn)行安排。網(wǎng)格模型中的每個(gè)多邊形被包圍在一個(gè)盒子里(稱為框界盒子,如下圖所示)。這個(gè)盒子代表了多邊形在任何方向上的范圍,只要多邊形的盒子被包圍在一個(gè)節(jié)點(diǎn)的三維空間里(完全或部分的),那么該多邊形就屬于這個(gè)節(jié)點(diǎn),一個(gè)多邊形可以屬于多個(gè)節(jié)點(diǎn),因?yàn)槎噙呅蔚姆秶赡軙?huì)穿過(guò)許多節(jié)點(diǎn)。

將多邊形分組為節(jié)點(diǎn)時(shí),需注意多邊形所在的空間是否很大,或者在一個(gè)很大的空間中是否有太多的多邊形,如果是就需要將節(jié)點(diǎn)拆分為更多的子節(jié)點(diǎn),然后再次搜索多邊形列表,將新的節(jié)點(diǎn)放入計(jì)算,繼續(xù)這個(gè)處理過(guò)程,直到所有的多邊形的分組足夠小,并且每個(gè)包含的多邊形樹(shù)也足夠少。為了優(yōu)化樹(shù)形結(jié)構(gòu),放棄所有那些沒(méi)有包含多邊形的節(jié)點(diǎn),刪除空的節(jié)點(diǎn)可以節(jié)省內(nèi)存,同時(shí)可以加快樹(shù)形結(jié)構(gòu)的搜索。

如下圖所示,可以將根節(jié)點(diǎn)拆分為4個(gè)較小的節(jié)點(diǎn)(使之成為一個(gè)四叉樹(shù)的節(jié)點(diǎn)),然后檢測(cè)每個(gè)節(jié)點(diǎn),并不斷地拆分其中較大的節(jié)點(diǎn),跳過(guò)空節(jié)點(diǎn)以便加快處理速度,最后得到一個(gè)完美的樹(shù)形結(jié)構(gòu)以 方便以后搜索。

 

搜索及繪制樹(shù)

如果構(gòu)成三維空間節(jié)點(diǎn)的那8個(gè)頂點(diǎn)中的任一個(gè)(可以被看作是立方體的拐角)位于視錐內(nèi),或者如果視錐自身是被包含在一個(gè)節(jié)點(diǎn)里,那么該節(jié)點(diǎn)就被認(rèn)為是位于視野之內(nèi)的。在確定了一個(gè)節(jié)點(diǎn)是可見(jiàn)的之后,對(duì)它的子節(jié)點(diǎn)(如果有的話)執(zhí)行同樣的檢測(cè),如果一個(gè)節(jié)點(diǎn)并不包含子節(jié)點(diǎn),則檢測(cè)當(dāng)前節(jié)點(diǎn)是否包含了沒(méi)有被繪制的多邊形。當(dāng)一個(gè)節(jié)點(diǎn)中的多邊形被繪制后,它們被標(biāo)識(shí)為已被繪制,并返回父節(jié)點(diǎn),同時(shí)搜索其余的子節(jié)點(diǎn)。在處理過(guò)程中可以看到,較高層次的節(jié)點(diǎn)連同它們的子節(jié)點(diǎn)一起被拋棄,以這種方式,就可以在渲染過(guò)程中刪除數(shù)千的多邊形,從而節(jié)省時(shí)間。如果一個(gè)節(jié)點(diǎn)被完全地包含在視錐里,那 就可以不用再搜索節(jié)點(diǎn)的任何子節(jié)點(diǎn),因?yàn)樗麄円餐耆话谝曞F里了。

當(dāng)使用Direct3D和樹(shù)形結(jié)構(gòu)時(shí),會(huì)發(fā)現(xiàn)一個(gè)網(wǎng)格模型可以包含多重的材質(zhì),但轉(zhuǎn)換材質(zhì)會(huì)是開(kāi)銷很大的操作(特別當(dāng)每個(gè)材質(zhì)使用了不同的紋理時(shí)),因此要謹(jǐn)慎處理。那么如何繪制所有可見(jiàn)的多邊形,又不用一次又一次地轉(zhuǎn)換材質(zhì)呢(即使材質(zhì)已經(jīng)被使用了)?這就是材質(zhì)分組的作用。材質(zhì)分組(material group)就是多邊形的集合,根據(jù)它們所指定的材質(zhì)被分組到一起。因?yàn)橐粋€(gè)網(wǎng)格模型可以包含多重材質(zhì),所以在一個(gè)指定的時(shí)間中,僅渲染那些屬于指定材質(zhì)的多邊形分組。以這種方式,僅需要設(shè)置一次所使用的材質(zhì)(以及隨后的紋理,如果存在的話),渲染使用材質(zhì)的多邊形,并繼續(xù)處理下一個(gè)材質(zhì)。

盡管材質(zhì)分組的使用聽(tīng)起來(lái)合乎邏輯,但多邊形根據(jù)材質(zhì)進(jìn)行分組使其很難處理NodeTree信息,不對(duì)樹(shù)形結(jié)構(gòu)進(jìn)行搜索就不知道哪些多邊形將被繪制。所以必須搜索樹(shù)形結(jié)構(gòu),并構(gòu)造需要被渲染的多邊形列表。完成搜索后,僅檢測(cè)那些屬于材質(zhì)的多邊形列表,并使用它們。材質(zhì)分組并沒(méi)有什么影響,只是要那些被繪制的多邊形更有次序。

 

創(chuàng)建NedeTree類

定義:

typedef unsigned long   ulong;
typedef unsigned 
short  ushort;
typedef unsigned 
char   uchar;
typedef 
char*           char_ptr;
typedef 
const char*     pcstr;
typedef unsigned 
long*  ulong_ptr; 
typedef unsigned 
short* ushort_ptr;

// enumerate the two types of tree structures
enum TREE_TYPES { QUADTREE = 0, OCTREE };

//=====================================================================================
// This calss encapsulate how to divide world space.
//=====================================================================================
typedef class NODE_TREE_MESH
{
private:    
    
// The VERTEX_INFO structure is a custom vertex structure than contains only the 3D coordinates.
    // This is used to retrieve coordinate information from a mesh's vertex buffer.
    typedef struct VERTEX
    {
        
float x, y, z;
    } *VERTEX_PTR;

    
// The POLYGON_INFO structure maintains a material group index,
    // the time it was last drawn (so youo don't redraw it many times over per frame),
    // and the three vertices used to render the polygon (which you'll read on later).
    typedef struct POLYGON
    {
        
ulong   mg_index;           // material group index
        ulong   render_timer;

        
ushort  vertex_index_0;
        
ushort  vertex_index_1;
        
ushort  vertex_index_2;

        POLYGON()
        {
            memset(
this, 0, sizeof(*this));
        }
    } *POLYGON_PTR;

    
// The node structure keeps count of the number of polygons in its 3D space, polygon index list,
    // the 3D coordinates of the node (as well as the radius, which is the distance from the center to
    // one edge making the node a perfect cube), and pointers to the child nodes.
    typedef struct NODE
    {
        
float       x_pos, y_pos, z_pos;   // center coordinate of node
        float       diameter;              // radius of node

        
ulong       num_polys;              // number of polygons in node
        ulong_ptr   poly_index_list;        // polygon index list

        NODE*       child_nodes[8];         
// child nodes information 4 = quad, 8 = oct.

        // constructor used to clear out variables
        NODE()
        {
            memset(
this, 0, sizeof(*this));
        }

        
// destructor to clear child nodes and variables
        ~NODE()
        {
            delete[] poly_index_list;
            poly_index_list = NULL;

            
// delete child nodes
            for(short i = 0; i < 8; i++)
            {
                delete child_nodes[i];
                child_nodes[i] = NULL;
            }
        }
    } *NODE_PTR;

    
// The material group structure uses IDirect3DIndexBuffer9 to store polygons vertex index
    // that need to be rendered in a single frame, also it maintains the number of polygons in
    // a material group and how many polygons to draw each frame.
    typedef struct MATERIAL_GROUP
    {
        
ulong   num_polys;          // number of polygons in group
        ulong   num_polys_to_draw;  // number of polygons to draw

        IDirect3DIndexBuffer9*  index_buffer; 
        ushort_ptr              index_ptr;

        
// clear out member data
        MATERIAL_GROUP()
        {
            memset(
this, 0, sizeof(*this));
        }

        
// free index buffer
        ~MATERIAL_GROUP()
        {
            
if(index_buffer)
                index_buffer->Release();

            index_buffer = NULL;
        }
    } *MATERIAL_GROUP_PTR;

private:
    
int                 m_tree_type;            // type of nodetree (QUADTREE or OCTREE)

    GRAPHICS_PTR        m_graphics;             
// parent graphics object
    FRUSTUM_PTR         m_frustum;              // viewing frustum

    
float               m_world_cube_diameter;  // diameter of world cube
    float               m_node_max_diameter;    // maximum node diameter

    NODE_PTR            m_root_node;            
// node list

    
ulong               m_num_mg;               // number of material group
    MATERIAL_GROUP_PTR  m_mg_list;              // material group list

    
ulong               m_max_polys_per_node;   // maximum number of polygons per node allow

    
ulong               m_num_polys;            // number of polygons in scene
    POLYGON_PTR         m_poly_list;            // list of polygons

    
ulong               m_render_timer;         // current draw timer

    S_MESH_PTR          m_root_mesh;            
// pointer to root mesh
    char_ptr            m_vertex_ptr;           // pointer to mesh vertices
    ulong               m_vertex_fvf;           // mesh vertex FVF
    ulong               m_num_bytes_per_vertex; // num bytes per vertex

private:
    
void _sort_node(NODE_PTR node,
                    
float x_pos, float y_pos, float z_pos,
                    
float diameter);

    
void _add_node(NODE_PTR node);

   BOOL _polygon_containe_in_node(POLYGON_PTR poly, 
                                  
float x_pos, float y_pos, float z_pos,
                                  
float diameter);

    
ulong _count_polygons_in_node(float x_pos, float y_pos, float z_pos,
                                  
float diameter);

public:
   NODE_TREE_MESH();
   ~NODE_TREE_MESH();

   BOOL create(GRAPHICS_PTR graphics, MESH_PTR mesh,
               
int tree_type = OCTREE, float node_max_diameter = 256.0f, long max_polys_per_node = 32);

   
void free();

   BOOL render(FRUSTUM_PTR frustum = NULL, 
float z_dist = 0.0f);

   
float get_closest_height(float x_pos, float y_pos, float z_pos);
   
float get_closest_height_below(float x_pos, float y_pos, float z_pos);
   
float get_closest_height_above(float x_pos, float y_pos, float z_pos);

   BOOL is_ray_intersect_mesh(
float x_start, float y_start, float z_start,
                        
float x_end, float y_end, float z_end,
                        
float* distance);    
} *NODE_TREE_MESH_PTR;

實(shí)現(xiàn):
//------------------------------------------------------------------------------
// Groups the polygons into nodes and splits the nodes into child nodes as needed.
//------------------------------------------------------------------------------
void NODE_TREE_MESH::_sort_node(NODE_PTR node,
                                
float x_pos, float y_pos, float z_pos,
                                
float diameter)
{
    
// error checking
    if(node == NULL)
        
return;

    
// store node coordinates and size
    node->x_pos    = x_pos;
    node->y_pos    = (m_tree_type == QUADTREE) ? 0.0f : y_pos;
    node->z_pos    = z_pos;
    node->diameter = diameter;

    
ulong num_polys_in_node;

    
// see if there are any polygons in the node
    if((num_polys_in_node = _count_polygons_in_node(x_pos, y_pos, z_pos, diameter)) == 0)
       
return;

    
// split node if diameter > m_node_max_diameter and too many polygons
    if(diameter > m_node_max_diameter && num_polys_in_node > m_max_polys_per_node)
    {
        
ulong divide_node_num = (m_tree_type == QUADTREE) ? 4 : 8;

        
for(ulong i = 0; i < divide_node_num; i++)
        {
            
float x_off = (((i % 2) < 1) ? -1.0f : 1.0f) * (diameter / 4);
            
float z_off = (((i % 4) < 2) ? -1.0f : 1.0f) * (diameter / 4);
            
float y_off = (((i % 8) < 4) ? -1.0f : 1.0f) * (diameter / 4);

            
// see if any polygons in new node boudning box
            if(_count_polygons_in_node(x_pos + x_off, y_pos + y_off, z_pos + z_off, diameter / 2))
            {
                node->child_nodes[i] = 
new NODE;    // create new child node

                // sort the polygons with the new child node
                _sort_node(node->child_nodes[i], x_pos + x_off, y_pos + y_off, z_pos + z_off, diameter / 2);
            }
        }

        
return;
    }

    
// allocate space for vertex index
    node->num_polys       = num_polys_in_node;
    node->poly_index_list = 
new ulong[num_polys_in_node];

    
// scan through polygon list, storing polygon index and assiging them.

    
ulong poly_index = 0;

    
for(ulong i = 0; i < m_num_polys; i++)
    {
        
// add polygon to node list if contained in 3D space
        if(_polygon_containe_in_node(&m_poly_list[i], x_pos, y_pos, z_pos, diameter))
            node->poly_index_list[poly_index++] = i;
    }
}

//------------------------------------------------------------------------------
// Check whether polygon is in node.
//------------------------------------------------------------------------------
BOOL NODE_TREE_MESH::_polygon_containe_in_node(POLYGON_PTR poly, 
                                               
float x_pos, float y_pos, float z_pos,
                                               
float diameter)
{
    
// get the polygon's vertices

    VERTEX_PTR vertex[3];

    vertex[0] = (VERTEX_PTR) &m_vertex_ptr[m_num_bytes_per_vertex * poly->vertex_index_0];
    vertex[1] = (VERTEX_PTR) &m_vertex_ptr[m_num_bytes_per_vertex * poly->vertex_index_1];
    vertex[2] = (VERTEX_PTR) &m_vertex_ptr[m_num_bytes_per_vertex * poly->vertex_index_2];

    
float x_min, x_max, y_min, y_max, z_min, z_max;

    
// check against x axis of specified 3D space
    
    x_min = min(vertex[0]->x, min(vertex[1]->x, vertex[2]->x));
    x_max = max(vertex[0]->x, max(vertex[1]->x, vertex[2]->x));

    
if(x_max < (x_pos - diameter / 2))
        
return FALSE;

    
if(x_min >  (x_pos + diameter / 2))
        
return FALSE;

    
// check against y axis of specified 3D space (only if octree tree type)
    if(m_tree_type == OCTREE)
    {
        y_min = min(vertex[0]->y, min(vertex[1]->y, vertex[2]->y));
        y_max = max(vertex[0]->y, max(vertex[1]->y, vertex[2]->y));

        
if(y_max < (y_pos - diameter / 2))
            
return FALSE;

        
if(y_min >  (y_pos + diameter / 2))
            
return FALSE;
    }

    
// check against z axis of specified 3D space

    z_min = min(vertex[0]->z, min(vertex[1]->z, vertex[2]->z));
    z_max = max(vertex[0]->z, max(vertex[1]->z, vertex[2]->z));

    
if(z_max < (z_pos - diameter / 2))
        
return FALSE;

    
if(z_min >  (z_pos + diameter / 2))
        
return FALSE;

    
return TRUE;
}

//------------------------------------------------------------------------------
// Count the number of polygons in node.
//------------------------------------------------------------------------------
ulong NODE_TREE_MESH::_count_polygons_in_node(float x_pos, float y_pos, float z_pos,
                                              
float diameter)
{
    
// return if no polygons to process
    if(m_num_polys == 0)
        
return 0;

    
// go through every polygon and keep count of those contained in the specified 3D space.

    
ulong poly_num_in_node = 0;

    
for(ulong i = 0; i < m_num_polys; i++)
    {
        
if(_polygon_containe_in_node(&m_poly_list[i], x_pos, y_pos, z_pos, diameter))
            poly_num_in_node++;
    }

    
return poly_num_in_node;
}

//------------------------------------------------------------------------------
// Adds a node into the list of nodes to draw.
//------------------------------------------------------------------------------
void NODE_TREE_MESH::_add_node(NODE_PTR node)
{
    
if(node == NULL)
        
return;

    
// perform frustum check based on tree type

    
float y_pos;

    
if(m_tree_type == QUADTREE)
        y_pos = 0.0f;
    
else
        y_pos = node->y_pos;

    
float node_radius = node->diameter / 2;
    BOOL  is_completely_contained = FALSE;

    
if(! m_frustum->is_rectangle_in(node->x_pos, y_pos, node->z_pos, 
                                    node_radius, node_radius, node_radius,
                                    &is_completely_contained))
    {
        
return;
    }

    
if(! is_completely_contained)
    {
        
// scan child nodes

        
short num = 0;
        
ulong child_nodes_num = (m_tree_type == QUADTREE) ? 4 : 8;

        
for(ulong i = 0; i < child_nodes_num; i++)
        {
            
if(node->child_nodes[i])
            {
                num++;
                _add_node(node->child_nodes[i]);
            }
        }

        
// do not need to go on if there was child nodes in this node
        if(num != 0)
            
return;
    }

    
// add contained polygons (if any)
    if(node->num_polys != 0)
    {
        
for(ulong i = 0; i < node->num_polys; i++)
        {
            
ulong poly_index = node->poly_index_list[i];

            
// get pointer to polygon
            POLYGON_PTR poly = &m_poly_list[poly_index];

            
// only draw if not done already 
            if(poly->render_timer != m_render_timer)
            {
                poly->render_timer = m_render_timer;

                
// get material group index of polygon
                ulong mg_index = poly->mg_index;

                
// make sure group is okay and material is not transparent
                if(mg_index < m_num_mg && m_root_mesh->m_materials[mg_index].Diffuse.a != 0.0f)
                {
                    
// copy polygon's vertex indices into index buffer
                    *m_mg_list[mg_index].index_ptr++ = poly->vertex_index_0;
                    *m_mg_list[mg_index].index_ptr++ = poly->vertex_index_1;
                    *m_mg_list[mg_index].index_ptr++ = poly->vertex_index_2;

                    
// increase count of polygons to draw in group
                    m_mg_list[mg_index].num_polys_to_draw++;
                }
            }
        }
    }
}

//------------------------------------------------------------------------------
// Constructor, initialize member data.
//------------------------------------------------------------------------------
NODE_TREE_MESH::NODE_TREE_MESH()
{
    memset(
this, 0, sizeof(*this));

    m_tree_type = OCTREE;
}

//------------------------------------------------------------------------------
// Destructor, release allocated memory.
//------------------------------------------------------------------------------
NODE_TREE_MESH::~NODE_TREE_MESH()
{
    free();
}

//------------------------------------------------------------------------------
// Release allocated memory.
//------------------------------------------------------------------------------
void NODE_TREE_MESH::free()
{
    delete m_root_node;
    m_root_node = NULL;

    m_num_polys = 0;
    delete[] m_poly_list;
    m_poly_list = NULL;

    m_num_mg = 0;
    delete[] m_mg_list;
    m_mg_list = NULL;

    m_graphics = NULL;
}

//------------------------------------------------------------------------------
// Create a node-tree mesh from a source MESH object and free old node-tree mesh, 
// specifying the maximum number of polygons in an area than the specific size 
// which forcing node splits.
//------------------------------------------------------------------------------
BOOL NODE_TREE_MESH::create(GRAPHICS_PTR graphics, MESH_PTR mesh,
                            
int tree_type, float node_max_diameter, long max_polys_per_node)
{
    
// free a prior mesh
    free();

    
// error checking
    if((m_graphics = graphics) == NULL)
        
return FALSE;

    
if(mesh == NULL || mesh->get_root_mesh()->m_num_materials == 0)
        
return FALSE;

    
// get mesh information

    m_root_mesh = mesh->get_root_mesh();

    ID3DXMesh* d3d_mesh = m_root_mesh->m_mesh;

    m_vertex_fvf           = d3d_mesh->GetFVF();
    m_num_bytes_per_vertex = D3DXGetFVFVertexSize(m_vertex_fvf);
    m_num_polys            = d3d_mesh->GetNumFaces();
    m_max_polys_per_node   = max_polys_per_node;

    
// create the polygon list and group
    m_poly_list = new POLYGON[m_num_polys];
    m_num_mg    = m_root_mesh->m_num_materials;
    m_mg_list   = 
new MATERIAL_GROUP[m_num_mg];

    ushort_ptr  index_ptr;
    ulong_ptr   attr_list;

    
// lock the index and attribute buffers
    d3d_mesh->LockIndexBuffer(D3DLOCK_READONLY, (void**)&index_ptr);
    d3d_mesh->LockAttributeBuffer(D3DLOCK_READONLY, &attr_list);

    
// load polygon information into structures
    for(ulong i = 0; i < m_num_polys; i++)
    {
        
ulong mg_index = attr_list[i];  // material group index

        m_poly_list[i].vertex_index_0 = *index_ptr++;
        m_poly_list[i].vertex_index_1 = *index_ptr++;
        m_poly_list[i].vertex_index_2 = *index_ptr++;
        m_poly_list[i].mg_index       = mg_index;
        m_poly_list[i].render_timer   = 0;
                
        m_mg_list[mg_index].num_polys++;
    }

    
// unlock buffers
    d3d_mesh->UnlockAttributeBuffer();
    d3d_mesh->UnlockIndexBuffer();

    
// build the group vertex index buffers
    for(ulong i = 0; i < m_num_mg; i++)
    {
        
if(m_mg_list[i].num_polys != 0)
        {
            UINT index_buffer_length = m_mg_list[i].num_polys * 3 * 
sizeof(ushort);

            m_graphics->get_device_com()->CreateIndexBuffer(index_buffer_length, D3DUSAGE_WRITEONLY,
                D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_mg_list[i].index_buffer, NULL);
        }
    }

    
// get the size of the bounding cube

    
float max_x, max_y, max_z;

    max_x = (
float) max(fabs(m_root_mesh->m_min.x), fabs(m_root_mesh->m_max.x));
    max_y = (
float) max(fabs(m_root_mesh->m_min.y), fabs(m_root_mesh->m_max.y));
    max_z = (
float) max(fabs(m_root_mesh->m_min.z), fabs(m_root_mesh->m_max.z));

    m_world_cube_diameter = max(max_x, max(max_y, max_z)) * 2.0f;
    m_node_max_diameter   = node_max_diameter;

    
// create the root node
    m_root_node = new NODE;

    
// sort polygons into nodes

    d3d_mesh->LockVertexBuffer(D3DLOCK_READONLY, (
void**)&m_vertex_ptr);

    _sort_node(m_root_node, 0.0f, 0.0f, 0.0f, m_world_cube_diameter);

    d3d_mesh->UnlockVertexBuffer();

    m_render_timer = 0;

    
return TRUE;
}

//------------------------------------------------------------------------------
// Render the current view using view transformation and overloaded distance of view.
// Also specify to use a pre-calculate frustum or force a calculation of own frustum.
//------------------------------------------------------------------------------
BOOL NODE_TREE_MESH::render(FRUSTUM_PTR frustum, float z_dist)
{
    
// error checking
    if(m_graphics == NULL || m_root_node == NULL || m_num_polys == 0)
        
return FALSE;

    
// construct the viewing frustum (if none passed)
    if((m_frustum = frustum) == NULL)
    {
        FRUSTUM view_frustum;  
// local viewing frustumn
        view_frustum.construct(m_graphics, z_dist);

        m_frustum = &view_frustum;
    }

    IDirect3DDevice9* d3d_device = m_graphics->get_device_com();

    D3DXMATRIX matrix;   
// matrix used for calculations

    // set the world transformation matrix to identity,
    // so that level mesh is rendered around the origin it was disigned.
    D3DXMatrixIdentity(&matrix);
    d3d_device->SetTransform(D3DTS_WORLD, &matrix);

    
// lock material group index buffer
    for(ulong i = 0; i < m_num_mg; i++)
    {
        
if(m_mg_list[i].num_polys != 0)
        {
            UINT total_vert_index_size = m_mg_list[i].num_polys * 3 * 
sizeof(ushort);

            m_mg_list[i].index_buffer->Lock(0, total_vert_index_size, (
void**) &m_mg_list[i].index_ptr, 0);
        }       

        m_mg_list[i].num_polys_to_draw = 0;
    }

    
// increase render frame timer
    m_render_timer++;

    
// add polygons to be drawn into material group list
    _add_node(m_root_node);

    IDirect3DVertexBuffer9* vertex_buffer = NULL;

    
// get vertex buffer pointer
    m_root_mesh->m_mesh->GetVertexBuffer(&vertex_buffer);

    
// set vertex shader and source
    d3d_device->SetStreamSource(0, vertex_buffer, 0, m_num_bytes_per_vertex);
    d3d_device->SetFVF(m_vertex_fvf);  

    UINT num_vertices = m_root_mesh->m_mesh->GetNumVertices();

    
// unlock vertex buffers and draw
    for(ulong i = 0; i < m_num_mg; i++)
    {
        
if(m_mg_list[i].num_polys != 0)
            m_mg_list[i].index_buffer->Unlock();

        
if(m_mg_list[i].num_polys_to_draw != 0)
        {
            UINT num_polys_to_draw = m_mg_list[i].num_polys_to_draw;

            d3d_device->SetMaterial(&m_root_mesh->m_materials[i]);
            d3d_device->SetTexture(0, m_root_mesh->m_textures[i]);
            d3d_device->SetIndices(m_mg_list[i].index_buffer);
            d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, num_vertices, 0, num_polys_to_draw);
        }
    }

    
// release vertex buffer
    if(vertex_buffer)
        vertex_buffer->Release();

    
return TRUE;
}
 

posted on 2007-10-24 00:30 lovedday 閱讀(882) 評(píng)論(3)  編輯 收藏 引用

評(píng)論

# 3D圖形引擎(3) 2008-01-17 18:10 瀟湘雨夢(mèng)

你好我是3D圖形引擎的新手,想看關(guān)于這方面的書(shū),請(qǐng)問(wèn)能推薦一下嗎  回復(fù)  更多評(píng)論   

# re: 創(chuàng)建3D圖形引擎(3) 2008-01-22 19:52 lovedday

據(jù)說(shuō)《3D Game Engine Programming》不錯(cuò),不過(guò)只有英文版的。  回復(fù)  更多評(píng)論   

# re: 創(chuàng)建3D圖形引擎(3) 2008-02-15 10:32 殘夢(mèng)

好東西 可惜我才開(kāi)始接觸這個(gè)東東 看著很費(fèi)勁 何況我用的是c#  回復(fù)  更多評(píng)論   


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


公告

導(dǎo)航

統(tǒng)計(jì)

常用鏈接

隨筆分類(178)

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

搜索

最新評(píng)論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲欧美不卡| 美女国内精品自产拍在线播放| 久久夜色精品亚洲噜噜国产mv| 午夜精品成人在线视频| 国产精品丝袜白浆摸在线| 国产精品99久久99久久久二8 | 国产一区二区高清视频| 久久久久成人精品| 久久嫩草精品久久久精品一| 亚洲国产欧美国产综合一区| 91久久精品国产91久久| 欧美成人免费大片| 亚洲伊人伊色伊影伊综合网| 亚洲欧美中文字幕| 亚洲国产经典视频| 99精品国产在热久久下载| 国产精品一二| 免费不卡亚洲欧美| 欧美色欧美亚洲另类七区| 久久精品日产第一区二区| 久久亚洲综合网| 亚洲午夜一区二区| 久久久伊人欧美| 亚洲视频成人| 久久夜色精品国产亚洲aⅴ| 一区二区不卡在线视频 午夜欧美不卡在 | 欧美日韩午夜激情| 久久亚洲精品欧美| 欧美体内she精视频在线观看| 久久精品一区四区| 欧美日在线观看| 欧美大片国产精品| 国产精品专区一| 亚洲精品久久久久久久久久久久| 国产精品日韩欧美大师| 亚洲国产成人在线视频| 国产亚洲欧美日韩一区二区| 亚洲国产精品一区二区久| 国产伦精品一区二区三区免费迷| 欧美高清视频一二三区| 国产精品天美传媒入口| 亚洲电影第1页| 国产视频精品va久久久久久| 一本色道久久综合亚洲精品婷婷 | 亚洲午夜精品视频| 欧美不卡高清| 嫩模写真一区二区三区三州| 国产精品视频男人的天堂| 亚洲精品中文字| 91久久精品国产91性色tv| 欧美一区免费视频| 久久国产手机看片| 国产精品爱久久久久久久| 亚洲精品免费在线| 日韩一级免费| 欧美国产一区二区在线观看| 欧美暴力喷水在线| 影音先锋一区| 久久伊人亚洲| 欧美jizz19性欧美| 亚洲第一久久影院| 麻豆av福利av久久av| 免费看黄裸体一级大秀欧美| 极品中文字幕一区| 这里只有精品视频| 一区二区三区波多野结衣在线观看| 另类亚洲自拍| 亚洲激情不卡| 一本色道综合亚洲| 欧美日一区二区在线观看 | 欧美偷拍一区二区| 亚洲视频成人| 久久国产日韩| 影音先锋日韩资源| 欧美高清视频在线观看| 亚洲免费播放| 亚洲欧美在线视频观看| 国产日韩在线不卡| 久久综合久久综合久久| 亚洲黄色性网站| 亚洲一区不卡| 国产亚洲福利| 免费欧美在线视频| 亚洲精品中文字| 久久精品主播| 亚洲精品一区二区三区四区高清| 欧美成人dvd在线视频| 日韩午夜电影在线观看| 欧美在线欧美在线| 亚洲国产日韩一级| 欧美三级乱码| 久久久不卡网国产精品一区| 亚洲风情亚aⅴ在线发布| 中文有码久久| 樱桃国产成人精品视频| 欧美日韩不卡一区| 玖玖玖免费嫩草在线影院一区| 亚洲国产精品ⅴa在线观看| 欧美另类高清视频在线| 亚洲欧美日韩第一区| 欧美二区乱c少妇| 亚洲欧美国产日韩中文字幕| 极品尤物久久久av免费看| 欧美日韩国产免费观看| 欧美影院一区| 欧美一级视频免费在线观看| 一色屋精品视频在线看| 欧美午夜精品久久久久久久 | 久久精品视频在线观看| 夜夜爽99久久国产综合精品女不卡| 久久精品国产99精品国产亚洲性色| 亚洲日本欧美天堂| 国产一区二区日韩精品| 欧美三级在线视频| 欧美成人xxx| 久久国产精品72免费观看| 一区二区三区久久久| 亚洲国产精品va| 久久久久久亚洲精品不卡4k岛国| 国产精品看片你懂得| 欧美成人一区二区三区片免费| 午夜免费电影一区在线观看| 日韩一级不卡| 亚洲国产日韩欧美在线图片| 久久精品在线免费观看| 欧美一区二区网站| 午夜久久久久久| 一区二区高清视频| 亚洲美洲欧洲综合国产一区| 亚洲高清三级视频| 激情综合激情| 韩国三级在线一区| 国产精品羞羞答答| 欧美性大战久久久久| 欧美国产日本韩| 欧美中在线观看| 久久高清免费观看| 性欧美在线看片a免费观看| 亚洲午夜免费视频| 亚洲深夜福利视频| 亚洲一区二区三区成人在线视频精品| 最新中文字幕亚洲| 亚洲人成高清| 亚洲美女av电影| 日韩视频不卡| 亚洲午夜日本在线观看| 亚洲欧美综合| 久久精品动漫| 久久性色av| 欧美成人一区二区三区| 欧美精品在线免费观看| 欧美日韩精品一区二区三区四区| 欧美日韩精品免费观看视频| 欧美婷婷六月丁香综合色| 国产精品区免费视频| 国产亚洲激情| 亚洲成色www久久网站| 91久久精品国产91性色tv| 中文精品一区二区三区| 亚洲天堂偷拍| 欧美在线日韩| 欧美激情一区二区久久久| 亚洲精品一区在线观看| 亚洲一区二区免费| 久久久精彩视频| 欧美精品在线播放| 国产精品午夜在线| 亚洲国产欧美日韩精品| 亚洲一线二线三线久久久| 久久福利影视| 亚洲国产精品黑人久久久 | 欧美a级一区| av成人手机在线| 久久9热精品视频| 欧美精品日韩www.p站| 国产美女精品一区二区三区| 一区二区在线视频播放| 亚洲无亚洲人成网站77777 | 久久久久久久久久码影片| 欧美激情一区二区三区蜜桃视频| 99综合电影在线视频| 久久经典综合| 欧美午夜激情视频| 亚洲国产一区二区三区a毛片| 亚洲影音一区| 亚洲福利电影| 欧美主播一区二区三区| 国产精品草草| 亚洲美女性视频| 久久久精品欧美丰满| 夜夜嗨av一区二区三区免费区| 久久久国产91| 国产欧美三级| 亚洲一区久久久| 亚洲黄网站在线观看| 欧美一区二区三区免费看| 欧美日韩亚洲三区| 亚洲国产欧美日韩精品| 免费短视频成人日韩| 先锋影音国产精品|