/* Lua constructor for matrix T. */ template<class T> int bind<T>::create( lua_State* L ) { int argc = lua_gettop(L); /* Default construct. */ if( argc == 0 ) { new (push(L)) T(); return 1; } if( auto A = lua_cast(L,1) ) { new (push(L)) T(*A); return 1; } /* Must provide all elements of the matrix. */ if( argc == (int)T::size() ) { typename T::scalar_t data[ T::size() ]; for( size_t i=1; i < T::size()+1; ++i ) data[i-1] = static_cast<typename T::scalar_t>( luaL_checknumber( L, i ) ); new (push(L)) T(data, data+T::size()); return 1; } return luaL_error(L, "Bad argument count for constructor." ); }
/* 创建一个4元数 mat.newQuaternion(x,y,z,w) mat.newQuaternion(mat3) mat.newQuaternion(axis,radian) axis是Vector3,radian是一个角度 mat.newQuaternion() */ static int newQuaternion(lua_State* L) { const char* meta = "mat.Quaternion"; if( lua_isnumber(L,1) ) { lua_bindComplete(L,meta,new Ogre::Quaternion( tonumber(1),tonumber(2),tonumber(3),tonumber(4) )); } else if( lua_isuserdata(L,1) ) { Ogre::Matrix3* m3 = (Ogre::Matrix3*)lua_isa(L,1,"Matrix3"); if( m3 ) { lua_bindComplete(L,meta,new Ogre::Quaternion(*m3)); } else { Ogre::Vector3* v3 = (Ogre::Vector3*)lua_cast(L,1,"Vector3"); if( v3 ) { lua_bindComplete(L,meta,new Ogre::Quaternion(Ogre::Radian(tonumber(2)),*v3)); } } } else { lua_bindComplete(L,meta,new Ogre::Quaternion()); } return 1; }
//处理两种情况,x,y,z和vec3 static Ogre::Vector3 get_vector3(lua_State* L,int n ) { if( lua_isnumber(L,n) ) { return Ogre::Vector3(tonumber(n),tonumber(n+1),tonumber(n+2)); } else { Ogre::Vector3* v3 = (Ogre::Vector3*)lua_cast(L,n,"Vector3"); if( v3 ) return *v3; } return Ogre::Vector3(); }
template<class T> T* bind<T>::lua_check( lua_State* L, int index ) { TUMBO_LUA_STACKASSERT(L,0); auto ptr = lua_cast(L,index); if( ptr ) return ptr; else { luaL_error(L, "Bad type for argument %d: Type %s required", index, NAME.c_str() ); return nullptr; } }
/* mat.newMatrix3() mat.newMatrix3(mat3) */ static int newMat3(lua_State* L) { const char* meta = "mat.Matrix3"; if( lua_isuserdata(L,1) ) { Ogre::Matrix3* m3 = (Ogre::Matrix3*)lua_cast(L,1,"Matrix3"); if( m3 ) { lua_bindComplete(L,meta,new Ogre::Matrix3(*m3)); } } else { lua_bindComplete(L,meta,new Ogre::Matrix3(Ogre::Matrix3::IDENTITY)); } return 1; }
/* Lua可以用3种方法创建一个Vec3向量 v = mat.newVec3() v = mat.newVec3(x,y,z) v = mat.newVec3(v3) */ static int newVec3(lua_State* L) { const char* meta = "mat.Vector3"; if( lua_isnumber(L,1) ) { lua_bindComplete(L,meta,new Ogre::Vector3( tonumber(1),tonumber(2),tonumber(3))); } else if( lua_isuserdata(L,1) ) { Ogre::Vector3* v3 = (Ogre::Vector3*)lua_cast(L,1,"Vector3"); if( v3 ) { lua_bindComplete(L,meta,new Ogre::Vector3(*v3)); } } else { lua_bindComplete(L,meta,new Ogre::Vector3(Ogre::Vector3::ZERO)); } return 1; }