頂点のstd :: vectorをfloat *に変換する最良の方法は何ですか?私は元のデータとしてvtxを持っています。これには、位置、通常、およびuvの2つの頂点が含まれています。また、同じ位置、通常、およびuvの頂点vのベクトルがあります。私が達成しようとしているのは、std :: vector vを使ってvtxと同じメモリレイアウトとデータを取得することです。memcpyを使ってvからvtx2にメモリをコピーしようとしましたが、それらを印刷すると、頂点のベクトル:: stuctsをfloatに変換する*
#include <iostream>
#include <vector>
using namespace std;
struct Vector3
{
float x;
float y;
float z;
};
struct Vector2
{
float x;
float y;
};
struct Vertex
{
Vector3 position;
Vector3 normal;
Vector2 uv;
};
int main(int argc, char *argv[])
{
const int n = 16;
float* vtx = new float[n];
// Vertex 1
// Position
vtx[0] = 1.0f;
vtx[1] = 2.0f;
vtx[2] = 3.0f;
// Normal
vtx[3] = 0.1f;
vtx[4] = 0.2f;
vtx[5] = 0.3f;
// UV
vtx[6] = 0.0f;
vtx[7] = 1.0f;
vtx += 8;
// Vertex 2
// Position
vtx[0] = 4.0f;
vtx[1] = 5.0f;
vtx[2] = 6.0f;
// Normal
vtx[3] = 0.2f;
vtx[4] = 0.3f;
vtx[5] = 0.4f;
// UV
vtx[6] = 0.0f;
vtx[7] = 1.0f;
vtx += 8;
for (int i = n; i>0; i--)
{
cout << *(vtx + i * -1) << endl;
}
vector<Vertex> v;
Vertex vt;
// Vertex 1
// Position
Vector3 pos1 = {1.0, 2.0, 3.0};
vt.position = pos1;
// Normal
Vector3 normal1 = {0.1, 0.2, 0.3};
vt.position = normal1;
// UV
Vector2 uv1 = {0.0, 1.0};
vt.uv = uv1;
v.push_back(vt);
// Vertex 2
// Position
Vector3 pos2 = {4.0, 5.0, 6.0};
vt.position = pos2;
// Normal
Vector3 normal2 = {0.2, 0.3, 0.4};
vt.position = normal2;
// UV
Vector2 uv2 = {0.0, 1.0};
vt.uv = uv2;
v.push_back(vt);
float* vtx2 = new float[n];
memcpy(vtx2, &v[0], v.size() * sizeof(Vertex));
for (int i = n; i>0; i--)
{
cout << *(vtx2 + i * -1) << endl;
}
delete[] vtx;
delete[] vtx2;
return 0;
}
あなたはそれをどうしようとしていますか? 'data'メンバにアクセスして基本となる配列へのポインタを得ることができますが、これは定数であり、直接変更しないでください。 – Donnie
私はいくつかのVBOを元々フロート*を使用していました。私は代わりにそれらを標準のベクトルに変換しようとしています:: Verticesの代わりに、同じメモリレイアウトを保持します。 – sabotage3d
*なぜ*メモリレイアウトを保存しますか?通常、標準のコンテナを使用する場合、メモリのレイアウトは問題ではありません。 – Beta