天行健 君子当自强而不息

D3D中的粒子系统(5)

新建网页 1

14.3.2 例子程序:焰火系统

本例程实现了一个焰火例子系统,运行效果如图14.3所示:

火系统类定义如下:

    class cFirework : public cParticleSystem
    {
   
public:
        cFirework(D3DXVECTOR3* origin, 
int num_particles);
        
virtual void reset_particle(sParticleAttribute* attr);
        
virtual void update(float time_delta);
        
virtual void pre_render();
    };

构造函数需要提供一个点作为粒子系统中的原点,和系统中的粒子数,原点是火焰爆发的那个点。

    cFirework::cFirework(D3DXVECTOR3* origin, int num_particles)
    {
        m_origin        = *origin;
        m_size            = 0.9f;
        m_vb_num        = 2048;
        m_vb_offset     = 0;
        m_vb_batch_num    = 512;
   
        
for(int i = 0; i < num_particles; i++)
            add_particle();
    }

reset_particle方法在原点位置初始化粒子系统,并在边界球内创建一个随机的速度,粒子系统中的每个例子有一个随机的颜色,我们定义粒子只能存活2秒。

    void cFirework::reset_particle(sParticleAttribute* attr)
    {
        attr->is_alive = 
true;
        attr->position = m_origin;
   
        D3DXVECTOR3 min = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
        D3DXVECTOR3 max = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
        get_random_vector(&attr->velocity, &min, &max);
   
        
// normalize to make spherical
   
        D3DXVec3Normalize(&attr->velocity, &attr->velocity);
   
        attr->velocity *= 100.0f;
   
        attr->color = D3DXCOLOR(get_random_float(0.0f, 1.0f),
                                get_random_float(0.0f, 1.0f),
                                get_random_float(0.0f, 1.0f),
                                1.0f);
   
        attr->age        = 0.0f;
        attr->life_time = 2.0f;    
// lives for 2 seconds
   
}

update方法更新每个粒子的位置,并在粒子超出自己的生活周期时杀死它。注意:这个系统不移除死掉的粒子,这么做是因为我们想产生一个新的火焰的时候,我们只要简单的重新设置已经存在的死了的火焰系统就可以了。这样我们不必频繁的去产生和释放粒子。

    void cFirework::update(float time_delta)
    {
        
for(list<sParticleAttribute>::iterator iter = m_particles.begin(); iter != m_particles.end(); iter++)
        {
            
// only update living particles
   
        if(iter->is_alive)
            {
                iter->position += iter->velocity * time_delta;
                iter->age       += time_delta;
   
                
if(iter->age > iter->life_time)    // kill
   
                iter->is_alive = false;
            }
        }
    }

重载pre_render以使绘制粒子时与地板颜色融合。
    void cFirework::pre_render()
    {
        cParticleSystem::pre_render();
   
        m_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
    }

执行程序:
    #include "d3dUtility.h"
    #include "camera.h"
    #include "ParticleSystem.h"
    #include <cstdlib>
    #include <ctime>
   
    #pragma warning(disable : 4100)
   
   
const int WIDTH  = 640;
   
const int HEIGHT = 480;
   
    IDirect3DDevice9*    g_device;
    cParticleSystem*    g_exploder;
    cCamera                g_camera(AIR_CRAFT);
   
   
    ////////////////////////////////////////////////////////////////////////////////////////////////////
   

   
bool setup()
    {    
        srand((unsigned 
int)time(NULL));
   
        
// create firwworlk system
   
        D3DXVECTOR3 origin(0.0f, 10.0f, 50.0f);
        g_exploder = 
new cFirework(&origin, 6000);
        g_exploder->init(g_device, "flare.bmp");
   
        
// setup a basic scnen, the scene will be created the first time this function is called.
   
        draw_basic_scene(g_device, 1.0f);
   
        
// set the projection matrix
   
    D3DXMATRIX proj;
        D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI/4.0f, (
float)WIDTH/HEIGHT, 1.0f, 1000.0f);
        g_device->SetTransform(D3DTS_PROJECTION, &proj);
        
        
return true;
    }
   
   
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
   

   
void cleanup()
    {    
        delete g_exploder;
   
        
// pass NULL for the first parameter to instruct cleanup
   
    draw_basic_scene(NULL, 0.0f);
    }
   
   
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
   

   
bool display(float time_delta)
    {
        
// update the camera
   

        
if( GetAsyncKeyState(VK_UP) & 0x8000f )
            g_camera.walk(4.0f * time_delta);
   
        
if( GetAsyncKeyState(VK_DOWN) & 0x8000f )
            g_camera.walk(-4.0f * time_delta);
   
        
if( GetAsyncKeyState(VK_LEFT) & 0x8000f )
            g_camera.yaw(-1.0f * time_delta);
        
        
if( GetAsyncKeyState(VK_RIGHT) & 0x8000f )
            g_camera.yaw(1.0f * time_delta);
   
        
if( GetAsyncKeyState('N') & 0x8000f )
            g_camera.strafe(-4.0f * time_delta);
   
        
if( GetAsyncKeyState('M') & 0x8000f )
            g_camera.strafe(4.0f * time_delta);
   
        
if( GetAsyncKeyState('W') & 0x8000f )
            g_camera.pitch(1.0f * time_delta);
   
        
if( GetAsyncKeyState('S') & 0x8000f )
            g_camera.pitch(-1.0f * time_delta);    
   
        
// update the view matrix representing the camera's new position/orientation
   
    D3DXMATRIX view_matrix;
        g_camera.get_view_matrix(&view_matrix);
        g_device->SetTransform(D3DTS_VIEW, &view_matrix);    
   
        g_exploder->update(time_delta);
   
        
if(g_exploder->is_dead())
            g_exploder->reset();
   
        
// render now
   

        g_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
   
        g_device->BeginScene();
   
        D3DXMATRIX identity_matrix;
        D3DXMatrixIdentity(&identity_matrix);
        g_device->SetTransform(D3DTS_WORLD, &identity_matrix);
   
        draw_basic_scene(g_device, 1.0f);
   
        
// order important, render firework last.
   
        g_device->SetTransform(D3DTS_WORLD, &identity_matrix);
        g_exploder->render();
   
        g_device->EndScene();
   
        g_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_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_device->Release();
   
        
return 0;
    }

下载源程序

posted on 2008-04-04 12:13 lovedday 阅读(1270) 评论(1)  编辑 收藏 引用

评论

# re: D3D中的粒子系统(5) 2011-07-23 20:43 Sayaka

很有用很有用!谢谢!  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论