gpt4 book ai didi

C++ 数据结构 : converting from TNT Vector to GL vec3

转载 作者:行者123 更新时间:2023-11-30 04:31:03 25 4
gpt4 key购买 nike

我使用的数据结构不是我自己写的,它返回一个 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 的迭代器并转换它返回的值。我在想类似下面的事情,所以告诉我它是否有意义。它似乎可以编译(没有“制造”错误):

realVec normal = this->meshPoints.getNormalForVertex(i);
PCReal* iter = normal.begin();

vec3(*iter++, *iter++, *iter++);

我需要这个,因为我正在做 gl 编程,而且我的着色器将 vec3 作为输入很方便。

最佳答案

您拥有的可能有效,但您可以通过一些安全检查来改进它,而且您根本不必使用迭代器。看起来 realVec 像大多数其他 vector 类一样提供 operator[]

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

关于C++ 数据结构 : converting from TNT Vector to GL vec3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8377102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com