非成员转换函数;投放不同类型,例如DirectX向量到OpenGL向量

编程入门 行业动态 更新时间:2024-10-28 21:26:55
本文介绍了非成员转换函数;投放不同类型,例如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向量

本文发布于:2023-08-04 17:24:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   不同类型   函数   成员   OpenGL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!