• <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>
            AstaTus
            -- 夏天不熱。。
            posts - 22,comments - 12,trackbacks - 0
            最近看的那本directx 的書的源碼都是用函數(shù),沒用c++的類,用起來超不爽,所以自己來,封裝了下,現(xiàn)在才看到《頂點(diǎn)的顏色》的那章,所以類還很不完整,以后慢慢改進(jìn)。。

            //Base.h
            /////////////////////////////////////////////
            #ifndef BASE_H
            #define BASE_H

            #include 
            <d3dx9.h>

            namespace LTT
            {
                D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR, s, D3DXCOLOR e, 
            float p);
                
                
            const D3DMATERIAL9 WHITE_MTRL = InitMtrl(WHITE, WHITE, WHITE, BLACK, 8.0f);

                
            const D3DMATERIAL9 RED_MTRL = InitMtrl(RED, RED, RED, BLACK, 8.0f);

                
            const D3DMATERIAL9 GREEN_MTRL = InitMtrl(GREEN, GREEN, GREEN, BLACK, 8.0f);

                
            const D3DMATERIAL9 BLUE_MTRL = InitMtrl(BLUE, BLUE, BLUE, BLACK, 8.0f);
                
                
            const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 8.0f);

                
            const D3DXCOLOR      WHITE( D3DCOLOR_XRGB(255255255) );
                
            const D3DXCOLOR      BLACK( D3DCOLOR_XRGB(  0,   0,   0) );
                
            const D3DXCOLOR        RED( D3DCOLOR_XRGB(255,   0,   0) );
                
            const D3DXCOLOR      GREEN( D3DCOLOR_XRGB(  0255,   0) );
                
            const D3DXCOLOR       BLUE( D3DCOLOR_XRGB(  0,   0255) );
                
            const D3DXCOLOR     YELLOW( D3DCOLOR_XRGB(255255,   0) );
                
            const D3DXCOLOR       CYAN( D3DCOLOR_XRGB(  0255255) );
                
            const D3DXCOLOR    MAGENTA( D3DCOLOR_XRGB(255,   0255) );


                D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3
            * direction, D3DXCOLOR* color);
            }

            #endif
            /////////////////////////////////////////////

            /////////Base.cpp
            ////////////////////////////////////////////

            #include "Base.h"
            using namespace LTT;


            namespace LTT
            {
                D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3
            * direction, D3DXCOLOR* color)
                
            {
                    D3DLIGHT9 light;

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

                    light.Type 
            = D3DLIGHT_DIRECTIONAL;
                    light.Ambient 
            = *color * 0.4f;
                    light.Diffuse 
            = *color;
                    light.Specular 
            = *color * 0.6f;
                    light.Direction 
            = *direction;

                    
            return light;
                }


                D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, 
                                            D3DXCOLOR, s, D3DXCOLOR e, 
            float p)
                
            {
                    D3DMATERIAL9 mtrl;

                    mtrl.Ambient 
            = a;
                    mtrl.Diffuse 
            = d;
                    mtrl.Specular 
            = s;
                    mtrl.Emissive 
            = e;
                    mtrl.Power 
            = p;

                    
            return mtrl;
                }



            }

            /////////////////////////////////////////////
            ////LTTFramework.h  框架類


            #ifndef LTTFRAMEWORK_H
            #define LTTFRAMEWORK_H

            #include 
            "Base.h"
            namespace LTT
            {
                
            class Framework
                
            {
                    
            public:
                        Framework(HINSTANCE hInstance);
                        
            virtual ~Framework();

                        
            bool InitD3D(HINSTANCE hInstance, int Height = 600int Width = 800bool Windowed = true);

                        
            static LRESULT CALLBACK  WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

                        
            int EnterMsgLoop();

                        
            virtual bool Render(float timeDelta);

                    
            protected:
                        IDirect3DDevice9
            * device;
                }
            ;
            }

            #endif
            /////////////////////////////////////////////

            ////////////////////////////////////////////
            /////////////LTTFramework.cpp

            #include "LTTFramework.h"

            using namespace LTT;

            namespace LTT
            {

                Framework::Framework(HINSTANCE hInstance)
                                            
                
            {
                    InitD3D(hInstance);
                }


                Framework::
            ~Framework()
                
            {}

                
            bool Framework::InitD3D(HINSTANCE hInstance, int Height, int Width, bool Windowed)
                
            {
                    WNDCLASSEX wc 
            ={sizeof(WNDCLASSEX), CS_CLASSDC, this->WndProc, 0L0L,
                                GetModuleHandle(NULL),NULL,NULL,NULL,NULL,
            "D3D",NULL}
            ;


                    
            if!RegisterClassEx(&wc) ) 
                    
            {
                        ::MessageBox(
            0"RegisterClass() - FAILED"00);
                        
            return false;
                    }

                        
                    HWND hwnd 
            = 0;
                    hwnd 
            = ::CreateWindow("D3D","3D Cube",WS_OVERLAPPEDWINDOW,100,100,800,600,
                        GetDesktopWindow(),NULL,wc.hInstance,NULL);

                    
            if!hwnd )
                    
            {
                        ::MessageBox(
            0"CreateWindow() - FAILED"00);
                        
            return false;
                    }


                    ::ShowWindow(hwnd, SW_SHOW);
                    ::UpdateWindow(hwnd);


            //////////////////////////////////////////////////////////////////////////////////////
                    int vp = 0;
                    IDirect3D9 
            *_pD3d9;
                    D3DCAPS9 cap;

                    _pD3d9 
            = Direct3DCreate9(D3D_SDK_VERSION);

                    _pD3d9
            ->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &cap);
                    
                    
            if(cap.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
                        vp 
            = D3DCREATE_HARDWARE_VERTEXPROCESSING;
                    
            else
                        vp 
            = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

                    D3DPRESENT_PARAMETERS d3dpp;
                    d3dpp.AutoDepthStencilFormat 
            = D3DFMT_D24S8;
                    d3dpp.BackBufferCount 
            = 1;
                    d3dpp.BackBufferFormat 
            = D3DFMT_A8R8G8B8;
                    d3dpp.BackBufferHeight 
            = Height;
                    d3dpp.BackBufferWidth 
            = Width;
                    d3dpp.EnableAutoDepthStencil 
            = true;
                    d3dpp.Flags 
            = 0;
                    d3dpp.FullScreen_RefreshRateInHz 
            = D3DPRESENT_RATE_DEFAULT;
                    d3dpp.hDeviceWindow 
            = hwnd;
                    d3dpp.MultiSampleQuality 
            = 0;
                    d3dpp.MultiSampleType 
            = D3DMULTISAMPLE_NONE;
                    d3dpp.PresentationInterval 
            = D3DPRESENT_INTERVAL_IMMEDIATE;
                    d3dpp.SwapEffect 
            = D3DSWAPEFFECT_DISCARD;
                    d3dpp.Windowed 
            = Windowed;

                    HRESULT hr;
                    hr 
            = _pD3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, vp, &d3dpp, &device);

                    
            if(FAILED(hr))
                    
            {
                        MessageBox(
            0"CreateDevice() is faild"00);
                        
            return false;

                    }


                    _pD3d9
            ->Release();

                    
            return true;
                }


                

                LRESULT CALLBACK Framework::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
                
            {
                    
            switch( msg )
                    
            {
                    
            case WM_DESTROY:
                        ::PostQuitMessage(
            0);
                        
            break;
                        
                    
            case WM_KEYDOWN:
                        
            if( wParam == VK_ESCAPE )
                            ::DestroyWindow(hwnd);
                        
            break;
                    }

                    
            return ::DefWindowProc(hwnd, msg, wParam, lParam);
                }



                
            int Framework::EnterMsgLoop()
                
            {
                    MSG msg;
                    ::ZeroMemory(
            &msg, sizeof(MSG));

                    
            static float lastTime = (float)timeGetTime(); 

                    
            while(msg.message != WM_QUIT)
                    
            {
                        
            if(::PeekMessage(&msg, 000, PM_REMOVE))
                        
            {
                            ::TranslateMessage(
            &msg);
                            ::DispatchMessage(
            &msg);
                        }

                        
            else
                        
            {    
                            
            float currTime  = (float)timeGetTime();
                            
            float timeDelta = (currTime - lastTime)*0.001f;

                            
            this->Render(timeDelta);

                            lastTime 
            = currTime;
                        }

                    }

                    
            return msg.wParam;
                }


                
            bool Framework::Render(float timeDelta)
                
            {

                    
            return true;
                }


            }

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

            /////////Application.h
            ///////////////////////////////////////////////

            #ifndef APPLICATION_H
            #define APPLICATION_H

            #include 
            "LTTFramework.h"
            using namespace LTT;

            namespace LTT
            {

                
            struct Vertex
                
            {
                    Vertex()
            {}

                    Vertex(
            float x, float y, float z)
                    
            {
                        _x 
            = x;     _y = y;  _z = z;
                    }


                    
            float _x, _y, _z;
                    
            float _nx, _y, _z;

                    
            static const DWORD FVF;
                }
            ;


                
            struct ColorVertex
                
            {
                    ColorVertex()
            {}

                    ColorVertex(
            float x, float y, float z, D3DCOLOR color)
                    
            {
                        _x 
            = x;     _y = y;  _z = z;
                        _color 
            = color;
                    }


                    
            float _x, _y, _z;
                    D3DCOLOR _color;
                    
            static const DWORD FVF;
                }
            ;
                


                
            class Application: public Framework
                
            {
                    
            public:
                        Application(HINSTANCE hInstance);
                        
            virtual ~Application();


                        
            virtual bool Setup() = 0;

                        
            //virtual bool Render(float timeDelta);

                        
            virtual void SetVertexBuffer(){};

                        
            virtual void SetIndexBuffer(){};



                        
            void Go();
            //            virtual void InitVertexBuffer() = 0;
            //            virtual void InitIndexBuffer() = 0;
                
            }

            #endif
            /////////////////////////////////////////////

            /////////////////////////////////////////////
            ////////////Application.cpp

            #include "Application.h"


            namespace LTT
            {

                
            const DWORD ColorVertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL;
                
            const DWORD Vertex::FVF = D3DFVF_XYZ;


                Application::Application(HINSTANCE hInstance):Framework(hInstance)
                
            {
                    
                }


                Application::
            ~Application()
                
            {
                    device
            ->Release();
                }


                
            void Application::Go()
                
            {
                    
            this->Setup();
                    EnterMsgLoop();
                }

            }

            ///////////////////////////////////////////////
            ///////////ExampleApplication.h//自己寫得一個(gè)sample 類,繼承了application類實(shí)現(xiàn)用戶的繪制


            #ifndef EXAMPLEAPPLICATION_H
            #define EXAMPLEAPPLICATION_H

            #include 
            "Application.h"

            namespace LTT
            {
                
            class ExampleApplication:public Application
                
            {
                    
            public:
                        ExampleApplication(HINSTANCE hInstance);

                        
            ~ExampleApplication();

                        
            bool Setup();

                        
            bool Render(float timeDelta);

                        
            void CleanUp();

                        
            virtual void SetVertexBuffer();

                        
            virtual void SetIndexBuffer();
                    
            private:
                        IDirect3DVertexBuffer9
            * Cube;
                        IDirect3DIndexBuffer9
            * CubeIndex;
                        ID3DXMesh
            * teapot;
                        D3DXMATRIX ObjWorldMatrices[
            2];
                }
            ;
            }

            #endif
            /////////////////////////////////////////////

            ////////////////////////////////////////////
            ///////////ExampleApplication.cpp

            #include "ExampleApplication.h"

            using namespace LTT;
            namespace LTT
            {

                ExampleApplication::ExampleApplication(HINSTANCE hInstance):Application(hInstance)
                
            {
                    

                }


                ExampleApplication::
            ~ExampleApplication()
                 
            {
                     Cube
            ->Release();
                    CubeIndex
            ->Release();
                     
                    
                 }



                
            bool ExampleApplication::Setup()
                
            {
                    
            // Nothing to setup in this sample.
                    SetVertexBuffer();

                    SetIndexBuffer();
                    
                    D3DXCreateTeapot(device, 
            &teapot, 0);
                    D3DXMatrixTranslation(
            &ObjWorldMatrices[0], 3.0f5.0f3.0f);
                    D3DXMatrixTranslation(
            &ObjWorldMatrices[1], 0.0f0.0f0.0f);


                    D3DXVECTOR3 position(
            5.0f3.0f5.0f);
                    D3DXVECTOR3 target(
            0.0f0.0f0.0f);
                    D3DXVECTOR3 up(
            0.0f1.0f0.0f);
                    D3DXMATRIX V;
                    D3DXMatrixLookAtLH(
            &V, &position, &target, &up);

                    device
            ->SetTransform(D3DTS_VIEW, &V);

                    D3DXMATRIX proj;
                    D3DXMatrixPerspectiveFovLH(
            &proj, D3DX_PI * 0.5f, (float)800 / (float)6001.0f1000.0f);
                    
                    device
            ->SetTransform(D3DTS_PROJECTION, &proj);

                    device
            ->SetRenderState(D3DRS_LIGHTING, false);

                    
            return true;
                }


                
            void ExampleApplication::SetVertexBuffer()
                
            {
                    device
            ->CreateVertexBuffer(8 * sizeof(ColorVertex), D3DUSAGE_WRITEONLY, ColorVertex::FVF, D3DPOOL_MANAGED, &Cube, 0);

                    ColorVertex
            * vertices;
                    Cube
            ->Lock(00,(void**)&vertices, 0);

                    vertices[
            0= ColorVertex(-1.0f-1.0f-1.0f, D3DCOLOR_XRGB(2552550));
                    vertices[
            1= ColorVertex(-1.0f,  1.0f-1.0f, D3DCOLOR_XRGB(2552550));
                    vertices[
            2= ColorVertex( 1.0f,  1.0f-1.0f, D3DCOLOR_XRGB(2552550));
                    vertices[
            3= ColorVertex( 1.0f-1.0f-1.0f, D3DCOLOR_XRGB(2550255));
                    vertices[
            4= ColorVertex(-1.0f-1.0f,  1.0f, D3DCOLOR_XRGB(2550255));
                    vertices[
            5= ColorVertex(-1.0f,  1.0f,  1.0f, D3DCOLOR_XRGB(0255255));
                    vertices[
            6= ColorVertex( 1.0f,  1.0f,  1.0f, D3DCOLOR_XRGB(0255255));
                    vertices[
            7= ColorVertex( 1.0f-1.0f,  1.0f, D3DCOLOR_XRGB(25500));

                    Cube
            ->Unlock();

                }


                
            void ExampleApplication::SetIndexBuffer()
                
            {
                    WORD
            * indices;
                    device
            ->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &CubeIndex, 0);
                    
                    
                    CubeIndex
            ->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;

                    CubeIndex
            ->Unlock();

                }


                
            bool ExampleApplication::Render(float timeDelta)
                
            {
                    
            if( device ) // Only use Device methods if we have a valid device.
                    {

                        D3DXMATRIX Rx, Ry;

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

                        
            // incremement y-rotation angle each frame
                        static float y = 0.0f;
                        D3DXMatrixRotationY(
            &Ry, y);
                        y 
            += timeDelta;

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

                        
            // combine x- and y-axis rotation transformations.
                        D3DXMATRIX p = Rx * Ry;

                        device
            ->SetTransform(D3DTS_WORLD, &p);
                        
            // Instruct the device to set each pixel on the back buffer black -
                        
            // D3DCLEAR_TARGET: 0x00000000 (black) - and to set each pixel on
                        
            // the depth buffer to a value of 1.0 - D3DCLEAR_ZBUFFER: 1.0f.
                        device->Clear(00, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff1.0f0);

                        device
            ->BeginScene();

                        
                        device
            ->SetStreamSource(0, Cube, 0sizeof(ColorVertex));
                        device
            ->SetFVF(ColorVertex::FVF);
                        device
            ->SetIndices(CubeIndex);


                    
            //    device->SetTransform(D3DTS_WORLD, &ObjWorldMatrices[1]);

                        device
            ->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
                        device
            ->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 008012);

                    
            //    device->SetTransform(D3DTS_WORLD, &ObjWorldMatrices[0]);
                    
            //    device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
                        
                    
            //    teapot->DrawSubset(0);

                        device
            ->EndScene();

                        
            // Swap the back and front buffers.
                        device->Present(0000);
                    }

                    
            return true;
                }


            }




            ///main函數(shù)。。
            int WINAPI WinMain(    
                               HINSTANCE hInstance,
                                HINSTANCE hPrevInstance,
                                LPSTR lpCmdLine,
                                
            int nShowCmd
                               )
            {
                LTT::ExampleApplication app(hInstance);
                app.Go();

                
            return 0;
            }

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



            個(gè)人感覺接口封裝的還不夠好。。用起來還不是那么的爽。。以后繼續(xù)優(yōu)化。。。^_^
            posted on 2008-09-20 22:10 AstaTus 閱讀(1337) 評論(0)  編輯 收藏 引用 所屬分類: DIRECTX9
            91久久九九无码成人网站| 伊人久久精品无码av一区| 精品久久久久久久久久中文字幕| 久久国产影院| 国产成人无码精品久久久性色| 久久狠狠高潮亚洲精品| 国产成人综合久久久久久| 久久亚洲AV成人无码软件| 久久精品无码一区二区无码| 久久国产免费| 91精品国产色综合久久| 久久精品国产一区二区电影| 国内精品伊人久久久久777| 国产福利电影一区二区三区久久久久成人精品综合 | 国产精品久久久久影院嫩草 | 国产69精品久久久久观看软件| 久久精品国产色蜜蜜麻豆| 9999国产精品欧美久久久久久| 久久精品国产亚洲AV忘忧草18| 狠狠精品久久久无码中文字幕 | 亚洲精品无码久久不卡| 亚洲嫩草影院久久精品| 亚洲va中文字幕无码久久不卡 | 亚洲国产高清精品线久久| 久久精品天天中文字幕人妻| 久久人人超碰精品CAOPOREN| 国产精品9999久久久久| 亚洲午夜久久久久久久久久| 色婷婷久久久SWAG精品| 亚洲成人精品久久| 久久精品国产网红主播| 7777精品伊人久久久大香线蕉| 精品国产乱码久久久久久浪潮| 久久青草国产手机看片福利盒子 | 久久精品天天中文字幕人妻| 亚洲国产成人久久精品动漫| 国产精品无码久久综合| 久久精品国产亚洲AV久| 日本久久久久久久久久| 久久久久久久综合日本| 久久久WWW免费人成精品|