Thanks for the reply!
I tried to do it for the SubSceneOverride with the apiMeshShape example. I created an MShaderInstance fTexturedShader similar to fShadedShader, and an MVertexBuffer fUVBuffer among fPositionBuffer and fNormalBuffer, as well as an MIndexBuffer fTexturedIndexBuffer.
When updating geometry I added positions, normals and uvs:
MVertexBufferArray texturedBuffers;
texturedBuffers.addBuffer(“positions”, fPositionBuffer);
texturedBuffers.addBuffer(“normals”, fNormalBuffer);
texturedBuffers.addBuffer(“uvs”, fUVBuffer);
setGeometryForRenderItem(*texturedItem, texturedBuffers, *fTexturedIndexBuffer, &bounds);
When filling the buffers I did:
// Acquire vertex buffer resources
const MVertexBufferDescriptor posDesc("", MGeometry::kPosition, MGeometry::kFloat, 3);
const MVertexBufferDescriptor normalDesc("", MGeometry::kNormal, MGeometry::kFloat, 3);
const MVertexBufferDescriptor uvDesc("", MGeometry::kTexture, MGeometry::kFloat, 2);
fPositionBuffer = new MVertexBuffer(posDesc);
fNormalBuffer = new MVertexBuffer(normalDesc);
fUVBuffer = new MVertexBuffer(uvDesc);
float* positions = (float*)fPositionBuffer->acquire(meshGeom->vertices.length(), true);
float* normals = (float*)fNormalBuffer->acquire(meshGeom->vertices.length(), true);
float* uvs = (float*)fUVBuffer->acquire(meshGeom->vertices.length(), true);
// Fill vertex data for shaded/wireframe
int vid = 0;
int pid = 0;
int nid = 0;
int uvid = 0;
int uv_len = meshGeom->uvcoords.uvcount();
for (unsigned int i=0; i<meshGeom->vertices.length(); i++)
{
MPoint position = meshGeom->vertices[i];
positions[pid++] = (float)position[0];
positions[pid++] = (float)position[1];
positions[pid++] = (float)position[2];
MVector normal = meshGeom->normals[i];
normals[nid++] = (float)normal[0];
normals[nid++] = (float)normal[1];
normals[nid++] = (float)normal[2];
if (uv_len > 0)
{
// If we are drawing the texture, make sure the coord
// arrays are in bounds.
float u, v;
int uvId1 = meshGeom->uvcoords.uvId(vid);
if ( uvId1 < uv_len ) {
meshGeom->uvcoords.getUV( uvId1, u, v );
uvs[uvid++] = u;
uvs[uvid++] = v;
}
}
vid++;
}
fPositionBuffer->commit(positions); positions = NULL;
fNormalBuffer->commit(normals); normals = NULL;
fUVBuffer->commit(uvs); uvs = NULL;
To fill the index buffers I coded:
fTexturedIndexBuffer = new MIndexBuffer(MGeometry::kUnsignedInt32);
unsigned int* texturedBuffer = (unsigned int*)fTexturedIndexBuffer->acquire(3*numTriangles, true);
// Fill index data for textured
unsigned int base = 0;
idx = 0;
for (int faceIdx=0; faceIdx<meshGeom->faceCount; faceIdx++)
{
// Ignore degenerate faces
int numVerts = meshGeom->face_counts[faceIdx];
if (numVerts > 2)
{
for (int v=1; v<numVerts-1; v++)
{
texturedBuffer[idx++] = meshGeom->face_connects[base];
texturedBuffer[idx++] = meshGeom->face_connects[base+v];
texturedBuffer[idx++] = meshGeom->face_connects[base+v+1];
}
base += numVerts;
}
}
fTexturedIndexBuffer->commit(texturedBuffer); texturedBuffer = NULL;
However, the plugin does not work. When plugging a texture from a surfaceShader node, no shader is shown.
Is the code properly written? At least the way how the UVBuffer and the index buffer are filled?
Many thanks!