C++数据结构:从 TNT Vector 转换为 GL vec3
我使用的数据结构不是我自己编写的,它返回一个 realVec。这是realVec的声明(在typedef.h中):
typedef TNT::Vector<PCReal> realVec;
TNT Vector的定义请参见:http ://calicam.sourceforge.net/doxygen/tnt__vector_8h_source.html
PCReal的定义:
typedef double PCReal;
我需要将这个realVec转换为以下内容vec3:
struct vec3 {
GLfloat x;
GLfloat y;
GLfloat z;
//
// --- Constructors and Destructors ---
//
vec3( GLfloat s = GLfloat(0.0) ) :
x(s), y(s), z(s) {}
vec3( GLfloat _x, GLfloat _y, GLfloat _z ) :
x(_x), y(_y), z(_z) {}
vec3( const vec3& v ) { x = v.x; y = v.y; z = v.z; }
vec3( const vec2& v, const float f ) { x = v.x; y = v.y; z = f; }
...
我对 C++ 很陌生,所以我的困惑可能在于使用 TNT::Vector 的迭代器并转换它返回的值。我在想类似下面的事情,所以请告诉我这是否有道理。它似乎可以编译(没有“make”错误):
realVec normal = this->meshPoints.getNormalForVertex(i);
PCReal* iter = normal.begin();
vec3(*iter++, *iter++, *iter++);
我需要这个,因为我正在进行 gl 编程,并且我的着色器将 vec3 作为输入很方便。
I'm using a data structure not written by myself that returns a realVec. This is the declaration of realVec (in typedef.h):
typedef TNT::Vector<PCReal> realVec;
For the definition of TNT Vector please see: http://calicam.sourceforge.net/doxygen/tnt__vector_8h_source.html
Definition of PCReal:
typedef double PCReal;
I need to convert this realVec into the following vec3:
struct vec3 {
GLfloat x;
GLfloat y;
GLfloat z;
//
// --- Constructors and Destructors ---
//
vec3( GLfloat s = GLfloat(0.0) ) :
x(s), y(s), z(s) {}
vec3( GLfloat _x, GLfloat _y, GLfloat _z ) :
x(_x), y(_y), z(_z) {}
vec3( const vec3& v ) { x = v.x; y = v.y; z = v.z; }
vec3( const vec2& v, const float f ) { x = v.x; y = v.y; z = f; }
...
I'm very new to C++ so my confusion probably lies in using TNT::Vector's iterator and converting values returned by it. I'm thinking something like the below, so tell me if it makes sense. It seems to compile (no 'make' errors):
realVec normal = this->meshPoints.getNormalForVertex(i);
PCReal* iter = normal.begin();
vec3(*iter++, *iter++, *iter++);
I need this because I'm doing gl programming, and it's convenient for my shaders to take vec3's as input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你所拥有的可能可行,但你可以通过一些安全检查来改进它,而且你根本不需要使用迭代器。看来 realVec 与大多数其他向量类一样提供了
operator[]
。What you have might work, but you could improve it with some safety checks, and you don't really have to use iterators at all. It appears realVec provides
operator[]
like most other vector classes.