C ++数据结构:从TNT Vector转换为GL vec3

时间:2011-12-04 17:21:49

标签: c++ data-structures iterator

我使用的是我自己编写的数据结构,它返回一个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作为输入。

1 个答案:

答案 0 :(得分:2)

你可能会有什么工作,但你可以通过一些安全检查来改进它,而你根本不需要使用迭代器。似乎realVec像大多数其他矢量类一样提供operator[]

realVec normal = this->meshPoints.getNormalForVertex(i);
if (normal.size() >= 3)
{
   vec3 x(normal[0], normal[1], normal[2]);
}