• <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中粒子效果實現示例


            源碼及素材下載

            大爆炸,煙霧痕跡甚至魔術飛彈尾部發出的微小火花,都是粒子(particle)所制造出來的特殊效果。在適當的時機,啟用alpha混合并繪制粒子,這樣粒子就能朝向觀察點(使用公告板),得到的結果就是混合對象的抽象拼貼,他們可以用于創建一些奇妙的效果。

            粒子奇妙的地方就在于粒子的大小實際上是任意的,原因在于可以創建一個縮放矩陣,使其同粒子多邊形的世界變換矩陣結合起來。也就是說,除非粒子紋理不同,否則只需要使用一個多邊形來繪制所有的粒子,無論如何,多邊形的數目都必須同紋理的數目保持一致。

            還需要創建粒子圖像,圖像中心為一個實心(不透明)圓形,向圖像的邊緣延伸,圖像逐漸變透明,如下圖所示:



            接著,需要設置4個頂點,這4個頂點使用了2個多邊形(可以使用三角形帶進行優化)。頂點的坐標表示粒子的缺省大小,稍后需要將粒子進行縮放,以適合這個大小。每個粒子都可以擁有獨特的屬性,包括粒子顏色(通過使用材質來實現)。

            接下來,將這個結構體同一個含有兩個多邊形(創建一個正方形)的頂點緩沖結合起來,以便將多邊形渲染到3D設備上。在被繪制出來之前,每個粒子都需要通過它自己的世界矩陣進行定向(當然使用公告板)。然后將世界變換矩陣同每個粒子的縮放變換矩陣組合起來,再設置一個材質(使用 IDirect3DDevice::SetMaterial函數),用來改變粒子的顏色。最后,繪制粒子。

            完整源碼如下所示:

            /***************************************************************************************
            PURPOSE:
                Particle Demo

            Required libraries:
              WINMM.lib, D3D9.LIB, D3DX9.LIB.
             **************************************************************************************
            */

            #include 
            <windows.h>
            #include 
            <stdio.h>
            #include 
            "d3d9.h"
            #include 
            "d3dx9.h"

            #pragma comment(lib, 
            "winmm.lib")
            #pragma comment(lib, 
            "d3d9.lib")
            #pragma comment(lib, 
            "d3dx9.lib")

            #pragma warning(disable : 
            4305 4244)

            #define WINDOW_WIDTH    400
            #define WINDOW_HEIGHT   400

            #define Safe_Release(p) if((p)) (p)->Release();

            // window handles, class and caption text.
            HWND g_hwnd;
            HINSTANCE g_inst;
            static char g_class_name[] = "ParticleClass";
            static char g_caption[]    = "Particle Demo";

            // the Direct3D and device object
            IDirect3D9* g_d3d = NULL;
            IDirect3DDevice9
            * g_d3d_device = NULL;

            // the particle vertex buffer and texture
            IDirect3DVertexBuffer9* g_particle_vb = NULL;
            IDirect3DTexture9
            *      g_particle_texture = NULL;

            // The particle vertex format and descriptor
            typedef struct
            {
                
            float x, y, z;      // 3D coordinates    
                D3DCOLOR diffuse;   // color
                float u, v;         // texture coordinates
            } VERTEX;

            #define VERTEX_FVF   (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

            // create a structure for tracking particles
            struct PARTICLE
            {
                
            float x_pos, y_pos, z_pos;  // coordinate
                float x_add, y_add, z_add;  // movement values
                float red, green, blue;     // colors
                long  timer, counter;       // current and update counter

                PARTICLE()
                {
                    
            // position particle at origin
                    x_pos = y_pos = z_pos = 0.0;

                    
            // get a random update counter
                    counter = rand() % 50 + 10;
                    timer 
            = 0;

                    
            // get a random speed
                    x_add = (float)(rand() % 11- 5.0;
                    y_add 
            = (float)(rand() % 11- 5.0;
                    z_add 
            = (float)(rand() % 11- 5.0;

                    
            // get a random color
                    red   = (float)(rand() % 101/ 100.0;
                    green 
            = (float)(rand() % 101/ 100.0;
                    blue  
            = (float)(rand() % 101/ 100.0;
                }
            };

            PARTICLE
            * g_particles = NULL;

            //--------------------------------------------------------------------------------
            // Window procedure.
            //--------------------------------------------------------------------------------
            long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
            {
                
            switch(msg)
                {
                
            case WM_DESTROY:
                    PostQuitMessage(
            0);
                    
            return 0;
                }

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

            //--------------------------------------------------------------------------------
            // Copy vertex data into vertex buffer, create texture from file.
            //--------------------------------------------------------------------------------
            BOOL Setup_Particles()
            {
                BYTE
            * vertex_ptr;

                VERTEX verts[] 
            = {
                    { 
            -50.0f50.0f0.0f0xFFFFFFFF0.0f0.0f },
                    {  
            50.0f50.0f0.0f0xFFFFFFFF1.0f0.0f },
                    { 
            -50.0f,  0.0f0.0f0xFFFFFFFF0.0f1.0f },
                    {  
            50.0f,  0.0f0.0f0xFFFFFFFF1.0f1.0f }
                };    

                
            // create vertex buffers and stuff in data       
                if(FAILED(g_d3d_device->CreateVertexBuffer(sizeof(verts), 0, VERTEX_FVF, D3DPOOL_DEFAULT, &g_particle_vb, NULL)))   
                    
            return FALSE;   

                
            // locks a range of vertex data and obtains a pointer to the vertex buffer memory
                if(FAILED(g_particle_vb->Lock(00, (void**)&vertex_ptr, 0)))
                    
            return FALSE;

                memcpy(vertex_ptr, verts, 
            sizeof(verts));

                
            // unlocks vertex data
                g_particle_vb->Unlock();    

                
            // get textures    
                D3DXCreateTextureFromFile(g_d3d_device, "Particle.bmp"&g_particle_texture);    
                
                
            // create some particles
                g_particles = new PARTICLE[512];

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Initialize d3d, d3d device, vertex buffer, texutre; set render state for d3d;
            // set perspective matrix.
            //--------------------------------------------------------------------------------
            BOOL Do_Init()
            {
                D3DPRESENT_PARAMETERS present_param;
                D3DDISPLAYMODE  display_mode;
                D3DXMATRIX mat_proj, mat_view;    

                
            // do a windowed mode initialization of Direct3D
                if((g_d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
                    
            return FALSE;

                
            // retrieves the current display mode of the adapter
                if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode)))
                    
            return FALSE;

                ZeroMemory(
            &present_param, sizeof(present_param));

                
            // initialize d3d presentation parameter
                present_param.Windowed               = TRUE;
                present_param.SwapEffect             
            = D3DSWAPEFFECT_DISCARD;
                present_param.BackBufferFormat       
            = display_mode.Format;
                present_param.EnableAutoDepthStencil 
            = TRUE;
                present_param.AutoDepthStencilFormat 
            = D3DFMT_D16;

                
            // creates a device to represent the display adapter
                if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd,
                                              D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
            &present_param, &g_d3d_device)))
                    
            return FALSE;     

                
            // set render state

                
            // enable d3d lighting
                g_d3d_device->SetRenderState(D3DRS_LIGHTING, TRUE);
                
            // enable z-buffer
                g_d3d_device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
                
            // set ambient light to highest level (to see particles)
                g_d3d_device->SetRenderState(D3DRS_AMBIENT, 0xFFFFFFFF);

                
            // create and set the projection matrix

                
            // builds a left-handed perspective projection matrix based on a field of view
                D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4.01.01.01000.0);

                
            // sets a single device transformation-related state
                g_d3d_device->SetTransform(D3DTS_PROJECTION, &mat_proj);

                
            // create and set the view transformation
                D3DXMatrixLookAtLH(&mat_view, &D3DXVECTOR3(0.0f0.0f-500.0f), &D3DXVECTOR3(0.0f0.0f0.0f), 
                                   
            &D3DXVECTOR3(0.0f1.0f0.0f));

                g_d3d_device
            ->SetTransform(D3DTS_VIEW, &mat_view);

                
            // create the meshes
                Setup_Particles();    

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Release all d3d resource.
            //--------------------------------------------------------------------------------
            BOOL Do_Shutdown()
            {
                delete[] g_particles;

                Safe_Release(g_particle_vb);
                Safe_Release(g_particle_texture);    
                Safe_Release(g_d3d_device);
                Safe_Release(g_d3d);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Render a frame.
            //--------------------------------------------------------------------------------
            BOOL Do_Frame()
            {
                D3DXMATRIX mat_view, mat_world, mat_transposed, mat_transform;
                
            static D3DMATERIAL9 s_material;
                
            static BOOL  s_is_mat_init = TRUE;
                
            static DWORD s_counter = timeGetTime();

                
            // limit to 30fps
                if(timeGetTime() < s_counter+33)
                    
            return TRUE;

                s_counter 
            = timeGetTime();

                
            // configure the material if first time called
                if(s_is_mat_init = TRUE)
                {
                    s_is_mat_init 
            = FALSE;
                    ZeroMemory(
            &s_material, sizeof(s_material));
                    s_material.Diffuse.a 
            = s_material.Ambient.a = 0.5f;
                }

                
            // clear device back buffer
                g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(064128255), 1.0f0);    

                
            // Begin scene
                if(SUCCEEDED(g_d3d_device->BeginScene()))
                {
                    
            // set the particle source, shader, texture.            
                    g_d3d_device->SetStreamSource(0, g_particle_vb, 0sizeof(VERTEX));
                    g_d3d_device
            ->SetFVF(VERTEX_FVF);
                    g_d3d_device
            ->SetTexture(0, g_particle_texture);

                    
            // get and set the transposed view matrix (billboard technique)
                    g_d3d_device->GetTransform(D3DTS_VIEW, &mat_view);
                    D3DXMatrixTranspose(
            &mat_transposed, &mat_view);

                    
            // enable alpha blending
                    g_d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
                    g_d3d_device
            ->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
                    g_d3d_device
            ->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);    

                    
            // loop through all particles and draw them
                    for(short i = 0; i < 512; i++)
                    {
                        
            // move particle first
                        g_particles[i].x_pos += g_particles[i].x_add;
                        g_particles[i].y_pos 
            += g_particles[i].y_add;
                        g_particles[i].z_pos 
            += g_particles[i].z_add;

                        
            // reverse movements if past counter
                        if((g_particles[i].timer += 1>= g_particles[i].counter)
                        {
                            g_particles[i].timer 
            = 0;
                            g_particles[i].x_add 
            *= -1.0f;
                            g_particles[i].y_add 
            *= -1.0f;
                            g_particles[i].z_add 
            *= -1.0f;
                        }

                        
            // setup the particle's world transformation
                        D3DXMatrixTranslation(&mat_transform, g_particles[i].x_pos, g_particles[i].y_pos, g_particles[i].z_pos);
                        D3DXMatrixMultiply(
            &mat_world, &mat_transform, &mat_transposed);
                        g_d3d_device
            ->SetTransform(D3DTS_WORLD, &mat_world);

                        
            // set the particle's material
                        s_material.Diffuse.r = s_material.Ambient.r = g_particles[i].red;
                        s_material.Diffuse.g 
            = s_material.Ambient.g = g_particles[i].green;
                        s_material.Diffuse.b 
            = s_material.Ambient.b = g_particles[i].blue;
                            
                        
            // Sets the material properties for the device
                        g_d3d_device->SetMaterial(&s_material);

                        
            // draw the particle
                        g_d3d_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 02);
                    }        

                    
            // release texture
                    g_d3d_device->SetTexture(0, NULL);

                    
            // end the scene
                    g_d3d_device->EndScene();
                }

                
            // present the contents of the next buffer in the sequence of back buffers owned by the device
                g_d3d_device->Present(NULL, NULL, NULL, NULL);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Main function, routine entry.
            //--------------------------------------------------------------------------------
            int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
            {
                WNDCLASSEX  win_class;
                MSG         msg;

                g_inst 
            = inst;

                
            // create window class and register it
                win_class.cbSize        = sizeof(win_class);
                win_class.style         
            = CS_CLASSDC;
                win_class.lpfnWndProc   
            = Window_Proc;
                win_class.cbClsExtra    
            = 0;
                win_class.cbWndExtra    
            = 0;
                win_class.hInstance     
            = inst;
                win_class.hIcon         
            = LoadIcon(NULL, IDI_APPLICATION);
                win_class.hCursor       
            = LoadCursor(NULL, IDC_ARROW);
                win_class.hbrBackground 
            = NULL;
                win_class.lpszMenuName  
            = NULL;
                win_class.lpszClassName 
            = g_class_name;
                win_class.hIconSm       
            = LoadIcon(NULL, IDI_APPLICATION);

                
            if(! RegisterClassEx(&win_class))
                    
            return FALSE;

                
            // create the main window
                g_hwnd = CreateWindow(g_class_name, g_caption, WS_CAPTION | WS_SYSMENU, 00,
                                      WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, inst, NULL);

                
            if(g_hwnd == NULL)
                    
            return FALSE;

                ShowWindow(g_hwnd, SW_NORMAL);
                UpdateWindow(g_hwnd);

                
            // initialize game
                if(Do_Init() == FALSE)
                    
            return FALSE;

                
            // start message pump, waiting for signal to quit.
                ZeroMemory(&msg, sizeof(MSG));

                
            while(msg.message != WM_QUIT)
                {
                    
            if(PeekMessage(&msg, NULL, 00, PM_REMOVE))
                    {
                        TranslateMessage(
            &msg);
                        DispatchMessage(
            &msg);
                    }
                    
                    
            // draw a frame
                    if(Do_Frame() == FALSE)
                        
            break;
                }

                
            // run shutdown function
                Do_Shutdown();

                UnregisterClass(g_class_name, inst);
                
                
            return (int) msg.wParam;
            }

            效果圖:



            posted on 2007-07-04 14:52 lovedday 閱讀(1900) 評論(0)  編輯 收藏 引用 所屬分類: ■ DirectX 9 Program

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            超级97碰碰碰碰久久久久最新| 久久久综合九色合综国产| 亚洲国产小视频精品久久久三级 | 久久综合狠狠综合久久| 国产精品久久久久久久久鸭| 亚洲一本综合久久| 久久无码AV中文出轨人妻| 99久久er这里只有精品18| 久久久久这里只有精品| 久久精品国产亚洲av高清漫画| 热久久国产精品| 久久这里只有精品18| 久久精品国产只有精品66| 男女久久久国产一区二区三区| 成人亚洲欧美久久久久| 国内精品伊人久久久久av一坑 | 偷窥少妇久久久久久久久| 日韩精品久久久久久| 亚洲乱码精品久久久久..| 欧美性猛交xxxx免费看久久久| 日韩精品久久无码中文字幕| 久久久无码精品午夜| 久久精品视频免费| 国产麻豆精品久久一二三| 久久99久久99精品免视看动漫| 久久九九久精品国产| 亚洲精品高清久久| 久久国产精品一区二区| 久久久久无码精品国产| 亚洲中文字幕久久精品无码APP | 精品精品国产自在久久高清| 久久人爽人人爽人人片AV| 久久精品国产亚洲av麻豆蜜芽| 久久久久久久国产免费看| 久久久久久青草大香综合精品| 色综合合久久天天综合绕视看 | 国产精品成人无码久久久久久| 久久午夜电影网| 18岁日韩内射颜射午夜久久成人| 国产精品美女久久久| 久久亚洲国产欧洲精品一|