日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

D3D中的粒子系统(4)

發(fā)布時(shí)間:2023/12/20 windows 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 D3D中的粒子系统(4) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

14.3具體的粒子系統(tǒng):雪、火、粒子槍

現(xiàn)在讓我們用cParticleSystem類開(kāi)始一個(gè)具體的粒子系統(tǒng),為了說(shuō)明用意,這些系統(tǒng)的設(shè)計(jì)很簡(jiǎn)單,沒(méi)有用到cParticleSystem類所提供的所有靈活性。我們實(shí)現(xiàn)雪、火、粒子槍系統(tǒng)。雪系統(tǒng)模擬下落的雪花,火系統(tǒng)模擬看上去像火焰的爆炸,粒子槍系統(tǒng)從照相機(jī)位置向?qū)γ姘l(fā)射出粒子(用鍵盤(pán))。
14.3.1 例子程序:雪

雪系統(tǒng)類定義如下:
??? class cSnow : public cParticleSystem
??? {
??? public:
??????? cSnow(cBoundingBox* bounding_box, int num_particles);
??????? virtual void reset_particle(sParticleAttribute* attr);
??????? virtual void update(float time_delta);
??? };

構(gòu)造函數(shù)提供一個(gè)點(diǎn)給邊界盒結(jié)構(gòu),邊界盒是粒子系統(tǒng)的成員。邊界盒描述雪花在哪個(gè)范圍內(nèi)(體積范圍)下落,如果雪花出了邊界盒,它將被殺死并再生。這樣,雪系統(tǒng)始終能保存有同樣數(shù)量的激粒子,構(gòu)造函數(shù)的實(shí)現(xiàn):
??? 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();
??? }

同樣注意:我們指定頂點(diǎn)緩存的尺寸,每一批的尺寸和開(kāi)始的偏移。

reset_particle方法創(chuàng)建一個(gè)雪花,在x、z軸隨機(jī)的位置并在邊界盒的范圍內(nèi)。設(shè)置y軸高度為邊界盒的頂部。我們給雪花一個(gè)速度,以便讓雪花下落時(shí)稍稍向左傾斜。雪花是白色的。
??? 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方法更新粒子和粒子間的位置,并且測(cè)試粒子是否在系統(tǒng)的邊界盒之外,如果它已經(jīng)跳出邊界盒,就再重新創(chuàng)建。
??? 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));
??????? }
??? }

執(zhí)行程序:
??? #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;
??? }

下載源程序

總結(jié)

以上是生活随笔為你收集整理的D3D中的粒子系统(4)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。