• <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)而不息

            Direct3D中的繪制(3)

            立方體——只比三角形稍微復(fù)雜一點(diǎn),這個程序渲染一個線框立方體。

            這個簡單的繪制和渲染立方體的程序的運(yùn)行結(jié)果如下圖所示:

             

            源程序:

            /**************************************************************************************
              Renders a spinning cube in wireframe mode.  Demonstrates vertex and index buffers, 
              world and view transformations, render states and drawing commands.
             *************************************************************************************
            */

            #include 
            "d3dUtility.h"

            #pragma warning(disable : 
            4100)

            const int WIDTH  = 640;
            const int HEIGHT = 480;

            IDirect3DDevice9
            *        g_d3d_device    = NULL;
            IDirect3DVertexBuffer9
            *    g_vertex_buffer = NULL;
            IDirect3DIndexBuffer9
            *    g_index_buffer    = NULL;

            class cVertex
            {
            public:
                
            float m_x, m_y, m_z;

                cVertex() {}

                cVertex(
            float x, float y, float z)
                {
                    m_x 
            = x;
                    m_y 
            = y;
                    m_z 
            = z;
                }
            };

            const DWORD VERTEX_FVF = D3DFVF_XYZ;

            ////////////////////////////////////////////////////////////////////////////////////////////////////

            bool setup()
            {    
                g_d3d_device
            ->CreateVertexBuffer(8 * sizeof(cVertex), D3DUSAGE_WRITEONLY, VERTEX_FVF, 
                                                 D3DPOOL_MANAGED, 
            &g_vertex_buffer, NULL);

                g_d3d_device
            ->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,
                                                D3DPOOL_MANAGED, 
            &g_index_buffer, NULL);

                
            // fill the buffers with the cube data

                cVertex
            * vertices;
                g_vertex_buffer
            ->Lock(00, (void**)&vertices, 0);
                
                
            // vertices of a unit cube
                vertices[0= cVertex(-1.0f-1.0f-1.0f);
                vertices[
            1= cVertex(-1.0f,  1.0f-1.0f);
                vertices[
            2= cVertex( 1.0f,  1.0f-1.0f);
                vertices[
            3= cVertex( 1.0f-1.0f-1.0f);
                vertices[
            4= cVertex(-1.0f-1.0f,  1.0f);
                vertices[
            5= cVertex(-1.0f,  1.0f,  1.0f);
                vertices[
            6= cVertex( 1.0f,  1.0f,  1.0f);
                vertices[
            7= cVertex( 1.0f-1.0f,  1.0f);

                g_vertex_buffer
            ->Unlock();

                
            // define the triangles of the cube
                WORD* indices = NULL;
                g_index_buffer
            ->Lock(00, (void**)&indices, 0);

                
            // front side
                indices[0]  = 0; indices[1]  = 1; indices[2]  = 2;
                indices[
            3]  = 0; indices[4]  = 2; indices[5]  = 3;

                
            // back side
                indices[6]  = 4; indices[7]  = 6; indices[8]  = 5;
                indices[
            9]  = 4; indices[10= 7; indices[11= 6;

                
            // left side
                indices[12= 4; indices[13= 5; indices[14= 1;
                indices[
            15= 4; indices[16= 1; indices[17= 0;

                
            // right side
                indices[18= 3; indices[19= 2; indices[20= 6;
                indices[
            21= 3; indices[22= 6; indices[23= 7;

                
            // top
                indices[24= 1; indices[25= 5; indices[26= 6;
                indices[
            27= 1; indices[28= 6; indices[29= 2;

                
            // bottom
                indices[30= 4; indices[31= 0; indices[32= 3;
                indices[
            33= 4; indices[34= 3; indices[35= 7;

                g_index_buffer
            ->Unlock();

                
            // position and aim the camera

                D3DXVECTOR3 position(
            0.0f0.0f-5.0f);
                D3DXVECTOR3 target(
            0.0f0.0f0.0f);
                D3DXVECTOR3 up(
            0.0f1.0f0.0f);

                D3DXMATRIX view_matrix;
                D3DXMatrixLookAtLH(
            &view_matrix, &position, &target, &up);

                g_d3d_device
            ->SetTransform(D3DTS_VIEW, &view_matrix);

                
            // set the projection matrix
                D3DXMATRIX proj;
                D3DXMatrixPerspectiveFovLH(
            &proj, D3DX_PI * 0.5f, (float)WIDTH/HEIGHT, 1.0f1000.0f);
                g_d3d_device
            ->SetTransform(D3DTS_PROJECTION, &proj);

                
            // set wireframe mode render state
                g_d3d_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

                
            return true;
            }

            void cleanup()
            {
                safe_release
            <IDirect3DVertexBuffer9*>(g_vertex_buffer);
                safe_release
            <IDirect3DIndexBuffer9*>(g_index_buffer);
            }

            bool display(float time_delta)
            {
                
            // spin the cube

                D3DXMATRIX rx, ry;

                
            // rotate 45 degree on x-axis
                D3DXMatrixRotationX(&rx, 3.14f/4.0f);

                
            // increment y-rotation angle each frame
                static float y = 0.0f;
                D3DXMatrixRotationY(
            &ry, y);
                y 
            += time_delta;

                
            // reset angle to zero when angle reaches 2*PI
                if(y >= 6.28f)
                    y 
            = 0.0f;

                
            // combine x and y axis ratation transformations
                D3DXMATRIX rxy = rx * ry;

                g_d3d_device
            ->SetTransform(D3DTS_WORLD, &rxy);

                
            // draw the scene

                g_d3d_device
            ->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff1.0f0);

                g_d3d_device
            ->BeginScene();

                g_d3d_device
            ->SetStreamSource(0, g_vertex_buffer, 0sizeof(cVertex));
                g_d3d_device
            ->SetIndices(g_index_buffer);
                g_d3d_device
            ->SetFVF(VERTEX_FVF);

                
            // draw cube
                g_d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 008012);

                g_d3d_device
            ->EndScene();

                g_d3d_device
            ->Present(NULL, NULL, NULL, NULL);

                
            return true;
            }

            LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
            {
                
            switch(msg)
                {
                
            case WM_DESTROY:
                    PostQuitMessage(
            0);
                    
            break;

                
            case WM_KEYDOWN:
                    
            if(word_param == VK_ESCAPE)
                        DestroyWindow(hwnd);
                    
            break;
                }

                
            return DefWindowProc(hwnd, msg, word_param, long_param);
            }

            int WINAPI WinMain(HINSTANCE inst, HINSTANCE, PSTR cmd_line, int cmd_show)
            {
                
            if(! init_d3d(inst, WIDTH, HEIGHT, true, D3DDEVTYPE_HAL, &g_d3d_device))
                {
                    MessageBox(NULL, 
            "init_d3d() - failed."0, MB_OK);
                    
            return 0;
                }

                
            if(! setup())
                {
                    MessageBox(NULL, 
            "Steup() - failed."0, MB_OK);
                    
            return 0;
                }

                enter_msg_loop(display);

                cleanup();
                g_d3d_device
            ->Release();

                
            return 0;
            }

             

            setup函數(shù)創(chuàng)建頂點(diǎn)和索引緩存,鎖定它們,把構(gòu)成立方體的頂點(diǎn)寫入頂點(diǎn)緩存,以及把定義立方體的三角形的索引寫入索引緩存。然后把攝象機(jī)向后移動幾個單位以便我們能夠看見在世界坐標(biāo)系中原點(diǎn)處被渲染的立方體。

             

            display方法有兩個任務(wù);它必須更新場景并且緊接著渲染它。既然想旋轉(zhuǎn)立方體,那么我們將對每一幀增加一個角度使立方體能在這一幀旋轉(zhuǎn)。對于這每一幀,立方體將被旋轉(zhuǎn)一個很小的角度,這樣我們看起來旋轉(zhuǎn)就會更平滑。接著我們使用IDirect3DDevice9::DrawIndexedPrimitive方法來繪制立方體。

             

            最后,我們釋放使用過的所有內(nèi)存。這意味著釋放頂點(diǎn)和索引緩存接口。

             

            下載立方體演示程序


            posted on 2008-03-14 14:48 lovedday 閱讀(584) 評論(0)  編輯 收藏 引用


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


            公告

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

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

            搜索

            最新評論

            久久久WWW成人免费精品| 久久99精品久久久大学生| 久久99热只有频精品8| 国产成人综合久久综合| 国产成人精品综合久久久| 亚洲美日韩Av中文字幕无码久久久妻妇 | 亚洲AV伊人久久青青草原| 狠狠色综合网站久久久久久久高清| 久久99热只有频精品8| 久久久久无码中| 国产亚洲综合久久系列| 亚洲Av无码国产情品久久| 99精品久久精品一区二区| 国产免费久久精品99re丫y| 国产午夜精品理论片久久影视| 无码八A片人妻少妇久久| 久久高潮一级毛片免费| 久久久精品国产sm调教网站| 青青热久久国产久精品| 久久精品嫩草影院| 久久夜色精品国产欧美乱| 日日狠狠久久偷偷色综合免费 | 亚洲国产精品久久| 久久国产亚洲精品| 久久精品国产一区二区电影| 99久久精品毛片免费播放| 婷婷久久香蕉五月综合加勒比 | 国产精品美女久久久久久2018| 欧美精品福利视频一区二区三区久久久精品 | 欧洲精品久久久av无码电影| 久久婷婷色香五月综合激情| 久久精品国产亚洲AV不卡| 久久99精品久久久久久噜噜| 国产精品久久久久影视不卡| 国产成人精品久久一区二区三区| 久久99精品国产自在现线小黄鸭 | 日韩乱码人妻无码中文字幕久久 | 久久人妻AV中文字幕| 四虎国产精品成人免费久久| 亚洲欧美一级久久精品| 久久精品久久久久观看99水蜜桃 |