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

            天行健 君子當自強而不息

            D3D中的紋理映射(3)

            該例程演示了怎樣對一個立方體映射板條紋理。

            截圖:

            vertex.h:

            #ifndef __VERTEX_H__
            #define __VERTEX_H__

            class cTextureVertex
            {
            public:
                
            float m_x, m_y, m_z;
                
            float m_nx, m_ny, m_nz;
                
            float m_u, m_v; // texture coordinates

                cTextureVertex() { }

                cTextureVertex(
            float x, float y, float z,
                               
            float nx, float ny, float nz,
                               
            float u, float v)
                {
                    m_x  
            = x;  m_y  = y;  m_z  = z;
                    m_nx 
            = nx; m_ny = ny; m_nz = nz;
                    m_u  
            = u;  m_v  = v;
                }    
            };

            #define TEXTURE_VERTEX_FVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)

            #endif

             


            cube.h:

            #ifndef __CUBE_H__
            #define __CUBE_H__

            #include 
            <d3dx9.h>

            class cCube
            {
            public:
                cCube(IDirect3DDevice9
            * d3d_device);
                
            ~cCube();

                
            void draw(const D3DMATRIX* world, const D3DMATERIAL9* material, IDirect3DTexture9* texture);

            private:
                IDirect3DDevice9
            *        m_d3d_device;
                IDirect3DVertexBuffer9
            *    m_vertex_buffer;
                IDirect3DIndexBuffer9
            *    m_index_buffer;
            };

            #endif

             


            cube.cpp:

            /****************************************************************************
              Provides an interface to create and render a cube.
             ***************************************************************************
            */

            #include 
            "cube.h"
            #include 
            "vertex.h"

            cCube::cCube(IDirect3DDevice9
            * d3d_device)
            {
                m_d3d_device 
            = d3d_device;

                m_d3d_device
            ->CreateVertexBuffer(24 * sizeof(cTextureVertex), D3DUSAGE_WRITEONLY, TEXTURE_VERTEX_FVF,
                    D3DPOOL_MANAGED, 
            &m_vertex_buffer, NULL);

                cTextureVertex
            * v;

                m_vertex_buffer
            ->Lock(00, (void**)&v, 0);

                
            // build box

                
            // fill in the front face vertex data
                v[0= cTextureVertex(-1.0f-1.0f-1.0f0.0f0.0f-1.0f0.0f0.0f);
                v[
            1= cTextureVertex(-1.0f,  1.0f-1.0f0.0f0.0f-1.0f0.0f1.0f);
                v[
            2= cTextureVertex( 1.0f,  1.0f-1.0f0.0f0.0f-1.0f1.0f1.0f);
                v[
            3= cTextureVertex( 1.0f-1.0f-1.0f0.0f0.0f-1.0f1.0f0.0f);

                
            // fill in the back face vertex data
                v[4= cTextureVertex(-1.0f-1.0f1.0f0.0f0.0f1.0f0.0f0.0f);
                v[
            5= cTextureVertex( 1.0f-1.0f1.0f0.0f0.0f1.0f0.0f1.0f);
                v[
            6= cTextureVertex( 1.0f,  1.0f1.0f0.0f0.0f1.0f1.0f1.0f);
                v[
            7= cTextureVertex(-1.0f,  1.0f1.0f0.0f0.0f1.0f1.0f0.0f);

                
            // fill in the top face vertex data
                v[8]  = cTextureVertex(-1.0f1.0f-1.0f0.0f1.0f0.0f0.0f0.0f);
                v[
            9]  = cTextureVertex(-1.0f1.0f,  1.0f0.0f1.0f0.0f0.0f1.0f);
                v[
            10= cTextureVertex( 1.0f1.0f,  1.0f0.0f1.0f0.0f1.0f1.0f);
                v[
            11= cTextureVertex( 1.0f1.0f-1.0f0.0f1.0f0.0f1.0f0.0f);

                
            // fill in the bottom face vertex data
                v[12= cTextureVertex(-1.0f-1.0f-1.0f0.0f-1.0f0.0f0.0f0.0f);
                v[
            13= cTextureVertex( 1.0f-1.0f-1.0f0.0f-1.0f0.0f0.0f1.0f);
                v[
            14= cTextureVertex( 1.0f-1.0f,  1.0f0.0f-1.0f0.0f1.0f1.0f);
                v[
            15= cTextureVertex(-1.0f-1.0f,  1.0f0.0f-1.0f0.0f1.0f0.0f);

                
            // fill in the left face vertex data
                v[16= cTextureVertex(-1.0f-1.0f,  1.0f-1.0f0.0f0.0f0.0f0.0f);
                v[
            17= cTextureVertex(-1.0f,  1.0f,  1.0f-1.0f0.0f0.0f0.0f1.0f);
                v[
            18= cTextureVertex(-1.0f,  1.0f-1.0f-1.0f0.0f0.0f1.0f1.0f);
                v[
            19= cTextureVertex(-1.0f-1.0f-1.0f-1.0f0.0f0.0f1.0f0.0f);

                
            // fill in the right face vertex data
                v[20= cTextureVertex( 1.0f-1.0f-1.0f1.0f0.0f0.0f0.0f0.0f);
                v[
            21= cTextureVertex( 1.0f,  1.0f-1.0f1.0f0.0f0.0f0.0f1.0f);
                v[
            22= cTextureVertex( 1.0f,  1.0f,  1.0f1.0f0.0f0.0f1.0f1.0f);
                v[
            23= cTextureVertex( 1.0f-1.0f,  1.0f1.0f0.0f0.0f1.0f0.0f);

                m_vertex_buffer
            ->Unlock();

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

                WORD
            * index_ptr = NULL;
                m_index_buffer
            ->Lock(00, (void**)&index_ptr, 0);

                
            // fill in the front face index data
                index_ptr[0= 0; index_ptr[1= 1; index_ptr[2= 2;
                index_ptr[
            3= 0; index_ptr[4= 2; index_ptr[5= 3;

                
            // fill in the back face index data
                index_ptr[6= 4; index_ptr[7]  = 5; index_ptr[8]  = 6;
                index_ptr[
            9= 4; index_ptr[10= 6; index_ptr[11= 7;

                
            // fill in the top face index data
                index_ptr[12= 8; index_ptr[13=  9; index_ptr[14= 10;
                index_ptr[
            15= 8; index_ptr[16= 10; index_ptr[17= 11;

                
            // fill in the bottom face index data
                index_ptr[18= 12; index_ptr[19= 13; index_ptr[20= 14;
                index_ptr[
            21= 12; index_ptr[22= 14; index_ptr[23= 15;

                
            // fill in the left face index data
                index_ptr[24= 16; index_ptr[25= 17; index_ptr[26= 18;
                index_ptr[
            27= 16; index_ptr[28= 18; index_ptr[29= 19;

                
            // fill in the right face index data
                index_ptr[30= 20; index_ptr[31= 21; index_ptr[32= 22;
                index_ptr[
            33= 20; index_ptr[34= 22; index_ptr[35= 23;

                m_index_buffer
            ->Unlock();
            }

            cCube::
            ~cCube()
            {
                
            if(m_vertex_buffer)
                {
                    m_vertex_buffer
            ->Release();
                    m_vertex_buffer 
            = NULL;
                }

                
            if(m_index_buffer)
                {
                    m_index_buffer
            ->Release();
                    m_index_buffer 
            = NULL;
                }
            }

            void cCube::draw(const D3DMATRIX* world, const D3DMATERIAL9* material, IDirect3DTexture9* texture)
            {
                
            if(world)
                    m_d3d_device
            ->SetTransform(D3DTS_WORLD, world);

                
            if(material)
                    m_d3d_device
            ->SetMaterial(material);

                
            if(texture)
                    m_d3d_device
            ->SetTexture(0, texture);

                m_d3d_device
            ->SetStreamSource(0, m_vertex_buffer, 0sizeof(cTextureVertex));
                m_d3d_device
            ->SetIndices(m_index_buffer);
                m_d3d_device
            ->SetFVF(TEXTURE_VERTEX_FVF);

                m_d3d_device
            ->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0024012);
            }

             

            TexCube.cpp:
            /**************************************************************************************
              Renders a textured cube.  Demonstrates creating a texture, setting texture filters, 
              enabling a texture, and texture coordinates.  Use the arrow keys to orbit the scene. 
             *************************************************************************************
            */

            #include 
            "d3dUtility.h"
            #include 
            "cube.h"
            #include 
            "vertex.h"

            #pragma warning(disable : 
            4100)

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

            IDirect3DDevice9
            *        g_d3d_device;
            cCube
            *                    g_cube;
            IDirect3DTexture9
            *        g_d3d_texture;

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

            bool setup()
            {    
                g_cube 
            = new cCube(g_d3d_device);

                
            // set a directional light

                D3DLIGHT9 light;
                ZeroMemory(
            &light, sizeof(light));

                light.Type        
            = D3DLIGHT_DIRECTIONAL;
                light.Ambient   
            = D3DXCOLOR(0.8f0.8f0.8f1.0f);
                light.Diffuse   
            = D3DXCOLOR(1.0f1.0f1.0f1.0f);
                light.Specular  
            = D3DXCOLOR(0.2f0.2f0.2f1.0f);
                light.Direction    
            = D3DXVECTOR3(1.0f-1.0f0.0f);

                
            // set and enable the light
                g_d3d_device->SetLight(0&light);
                g_d3d_device
            ->LightEnable(0, TRUE);

                
            // turn off specular lighting and instruct Direct3D to renormalize normals
                g_d3d_device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
                g_d3d_device
            ->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
                
                D3DXCreateTextureFromFile(g_d3d_device, 
            "crate.jpg"&g_d3d_texture);

                
            // set texture filter states
                g_d3d_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
                g_d3d_device
            ->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
                g_d3d_device
            ->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
                
                
            // 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);
                
                
            return true;
            }

            void cleanup()
            {
                safe_delete
            <cCube*>(g_cube);
                safe_release
            <IDirect3DTexture9*>(g_d3d_texture);
            }

            bool display(float time_delta)
            {
                
            // update the scene: update camera position

                
            static float angle = (3.0f * D3DX_PI) / 2.0f;
                
            static float height = 2.0f;

                
            if(GetAsyncKeyState(VK_LEFT) & 0x8000f)
                    angle 
            -= 0.5f * time_delta;

                
            if(GetAsyncKeyState(VK_RIGHT) & 0x8000f)
                    angle 
            += 0.5f * time_delta;

                
            if(GetAsyncKeyState(VK_UP) & 0x8000f)
                    height 
            += 5.0f * time_delta;

                
            if(GetAsyncKeyState(VK_DOWN) & 0x8000f)
                    height 
            -= 5.0f * time_delta;

                D3DXVECTOR3 position(cosf(angle) 
            * 3.0f, height, sinf(angle) * 3.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);

                
            // draw the scene

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

                g_d3d_device
            ->BeginScene();
                g_cube
            ->draw(NULL, &WHITE_MATERIAL, g_d3d_texture);        
                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;
            }


            下載源程序


            posted on 2008-03-17 17:20 lovedday 閱讀(716) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久精品18| 国产美女久久精品香蕉69| 99久久国产综合精品网成人影院| 亚洲伊人久久精品影院 | 久久精品无码免费不卡| 亚洲综合精品香蕉久久网97| 国内精品久久久久久久亚洲| 久久久国产精华液| 久久国产精品99国产精| 久久久久免费视频| 久久久久99精品成人片试看| 久久精品一区二区三区中文字幕| 久久精品综合网| 精品久久久无码人妻中文字幕| 久久久久无码精品国产不卡| 国产精品热久久毛片| 国产精品久久久香蕉| 久久综合狠狠色综合伊人| 久久久久国产精品人妻| 欧美日韩中文字幕久久久不卡 | 久久久久久久久久久免费精品| 人妻无码精品久久亚瑟影视| 人人狠狠综合久久亚洲婷婷 | 国产精品久久久久无码av| 大香网伊人久久综合网2020| 久久久亚洲欧洲日产国码是AV| 久久精品三级视频| 国产精品久久久久久搜索| 久久人与动人物a级毛片| 久久一区二区三区99| 国产精品热久久无码av| 亚洲国产精品婷婷久久| 久久国产高清字幕中文| 久久久久99精品成人片欧美| 三上悠亚久久精品| 亚洲第一极品精品无码久久 | 99久久精品费精品国产| 久久精品成人免费看| 久久国产精品成人免费| 99久久精品日本一区二区免费| 99久久精品费精品国产一区二区|