Trying to correctly set the VertexBuffer for the geometry of a Model created from scratch. We know how to do this in general (following for e.g. the DynamicGeometries sample), but cannot quite get it right when we want to set the color of the model vertex by vertex, e.g., so the object is red in some spots and blue in another, or whatever.
So we are going to do
SharedPtr<VertexBuffer> vb(new VertexBuffer(context));
vb->SetSize(numVertices, MASK_POSITION|MASK_NORMAL|MASK_COLOR);
vb->SetData((void*)*newVertexData);
but we need to initialize newVertexData first. I’ve reproduced the outline of how we do this below. It’s probably more complex than it really needs to be, but since we include colors we seem to need both floats and unsigned chars stored in the same object newVertexData. We would have done this with void* types but we are using Visual Studio 2012 and it seems that over there one must use char*.
The error is we typically get either no object appearing at all, or a crazy shape. So we want to check here if our approach is on the right track because it’s hard to interpret the results we’re getting. Okay here’s that outline of what we tried:
char** newVertexData = (char**)malloc(numVertices * 10 * sizeof(char*));
for(int i = 0; i < numVertices; i++)
{
newVertexData[10 * i + 0] = (char*)malloc(sizeof(float));
newVertexData[10 * i + 1] = (char*)malloc(sizeof(float));
newVertexData[10 * i + 2] = (char*)malloc(sizeof(float));
newVertexData[10 * i + 3] = (char*)malloc(sizeof(float));
newVertexData[10 * i + 4] = (char*)malloc(sizeof(float));
newVertexData[10 * i + 5] = (char*)malloc(sizeof(float));
newVertexData[10 * i + 6] = (char*)malloc(sizeof(unsigned char));
newVertexData[10 * i + 7] = (char*)malloc(sizeof(unsigned char));
newVertexData[10 * i + 8] = (char*)malloc(sizeof(unsigned char));
newVertexData[10 * i + 9] = (char*)malloc(sizeof(unsigned char));
}
for(int i = 0; i < triCount; i++)
{
//v1
*((float*)newVertexData[30 * i + 0]) = (float)verts[3 * faces[3 * i + 0] + 0];
*((float*)newVertexData[30 * i + 1]) = (float)verts[3 * faces[3 * i + 0] + 1];
*((float*)newVertexData[30 * i + 2]) = (float)verts[3 * faces[3 * i + 0] + 2];
//n1
*((float*)newVertexData[30 * i + 3]) = n.x_;
*((float*)newVertexData[30 * i + 4]) = n.y_;
*((float*)newVertexData[30 * i + 5]) = n.z_;
//c1
*((unsigned char*)newVertexData[30 * i + 6]) = (unsigned char) 255;
*((unsigned char*)newVertexData[30 * i + 7]) = (unsigned char) 0;
*((unsigned char*)newVertexData[30 * i + 8]) = (unsigned char) 0;
*((unsigned char*)newVertexData[30 * i + 9]) = (unsigned char) 255;
//v2
*((float*)newVertexData[30 * i + 10]) = (float)verts[3 * faces[3 * i + 1] + 0];
*((float*)newVertexData[30 * i + 11]) = (float)verts[3 * faces[3 * i + 1] + 1];
*((float*)newVertexData[30 * i + 12]) = (float)verts[3 * faces[3 * i + 1] + 2];
//n2
*((float*)newVertexData[30 * i + 13]) = n.x_;
*((float*)newVertexData[30 * i + 14]) = n.y_;
*((float*)newVertexData[30 * i + 15]) = n.z_;
//c2
*((unsigned char*)newVertexData[30 * i + 16]) = (unsigned char) 0;
*((unsigned char*)newVertexData[30 * i + 17]) = (unsigned char) 255;
*((unsigned char*)newVertexData[30 * i + 18]) = (unsigned char) 0;
*((unsigned char*)newVertexData[30 * i + 19]) = (unsigned char) 255;
//v3
*((float*)newVertexData[30 * i + 20]) = (float)verts[3 * faces[3 * i + 2] + 0];
*((float*)newVertexData[30 * i + 21]) = (float)verts[3 * faces[3 * i + 2] + 1];
*((float*)newVertexData[30 * i + 22]) = (float)verts[3 * faces[3 * i + 2] + 2];
//n3
*((float*)newVertexData[30 * i + 23]) = n.x_;
*((float*)newVertexData[30 * i + 24]) = n.y_;
*((float*)newVertexData[30 * i + 25]) = n.z_;
//c3
*((unsigned char*)newVertexData[30 * i + 26]) = (unsigned char) 0;
*((unsigned char*)newVertexData[30 * i + 27]) = (unsigned char) 0;
*((unsigned char*)newVertexData[30 * i + 28]) = (unsigned char) 255;
*((unsigned char*)newVertexData[30 * i + 29]) = (unsigned char) 255;
}
The vertex data is stored in newVertexData. For each vertex, the first 3 entries are vertex coordinates, next 3 are normal coordinates, and next 4 are for the color.
Thanks for any help.