summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/common/ps/particle_system.cpp
diff options
context:
space:
mode:
author mooglyguy <therealmogminer@gmail.com>2018-12-05 19:45:08 +0100
committer mooglyguy <therealmogminer@gmail.com>2018-12-05 19:45:08 +0100
commit0bd02131b644b61088789f52f31b750c9aecaa6d (patch)
tree811c679a1bba8b24fc7967cdfe73640254d64156 /3rdparty/bgfx/examples/common/ps/particle_system.cpp
parent9a81ec7eaf00d73a23db5c003dc45b55d4b76c4a (diff)
3rdparty: Updated bgfx, bimg, and bx to latest upstream. [Ryan Holtz]
Diffstat (limited to '3rdparty/bgfx/examples/common/ps/particle_system.cpp')
-rw-r--r--3rdparty/bgfx/examples/common/ps/particle_system.cpp133
1 files changed, 61 insertions, 72 deletions
diff --git a/3rdparty/bgfx/examples/common/ps/particle_system.cpp b/3rdparty/bgfx/examples/common/ps/particle_system.cpp
index 7ad93331a37..c7a57d5b0fd 100644
--- a/3rdparty/bgfx/examples/common/ps/particle_system.cpp
+++ b/3rdparty/bgfx/examples/common/ps/particle_system.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
+ * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
@@ -98,8 +98,8 @@ namespace ps
{
struct Particle
{
- float start[3];
- float end[2][3];
+ bx::Vec3 start;
+ bx::Vec3 end[2];
float blendStart;
float blendEnd;
float scaleStart;
@@ -237,6 +237,8 @@ namespace ps
const uint32_t numParticles = uint32_t(m_dt / timePerParticle);
m_dt -= numParticles * timePerParticle;
+ constexpr bx::Vec3 up = { 0.0f, 1.0f, 0.0f };
+
float time = 0.0f;
for (uint32_t ii = 0
; ii < numParticles && m_num < m_max
@@ -246,84 +248,81 @@ namespace ps
Particle& particle = m_particles[m_num];
m_num++;
- const float up[3] = { 0.0f, 1.0f, 0.0f };
-
- float pos[3];
+ bx::Vec3 pos;
switch (m_shape)
{
default:
case EmitterShape::Sphere:
- bx::randUnitSphere(pos, &m_rng);
+ pos = bx::randUnitSphere(&m_rng);
break;
case EmitterShape::Hemisphere:
- bx::randUnitHemisphere(pos, &m_rng, up);
+ pos = bx::randUnitHemisphere(&m_rng, up);
break;
case EmitterShape::Circle:
- bx::randUnitCircle(pos, &m_rng);
+ pos = bx::randUnitCircle(&m_rng);
break;
case EmitterShape::Disc:
{
- float tmp[3];
- bx::randUnitCircle(tmp, &m_rng);
- bx::vec3Mul(pos, tmp, bx::frnd(&m_rng) );
+ const bx::Vec3 tmp = bx::randUnitCircle(&m_rng);
+ pos = bx::mul(tmp, bx::frnd(&m_rng) );
}
break;
case EmitterShape::Rect:
- pos[0] = bx::frndh(&m_rng);
- pos[1] = 0.0f;
- pos[2] = bx::frndh(&m_rng);
+ pos =
+ {
+ bx::frndh(&m_rng),
+ 0.0f,
+ bx::frndh(&m_rng),
+ };
break;
}
- float dir[3];
+ bx::Vec3 dir;
switch (m_direction)
{
default:
case EmitterDirection::Up:
- bx::vec3Move(dir, up);
+ dir = up;
break;
case EmitterDirection::Outward:
- bx::vec3Norm(dir, pos);
+ dir = bx::normalize(pos);
break;
}
- float start[3];
- float end[3];
- const float startOffset = bx::flerp(m_uniforms.m_offsetStart[0], m_uniforms.m_offsetStart[1], bx::frnd(&m_rng) );
- bx::vec3Mul(start, pos, startOffset);
+ const float startOffset = bx::lerp(m_uniforms.m_offsetStart[0], m_uniforms.m_offsetStart[1], bx::frnd(&m_rng) );
+ const bx::Vec3 start = bx::mul(pos, startOffset);
- const float endOffset = bx::flerp(m_uniforms.m_offsetEnd[0], m_uniforms.m_offsetEnd[1], bx::frnd(&m_rng) );
- float tmp1[3];
- bx::vec3Mul(tmp1, dir, endOffset);
- bx::vec3Add(end, tmp1, start);
+ const float endOffset = bx::lerp(m_uniforms.m_offsetEnd[0], m_uniforms.m_offsetEnd[1], bx::frnd(&m_rng) );
+ const bx::Vec3 tmp1 = bx::mul(dir, endOffset);
+ const bx::Vec3 end = bx::add(tmp1, start);
particle.life = time;
- particle.lifeSpan = bx::flerp(m_uniforms.m_lifeSpan[0], m_uniforms.m_lifeSpan[1], bx::frnd(&m_rng) );
+ particle.lifeSpan = bx::lerp(m_uniforms.m_lifeSpan[0], m_uniforms.m_lifeSpan[1], bx::frnd(&m_rng) );
- float gravity[3] = { 0.0f, -9.81f * m_uniforms.m_gravityScale * bx::fsq(particle.lifeSpan), 0.0f };
+ const bx::Vec3 gravity = { 0.0f, -9.81f * m_uniforms.m_gravityScale * bx::square(particle.lifeSpan), 0.0f };
- bx::vec3MulMtx(particle.start, start, mtx);
- bx::vec3MulMtx(particle.end[0], end, mtx);
- bx::vec3Add(particle.end[1], particle.end[0], gravity);
+ particle.start = bx::mul(start, mtx);
+ particle.end[0] = bx::mul(end, mtx);
+ particle.end[1] = bx::add(particle.end[0], gravity);
bx::memCopy(particle.rgba, m_uniforms.m_rgba, BX_COUNTOF(m_uniforms.m_rgba)*sizeof(uint32_t) );
- particle.blendStart = bx::flerp(m_uniforms.m_blendStart[0], m_uniforms.m_blendStart[1], bx::frnd(&m_rng) );
- particle.blendEnd = bx::flerp(m_uniforms.m_blendEnd[0], m_uniforms.m_blendEnd[1], bx::frnd(&m_rng) );
+ particle.blendStart = bx::lerp(m_uniforms.m_blendStart[0], m_uniforms.m_blendStart[1], bx::frnd(&m_rng) );
+ particle.blendEnd = bx::lerp(m_uniforms.m_blendEnd[0], m_uniforms.m_blendEnd[1], bx::frnd(&m_rng) );
- particle.scaleStart = bx::flerp(m_uniforms.m_scaleStart[0], m_uniforms.m_scaleStart[1], bx::frnd(&m_rng) );
- particle.scaleEnd = bx::flerp(m_uniforms.m_scaleEnd[0], m_uniforms.m_scaleEnd[1], bx::frnd(&m_rng) );
+ particle.scaleStart = bx::lerp(m_uniforms.m_scaleStart[0], m_uniforms.m_scaleStart[1], bx::frnd(&m_rng) );
+ particle.scaleEnd = bx::lerp(m_uniforms.m_scaleEnd[0], m_uniforms.m_scaleEnd[1], bx::frnd(&m_rng) );
time += timePerParticle;
}
}
- uint32_t render(const float _uv[4], const float* _mtxView, const float* _eye, uint32_t _first, uint32_t _max, ParticleSort* _outSort, PosColorTexCoord0Vertex* _outVertices)
+ uint32_t render(const float _uv[4], const float* _mtxView, const bx::Vec3& _eye, uint32_t _first, uint32_t _max, ParticleSort* _outSort, PosColorTexCoord0Vertex* _outVertices)
{
bx::EaseFn easeRgba = bx::getEaseFunc(m_uniforms.m_easeRgba);
bx::EaseFn easePos = bx::getEaseFunc(m_uniforms.m_easePos);
@@ -332,8 +331,8 @@ namespace ps
Aabb aabb =
{
- { bx::kHuge, bx::kHuge, bx::kHuge },
- { -bx::kHuge, -bx::kHuge, -bx::kHuge },
+ { bx::kInfinity, bx::kInfinity, bx::kInfinity },
+ { -bx::kInfinity, -bx::kInfinity, -bx::kInfinity },
};
for (uint32_t jj = 0, num = m_num, current = _first
@@ -345,45 +344,38 @@ namespace ps
const float ttPos = easePos(particle.life);
const float ttScale = easeScale(particle.life);
- const float ttBlend = bx::fsaturate(easeBlend(particle.life) );
- const float ttRgba = bx::fsaturate(easeRgba(particle.life) );
-
- float p0[3];
- bx::vec3Lerp(p0, particle.start, particle.end[0], ttPos);
-
- float p1[3];
- bx::vec3Lerp(p1, particle.end[0], particle.end[1], ttPos);
+ const float ttBlend = bx::clamp(easeBlend(particle.life), 0.0f, 1.0f);
+ const float ttRgba = bx::clamp(easeRgba(particle.life), 0.0f, 1.0f);
- float pos[3];
- bx::vec3Lerp(pos, p0, p1, ttPos);
+ const bx::Vec3 p0 = bx::lerp(particle.start, particle.end[0], ttPos);
+ const bx::Vec3 p1 = bx::lerp(particle.end[0], particle.end[1], ttPos);
+ const bx::Vec3 pos = bx::lerp(p0, p1, ttPos);
ParticleSort& sort = _outSort[current];
- float tmp[3];
- bx::vec3Sub(tmp, _eye, pos);
- sort.dist = bx::fsqrt(bx::vec3Dot(tmp, tmp) );
+ const bx::Vec3 tmp0 = bx::sub(_eye, pos);
+ sort.dist = bx::length(tmp0);
sort.idx = current;
uint32_t idx = uint32_t(ttRgba*4);
- float ttmod = bx::fmod(ttRgba, 0.25f)/0.25f;
+ float ttmod = bx::mod(ttRgba, 0.25f)/0.25f;
uint32_t rgbaStart = particle.rgba[idx];
uint32_t rgbaEnd = particle.rgba[idx+1];
- float rr = bx::flerp( ( (uint8_t*)&rgbaStart)[0], ( (uint8_t*)&rgbaEnd)[0], ttmod)/255.0f;
- float gg = bx::flerp( ( (uint8_t*)&rgbaStart)[1], ( (uint8_t*)&rgbaEnd)[1], ttmod)/255.0f;
- float bb = bx::flerp( ( (uint8_t*)&rgbaStart)[2], ( (uint8_t*)&rgbaEnd)[2], ttmod)/255.0f;
- float aa = bx::flerp( ( (uint8_t*)&rgbaStart)[3], ( (uint8_t*)&rgbaEnd)[3], ttmod)/255.0f;
+ float rr = bx::lerp( ( (uint8_t*)&rgbaStart)[0], ( (uint8_t*)&rgbaEnd)[0], ttmod)/255.0f;
+ float gg = bx::lerp( ( (uint8_t*)&rgbaStart)[1], ( (uint8_t*)&rgbaEnd)[1], ttmod)/255.0f;
+ float bb = bx::lerp( ( (uint8_t*)&rgbaStart)[2], ( (uint8_t*)&rgbaEnd)[2], ttmod)/255.0f;
+ float aa = bx::lerp( ( (uint8_t*)&rgbaStart)[3], ( (uint8_t*)&rgbaEnd)[3], ttmod)/255.0f;
- float blend = bx::flerp(particle.blendStart, particle.blendEnd, ttBlend);
- float scale = bx::flerp(particle.scaleStart, particle.scaleEnd, ttScale);
+ float blend = bx::lerp(particle.blendStart, particle.blendEnd, ttBlend);
+ float scale = bx::lerp(particle.scaleStart, particle.scaleEnd, ttScale);
uint32_t abgr = toAbgr(rr, gg, bb, aa);
- float udir[3] = { _mtxView[0]*scale, _mtxView[4]*scale, _mtxView[8]*scale };
- float vdir[3] = { _mtxView[1]*scale, _mtxView[5]*scale, _mtxView[9]*scale };
+ const bx::Vec3 udir = { _mtxView[0]*scale, _mtxView[4]*scale, _mtxView[8]*scale };
+ const bx::Vec3 vdir = { _mtxView[1]*scale, _mtxView[5]*scale, _mtxView[9]*scale };
PosColorTexCoord0Vertex* vertex = &_outVertices[current*4];
- bx::vec3Sub(tmp, pos, udir);
- bx::vec3Sub(&vertex->m_x, tmp, vdir);
+ bx::store(&vertex->m_x, bx::sub(bx::sub(pos, udir), vdir) );
aabbExpand(aabb, &vertex->m_x);
vertex->m_abgr = abgr;
vertex->m_u = _uv[0];
@@ -391,8 +383,7 @@ namespace ps
vertex->m_blend = blend;
++vertex;
- bx::vec3Add(tmp, pos, udir);
- bx::vec3Sub(&vertex->m_x, tmp, vdir);
+ bx::store(&vertex->m_x, bx::sub(bx::add(pos, udir), vdir) );
aabbExpand(aabb, &vertex->m_x);
vertex->m_abgr = abgr;
vertex->m_u = _uv[2];
@@ -400,8 +391,7 @@ namespace ps
vertex->m_blend = blend;
++vertex;
- bx::vec3Add(tmp, pos, udir);
- bx::vec3Add(&vertex->m_x, tmp, vdir);
+ bx::store(&vertex->m_x, bx::add(bx::add(pos, udir), vdir) );
aabbExpand(aabb, &vertex->m_x);
vertex->m_abgr = abgr;
vertex->m_u = _uv[2];
@@ -409,8 +399,7 @@ namespace ps
vertex->m_blend = blend;
++vertex;
- bx::vec3Sub(tmp, pos, udir);
- bx::vec3Add(&vertex->m_x, tmp, vdir);
+ bx::store(&vertex->m_x, bx::add(bx::sub(pos, udir), vdir) );
aabbExpand(aabb, &vertex->m_x);
vertex->m_abgr = abgr;
vertex->m_u = _uv[0];
@@ -534,7 +523,7 @@ namespace ps
m_num = numParticles;
}
- void render(uint8_t _view, const float* _mtxView, const float* _eye)
+ void render(uint8_t _view, const float* _mtxView, const bx::Vec3& _eye)
{
if (0 != m_num)
{
@@ -604,8 +593,8 @@ namespace ps
BX_FREE(m_allocator, particleSort);
bgfx::setState(0
- | BGFX_STATE_RGB_WRITE
- | BGFX_STATE_ALPHA_WRITE
+ | BGFX_STATE_WRITE_RGB
+ | BGFX_STATE_WRITE_A
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CW
| BGFX_STATE_BLEND_NORMAL
@@ -751,7 +740,7 @@ void psUpdate(float _dt)
s_ctx.update(_dt);
}
-void psRender(uint8_t _view, const float* _mtxView, const float* _eye)
+void psRender(uint8_t _view, const float* _mtxView, const bx::Vec3& _eye)
{
s_ctx.render(_view, _mtxView, _eye);
}