@ -28,6 +28,38 @@
# include <reactphysics3d/mathematics/Vector3.h>
# include <cassert>
# if __STDCPP_BFLOAT16_T__ || __STDCPP_FLOAT16_T__
# include <stdfloat>
# endif
namespace {
uint16_t quantize ( float v ) {
union { float f ; uint32_t ui ; } u = { v } ;
uint32_t ui = u . ui ;
int s = ( ui > > 16 ) & 0x8000 ;
int em = ui & 0x7fffffff ;
int h = ( em - ( 112 < < 23 ) + ( 1 < < 12 ) ) > > 13 ;
h = ( em < ( 113 < < 23 ) ) ? 0 : h ;
h = ( em > = ( 143 < < 23 ) ) ? 0x7c00 : h ;
h = ( em > ( 255 < < 23 ) ) ? 0x7e00 : h ;
return ( uint16_t ) ( s | h ) ;
}
float dequantize ( uint16_t h ) {
uint32_t s = unsigned ( h & 0x8000 ) < < 16 ;
int em = h & 0x7fff ;
int r = ( em + ( 112 < < 10 ) ) < < 13 ;
r = ( em < ( 1 < < 10 ) ) ? 0 : r ;
r + = ( em > = ( 31 < < 10 ) ) ? ( 112 < < 23 ) : 0 ;
union { float f ; uint32_t ui ; } u ;
u . ui = s | r ;
return u . f ;
}
}
using namespace reactphysics3d ;
// Constructor without vertices normals
@ -253,6 +285,28 @@ void TriangleVertexArray::getTriangleVertices(uint32 triangleIndex, Vector3* out
outTriangleVertices [ k ] [ 1 ] = decimal ( vertices [ 1 ] ) ;
outTriangleVertices [ k ] [ 2 ] = decimal ( vertices [ 2 ] ) ;
}
else if ( mVertexDataType = = TriangleVertexArray : : VertexDataType : : VERTEX_SHORT_TYPE ) {
const uint16_t * vertices = static_cast < const uint16_t * > ( vertexPointer ) ;
outTriangleVertices [ k ] [ 0 ] = decimal ( : : dequantize ( vertices [ 0 ] ) ) ;
outTriangleVertices [ k ] [ 1 ] = decimal ( : : dequantize ( vertices [ 1 ] ) ) ;
outTriangleVertices [ k ] [ 2 ] = decimal ( : : dequantize ( vertices [ 2 ] ) ) ;
}
# if __STDCPP_FLOAT16_T__
else if ( mVertexDataType = = TriangleVertexArray : : VertexDataType : : VERTEX_FLOAT16_TYPE ) {
const std : : float16_t * vertices = static_cast < const std : : float16_t * > ( vertexPointer ) ;
outTriangleVertices [ k ] [ 0 ] = decimal ( vertices [ 0 ] ) ;
outTriangleVertices [ k ] [ 1 ] = decimal ( vertices [ 1 ] ) ;
outTriangleVertices [ k ] [ 2 ] = decimal ( vertices [ 2 ] ) ;
}
# endif
# if __STDCPP_BFLOAT16_T__
else if ( mVertexDataType = = TriangleVertexArray : : VertexDataType : : VERTEX_BFLOAT16_TYPE ) {
const std : : bfloat16_t * vertices = static_cast < const std : : bfloat16_t * > ( vertexPointer ) ;
outTriangleVertices [ k ] [ 0 ] = decimal ( vertices [ 0 ] ) ;
outTriangleVertices [ k ] [ 1 ] = decimal ( vertices [ 1 ] ) ;
outTriangleVertices [ k ] [ 2 ] = decimal ( vertices [ 2 ] ) ;
}
# endif
else {
assert ( false ) ;
}
@ -291,6 +345,28 @@ void TriangleVertexArray::getTriangleVerticesNormals(uint32 triangleIndex, Vecto
outTriangleVerticesNormals [ k ] [ 1 ] = decimal ( normal [ 1 ] ) ;
outTriangleVerticesNormals [ k ] [ 2 ] = decimal ( normal [ 2 ] ) ;
}
else if ( mVertexNormaldDataType = = TriangleVertexArray : : NormalDataType : : NORMAL_SHORT_TYPE ) {
const uint16_t * normal = static_cast < const uint16_t * > ( vertexNormalPointer ) ;
outTriangleVerticesNormals [ k ] [ 0 ] = decimal ( dequantize ( normal [ 0 ] ) ) ;
outTriangleVerticesNormals [ k ] [ 1 ] = decimal ( dequantize ( normal [ 1 ] ) ) ;
outTriangleVerticesNormals [ k ] [ 2 ] = decimal ( dequantize ( normal [ 2 ] ) ) ;
}
# if __STDCPP_FLOAT16_T__
else if ( mVertexNormaldDataType = = TriangleVertexArray : : NormalDataType : : NORMAL_FLOAT16_TYPE ) {
const std : : float16_t * normal = static_cast < const std : : float16_t * > ( vertexNormalPointer ) ;
outTriangleVerticesNormals [ k ] [ 0 ] = decimal ( normal [ 0 ] ) ;
outTriangleVerticesNormals [ k ] [ 1 ] = decimal ( normal [ 1 ] ) ;
outTriangleVerticesNormals [ k ] [ 2 ] = decimal ( normal [ 2 ] ) ;
}
# endif
# if __STDCPP_BFLOAT16_T__
else if ( mVertexNormaldDataType = = TriangleVertexArray : : NormalDataType : : NORMAL_BFLOAT16_TYPE ) {
const std : : bfloat16_t * normal = static_cast < const std : : bfloat16_t * > ( vertexNormalPointer ) ;
outTriangleVerticesNormals [ k ] [ 0 ] = decimal ( normal [ 0 ] ) ;
outTriangleVerticesNormals [ k ] [ 1 ] = decimal ( normal [ 1 ] ) ;
outTriangleVerticesNormals [ k ] [ 2 ] = decimal ( normal [ 2 ] ) ;
}
# endif
else {
assert ( false ) ;
}
@ -322,6 +398,28 @@ void TriangleVertexArray::getVertex(uint32 vertexIndex, Vector3* outVertex) {
( * outVertex ) [ 1 ] = decimal ( vertices [ 1 ] ) ;
( * outVertex ) [ 2 ] = decimal ( vertices [ 2 ] ) ;
}
else if ( mVertexDataType = = TriangleVertexArray : : VertexDataType : : VERTEX_SHORT_TYPE ) {
const uint16_t * vertices = static_cast < const uint16_t * > ( vertexPointer ) ;
( * outVertex ) [ 0 ] = decimal ( dequantize ( vertices [ 0 ] ) ) ;
( * outVertex ) [ 1 ] = decimal ( dequantize ( vertices [ 1 ] ) ) ;
( * outVertex ) [ 2 ] = decimal ( dequantize ( vertices [ 2 ] ) ) ;
}
# if __STDCPP_FLOAT16_T__
else if ( mVertexDataType = = TriangleVertexArray : : VertexDataType : : VERTEX_FLOAT16_TYPE ) {
const std : : float16_t * vertices = static_cast < const std : : float16_t * > ( vertexPointer ) ;
( * outVertex ) [ 0 ] = decimal ( vertices [ 0 ] ) ;
( * outVertex ) [ 1 ] = decimal ( vertices [ 1 ] ) ;
( * outVertex ) [ 2 ] = decimal ( vertices [ 2 ] ) ;
}
# endif
# if __STDCPP_BFLOAT16_T__
else if ( mVertexDataType = = TriangleVertexArray : : VertexDataType : : VERTEX_BFLOAT16_TYPE ) {
const std : : bfloat16_t * vertices = static_cast < const std : : bfloat16_t * > ( vertexPointer ) ;
( * outVertex ) [ 0 ] = decimal ( vertices [ 0 ] ) ;
( * outVertex ) [ 1 ] = decimal ( vertices [ 1 ] ) ;
( * outVertex ) [ 2 ] = decimal ( vertices [ 2 ] ) ;
}
# endif
else {
assert ( false ) ;
}
@ -352,6 +450,28 @@ void TriangleVertexArray::getNormal(uint32 vertexIndex, Vector3* outNormal) {
( * outNormal ) [ 1 ] = decimal ( normal [ 1 ] ) ;
( * outNormal ) [ 2 ] = decimal ( normal [ 2 ] ) ;
}
else if ( mVertexNormaldDataType = = TriangleVertexArray : : NormalDataType : : NORMAL_SHORT_TYPE ) {
const uint16_t * normal = static_cast < const uint16_t * > ( vertexNormalPointer ) ;
( * outNormal ) [ 0 ] = decimal ( dequantize ( normal [ 0 ] ) ) ;
( * outNormal ) [ 1 ] = decimal ( dequantize ( normal [ 1 ] ) ) ;
( * outNormal ) [ 2 ] = decimal ( dequantize ( normal [ 2 ] ) ) ;
}
# if __STDCPP_FLOAT16_T__
else if ( mVertexNormaldDataType = = TriangleVertexArray : : NormalDataType : : NORMAL_FLOAT16_TYPE ) {
const std : : float16_t * normal = static_cast < const std : : float16_t * > ( vertexNormalPointer ) ;
( * outNormal ) [ 0 ] = decimal ( normal [ 0 ] ) ;
( * outNormal ) [ 1 ] = decimal ( normal [ 1 ] ) ;
( * outNormal ) [ 2 ] = decimal ( normal [ 2 ] ) ;
}
# endif
# if __STDCPP_BFLOAT16_T__
else if ( mVertexNormaldDataType = = TriangleVertexArray : : NormalDataType : : NORMAL_BFLOAT16_TYPE ) {
const std : : bfloat16_t * normal = static_cast < const std : : bfloat16_t * > ( vertexNormalPointer ) ;
( * outNormal ) [ 0 ] = decimal ( normal [ 0 ] ) ;
( * outNormal ) [ 1 ] = decimal ( normal [ 1 ] ) ;
( * outNormal ) [ 2 ] = decimal ( normal [ 2 ] ) ;
}
# endif
else {
assert ( false ) ;
}