• <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 閱讀(1088) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久精品水蜜桃av综合天堂| 欧美亚洲国产精品久久| 亚洲国产精品无码久久青草 | 久久久久高潮综合影院| 2019久久久高清456| 精品久久久无码中文字幕| 久久无码AV中文出轨人妻| 精品国产99久久久久久麻豆| 青青热久久国产久精品 | 久久婷婷色综合一区二区| 99久久免费国产特黄| 77777亚洲午夜久久多喷| 久久无码人妻一区二区三区| 亚洲国产精品人久久| 久久国产亚洲精品| 久久免费精品一区二区| 色综合久久夜色精品国产| 久久久久久久综合综合狠狠| 久久久久人妻一区精品性色av| 日韩一区二区三区视频久久| 久久夜色精品国产| 亚洲精品高清一二区久久| 久久夜色精品国产噜噜麻豆 | 国产香蕉久久精品综合网| 99精品国产免费久久久久久下载| 久久午夜综合久久| 亚洲Av无码国产情品久久| 无码人妻精品一区二区三区久久久| 国产亚洲精品久久久久秋霞| 国产精品久久毛片完整版| 国产精品va久久久久久久| 久久久国产视频| 国产成人精品久久二区二区| 久久久久亚洲AV无码专区网站| 久久久精品国产免大香伊| 欧美国产成人久久精品| 国产精品99久久久久久人| 国内精品久久久久影院老司 | 久久夜色精品国产亚洲| 日本精品久久久久影院日本| 波多野结衣AV无码久久一区|