Просмотр исходного кода

Added function to calculate rotated tangent frame.

Branimir Karadžić 9 лет назад
Родитель
Сommit
466c03d404
1 измененных файлов с 38 добавлено и 0 удалено
  1. 38 0
      include/bx/fpumath.h

+ 38 - 0
include/bx/fpumath.h

@@ -341,6 +341,7 @@ namespace bx
 		_result[2] = 1.0f / _a[2];
 		_result[2] = 1.0f / _a[2];
 	}
 	}
 
 
+	/// Calculate tangent frame from normal.
 	inline void vec3TangentFrame(const float* __restrict _n, float* __restrict _t, float* __restrict _b)
 	inline void vec3TangentFrame(const float* __restrict _n, float* __restrict _t, float* __restrict _b)
 	{
 	{
 		const float nx = _n[0];
 		const float nx = _n[0];
@@ -365,6 +366,24 @@ namespace bx
 		bx::vec3Cross(_b, _n, _t);
 		bx::vec3Cross(_b, _n, _t);
 	}
 	}
 
 
+	/// Calculate tangent frame from normal and angle.
+	inline void vec3TangentFrame(const float* __restrict _n, float _angle, float* __restrict _t, float* __restrict _b)
+	{
+		const float nx = _n[0];
+		const float ny = _n[1];
+		const float nz = _n[2];
+
+		const float sa   = fsin(_angle);
+		const float ca   = fcos(_angle);
+		const float omca = 1.0f - ca;
+
+		_t[0] = omca * nx * nx +      ca;
+		_t[1] = omca * nx * ny - nz * sa;
+		_t[2] = omca * nx * nz + ny * sa;
+
+		bx::vec3Cross(_b, _n, _t);
+	}
+
 	inline void quatIdentity(float* _result)
 	inline void quatIdentity(float* _result)
 	{
 	{
 		_result[0] = 0.0f;
 		_result[0] = 0.0f;
@@ -574,6 +593,25 @@ namespace bx
 		_result[15] = 1.0f;
 		_result[15] = 1.0f;
 	}
 	}
 
 
+	inline void mtxFromNormal(float* __restrict _result, const float* __restrict _normal, float _angle, float _scale, const float* __restrict _pos)
+	{
+		float tangent[3];
+		float bitangent[3];
+		vec3TangentFrame(_normal, _angle, tangent, bitangent);
+
+		vec3Mul(&_result[ 0], bitangent, _scale);
+		vec3Mul(&_result[ 4], _normal,   _scale);
+		vec3Mul(&_result[ 8], tangent,   _scale);
+
+		_result[ 3] = 0.0f;
+		_result[ 7] = 0.0f;
+		_result[11] = 0.0f;
+		_result[12] = _pos[0];
+		_result[13] = _pos[1];
+		_result[14] = _pos[2];
+		_result[15] = 1.0f;
+	}
+
 	inline void mtxQuat(float* __restrict _result, const float* __restrict _quat)
 	inline void mtxQuat(float* __restrict _result, const float* __restrict _quat)
 	{
 	{
 		const float x = _quat[0];
 		const float x = _quat[0];