|
|
@@ -71,7 +71,7 @@ uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
|
|
|
return packUint32(xx, yy, zz, ww);
|
|
|
}
|
|
|
|
|
|
-void unpackF4u(float _result[3], uint32_t _packed)
|
|
|
+void unpackF4u(float _result[4], uint32_t _packed)
|
|
|
{
|
|
|
uint8_t unpacked[4];
|
|
|
unpackUint32(unpacked, _packed);
|
|
|
@@ -177,13 +177,12 @@ static const bgfx::Memory* loadTexture(const char* _name)
|
|
|
return load(filePath);
|
|
|
}
|
|
|
|
|
|
-template<typename Ty>
|
|
|
-void calcTangents(const uint16_t* _indices, uint32_t _numIndices, Ty* _vertices, uint16_t _numVertices)
|
|
|
-{
|
|
|
- float* tangents = new float[6*_numVertices];
|
|
|
- memset(tangents, 0, 6*_numVertices*sizeof(float) );
|
|
|
-
|
|
|
- float* tan = tangents;
|
|
|
+template<typename Ty>
|
|
|
+void calcTangents(const uint16_t* _indices, uint32_t _numIndices, Ty* _vertices, uint16_t _numVertices)
|
|
|
+{
|
|
|
+ float* tangents = new float[6*_numVertices];
|
|
|
+ memset(tangents, 0, 6*_numVertices*sizeof(float) );
|
|
|
+
|
|
|
for (uint32_t ii = 0, num = _numIndices/3; ii < num; ++ii)
|
|
|
{
|
|
|
const uint16_t* indices = &_indices[ii*3];
|
|
|
@@ -191,67 +190,67 @@ void calcTangents(const uint16_t* _indices, uint32_t _numIndices, Ty* _vertices,
|
|
|
const Ty& v1 = _vertices[indices[1] ];
|
|
|
const Ty& v2 = _vertices[indices[2] ];
|
|
|
|
|
|
- const float bax = v1.m_x - v0.m_x;
|
|
|
- const float bay = v1.m_y - v0.m_y;
|
|
|
- const float baz = v1.m_z - v0.m_z;
|
|
|
- const float bau = v1.m_u - v0.m_u;
|
|
|
- const float bav = v1.m_v - v0.m_v;
|
|
|
-
|
|
|
- const float cax = v2.m_x - v0.m_x;
|
|
|
- const float cay = v2.m_y - v0.m_y;
|
|
|
- const float caz = v2.m_z - v0.m_z;
|
|
|
- const float cau = v2.m_u - v0.m_u;
|
|
|
- const float cav = v2.m_v - v0.m_v;
|
|
|
-
|
|
|
- const float det = (bau * cav - bav * cau);
|
|
|
- const float invDet = 1.0f / det;
|
|
|
-
|
|
|
- const float tx = (bax * cav - cax * bav) * invDet;
|
|
|
- const float ty = (bay * cav - cay * bav) * invDet;
|
|
|
- const float tz = (baz * cav - caz * bav) * invDet;
|
|
|
-
|
|
|
- const float bx = (cax * bau - bax * cau) * invDet;
|
|
|
- const float by = (cay * bau - bay * cau) * invDet;
|
|
|
- const float bz = (caz * bau - baz * cau) * invDet;
|
|
|
-
|
|
|
- for (uint32_t jj = 0; jj < 3; ++jj)
|
|
|
- {
|
|
|
- float* tanu = &tangents[indices[jj]*6];
|
|
|
- float* tanv = &tanu[3];
|
|
|
- tanu[0] += tx;
|
|
|
- tanu[1] += ty;
|
|
|
- tanu[2] += tz;
|
|
|
-
|
|
|
- tanv[0] += bx;
|
|
|
- tanv[1] += by;
|
|
|
- tanv[2] += bz;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for (uint32_t ii = 0; ii < _numVertices; ++ii)
|
|
|
- {
|
|
|
- Ty& v0 = _vertices[ii];
|
|
|
- const float* tanu = &tangents[ii*6];
|
|
|
- const float* tanv = &tangents[ii*6 + 3];
|
|
|
-
|
|
|
- float normal[3];
|
|
|
- unpackF4u(normal, v0.m_normal);
|
|
|
- float ndt = vec3Dot(normal, tanu);
|
|
|
-
|
|
|
- float nxt[3];
|
|
|
- vec3Cross(nxt, normal, tanu);
|
|
|
-
|
|
|
- float tmp[3];
|
|
|
- tmp[0] = tanu[0] - normal[0] * ndt;
|
|
|
- tmp[1] = tanu[1] - normal[1] * ndt;
|
|
|
- tmp[2] = tanu[2] - normal[2] * ndt;
|
|
|
-
|
|
|
- float tangent[3];
|
|
|
- vec3Norm(tangent, tmp);
|
|
|
-
|
|
|
- float tw = vec3Dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
|
|
|
- v0.m_tangent = packF4u(tangent[0], tangent[1], tangent[2], tw);
|
|
|
- }
|
|
|
+ const float bax = v1.m_x - v0.m_x;
|
|
|
+ const float bay = v1.m_y - v0.m_y;
|
|
|
+ const float baz = v1.m_z - v0.m_z;
|
|
|
+ const float bau = v1.m_u - v0.m_u;
|
|
|
+ const float bav = v1.m_v - v0.m_v;
|
|
|
+
|
|
|
+ const float cax = v2.m_x - v0.m_x;
|
|
|
+ const float cay = v2.m_y - v0.m_y;
|
|
|
+ const float caz = v2.m_z - v0.m_z;
|
|
|
+ const float cau = v2.m_u - v0.m_u;
|
|
|
+ const float cav = v2.m_v - v0.m_v;
|
|
|
+
|
|
|
+ const float det = (bau * cav - bav * cau);
|
|
|
+ const float invDet = 1.0f / det;
|
|
|
+
|
|
|
+ const float tx = (bax * cav - cax * bav) * invDet;
|
|
|
+ const float ty = (bay * cav - cay * bav) * invDet;
|
|
|
+ const float tz = (baz * cav - caz * bav) * invDet;
|
|
|
+
|
|
|
+ const float bx = (cax * bau - bax * cau) * invDet;
|
|
|
+ const float by = (cay * bau - bay * cau) * invDet;
|
|
|
+ const float bz = (caz * bau - baz * cau) * invDet;
|
|
|
+
|
|
|
+ for (uint32_t jj = 0; jj < 3; ++jj)
|
|
|
+ {
|
|
|
+ float* tanu = &tangents[indices[jj]*6];
|
|
|
+ float* tanv = &tanu[3];
|
|
|
+ tanu[0] += tx;
|
|
|
+ tanu[1] += ty;
|
|
|
+ tanu[2] += tz;
|
|
|
+
|
|
|
+ tanv[0] += bx;
|
|
|
+ tanv[1] += by;
|
|
|
+ tanv[2] += bz;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (uint32_t ii = 0; ii < _numVertices; ++ii)
|
|
|
+ {
|
|
|
+ Ty& v0 = _vertices[ii];
|
|
|
+ const float* tanu = &tangents[ii*6];
|
|
|
+ const float* tanv = &tangents[ii*6 + 3];
|
|
|
+
|
|
|
+ float normal[4];
|
|
|
+ unpackF4u(normal, v0.m_normal);
|
|
|
+ float ndt = vec3Dot(normal, tanu);
|
|
|
+
|
|
|
+ float nxt[3];
|
|
|
+ vec3Cross(nxt, normal, tanu);
|
|
|
+
|
|
|
+ float tmp[3];
|
|
|
+ tmp[0] = tanu[0] - normal[0] * ndt;
|
|
|
+ tmp[1] = tanu[1] - normal[1] * ndt;
|
|
|
+ tmp[2] = tanu[2] - normal[2] * ndt;
|
|
|
+
|
|
|
+ float tangent[3];
|
|
|
+ vec3Norm(tangent, tmp);
|
|
|
+
|
|
|
+ float tw = vec3Dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
|
|
|
+ v0.m_tangent = packF4u(tangent[0], tangent[1], tangent[2], tw);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
int _main_(int _argc, char** _argv)
|
|
|
@@ -317,8 +316,8 @@ int _main_(int _argc, char** _argv)
|
|
|
bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
|
|
|
|
|
|
// Create texture sampler uniforms.
|
|
|
- bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
|
|
|
- bgfx::UniformHandle u_texNormal = bgfx::createUniform("u_texNormal", bgfx::UniformType::Uniform1iv);
|
|
|
+ bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
|
|
|
+ bgfx::UniformHandle u_texNormal = bgfx::createUniform("u_texNormal", bgfx::UniformType::Uniform1iv);
|
|
|
|
|
|
uint16_t numLights = 4;
|
|
|
bgfx::UniformHandle u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Uniform4fv, numLights);
|
|
|
@@ -392,10 +391,10 @@ int _main_(int _argc, char** _argv)
|
|
|
|
|
|
float lightRgbInnerR[4][4] =
|
|
|
{
|
|
|
- 1.0f, 0.7f, 0.2f, 0.8f,
|
|
|
- 0.7f, 0.2f, 1.0f, 0.8f,
|
|
|
- 0.2f, 1.0f, 0.7f, 0.8f,
|
|
|
- 1.0f, 0.4f, 0.2f, 0.8f,
|
|
|
+ { 1.0f, 0.7f, 0.2f, 0.8f },
|
|
|
+ { 0.7f, 0.2f, 1.0f, 0.8f },
|
|
|
+ { 0.2f, 1.0f, 0.7f, 0.8f },
|
|
|
+ { 1.0f, 0.4f, 0.2f, 0.8f },
|
|
|
};
|
|
|
|
|
|
bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, numLights);
|