This commit is contained in:
Branimir Karadžić 2018-12-10 19:39:00 -08:00
parent d95a7bd4b8
commit 0740b29de7
5 changed files with 39 additions and 54 deletions

View File

@ -224,11 +224,9 @@ public:
float mtxInv[16]; float mtxInv[16];
bx::mtxInverse(mtxInv, mtx); bx::mtxInverse(mtxInv, mtx);
float lightDirModel[4] = { -0.4f, -0.5f, -1.0f, 0.0f };
float lightDirModelN[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
bx::vec3Norm(lightDirModelN, lightDirModel);
float lightDirTime[4]; float lightDirTime[4];
bx::vec4MulMtx(lightDirTime, lightDirModelN, mtxInv); const bx::Vec3 lightDirModelN = bx::normalize(bx::Vec3{-0.4f, -0.5f, -1.0f});
bx::store(lightDirTime, bx::mul(lightDirModelN, mtxInv) );
lightDirTime[3] = time; lightDirTime[3] = time;
bgfx::setUniform(u_lightDirTime, lightDirTime); bgfx::setUniform(u_lightDirTime, lightDirTime);

View File

@ -155,26 +155,21 @@ void mtxBillboard(float* __restrict _result
} }
void planeNormal(float* __restrict _result void planeNormal(float* __restrict _result
, const float* __restrict _v0 , const float* __restrict _v0
, const float* __restrict _v1 , const float* __restrict _v1
, const float* __restrict _v2 , const float* __restrict _v2
) )
{ {
float vec0[3], vec1[3]; const bx::Vec3 v0 = bx::load(_v0);
float cross[3]; const bx::Vec3 v1 = bx::load(_v1);
const bx::Vec3 v2 = bx::load(_v2);
const bx::Vec3 vec0 = bx::sub(v1, v0);
const bx::Vec3 vec1 = bx::sub(v2, v1);
const bx::Vec3 cross = bx::cross(vec0, vec1);
vec0[0] = _v1[0] - _v0[0]; bx::store(_result, bx::normalize(cross) );
vec0[1] = _v1[1] - _v0[1];
vec0[2] = _v1[2] - _v0[2];
vec1[0] = _v2[0] - _v1[0]; _result[3] = -bx::dot(bx::load(_result), bx::load(_v0) );
vec1[1] = _v2[1] - _v1[1];
vec1[2] = _v2[2] - _v1[2];
bx::vec3Cross(cross, vec0, vec1);
bx::vec3Norm(_result, cross);
_result[3] = -bx::vec3Dot(_result, _v0);
} }
struct Uniforms struct Uniforms

View File

@ -358,7 +358,7 @@ public:
bx::vec4MulMtx(ray_world, ray_eye, invViewMtx); bx::vec4MulMtx(ray_world, ray_eye, invViewMtx);
float ray_dir[3]; float ray_dir[3];
bx::vec3Norm(ray_dir, ray_world); bx::store(ray_dir, bx::normalize(bx::load(ray_world) ) );
ray_dir[0] *= -1.0; ray_dir[0] *= -1.0;
ray_dir[1] *= -1.0; ray_dir[1] *= -1.0;
ray_dir[2] *= -1.0; ray_dir[2] *= -1.0;

View File

@ -285,13 +285,13 @@ void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl
uint32_t i1 = indices[1]; uint32_t i1 = indices[1];
uint32_t i2 = indices[2]; uint32_t i2 = indices[2];
bgfx::vertexUnpack(&v0.m_x, bgfx::Attrib::Position, _decl, _vertices, i0); bgfx::vertexUnpack(&v0.m_x, bgfx::Attrib::Position, _decl, _vertices, i0);
bgfx::vertexUnpack(&v0.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i0); bgfx::vertexUnpack(&v0.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i0);
bgfx::vertexUnpack(&v1.m_x, bgfx::Attrib::Position, _decl, _vertices, i1); bgfx::vertexUnpack(&v1.m_x, bgfx::Attrib::Position, _decl, _vertices, i1);
bgfx::vertexUnpack(&v1.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i1); bgfx::vertexUnpack(&v1.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i1);
bgfx::vertexUnpack(&v2.m_x, bgfx::Attrib::Position, _decl, _vertices, i2); bgfx::vertexUnpack(&v2.m_x, bgfx::Attrib::Position, _decl, _vertices, i2);
bgfx::vertexUnpack(&v2.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i2); bgfx::vertexUnpack(&v2.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i2);
const float bax = v1.m_x - v0.m_x; const float bax = v1.m_x - v0.m_x;
@ -333,25 +333,21 @@ void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl
for (uint32_t ii = 0; ii < _numVertices; ++ii) for (uint32_t ii = 0; ii < _numVertices; ++ii)
{ {
const float* tanu = &tangents[ii*6]; const bx::Vec3 tanu = bx::load(&tangents[ii*6]);
const float* tanv = &tangents[ii*6 + 3]; const bx::Vec3 tanv = bx::load(&tangents[ii*6 + 3]);
float normal[4]; float nxyzw[4];
bgfx::vertexUnpack(normal, bgfx::Attrib::Normal, _decl, _vertices, ii); bgfx::vertexUnpack(nxyzw, bgfx::Attrib::Normal, _decl, _vertices, ii);
float ndt = bx::vec3Dot(normal, tanu);
float nxt[3]; const bx::Vec3 normal = bx::load(nxyzw);
bx::vec3Cross(nxt, normal, tanu); const float ndt = bx::dot(normal, tanu);
const bx::Vec3 nxt = bx::cross(normal, tanu);
float tmp[3]; const bx::Vec3 tmp = bx::sub(tanu, bx::mul(normal, ndt) );
tmp[0] = tanu[0] - normal[0] * ndt;
tmp[1] = tanu[1] - normal[1] * ndt;
tmp[2] = tanu[2] - normal[2] * ndt;
float tangent[4]; float tangent[4];
bx::vec3Norm(tangent, tmp); bx::store(tangent, bx::normalize(tmp) );
tangent[3] = bx::dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
tangent[3] = bx::vec3Dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
bgfx::vertexPack(tangent, true, bgfx::Attrib::Tangent, _decl, _vertices, ii); bgfx::vertexPack(tangent, true, bgfx::Attrib::Tangent, _decl, _vertices, ii);
} }

View File

@ -236,25 +236,21 @@ void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl
for (uint32_t ii = 0; ii < _numVertices; ++ii) for (uint32_t ii = 0; ii < _numVertices; ++ii)
{ {
const float* tanu = &tangents[ii*6]; const bx::Vec3 tanu = bx::load(&tangents[ii*6]);
const float* tanv = &tangents[ii*6 + 3]; const bx::Vec3 tanv = bx::load(&tangents[ii*6 + 3]);
float normal[4]; float nxyzw[4];
bgfx::vertexUnpack(normal, bgfx::Attrib::Normal, _decl, _vertices, ii); bgfx::vertexUnpack(nxyzw, bgfx::Attrib::Normal, _decl, _vertices, ii);
float ndt = bx::vec3Dot(normal, tanu);
float nxt[3]; const bx::Vec3 normal = bx::load(nxyzw);
bx::vec3Cross(nxt, normal, tanu); const float ndt = bx::dot(normal, tanu);
const bx::Vec3 nxt = bx::cross(normal, tanu);
float tmp[3]; const bx::Vec3 tmp = bx::sub(tanu, bx::mul(normal, ndt) );
tmp[0] = tanu[0] - normal[0] * ndt;
tmp[1] = tanu[1] - normal[1] * ndt;
tmp[2] = tanu[2] - normal[2] * ndt;
float tangent[4]; float tangent[4];
bx::vec3Norm(tangent, tmp); bx::store(tangent, bx::normalize(tmp) );
tangent[3] = bx::dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
tangent[3] = bx::vec3Dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
bgfx::vertexPack(tangent, true, bgfx::Attrib::Tangent, _decl, _vertices, ii); bgfx::vertexPack(tangent, true, bgfx::Attrib::Tangent, _decl, _vertices, ii);
} }
@ -1002,7 +998,7 @@ int main(int _argc, const char* _argv[])
if (hasNormal) if (hasNormal)
{ {
float normal[4]; float normal[4];
bx::vec3Norm(normal, (float*)&normals[index.m_normal]); bx::store(normal, bx::normalize(bx::load(&normals[index.m_normal]) ) );
bgfx::vertexPack(normal, true, bgfx::Attrib::Normal, decl, vertices); bgfx::vertexPack(normal, true, bgfx::Attrib::Normal, decl, vertices);
} }