• <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中的粒子系統(6)

            新建網頁 1

            14.3.3 例子程序:粒子槍

            本例程實現了一個粒子槍系統,運行效果如圖14.4所示:

            下面是粒子槍系統的定義:

                class cParticleGun : public cParticleSystem
                {
               
            private:
                    cCamera* m_camera;
               
               
            public:
                    cParticleGun(cCamera* camera);
                    
            virtual void reset_particle(sParticleAttribute* attr);
                    
            virtual void update(float time_delta);
                };

            構造函數需要提供一個照相機的位置點,這是因為系統需要知道照相機的位置及朝向,以決定在哪創建一個粒子。

                cParticleGun::cParticleGun(cCamera* camera)
                {
                    m_camera        = camera;
                    m_size            = 0.8f;
                    m_vb_num        = 2048;
                    m_vb_offset        = 0;
                    m_vb_batch_num    = 512;
                }

            reset_particle方法設置粒子的位置為當前照相機的位置,并且設置粒子運動的速度為照像機方向100倍。這樣,子彈將射向我們正在看的方向,粒子顏色為綠色。

                void cParticleGun::reset_particle(sParticleAttribute* attr)
                {
                    attr->is_alive = 
            true;
               
                    D3DXVECTOR3 camera_dir;    
                    m_camera->get_look(&camera_dir);
               
                    attr->position      = m_camera->m_pos;    
            // change to camera position
               
                    attr->position.y -= 1.0f;                // sightly below camera so it looks like we're carrying a gun
               
                    // travels in the direction the camera is looking
               
                    attr->velocity = camera_dir * 100.0f;
               
                    attr->color        = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);    
            // green
               
                attr->age        = 0.0f;
                    attr->life_time = 1.0f;    
            // lives for 1 seconds
               
            }

            update方法更新粒子的位置,并且殺死超過其生命周期的粒子,然后,我們搜索粒子列表刪除已經死了的粒子。

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

            執行程序:
                #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_gun;
                cCamera                g_camera(AIR_CRAFT);
               
               
                ////////////////////////////////////////////////////////////////////////////////////////////////////
               

               
            bool setup()
                {    
                    srand((unsigned 
            int)time(NULL));
               
                    
            // create laser
               
                g_gun = new cParticleGun(&g_camera);
                    g_gun->init(g_device, "flare_alpha.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_gun;
               
                    
            // pass NULL for the first parameter to instruct cleanup
               
                draw_basic_scene(NULL, 1.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_gun->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 firework last.
               
                    g_device->SetTransform(D3DTS_WORLD, &identity_matrix);
                    g_gun->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);
               
                        
            // Note: we use the message system over GetAsyncKeyState because GetAsyncKeyState was adding 
                        // particles too fast.  The message system is slower and does not add them as fast.  
                        // This isn't the best solution, but works for illustration purposes.
               
                    if(word_param == VK_SPACE)
                            g_gun->add_particle();
               
                        
            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 13:12 lovedday 閱讀(1094) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            26uuu久久五月天| 久久久久亚洲AV综合波多野结衣 | 亚洲国产精品无码久久久蜜芽| 亚洲国产日韩综合久久精品| 99久久综合国产精品免费| 久久精品国产亚洲AV大全| 国产99久久久国产精免费| 亚洲欧美日韩中文久久| 久久精品嫩草影院| 国产69精品久久久久APP下载 | 老色鬼久久亚洲AV综合| 久久有码中文字幕| 嫩草伊人久久精品少妇AV| 久久99精品久久久久久9蜜桃| 久久精品国产亚洲AV无码麻豆| 国产福利电影一区二区三区,免费久久久久久久精 | 久久精品一区二区三区AV| 久久精品国产亚洲7777| 91精品国产高清久久久久久91| 人妻中文久久久久| 久久久久无码精品国产app| 久久人人爽人人爽人人AV| 久久精品无码一区二区app| 狠狠色婷婷久久一区二区三区| 麻豆一区二区99久久久久| 日韩美女18网站久久精品| 久久婷婷久久一区二区三区| 久久国产精品久久久| 一本色道久久88—综合亚洲精品| 波多野结衣久久精品| 久久露脸国产精品| 久久久久亚洲AV无码专区桃色| 国产成人久久精品激情| 一日本道伊人久久综合影| 久久人搡人人玩人妻精品首页 | 久久AⅤ人妻少妇嫩草影院| 国产精品99久久精品| 精品久久久久久无码人妻热 | 狠狠人妻久久久久久综合| 18岁日韩内射颜射午夜久久成人 | 久久久久久久女国产乱让韩|