Presentation is loading. Please wait.

Presentation is loading. Please wait.

Részecskerendszerek, spriteok, plakátok, instancing Szécsi László.

Similar presentations


Presentation on theme: "Részecskerendszerek, spriteok, plakátok, instancing Szécsi László."— Presentation transcript:

1 Részecskerendszerek, spriteok, plakátok, instancing Szécsi László

2 Eddig jutottunk….

3 Új osztály: Lab4Particles copy&paste: Lab3EnvMap.h -> Lab4Particles.h copy&paste: Lab3EnvMap.cpp -> Lab4Particles.cpp search&replace ebben a két fileban:Lab3EnvMap->Lab4Particles copy&paste: envmap.fx -> billboard.fx Effect betöltésnél: billboard.fx

4 Lab3 példány helyett Lab4 példányt hozzunk létre #include “Lab4Particles.h” HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { engine = new Lab3EnvMap(pd3dDevice); engine = new Lab4Particles(pd3dDevice); Globális példány kicserélése FILEGraphGame.cppOPdel/add codeFUNCOnD3D9CreateDevice

5 Próba WASD + egér

6 class Lab4Particles { LPDIRECT3DVERTEXBUFFER9 billboardVertexBuffer; LPDIRECT3DINDEXBUFFER9 billboardIndexBuffer; LPDIRECT3DVERTEXBUFFER9 particleInstanceBuffer; LPDIRECT3DVERTEXDECLARATION9 particleSystemVertexDecl; unsigned int nMaxParticles; Plakát geometria FILELab4Particles.hOPnew memberCLASSLab4Particles

7 class Particle { friend class Lab4Particles; D3DXVECTOR3 pos; D3DXVECTOR3 velocity; float age[2]; }; Részecske class FILEParticle.h Particle.cppOPnew classCLASSParticle

8 #include "Particle.h" Részecske class FILELab4Particles.cppOPnew includeCLASSLab4Particles

9 Lab4Particles::Lab4Particles( LPDIRECT3DDEVICE9 device) :EngineInterface(device), nMaxParticles(20) { Maximum részecskeszám inticializálása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODLab4Particles

10 Vertex layout megadása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources HRESULT Lab4Particles::createDefaultResources() { D3DVERTEXELEMENT9 particleSystemVertexElements[] ={ {0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, {1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {1, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1}, {1, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2}, D3DDECL_END() };

11 device->CreateVertexDeclaration( particleSystemVertexElements, &particleSystemVertexDecl); Vertex layout megadása folyt. FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources

12 HRESULT Lab4Particles:: releaseDefaultResources() { particleSystemVertexDecl ->Release(); Vertex desc felszabadítása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODreleaseDefaultResources

13 HRESULT Lab4Particles::createDefaultResources(){ device->CreateVertexBuffer( 4 * sizeof(D3DXVECTOR4), D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, D3DFVF_XYZW, D3DPOOL_DEFAULT, &billboardVertexBuffer, NULL); Billboard vertex buffer létrehozása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources

14 D3DXVECTOR4* vertexData; billboardVertexBuffer->Lock(0, 0, (void**)&vertexData, D3DLOCK_DISCARD); //float4(offset.x, offset.y, texcoord.u, texcoord.v) vertexData[0] = D3DXVECTOR4(-1.0f, -1.0f, 0.0f, 0.0f); vertexData[1] = D3DXVECTOR4(1.0f, -1.0f, 1.0f, 0.0f); vertexData[2] = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); vertexData[3] = D3DXVECTOR4(-1.0f, 1.0f, 0.0f, 1.0f); billboardVertexBuffer->Unlock(); Billboard vertex buffer feltöltése FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources

15 device->CreateIndexBuffer( 6 * sizeof(short), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &billboardIndexBuffer, NULL); Index vertex buffer létrehozása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources

16 short* indexData; billboardIndexBuffer->Lock(0, 0, (void**)&indexData, D3DLOCK_DISCARD); indexData[0] = 0; indexData[1] = 3; indexData[2] = 1; indexData[3] = 2; indexData[4] = 1; indexData[5] = 3; billboardIndexBuffer->Unlock(); Index vertex buffer feltöltése FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources

17 device->CreateVertexBuffer( sizeof(Particle) * nMaxParticles, D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &particleInstanceBuffer, NULL); Példány adatok buffere (instance buffer) FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources

18 Particle* particleData; particleInstanceBuffer->Lock(0, 0, (void**)&particleData, D3DLOCK_DISCARD); for(int i = 0; i < nMaxParticles; i++){ float x = (float)rand() / RAND_MAX * 20; float y = (float)rand() / RAND_MAX * 20; float z = (float)rand() / RAND_MAX * 20; particleData[i].pos = D3DXVECTOR3(x, y, z); } particleInstanceBuffer->Unlock(); Példány adatok feltöltése FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources

19 HRESULT lab4Particles:: releaseDefaultResources(){ billboardVertexBuffer ->Release(); billboardIndexBuffer ->Release(); particleInstanceBuffer ->Release(); Bufferek felszabadítása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODreleaseDefaultResources

20 effect->EndPass(); effect->End(); effect-> SetTechnique("billboard"); effect->Begin(&a, 0); effect->BeginPass(0); Technique beállítás rajzoláshoz FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODrender

21 device->SetStreamSourceFreq (0, D3DSTREAMSOURCE_INDEXEDDATA | nMaxParticles); device->SetStreamSource (0, billboardVertexBuffer, 0, sizeof(float) * 4); device->SetStreamSourceFreq (1, D3DSTREAMSOURCE_INSTANCEDATA | 1); device->SetStreamSource (1, particleInstanceBuffer, 0, sizeof(Particle)); device->SetVertexDeclaration (particleSystemVertexDecl); device->SetIndices(billboardIndexBuffer); Bufferek inputra kötése FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODrender

22 device->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2); device->SetStreamSourceFreq(0, 1); device->SetStreamSource(0, NULL, 0, 0); device->SetStreamSourceFreq(1, 1); device->SetStreamSource(1, NULL, 0, 0); device->SetIndices(0); effect->EndPass(); effect->End(); Rajzolás és az instancing kikapcsolása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODrender

23 struct ParticleInput{ float4 pos: POSITION; float4 offset: TEXCOORD0; float3 velocity: TEXCOORD1; float2 age: TEXCOORD2; }; struct ParticleOutput{ float4 pos: POSITION; float2 tex: TEXCOORD0; float4 color: TEXCOORD1; }; Input-output szemantika FILEbillboard.fxOPnew structFUNC::

24 float2 billboardSize = float2(5, 5); ParticleOutput vsParticle( ParticleInput input) { ParticleOutput output = (ParticleOutput) 0; output.pos = mul(input.pos, modelViewProjMatrix); output.pos.xy += input.offset.xy * billboardSize; output.tex = input.offset.zw; output.color = 1; return output; } Vertex shader FILEbillboard.fxOPnew funcFUNCvsParticle

25 float4 psParticle( ParticleOutput input):COLOR { return input.color; } Pixel shader FILEbillboard.fxOPnew funcFUNCpsParticle

26 technique billboard{ pass ExamplePass { VertexShader = compile vs_3_0vsParticle(); PixelShader = compile ps_2_0psParticle(); } Billboard technique FILEbillboard.fxOPnew techniqueFUNC::

27 Próba WASD + egér

28 class Lab4Particles { LPDIRECT3DTEXTURE9 particleTexture; Részecske textúra FILELab4Particles.hOPnew memberCLASSLab4Particles

29 HRESULT Lab4Particles:: createManagedResources() { D3DXCreateTextureFromFile( device, L"media//particle.dds", &particleTexture); Textúra betöltése FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODreleaseManagedResources

30 HRESULT Lab4Particles:: releaseManagedResources() { particleTexture->Release(); Textúra felszabadítása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateManagedResources

31 void Lab4Particles::render() { effect->SetTexture("colorMap", particleTexture); effect->CommitChanges(); Textúra bekötése FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODrender

32 texture colorMap; sampler2D colorMapSampler = sampler_state{ texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; Új textúra és sampler FILEbillboard.fxOPnew varFUNC::

33 float4 psParticle( ParticleInput input):COLOR { return input.color * tex2D(colorMapSampler, input.tex); } Új textúra és sampler FILEbillboard.fxOPedit codeFUNCpsParticle

34 Próba WASD + egér

35 technique billboard{ pass ExamplePass{ AlphaBlendEnable = true; SrcBlend = SrcAlpha; DestBlend = InvSrcAlpha; ZWriteEnable = false; VertexShader = compile vs_3_0 vsParticle(); PixelShader = compile ps_2_0 psParticle(); } Alpha blending FILEbillboard.fxOPedit scriptFUNC::

36 Próba WASD + egér

37 technique billboard{ pass ExamplePass{ AlphaBlendEnable = true; SrcBlend = SrcAlpha; DestBlend = One; ZWriteEnable = false; VertexShader = compile vs_3_0 vsParticle(); PixelShader = compile ps_2_0 psParticle(); } Additive blending FILEbillboard.fxOPedit codeFUNC::

38 Próba WASD + egér

39 float4 psParticle( ParticleOutput input):COLOR { float4 color = input.color * tex2D(colorMapSampler, input.tex); color.rgb = float3(color.a, pow(color.a, 4), pow(color.a, 10)); return color; } Játsszunk a színekkel FILEbillboard.fxOPedit codeFUNCpsParticle

40 Próba WASD + egér

41 class Particle { void reborn(){ float x = (float)rand() / RAND_MAX; float y = (float)rand() / RAND_MAX; float z = (float)rand() / RAND_MAX; x = x * 2 - 1; y = y * 2 - 1; z = z * 2 - 1; pos = D3DXVECTOR3(x, y, z); age[0] = 0; age[1] = (float)rand() / RAND_MAX * 3.0; velocity = D3DXVECTOR3(x*5, y*5, z*5); } Részecske születés FILEParticle.hOPnew methodCLASSParticle METHODreborn

42 HRESULT Lab4Particles:: createDefaultResources() { for(int i = 0; i < nMaxParticles; i++) { particleData[i].reborn(); } Részecskék inicializálása FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODcreateDefaultResources gy.k.: ami szürke, azt nem kell még egyszer bemásolni!

43 class Particle{ void move(float dt) { pos = pos + velocity * dt; age[0] += dt; if(age[0] > age[1]) reborn(); } Részecske animáció FILEParticle.hOPnew methodCLASSParticle METHODmove

44 void Lab4Particles::animate(double dt, double t){ camera.FrameMove(dt); void* instanceData; particleInstanceBuffer-> Lock(0, 0, &instanceData, 0); Particle* particleData = (Particle*) instanceData; for(int i = 0; i < nMaxParticles; i++) particleData[i].move(dt); particleInstanceBuffer->Unlock(); } Részecskerendszer animáció FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODanimate

45 Próba WASD + egér

46 float2 billboardSize = float2(5.0, 5.0); ParticleOutput vsParticle(ParticleInput input) { ParticleOutput output = (ParticleOutput) 0; output.pos = mul(input.pos, modelViewProjMatrix); float relAge = input.age[0] / input.age[1]; output.pos.xy += input.offset.xy * billboardSize * 6.0 * relAge; output.color = 1 - abs(relAge * ); output.tex = input.offset * float2(0.5, 0.5); return output; } Korfüggő méret, intenzitás és átlátszóság FILEbillboard.fxOPedit codeFUNCvsParticle

47 Próba WASD + egér

48 technique billboard{ pass ExamplePass{ AlphaBlendEnable = true; SrcBlend = SrcAlpha; DestBlend = InvSrcAlpha; ZWriteEnable = false; VertexShader = compile vs_3_0 vsParticle(); PixelShader = compile ps_2_0 psParticle(); } Alpha blending visszaállítása FILEbillboard.fxOPedit scriptFUNC::

49 class Particle; class Lab4Particles { Particle* particles; Részecske tömb rendszermemóriában FILELab4Particles.hOPnew memberCLASSLab4Particles

50 Lab4Particles::Lab4Particles(LPD IRECT3DDEVICE9 device) :EngineInterface(device), nMaxParticles(20) { particles = new Particle[nMaxParticles]; for(int i = 0; i < nMaxParticles; i++) particles[i].reborn(); Részecske tömb allokálás FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODLab4Particles

51 Lab4Particles::~Lab4Particles() { delete particles; } Részecske tömb allokálás FILELab4Particles.cppOPnew methodCLASSLab4Particles METHOD~Lab4Particles

52 class EngineInterface { virtual ~EngineInterface(){} Ugye virtuális a destruktor? FILEEngineInterface.hOPverify codeCLASSEngineInterface METHOD~EngineInterface

53 Particle* particleData; particleInstanceBuffer->Lock(0, 0, (void**)&particleData, D3DLOCK_DISCARD); for(int i = 0; i < nMaxParticles; i++){ float x = (float)rand() / RAND_MAX * 20; float y = (float)rand() / RAND_MAX * 20; float z = (float)rand() / RAND_MAX * 20; particleData[i].pos = D3DXVECTOR3(x, y, z); particleData[i].reborn(); } particleInstanceBuffer->Unlock(); Instance buffer init törlés FILELab4Particles.cppOPdelete codeCLASSLab4Particles METHODcreateDefaultResources

54 void* instanceData; particleInstanceBuffer-> Lock(0, 0, &instanceData, 0); Particle* particleData = (Particle*) instanceData; for(int i = 0; i < nMaxParticles; i++) particleData[i].move(dt); particleInstanceBuffer->Unlock(); Instance bufferben közvetlen anim törlés FILELab4Particles.cppOPdelete codeCLASSLab4Particles METHODanimate

55 for(int i = 0; i < nMaxParticles; i++) particles[i].move(dt); //ide jön a rendezés void* instanceData; particleInstanceBuffer->Lock(0, 0, &instanceData, 0); memcpy(instanceData, particles, sizeof(Particle) * nMaxParticles); particleInstanceBuffer->Unlock(); Tömb animációja, feltöltése FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODanimate

56 Próba hibás takarás

57 const D3DXVECTOR3& ahead = *camera.GetWorldAhead(); for(int j = 1; j < nMaxParticles; j++) for(int k = 0; k < nMaxParticles-1; k++){ float distk = D3DXVec3Dot(&ahead, &particles[k].pos); float distk1 = D3DXVec3Dot(&ahead, &particles[k+1].pos); if( distk < distk1){ Particle p = particles[k+1]; particles[k+1] = particles[k]; particles[k] = p; } Buborékrendezés kamera z szerint FILELab4Particles.cppOPadd codeCLASSLab4Particles METHODanimate

58 float4 psParticle(ParticleOutput input):COLOR { float4 color = tex2D(colorMapSampler, input.tex); color.rgb = float3(color.a, pow(color.a, 4), pow(color.a, 10)); return float4(color.rgb, color.a * input.color.a); } Életkorfüggő sötétedés ne legyen FILEbillboard.fxOPedit codeFUNCpsParticle

59 Próba most már jó a sorrend, de ha változik, hirtelen váltás van (popping) megoldás: elvi szinten nincs enyhítés: több halványabb billboard

60 float4 color = tex2D(colorMapSampler, input.tex); color.rgb = float3(color.a, pow(color.a, 4), pow(color.a, 10)); return float4(color.rgb, color.a * input.color.a * 0.1); Halványítás FILEbillboard.fxOPedit codeFUNCpsParticle

61 Lab4Particles::Lab4Particles( LPDIRECT3DDEVICE9 device) :EngineInterface(device), nMaxParticles(200) { Több részecske FILELab4Particles.cppOPedit codeCLASSLab4Particles METHODLab4Particles


Download ppt "Részecskerendszerek, spriteok, plakátok, instancing Szécsi László."

Similar presentations


Ads by Google