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

发布于 2024-12-19 09:00:05 字数 1115 浏览 0 评论 0原文

我使用的数据结构不是我自己编写的,它返回一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-12-26 09:00:05

你所拥有的可能可行,但你可以通过一些安全检查来改进它,而且你根本不需要使用迭代器。看来 realVec 与大多数其他向量类一样提供了 operator[]

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

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.

realVec normal = this->meshPoints.getNormalForVertex(i);
if (normal.size() >= 3)
{
   vec3 x(normal[0], normal[1], normal[2]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文