00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef NET_CONVERTERS_H
00028 #define NET_CONVERTERS_H
00029
00030 #include <PVLE/Config.h>
00031
00032 #ifdef PVLE_NETWORKING
00033
00036
00037 #include <tnl/tnlbitstream.h>
00038 #include <PVLE/Export.h>
00039 #include <PVLE/Util/Util.h>
00040 #include <string>
00041 #include <limits.h>
00042 #include <ode/common.h>
00043
00044 #if defined(dSINGLE)
00045 #include <osg/Vec3f>
00046 #include <osg/Vec4f>
00047 #include <osg/Quat>
00048 #include <osg/Matrixf>
00049 #elif defined(dDOUBLE)
00050 #include <osg/Vec3d>
00051 #include <osg/Vec4d>
00052 #include <osg/Quat>
00053 #include <osg/Matrixd>
00054 #else
00055 #error You must #define dSINGLE or dDOUBLE
00056 #endif
00057
00058
00059 namespace Net {
00060
00061 inline TNL::Point3F toNetVec3(const osg::Vec3f & v) {
00062 TNL::Point3F res;
00063 res.x = v.x();
00064 res.y = v.y();
00065 res.z = v.z();
00066 return res;
00067 }
00068
00069 inline osg::Vec3f toGraphVec3(const TNL::Point3F & v) { return osg::Vec3f(v.x, v.y, v.z); }
00070
00071
00072
00073
00074 inline void writeSignedFloat(TNL::BitStream * pStream, float v, float maxAbsValue, TNL::U8 bitCount) {
00075 ASSERT(v>=-maxAbsValue && v<=maxAbsValue);
00076 pStream->writeSignedFloat(v/maxAbsValue, bitCount);
00077 }
00078 inline float readSignedFloat(TNL::BitStream * pStream, float maxAbsValue, TNL::U8 bitCount) {
00079 return pStream->readSignedFloat(bitCount) * maxAbsValue;
00080 }
00081 inline void writeFloat(TNL::BitStream * pStream, float v, float maxAbsValue, TNL::U8 bitCount) {
00082 ASSERT(v>=0);
00083 ASSERT(v<=maxAbsValue);
00084 pStream->writeFloat(v/maxAbsValue, bitCount);
00085 }
00086 inline float readFloat(TNL::BitStream * pStream, float maxAbsValue, TNL::U8 bitCount) {
00087 return pStream->readFloat(bitCount) * maxAbsValue;
00088 }
00089
00090
00091 inline void writePointCompressed(TNL::BitStream * pStream, const osg::Vec3f & v, TNL::F32 scale) {
00092 pStream->writePointCompressed(toNetVec3(v), scale);
00093 }
00094 inline osg::Vec3f readPointCompressed(TNL::BitStream * pStream) {
00095 TNL::Point3F point;
00096 pStream->readPointCompressed(&point, 1.f);
00097 return toGraphVec3(point);
00098 }
00099
00100
00101 inline void write(TNL::BitStream * pStream, const osg::Vec3f & v) {
00102 pStream->write(v.x());
00103 pStream->write(v.y());
00104 pStream->write(v.z());
00105 }
00106 inline void read(TNL::BitStream * pStream, osg::Vec3f & v) {
00107 osg::Vec3f::value_type temp;
00108 pStream->read(&temp);
00109 v.x() = temp;
00110 pStream->read(&temp);
00111 v.y() = temp;
00112 pStream->read(&temp);
00113 v.z() = temp;
00114 }
00115
00116
00117 inline void write(TNL::BitStream * pStream, const osg::Quat & q) {
00118 #ifdef PVLE_NET_SEND_QUATERNION_NATIVE_PRECISION
00119 pStream->write(q.x());
00120 pStream->write(q.y());
00121 pStream->write(q.z());
00122 pStream->write(q.w());
00123 #else
00124 pStream->write(static_cast<float>(q.x()));
00125 pStream->write(static_cast<float>(q.y()));
00126 pStream->write(static_cast<float>(q.z()));
00127 pStream->write(static_cast<float>(q.w()));
00128 #endif
00129 }
00130 inline void read(TNL::BitStream * pStream, osg::Quat & q) {
00131 #ifdef PVLE_NET_SEND_QUATERNION_NATIVE_PRECISION
00132 osg::Quat::value_type temp;
00133 #else
00134 float temp;
00135 #endif
00136 pStream->read(&temp);
00137 q.x() = temp;
00138 pStream->read(&temp);
00139 q.y() = temp;
00140 pStream->read(&temp);
00141 q.z() = temp;
00142 pStream->read(&temp);
00143 q.w() = temp;
00144 }
00145
00146 inline void write(TNL::BitStream * pStream, const std::string & v) {
00147 ASSERT(v.length() < USHRT_MAX);
00148 pStream->write(static_cast<USHORT>(v.length()));
00149 pStream->write(v.length(), v.c_str());
00150 }
00152 inline void read(TNL::BitStream * pStream, std::string & v) {
00153 USHORT len;
00154 pStream->read(&len);
00155
00156 std::string::value_type * pBuffer = new std::string::value_type[len+1];
00157 pStream->read(len, pBuffer);
00158 pBuffer[len] = 0;
00159 v = pBuffer;
00160 delete[] pBuffer;
00161 }
00162
00163 PVLE_EXPORT void writeMatrix(TNL::BitStream * pStream, const osg::Matrixf & m);
00164 PVLE_EXPORT void readMatrix(TNL::BitStream * pStream, osg::Matrixf & m);
00165
00166 }
00167
00168 #endif // PVLE_NETWORKING
00169
00170 #endif // NET_CONVERTERS_H