非成员转换函数;投放不同类型,例如DirectX向量到OpenGL向量 [英] Non-member conversion functions; Casting different types, e.g. DirectX vector to OpenGL vector

查看:136
本文介绍了非成员转换函数;投放不同类型,例如DirectX向量到OpenGL向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个需要在3D引擎,物理引擎和脚本语言之间移动值的游戏引擎。因为我需要将来自物理引擎的向量经常应用到3D对象,并且希望能够通过脚本系统控制3D以及物理对象,我需要一种机制来转换一种类型的向量(例如, vector3d< float> )到另一种类型的向量(例如 btVector3 )。不幸的是,我不能假设类/结构如何布局,所以一个简单的 reinterpret_cast 可能不会。

I am currently working on a game "engine" that needs to move values between a 3D engine, a physics engine and a scripting language. Since I need to apply vectors from the physics engine to 3D objects very often and want to be able to control both the 3D, as well as the physics objects through the scripting system, I need a mechanism to convert a vector of one type (e.g. vector3d<float>) to a vector of the other type (e.g. btVector3). Unfortunately I can make no assumptions on how the classes/structs are laid out, so a simple reinterpret_cast probably won't do.

所以问题是:是否有某种静态/非成员转换方法来实现基本上这样:

So the question is: Is there some sort of 'static'/non-member casting method to achieve basically this:

vector3d<float> operator vector3d<float>(btVector3 vector) {
    // convert and return
}

btVector3 operator btVector3(vector3d<float> vector) {
    // convert and return
}

现在这将不会编译,因为铸造操作符需要是成员方法。
错误C2801:'operator foo'必须是非静态成员

Right now this won't compile since casting operators need to be member methods. (error C2801: 'operator foo' must be a non-static member)

推荐答案

我建议把它们写成一对免费的函数(即不用担心他们是运算符):

I would suggest writing them as a pair of free functions (i.e. don't worry about making them 'operators'):

vector3d<float> vector3dFromBt(const btVector3& src) {
    // convert and return
}

btVector3 btVectorFrom3d(const vector3d<float>& src) {
    // convert and return
}

void f(void)
{
  vector3d<float> one;   
// ...populate...   
  btVector3 two(btVectorFrom3d(one));    
// ...
  vector3d<float> three(vector3dFromBt(two));
}

这篇关于非成员转换函数;投放不同类型,例如DirectX向量到OpenGL向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆