【发布时间】:2013-10-08 15:15:14
【问题描述】:
我正在使用 OpenGL 开发渲染器。
我有第一节课,几何:
class Geometry
{
public:
void setIndices( const unsigned int* indices, int indicesCount );
private:
std::vector<unsigned char> colors;
std::vector<float> positions;
std::vector<unsigned int> indices;
};
有时,我的几何图形需要为他的不同类型的索引存储,数据可以是:
1. std::vector<unsigned char>
2. std::vector<short>
3. std::vector<int>
// I've already think about std::vector<void>, but it sound dirty :/.
目前,我在任何地方都使用 unsigned int,当我想将其设置为我的几何图形时,我会转换我的数据:
const char* indices = { 0, 1, 2, 3 };
geometry.setIndices( (const unsigned int*) indices, 4 );
稍后,我想在运行时更新或读取这个数组(数组有时可以存储超过 60000 个索引),所以我做了这样的事情:
std::vector<unsigned int>* indices = geometry.getIndices();
indices->resize(newIndicesCount);
std::vector<unsigned int>::iterator it = indices->begin();
问题是我的迭代器循环在一个 unsigned int 数组上,所以迭代器转到 4 个字节到 4 个字节,我的初始数据可以是 char(所以 1 个字节到 1 个字节)。无法读取我的初始数据或用新数据更新它。
当我想更新我的向量时,我唯一的解决方案是创建一个新数组,用数据填充它,然后将其转换为一个无符号整数数组,我想迭代我的索引指针。
- 我怎样才能做一些通用的事情(使用 unsigned int、char 和 short)?
- 如何在不复制的情况下遍历数组?
感谢您的宝贵时间!
【问题讨论】:
-
看来你的问题是你调用
setIndices()时的演员阵容,而不是在使用矢量迭代器时!
标签: c++ arrays architecture vector