Struct mit dynamic array
Verfasst: 06.08.2013, 09:49
Aktuell implementiere ich für mich einen ASSIMP Importer. Um beim Auslesen der Texturdaten nicht für jede Channel Kombinationsmöglichkeit ein eigenes Texture-Struct zu definieren (Nutze VBO-Multistreaming) wollte ich ein TextureStruct verwenden, dass ein dynamisch allokiertes Array nutzt:
Beim Import der Texturdaten mache ich folgendes:
Irgendwie wird nur Müll gemapped. Habe ich bei der Berechnung der Bytegröße evtl. einen gedanklichen Fehler?
Code: Alles auswählen
struct TXSTREAM
{
float* uv;
};
Code: Alles auswählen
unsigned int channels = 1; // mesh->GetNumUVChannels();
std::shared_ptr<brGraphics::brVertexDeclaration> vxDecl = system.createVertexDeclaration();
for(int c=0; c<channels; c++){
vxDecl->addElement(brGraphics::brVertexElement(brGraphics::brVertexElement::TYPE_FLOAT_2,
brGraphics::brVertexElement::USE_TEXCOORD,
sizeof(float)*(c*2)));
}
std::vector<TXSTREAM> texCoords;
for(unsigned int v = 0 ; v < mesh->mNumVertices; v++)
{
struct TXSTREAM coord;
coord.uv = (float*)malloc((channels*2)*sizeof(float));
for(int c=0; c<channels; c++){
const aiVector3D texCoord = (mesh->mTextureCoords[c][v]);
coord.uv[0+(c*2)] = texCoord.x;
coord.uv[1+(c*2)] = texCoord.y;
}
texCoords.push_back(coord);
}
unsigned int size = texCoords.size();
unsigned int bytes = sizeof(TXSTREAM)+((channels*2)*sizeof(float));
std::shared_ptr<brGraphics::brVertexBuffer> tbo =
system.createVertexBuffer(brGraphics::brVertexBuffer::USE_STATIC,
bytes,
size);
tbo->writeData(0, size, &texCoords[0]);
tbo->setVertexDeclaration(vxDecl);