gpt4 book ai didi

C++。每帧更新和复制一系列值。我应该使用哪种类类型?

转载 作者:搜寻专家 更新时间:2023-10-31 01:52:46 25 4
gpt4 key购买 nike

我在每一帧生成一个值集合,我想将这些值添加到特定索引处的更大值集合。

这是我正在生成的集合的典型示例

std::vector<glm::vec3> corners;
corners.reserve(8);
//glm::vec3 corners[8];
//std::list<glm::vec3> corners;

corners[i++] = position - hX + hY + hZ;
corners[i++] = position + hX + hY + hZ;
corners[i++] = position + hX - hY + hZ;
corners[i++] = position - hX - hY + hZ;
corners[i++] = position - hX + hY - hZ;
corners[i++] = position + hX + hY - hZ;
corners[i++] = position + hX - hY - hZ;
corners[i++] = position - hX - hY - hZ;

然后我有一个更大的 glm::vec3 值集合,我想将上面的值复制到特定索引处。

std::vector<glm::vec3> vertices;
vertices.assign(maxVertices, 0);

C# 等价物是

corners.CopyTo(vertices, index);

我可以使用什么类类型来有效地生成较小的集合并将其复制到较大的集合,而无需在每一帧中生成太多开销?

我可以将每个新生成的较小集合分配给较大集合的末尾,这样索引值就可以被忽略。

最佳答案

在您的代码中,您应该corners.resize(8),而不是corners.reserve(8)。此外,如果我没理解错的话,你好像总是有8个角?然后使用数组:

typedef std::array<glm::vec3, 8> Corners; 
// or in pre-c++0B code use boost::array same way
// or if neither is available then just raw glm::vec3[8] can do.

然后可以通过初始化完成填充:

// somehow we have position, hX, hY and hZ
Corners corners =
{
position - hX + hY + hZ,
position + hX + hY + hZ,
position + hX - hY + hZ,
position - hX - hY + hZ,
position - hX + hY - hZ,
position + hX + hY - hZ,
position + hX - hY - hZ,
position - hX - hY - hZ
};

可以使用标准拷贝进行复制:

std::vector<glm::vec3> vertices;
// somehow it gets filled

// somewhere comes index

std::copy( corners.begin(), corners.end(), vertices.begin() + index );
// with raw glm::vec3[8] std::copy( corners, corners + 8, vertices.begin() + index );

如果你需要插入而不是复制那么使用vertices.insert()

关于C++。每帧更新和复制一系列值。我应该使用哪种类类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12126404/

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