Sfoglia il codice sorgente

Message.cpp: vec3_to_bits method implemented

mikymod 13 anni fa
parent
commit
b84e06285f
3 ha cambiato i file con 54 aggiunte e 17 eliminazioni
  1. 6 0
      src/core/math/MathUtils.h
  2. 46 13
      src/network/Message.cpp
  3. 2 4
      src/network/Message.h

+ 6 - 0
src/core/math/MathUtils.h

@@ -32,6 +32,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #define BIT(i) (1 << i)
 
+#define FLOATSIGNBITSET(f)		((*(const unsigned long *)&(f)) >> 31)
+// #define FLOATSIGNBITNOTSET(f)	((~(*(const unsigned long *)&(f))) >> 31)
+// #define FLOATNOTZERO(f)			((*(const unsigned long *)&(f)) & ~(1<<31) )
+// #define INTSIGNBITSET(i)		(((const unsigned long)(i)) >> 31)
+// #define INTSIGNBITNOTSET(i)		((~((const unsigned long)(i))) >> 31)
+
 namespace crown
 {
 

+ 46 - 13
src/network/Message.cpp

@@ -1,5 +1,6 @@
 #include <cassert>
 
+#include "MathUtils.h"
 #include "Message.h"
 
 namespace crown
@@ -325,14 +326,9 @@ void Message::write_real(real f, int32_t exp_bits, int32_t mant_bits)
 	//TODO:need to implement floatToBits function
 }
 
-void Message::write_angle(real f)
-{
-	// needs to be implemented
-}
-
 void Message::write_vec3(const Vec3& v, int32_t num_bits)
 {
-	
+	write_bits(vec3_to_bits(v, num_bits), num_bits);
 }
 
 void Message::write_string(const char* s, int32_t max_len, bool make7Bit)
@@ -469,11 +465,6 @@ real Message::read_real(int32_t exp_bits, int32_t mant_bits) const
   
 }
 
-real Message::read_angle() const
-{
-  
-}
-
 Vec3 Message::read_vec3(int32_t num_bits) const
 {
   
@@ -494,7 +485,49 @@ void Message::read_ipv4addr(os::IPv4Address* addr) const
   
 }
 
-// static int32_t		vec3_to_bits(const Vec3& v, int32_t num_bits);
-// static Vec3		bits_to_vec3(int32_t bits, int32_t num_bits);
+int32_t Message::vec3_to_bits(const Vec3& v, int32_t num_bits)
+{
+	assert(num_bits >= 6 && num_bits <= 32);
+	assert(v.squared_length() - 1.0f < 0.01f);
+  
+	int32_t max; 
+	int32_t bits;
+	float bias;
+
+	num_bits /= 3;
+	max = (1 << (num_bits - 1)) - 1;
+	bias = 0.5f / max;
+
+	bits = FLOATSIGNBITSET(v.x) << (num_bits * 3 - 1);
+	bits |= ((int32_t)((math::abs(v.x) + bias) * max)) << (num_bits * 2);
+	bits |= FLOATSIGNBITSET(v.y) << (num_bits * 2 - 1);
+	bits |= ((int32_t)((math::abs(v.y) + bias) * max)) << (num_bits * 1);
+	bits |= FLOATSIGNBITSET(v.z) << (num_bits * 1 - 1);
+	bits |= ((int32_t)((math::abs(v.z) + bias) * max)) << (num_bits * 0);
+	
+	return bits;  
+}
+
+Vec3 Message::bits_to_vec3(int32_t bits, int32_t num_bits)
+{
+	assert(num_bits >= 6 && num_bits <= 32);
+  
+	static float sign[2] = {1.0f, -1.0f};
+	int max;
+	float inv_max;
+	Vec3 v;
+
+	num_bits /= 3;
+	max = (1 << (num_bits - 1)) - 1;
+	inv_max = 1.0f / max;
+
+	v.x = sign[(bits >> (num_bits * 3 - 1)) & 1] * ((bits >> (num_bits * 2)) & max) * inv_max;
+	v.y = sign[(bits >> (num_bits * 2 - 1)) & 1] * ((bits >> (num_bits * 1)) & max) * inv_max;
+	v.z = sign[(bits >> (num_bits * 1 - 1)) & 1] * ((bits >> (num_bits * 0)) & max) * inv_max;
+	v.normalize();
+	
+	return v;
+}
+
 }
 }

+ 2 - 4
src/network/Message.h

@@ -54,7 +54,6 @@ namespace network
 		void			write_int64(int32_t c);
 		void			write_real(real f);
 		void			write_real(real f, int32_t exp_bits, int32_t mant_bits);
-		void			write_angle(real f);
 		void			write_vec3(const Vec3& v, int32_t num_bits);
 		void			write_string(const char* s, int32_t max_len = -1, bool make7Bit = true);
 		void			write_data(const void* data, int32_t length);
@@ -71,14 +70,13 @@ namespace network
 		int32_t			read_int64() const;
 		real			read_real() const;
 		real			read_real(int32_t exp_bits, int32_t mant_bits) const;
-		real			read_angle() const;
 		Vec3			read_vec3(int32_t num_bits) const;
 		int32_t			read_string(char* buffer, int32_t buffer_size) const;
 		int32_t			read_data(void* data, int32_t length) const;
 		void			read_ipv4addr(os::IPv4Address* addr) const;
 
-// 		static int32_t		vec3_to_bits(const Vec3& v, int32_t num_bits);
-// 		static Vec3		bits_to_vec3(int32_t bits, int32_t num_bits);
+ 		static int32_t		vec3_to_bits(const Vec3& v, int32_t num_bits);
+ 		static Vec3		bits_to_vec3(int32_t bits, int32_t num_bits);
 
 	private: