• <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>

            天行健 君子當(dāng)自強(qiáng)而不息

            D3D中的粒子系統(tǒng)(6)

            新建網(wǎng)頁 1

            14.3.3 例子程序:粒子槍

            本例程實(shí)現(xiàn)了一個(gè)粒子槍系統(tǒng),運(yùn)行效果如圖14.4所示:

            下面是粒子槍系統(tǒng)的定義:

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

            構(gòu)造函數(shù)需要提供一個(gè)照相機(jī)的位置點(diǎn),這是因?yàn)橄到y(tǒng)需要知道照相機(jī)的位置及朝向,以決定在哪創(chuàng)建一個(gè)粒子。

                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方法設(shè)置粒子的位置為當(dāng)前照相機(jī)的位置,并且設(shè)置粒子運(yùn)動的速度為照像機(jī)方向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方法更新粒子的位置,并且殺死超過其生命周期的粒子,然后,我們搜索粒子列表刪除已經(jīng)死了的粒子。

                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();
                }

            執(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_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)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評論

            久久久无码人妻精品无码| 久久久久久亚洲精品无码| 亚洲精品国精品久久99热| 久久影院亚洲一区| 四虎影视久久久免费| 精品免费久久久久国产一区 | 91久久福利国产成人精品| 久久精品a亚洲国产v高清不卡 | 色欲久久久天天天综合网| 国产成人精品综合久久久| 国产精品99久久久久久宅男小说| 久久综合九色欧美综合狠狠| 一日本道伊人久久综合影| 久久99九九国产免费看小说| 狠狠色噜噜色狠狠狠综合久久 | 九九精品99久久久香蕉| 久久99精品国产麻豆| 青青草国产精品久久| 久久婷婷五月综合成人D啪| 精品国产乱码久久久久久人妻 | 久久久久久无码国产精品中文字幕| 久久国产精品免费一区二区三区| 久久久久99精品成人片| 精品国产乱码久久久久久人妻 | 99国产欧美精品久久久蜜芽| 亚洲伊人久久大香线蕉苏妲己| Xx性欧美肥妇精品久久久久久 | AAA级久久久精品无码区| 人妻精品久久久久中文字幕| 亚洲AV无码1区2区久久| 88久久精品无码一区二区毛片| 无码精品久久一区二区三区 | 久久精品国产亚洲AV不卡| 久久国产乱子精品免费女| 手机看片久久高清国产日韩| 国内精品九九久久久精品| 久久久久久极精品久久久| 久久久久久亚洲精品成人| 精品国产乱码久久久久久浪潮| 狠狠色丁香久久婷婷综合图片| 久久精品国产久精国产|