D3D中的粒子系统(4)
14.3具體的粒子系統:雪、火、粒子槍
現在讓我們用cParticleSystem類開始一個具體的粒子系統,為了說明用意,這些系統的設計很簡單,沒有用到cParticleSystem類所提供的所有靈活性。我們實現雪、火、粒子槍系統。雪系統模擬下落的雪花,火系統模擬看上去像火焰的爆炸,粒子槍系統從照相機位置向對面發射出粒子(用鍵盤)。
14.3.1 例子程序:雪
雪系統類定義如下:
??? class cSnow : public cParticleSystem
??? {
??? public:
??????? cSnow(cBoundingBox* bounding_box, int num_particles);
??????? virtual void reset_particle(sParticleAttribute* attr);
??????? virtual void update(float time_delta);
??? };
構造函數提供一個點給邊界盒結構,邊界盒是粒子系統的成員。邊界盒描述雪花在哪個范圍內(體積范圍)下落,如果雪花出了邊界盒,它將被殺死并再生。這樣,雪系統始終能保存有同樣數量的激粒子,構造函數的實現:
??? cSnow::cSnow(cBoundingBox* bounding_box, int num_particles)
??? {
??????? m_bounding_box??? = *bounding_box;
??????? m_size??????????? = 0.25f;
??????? 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方法創建一個雪花,在x、z軸隨機的位置并在邊界盒的范圍內。設置y軸高度為邊界盒的頂部。我們給雪花一個速度,以便讓雪花下落時稍稍向左傾斜。雪花是白色的。
??? void cSnow::reset_particle(sParticleAttribute* attr)
??? {
??????? attr->is_alive = true;
??????? // get random x, z coordinate for the position of the snow flake
??????????? get_random_vector(&attr->position, &m_bounding_box.m_min, &m_bounding_box.m_max);
??????? // no randomness for height (y-coordinate).?
??????? // Snow flake always starts at the top of bounding box.
??????????? attr->position.y = m_bounding_box.m_max.y;
??????? // snow flakes fall downwards and slightly to the left
??????????? attr->velocity.x = get_random_float(0.0f, 1.0f) * (-3.0f);
??????? attr->velocity.y = get_random_float(0.0f, 1.0f) * (-10.0f);
??????? attr->velocity.z = 0.0f;
??????? // white snow flake
??????? attr->color = WHITE;
??? }
update方法更新粒子和粒子間的位置,并且測試粒子是否在系統的邊界盒之外,如果它已經跳出邊界盒,就再重新創建。
??? void cSnow::update(float time_delta)
??? {
??????? for(list<sParticleAttribute>::iterator iter = m_particles.begin(); iter != m_particles.end(); iter++)
??????? {
??????????? iter->position += iter->velocity * time_delta;
??????????? // is the point outside bounds?
??????????? if(! m_bounding_box.is_point_inside(iter->position))
??????????????? // recycle dead particles, so respawn it.
??????????????????? reset_particle(&(*iter));
??????? }
??? }
執行程序:
??? #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_snow;
??? cCamera??????????????? g_camera(AIR_CRAFT);
???????
??? bool setup()
??? {???
??????? srand((unsigned int)time(NULL));
??????? // create snow system
??????? cBoundingBox bounding_box;
??????? bounding_box.m_min = D3DXVECTOR3(-10.0f, -10.0f, -10.0f);
??????? bounding_box.m_max = D3DXVECTOR3(10.0f, 10.0f, 10.0f);
??????? g_snow = new cSnow(&bounding_box, 5000);
??????? g_snow->init(g_device, "snowflake.dds");
??????? // 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_snow;
??????? // 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_snow->update(time_delta);
??????? // 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 snow last.
??????????? g_device->SetTransform(D3DTS_WORLD, &identity_matrix);
??????? g_snow->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;
??? }
下載源程序
總結
以上是生活随笔為你收集整理的D3D中的粒子系统(4)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 安全基础 第七章 验证与授权 密
- 下一篇: AOP技术基础(转)