From 7fbbc1f98d5411ff03f273a078df154e2891df7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 4 Jan 2017 22:07:56 -0800 Subject: [PATCH] ps: Added aabb calculation for emitter. --- examples/32-particles/particles.cpp | 9 +++---- examples/common/bounds.cpp | 6 +++++ examples/common/bounds.h | 3 +++ examples/common/ps/particle_system.cpp | 35 +++++++++++++++++++++++--- examples/common/ps/particle_system.h | 5 ++++ 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/examples/32-particles/particles.cpp b/examples/32-particles/particles.cpp index ce084429e..21ccafc4f 100644 --- a/examples/32-particles/particles.cpp +++ b/examples/32-particles/particles.cpp @@ -393,11 +393,10 @@ class Particles : public entry::AppI if (showBounds) { -// Aabb aabb; -// toAabb(aabb, tvb.data, tvb.size/tvb.stride, tvb.stride); - -// ddSetColor(0xff0000ff); -// ddDraw(aabb); + Aabb aabb; + psGetAabb(m_emitter[currentEmitter].m_handle, aabb); + ddSetColor(0xff0000ff); + ddDraw(aabb); } diff --git a/examples/common/bounds.cpp b/examples/common/bounds.cpp index 96a775024..6afd5755d 100644 --- a/examples/common/bounds.cpp +++ b/examples/common/bounds.cpp @@ -182,6 +182,12 @@ void aabbExpand(Aabb& _aabb, float _factor) _aabb.m_max[2] += _factor; } +void aabbExpand(Aabb& _aabb, const float* _pos) +{ + bx::vec3Min(_aabb.m_min, _aabb.m_min, _pos); + bx::vec3Max(_aabb.m_max, _aabb.m_max, _pos); +} + uint32_t aabbOverlapTest(const Aabb& _aabb0, const Aabb& _aabb1) { const uint32_t ltMinX = _aabb0.m_max[0] < _aabb1.m_min[0]; diff --git a/examples/common/bounds.h b/examples/common/bounds.h index fcfd1872c..604892c3f 100644 --- a/examples/common/bounds.h +++ b/examples/common/bounds.h @@ -84,6 +84,9 @@ void toAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _num /// Expand AABB. void aabbExpand(Aabb& _aabb, float _factor); +/// Expand AABB with xyz. +void aabbExpand(Aabb& _aabb, const float* _pos); + /// Calculate surface area of axis aligned bounding box. float calcAreaAabb(const Aabb& _aabb); diff --git a/examples/common/ps/particle_system.cpp b/examples/common/ps/particle_system.cpp index 1918d28f3..96fb7f614 100644 --- a/examples/common/ps/particle_system.cpp +++ b/examples/common/ps/particle_system.cpp @@ -191,6 +191,7 @@ namespace ps void reset() { m_num = 0; + memset(&m_aabb, 0, sizeof(Aabb) ); } void update(float _dt) @@ -321,13 +322,19 @@ namespace ps } } - uint32_t render(const float* _mtxView, const float* _eye, uint32_t _first, uint32_t _max, ParticleSort* _outSort, PosColorTexCoord0Vertex* _outVertices) const + uint32_t render(const float* _mtxView, const float* _eye, uint32_t _first, uint32_t _max, ParticleSort* _outSort, PosColorTexCoord0Vertex* _outVertices) { bx::EaseFn easeRgba = s_easeFunc[m_uniforms.m_easeRgba]; bx::EaseFn easePos = s_easeFunc[m_uniforms.m_easePos]; bx::EaseFn easeBlend = s_easeFunc[m_uniforms.m_easeBlend]; bx::EaseFn easeScale = s_easeFunc[m_uniforms.m_easeScale]; + Aabb aabb = + { + { HUGE_VALF, HUGE_VALF, HUGE_VALF }, + { -HUGE_VALF, -HUGE_VALF, -HUGE_VALF }, + }; + for (uint32_t jj = 0, num = m_num, current = _first ; jj < num && current < _max ; ++jj, ++current @@ -376,6 +383,7 @@ namespace ps PosColorTexCoord0Vertex* vertex = &_outVertices[current*4]; bx::vec3Sub(tmp, pos, udir); bx::vec3Sub(&vertex->m_x, tmp, vdir); + aabbExpand(aabb, &vertex->m_x); vertex->m_abgr = abgr; vertex->m_u = 0.0f; vertex->m_v = 0.0f; @@ -384,6 +392,7 @@ namespace ps bx::vec3Add(tmp, pos, udir); bx::vec3Sub(&vertex->m_x, tmp, vdir); + aabbExpand(aabb, &vertex->m_x); vertex->m_abgr = abgr; vertex->m_u = 1.0f; vertex->m_v = 0.0f; @@ -392,6 +401,7 @@ namespace ps bx::vec3Add(tmp, pos, udir); bx::vec3Add(&vertex->m_x, tmp, vdir); + aabbExpand(aabb, &vertex->m_x); vertex->m_abgr = abgr; vertex->m_u = 1.0f; vertex->m_v = 1.0f; @@ -400,6 +410,7 @@ namespace ps bx::vec3Sub(tmp, pos, udir); bx::vec3Add(&vertex->m_x, tmp, vdir); + aabbExpand(aabb, &vertex->m_x); vertex->m_abgr = abgr; vertex->m_u = 0.0f; vertex->m_v = 1.0f; @@ -407,6 +418,8 @@ namespace ps ++vertex; } + m_aabb = aabb; + return m_num; } @@ -417,6 +430,8 @@ namespace ps bx::RngMwc m_rng; EmitterUniforms m_uniforms; + Aabb m_aabb; + Particle* m_particles; uint32_t m_num; uint32_t m_max; @@ -455,7 +470,7 @@ namespace ps bgfx::RendererType::Enum type = bgfx::getRendererType(); m_particleProgram = bgfx::createProgram( - bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_particle") + bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_particle") , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_particle") , true ); @@ -519,7 +534,7 @@ namespace ps for (uint16_t ii = 0, numEmitters = m_emitterAlloc->getNumHandles(); ii < numEmitters; ++ii) { const uint16_t idx = m_emitterAlloc->getHandleAt(ii); - const Emitter& emitter = m_emitter[idx]; + Emitter& emitter = m_emitter[idx]; pos += emitter.render(_mtxView, _eye, pos, max, particleSort, vertices); } @@ -591,6 +606,15 @@ namespace ps } } + void getAabb(EmitterHandle _handle, Aabb& _outAabb) + { + BX_CHECK(m_emitterAlloc.isValid(_handle.idx) + , "getAabb handle %d is not valid." + , _handle.idx + ); + _outAabb = m_emitter[_handle.idx].m_aabb; + } + void destroyEmitter(EmitterHandle _handle) { BX_CHECK(m_emitterAlloc.isValid(_handle.idx) @@ -658,6 +682,11 @@ void psUpdateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms) s_ctx.updateEmitter(_handle, _uniforms); } +void psGetAabb(EmitterHandle _handle, Aabb& _outAabb) +{ + s_ctx.getAabb(_handle, _outAabb); +} + void psDestroyEmitter(EmitterHandle _handle) { s_ctx.destroyEmitter(_handle); diff --git a/examples/common/ps/particle_system.h b/examples/common/ps/particle_system.h index 8c80f3e90..be7065824 100644 --- a/examples/common/ps/particle_system.h +++ b/examples/common/ps/particle_system.h @@ -10,6 +10,8 @@ #include #include +#include "../bounds.h" + struct EmitterShape { enum Enum @@ -74,6 +76,9 @@ EmitterHandle psCreateEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum /// void psUpdateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms = NULL); +/// +void psGetAabb(EmitterHandle _handle, Aabb& _outAabb); + /// void psDestroyEmitter(EmitterHandle _handle);