当前位置: 首页 > news >正文

D3D中粒子效果实现示例

源码及素材下载

大爆炸,烟雾痕迹甚至魔术飞弹尾部发出的微小火花,都是粒子(particle)所制造出来的特殊效果。在适当的时机,启用alpha混合并绘制粒子,这样粒子就能朝向观察点(使用公告板),得到的结果就是混合对象的抽象拼贴,他们可以用于创建一些奇妙的效果。

粒子奇妙的地方就在于粒子的大小实际上是任意的,原因在于可以创建一个缩放矩阵,使其同粒子多边形的世界变换矩阵结合起来。也就是说,除非粒子纹理不同,否则只需要使用一个多边形来绘制所有的粒子,无论如何,多边形的数目都必须同纹理的数目保持一致。

还需要创建粒子图像,图像中心为一个实心(不透明)圆形,向图像的边缘延伸,图像逐渐变透明,如下图所示:



接着,需要设置4个顶点,这4个顶点使用了2个多边形(可以使用三角形带进行优化)。顶点的坐标表示粒子的缺省大小,稍后需要将粒子进行缩放,以适合这个大小。每个粒子都可以拥有独特的属性,包括粒子颜色(通过使用材质来实现)。

接下来,将这个结构体同一个含有两个多边形(创建一个正方形)的顶点缓冲结合起来,以便将多边形渲染到3D设备上。在被绘制出来之前,每个粒子都需要通过它自己的世界矩阵进行定向(当然使用公告板)。然后将世界变换矩阵同每个粒子的缩放变换矩阵组合起来,再设置一个材质(使用 IDirect3DDevice::SetMaterial函数),用来改变粒子的颜色。最后,绘制粒子。

完整源码如下所示:

/* **************************************************************************************
PURPOSE:
    Particle Demo

Required libraries:
  WINMM.lib, D3D9.LIB, D3DX9.LIB.
 **************************************************************************************
*/

#include 
< windows.h >
#include 
< stdio.h >
#include 
" d3d9.h "
#include 
" d3dx9.h "

#pragma comment(lib, 
" winmm.lib " )
#pragma comment(lib, 
" d3d9.lib " )
#pragma comment(lib, 
" d3dx9.lib " )

#pragma warning(disable : 
4305   4244 )

#define  WINDOW_WIDTH    400
#define  WINDOW_HEIGHT   400

#define  Safe_Release(p) if((p)) (p)->Release();

//  window handles, class and caption text.
HWND g_hwnd;
HINSTANCE g_inst;
static   char  g_class_name[]  =   " ParticleClass " ;
static   char  g_caption[]     =   " Particle Demo " ;

//  the Direct3D and device object
IDirect3D9 *  g_d3d  =  NULL;
IDirect3DDevice9
*  g_d3d_device  =  NULL;

//  the particle vertex buffer and texture
IDirect3DVertexBuffer9 *  g_particle_vb  =  NULL;
IDirect3DTexture9
*       g_particle_texture  =  NULL;

//  The particle vertex format and descriptor
typedef  struct
{
    
float  x, y, z;       //  3D coordinates    
    D3DCOLOR diffuse;    //  color
     float  u, v;          //  texture coordinates
} VERTEX;

#define  VERTEX_FVF   (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

//  create a structure for tracking particles
struct  PARTICLE
{
    
float  x_pos, y_pos, z_pos;   //  coordinate
     float  x_add, y_add, z_add;   //  movement values
     float  red, green, blue;      //  colors
     long   timer, counter;        //  current and update counter

    PARTICLE()
    {
        
//  position particle at origin
        x_pos  =  y_pos  =  z_pos  =   0.0 ;

        
//  get a random update counter
        counter  =  rand()  %   50   +   10 ;
        timer 
=   0 ;

        
//  get a random speed
        x_add  =  ( float )(rand()  %   11 -   5.0 ;
        y_add 
=  ( float )(rand()  %   11 -   5.0 ;
        z_add 
=  ( float )(rand()  %   11 -   5.0 ;

        
//  get a random color
        red    =  ( float )(rand()  %   101 /   100.0 ;
        green 
=  ( float )(rand()  %   101 /   100.0 ;
        blue  
=  ( float )(rand()  %   101 /   100.0 ;
    }
};

PARTICLE
*  g_particles  =  NULL;

// --------------------------------------------------------------------------------
//  Window procedure.
// --------------------------------------------------------------------------------
long  WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    
switch (msg)
    {
    
case  WM_DESTROY:
        PostQuitMessage(
0 );
        
return   0 ;
    }

    
return  ( long ) DefWindowProc(hwnd, msg, wParam, lParam);
}

// --------------------------------------------------------------------------------
//  Copy vertex data into vertex buffer, create texture from file.
// --------------------------------------------------------------------------------
BOOL Setup_Particles()
{
    BYTE
*  vertex_ptr;

    VERTEX verts[] 
=  {
        { 
- 50.0f 50.0f 0.0f 0xFFFFFFFF 0.0f 0.0f  },
        {  
50.0f 50.0f 0.0f 0xFFFFFFFF 1.0f 0.0f  },
        { 
- 50.0f ,   0.0f 0.0f 0xFFFFFFFF 0.0f 1.0f  },
        {  
50.0f ,   0.0f 0.0f 0xFFFFFFFF 1.0f 1.0f  }
    };    

    
//  create vertex buffers and stuff in data       
     if (FAILED(g_d3d_device -> CreateVertexBuffer( sizeof (verts),  0 , VERTEX_FVF, D3DPOOL_DEFAULT,  & g_particle_vb, NULL)))   
        
return  FALSE;   

    
//  locks a range of vertex data and obtains a pointer to the vertex buffer memory
     if (FAILED(g_particle_vb -> Lock( 0 0 , ( void ** ) & vertex_ptr,  0 )))
        
return  FALSE;

    memcpy(vertex_ptr, verts, 
sizeof (verts));

    
//  unlocks vertex data
    g_particle_vb -> Unlock();    

    
//  get textures    
    D3DXCreateTextureFromFile(g_d3d_device,  " Particle.bmp " & g_particle_texture);    
    
    
//  create some particles
    g_particles  =   new  PARTICLE[ 512 ];

    
return  TRUE;
}

// --------------------------------------------------------------------------------
//  Initialize d3d, d3d device, vertex buffer, texutre; set render state for d3d;
//  set perspective matrix.
// --------------------------------------------------------------------------------
BOOL Do_Init()
{
    D3DPRESENT_PARAMETERS present_param;
    D3DDISPLAYMODE  display_mode;
    D3DXMATRIX mat_proj, mat_view;    

    
//  do a windowed mode initialization of Direct3D
     if ((g_d3d  =  Direct3DCreate9(D3D_SDK_VERSION))  ==  NULL)
        
return  FALSE;

    
//  retrieves the current display mode of the adapter
     if (FAILED(g_d3d -> GetAdapterDisplayMode(D3DADAPTER_DEFAULT,  & display_mode)))
        
return  FALSE;

    ZeroMemory(
& present_param,  sizeof (present_param));

    
//  initialize d3d presentation parameter
    present_param.Windowed                =  TRUE;
    present_param.SwapEffect             
=  D3DSWAPEFFECT_DISCARD;
    present_param.BackBufferFormat       
=  display_mode.Format;
    present_param.EnableAutoDepthStencil 
=  TRUE;
    present_param.AutoDepthStencilFormat 
=  D3DFMT_D16;

    
//  creates a device to represent the display adapter
     if (FAILED(g_d3d -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd,
                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
& present_param,  & g_d3d_device)))
        
return  FALSE;     

    
//  set render state

    
//  enable d3d lighting
    g_d3d_device -> SetRenderState(D3DRS_LIGHTING, TRUE);
    
//  enable z-buffer
    g_d3d_device -> SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
    
//  set ambient light to highest level (to see particles)
    g_d3d_device -> SetRenderState(D3DRS_AMBIENT,  0xFFFFFFFF );

    
//  create and set the projection matrix

    
//  builds a left-handed perspective projection matrix based on a field of view
    D3DXMatrixPerspectiveFovLH( & mat_proj, D3DX_PI / 4.0 1.0 1.0 1000.0 );

    
//  sets a single device transformation-related state
    g_d3d_device -> SetTransform(D3DTS_PROJECTION,  & mat_proj);

    
//  create and set the view transformation
    D3DXMatrixLookAtLH( & mat_view,  & D3DXVECTOR3( 0.0f 0.0f - 500.0f ),  & D3DXVECTOR3( 0.0f 0.0f 0.0f ), 
                       
& D3DXVECTOR3( 0.0f 1.0f 0.0f ));

    g_d3d_device
-> SetTransform(D3DTS_VIEW,  & mat_view);

    
//  create the meshes
    Setup_Particles();    

    
return  TRUE;
}

// --------------------------------------------------------------------------------
//  Release all d3d resource.
// --------------------------------------------------------------------------------
BOOL Do_Shutdown()
{
    delete[] g_particles;

    Safe_Release(g_particle_vb);
    Safe_Release(g_particle_texture);    
    Safe_Release(g_d3d_device);
    Safe_Release(g_d3d);

    
return  TRUE;
}

// --------------------------------------------------------------------------------
//  Render a frame.
// --------------------------------------------------------------------------------
BOOL Do_Frame()
{
    D3DXMATRIX mat_view, mat_world, mat_transposed, mat_transform;
    
static  D3DMATERIAL9 s_material;
    
static  BOOL  s_is_mat_init  =  TRUE;
    
static  DWORD s_counter  =  timeGetTime();

    
//  limit to 30fps
     if (timeGetTime()  <  s_counter + 33 )
        
return  TRUE;

    s_counter 
=  timeGetTime();

    
//  configure the material if first time called
     if (s_is_mat_init  =  TRUE)
    {
        s_is_mat_init 
=  FALSE;
        ZeroMemory(
& s_material,  sizeof (s_material));
        s_material.Diffuse.a 
=  s_material.Ambient.a  =   0.5f ;
    }

    
//  clear device back buffer
    g_d3d_device -> Clear( 0 , NULL, D3DCLEAR_TARGET  |  D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA( 0 64 128 255 ),  1.0f 0 );    

    
//  Begin scene
     if (SUCCEEDED(g_d3d_device -> BeginScene()))
    {
        
//  set the particle source, shader, texture.            
        g_d3d_device -> SetStreamSource( 0 , g_particle_vb,  0 sizeof (VERTEX));
        g_d3d_device
-> SetFVF(VERTEX_FVF);
        g_d3d_device
-> SetTexture( 0 , g_particle_texture);

        
//  get and set the transposed view matrix (billboard technique)
        g_d3d_device -> GetTransform(D3DTS_VIEW,  & mat_view);
        D3DXMatrixTranspose(
& mat_transposed,  & mat_view);

        
//  enable alpha blending
        g_d3d_device -> SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
        g_d3d_device
-> SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
        g_d3d_device
-> SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);    

        
//  loop through all particles and draw them
         for ( short  i  =   0 ; i  <   512 ; i ++ )
        {
            
//  move particle first
            g_particles[i].x_pos  +=  g_particles[i].x_add;
            g_particles[i].y_pos 
+=  g_particles[i].y_add;
            g_particles[i].z_pos 
+=  g_particles[i].z_add;

            
//  reverse movements if past counter
             if ((g_particles[i].timer  +=   1 >=  g_particles[i].counter)
            {
                g_particles[i].timer 
=   0 ;
                g_particles[i].x_add 
*=   - 1.0f ;
                g_particles[i].y_add 
*=   - 1.0f ;
                g_particles[i].z_add 
*=   - 1.0f ;
            }

            
//  setup the particle's world transformation
            D3DXMatrixTranslation( & mat_transform, g_particles[i].x_pos, g_particles[i].y_pos, g_particles[i].z_pos);
            D3DXMatrixMultiply(
& mat_world,  & mat_transform,  & mat_transposed);
            g_d3d_device
-> SetTransform(D3DTS_WORLD,  & mat_world);

            
//  set the particle's material
            s_material.Diffuse.r  =  s_material.Ambient.r  =  g_particles[i].red;
            s_material.Diffuse.g 
=  s_material.Ambient.g  =  g_particles[i].green;
            s_material.Diffuse.b 
=  s_material.Ambient.b  =  g_particles[i].blue;
                
            
//  Sets the material properties for the device
            g_d3d_device -> SetMaterial( & s_material);

            
//  draw the particle
            g_d3d_device -> DrawPrimitive(D3DPT_TRIANGLESTRIP,  0 2 );
        }        

        
//  release texture
        g_d3d_device -> SetTexture( 0 , NULL);

        
//  end the scene
        g_d3d_device -> EndScene();
    }

    
//  present the contents of the next buffer in the sequence of back buffers owned by the device
    g_d3d_device -> Present(NULL, NULL, NULL, NULL);

    
return  TRUE;
}

// --------------------------------------------------------------------------------
//  Main function, routine entry.
// --------------------------------------------------------------------------------
int  WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line,  int  cmd_show)
{
    WNDCLASSEX  win_class;
    MSG         msg;

    g_inst 
=  inst;

    
//  create window class and register it
    win_class.cbSize         =   sizeof (win_class);
    win_class.style         
=  CS_CLASSDC;
    win_class.lpfnWndProc   
=  Window_Proc;
    win_class.cbClsExtra    
=   0 ;
    win_class.cbWndExtra    
=   0 ;
    win_class.hInstance     
=  inst;
    win_class.hIcon         
=  LoadIcon(NULL, IDI_APPLICATION);
    win_class.hCursor       
=  LoadCursor(NULL, IDC_ARROW);
    win_class.hbrBackground 
=  NULL;
    win_class.lpszMenuName  
=  NULL;
    win_class.lpszClassName 
=  g_class_name;
    win_class.hIconSm       
=  LoadIcon(NULL, IDI_APPLICATION);

    
if ( !  RegisterClassEx( & win_class))
        
return  FALSE;

    
//  create the main window
    g_hwnd  =  CreateWindow(g_class_name, g_caption, WS_CAPTION  |  WS_SYSMENU,  0 0 ,
                          WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, inst, NULL);

    
if (g_hwnd  ==  NULL)
        
return  FALSE;

    ShowWindow(g_hwnd, SW_NORMAL);
    UpdateWindow(g_hwnd);

    
//  initialize game
     if (Do_Init()  ==  FALSE)
        
return  FALSE;

    
//  start message pump, waiting for signal to quit.
    ZeroMemory( & msg,  sizeof (MSG));

    
while (msg.message  !=  WM_QUIT)
    {
        
if (PeekMessage( & msg, NULL,  0 0 , PM_REMOVE))
        {
            TranslateMessage(
& msg);
            DispatchMessage(
& msg);
        }
        
        
//  draw a frame
         if (Do_Frame()  ==  FALSE)
            
break ;
    }

    
//  run shutdown function
    Do_Shutdown();

    UnregisterClass(g_class_name, inst);
    
    
return  ( int ) msg.wParam;
}

效果图:


转载于:https://www.cnblogs.com/flying_bat/archive/2007/07/04/871684.html

相关文章:

  • Oracle GoldenGate (ogg) 11.2.1.0.20 是最后一个支持oracle db 10g的 ogg版本号
  • adapter facade 和 proxy
  • 好用的在线web页面测试,移动页面测试工具webpagetest使用图文教程
  • 安装 MariaDB
  • dotText源码阅读(4)--DTO和数据访问
  • Java之JDBC学习
  • 上海:法制宣传上公交 移动电视受好评
  • 基于FPGA的XPT2046触摸控制器设计
  • Tomcat漏洞利用
  • jQuery获取select中全部option值
  • Dubbo之旅--集群容错和负载均衡
  • 动态权限树控件
  • 南桥-- 算法训练 2的次幂表示
  • 第九章:XML文档集成---AxInternalBase API
  • 微信登陆
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • 分享的文章《人生如棋》
  • [译]Python中的类属性与实例属性的区别
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • co模块的前端实现
  • ES6简单总结(搭配简单的讲解和小案例)
  • Facebook AccountKit 接入的坑点
  • go语言学习初探(一)
  • JavaScript实现分页效果
  • JS基础之数据类型、对象、原型、原型链、继承
  • js作用域和this的理解
  • PV统计优化设计
  • Redis学习笔记 - pipline(流水线、管道)
  • Yii源码解读-服务定位器(Service Locator)
  • 编写高质量JavaScript代码之并发
  • 测试如何在敏捷团队中工作?
  • 番外篇1:在Windows环境下安装JDK
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 设计模式走一遍---观察者模式
  • 实现菜单下拉伸展折叠效果demo
  • 怎么将电脑中的声音录制成WAV格式
  • 最近的计划
  • #《AI中文版》V3 第 1 章 概述
  • $forceUpdate()函数
  • (javascript)再说document.body.scrollTop的使用问题
  • (LeetCode 49)Anagrams
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (接口封装)
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • .net core开源商城系统源码,支持可视化布局小程序
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • @我的前任是个极品 微博分析
  • [ Linux Audio 篇 ] 音频开发入门基础知识
  • [ 蓝桥杯Web真题 ]-Markdown 文档解析
  • [20150904]exp slow.txt