Skip to content

Commit ee51f4f

Browse files
committed
ovis: add loadMesh
1 parent ae52320 commit ee51f4f

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

modules/ovis/include/opencv2/ovis.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,23 @@ CV_EXPORTS_W void createGridMesh(const String& name, const Size2f& size, const S
414414
*/
415415
CV_EXPORTS_W void createTriangleMesh(const String& name, InputArray vertices, InputArray normals = noArray(), InputArray indices = noArray());
416416

417+
/**
418+
* Loads a mesh from a known resource location.
419+
*
420+
* The file format is determined by the file extension. Supported formats are Ogre @c .mesh and everything that Assimp can load.
421+
* @param meshname Name of the mesh file
422+
* @param vertices vertex coordinates, each value contains 3 floats
423+
* @param indices per-face list of vertices, each value contains 3 ints
424+
* @param normals per-vertex normals, each value contains 3 floats
425+
* @param colors per-vertex colors, each value contains 4 uchars
426+
* @param texCoords per-vertex texture coordinates, each value contains 2 floats
427+
*
428+
* @see addResourceLocation()
429+
*/
430+
CV_EXPORTS_W void loadMesh(const String& meshname, OutputArray vertices, OutputArray indices,
431+
OutputArray normals = noArray(), OutputArray colors = noArray(),
432+
OutputArray texCoords = noArray());
433+
417434
/// @deprecated use setMaterialProperty
418435
CV_EXPORTS_W void updateTexture(const String& name, InputArray image);
419436
//! @}

modules/ovis/src/ovis.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -1155,5 +1155,77 @@ void updateTexture(const String& name, InputArray image)
11551155
CV_Assert(tex);
11561156
_createTexture(name, image.getMat());
11571157
}
1158+
1159+
void loadMesh(const String& meshname, OutputArray vertices, OutputArray indices, OutputArray normals, OutputArray colors, OutputArray texCoords)
1160+
{
1161+
CV_Assert(_app);
1162+
1163+
auto mesh = MeshManager::getSingleton().load(meshname, RESOURCEGROUP_NAME);
1164+
auto smeshes = mesh->getSubMeshes();
1165+
CV_Assert(smeshes.size() == 1);
1166+
1167+
auto smesh = smeshes.front();
1168+
1169+
CV_Assert(smesh->operationType == RenderOperation::OT_TRIANGLE_LIST);
1170+
1171+
if (auto ibuf = smesh->indexData->indexBuffer)
1172+
{
1173+
auto idtype = ibuf->getType() == HardwareIndexBuffer::IT_16BIT ? CV_16S : CV_32S;
1174+
auto imat = Mat(smesh->indexData->indexCount, 3, idtype);
1175+
ibuf->readData(0, ibuf->getSizeInBytes(), imat.ptr());
1176+
imat.copyTo(indices);
1177+
}
1178+
1179+
auto vertexData = smesh->useSharedVertices ? mesh->sharedVertexData : smesh->vertexData;
1180+
DefaultHardwareBufferManagerBase swhbm;
1181+
1182+
// download all buffers to CPU for reorganization
1183+
auto tmpVertexData = vertexData->clone(true, &swhbm);
1184+
auto tgtDecl = swhbm.createVertexDeclaration();
1185+
tgtDecl->addElement(0, 0, VET_FLOAT3, VES_POSITION); // separate position buffer
1186+
1187+
bool has_normals = vertexData->vertexDeclaration->findElementBySemantic(VES_NORMAL);
1188+
bool has_texcoords = vertexData->vertexDeclaration->findElementBySemantic(VES_TEXTURE_COORDINATES);
1189+
bool has_colors = vertexData->vertexDeclaration->findElementBySemantic(VES_DIFFUSE);
1190+
if (has_normals)
1191+
tgtDecl->addElement(1, 0, VET_FLOAT3, VES_NORMAL); // separate normal buffer
1192+
if (has_texcoords)
1193+
tgtDecl->addElement(2, 0, VET_FLOAT2, VES_TEXTURE_COORDINATES); // separate texcoord buffer
1194+
if (has_colors)
1195+
tgtDecl->addElement(3, 0, VET_UBYTE4_NORM, VES_DIFFUSE); // separate color buffer
1196+
1197+
tmpVertexData->reorganiseBuffers(tgtDecl);
1198+
1199+
// copy data
1200+
auto vertmat = Mat(vertexData->vertexCount, 3, CV_32F);
1201+
auto posbuf = tmpVertexData->vertexBufferBinding->getBuffer(0);
1202+
posbuf->readData(0, posbuf->getSizeInBytes(), vertmat.ptr());
1203+
vertmat.copyTo(vertices);
1204+
1205+
if(has_normals && normals.needed())
1206+
{
1207+
auto normmat = Mat(vertexData->vertexCount, 3, CV_32F);
1208+
auto nbuf = tmpVertexData->vertexBufferBinding->getBuffer(1);
1209+
nbuf->readData(0, nbuf->getSizeInBytes(), normmat.ptr());
1210+
normmat.copyTo(normals);
1211+
}
1212+
1213+
if(has_texcoords && texCoords.needed())
1214+
{
1215+
auto texmat = Mat(vertexData->vertexCount, 2, CV_32F);
1216+
auto tbuf = tmpVertexData->vertexBufferBinding->getBuffer(2);
1217+
tbuf->readData(0, tbuf->getSizeInBytes(), texmat.ptr());
1218+
texmat.copyTo(texCoords);
1219+
}
1220+
1221+
if(has_colors && colors.needed())
1222+
{
1223+
auto colmat = Mat(vertexData->vertexCount, 4, CV_8U);
1224+
auto cbuf = tmpVertexData->vertexBufferBinding->getBuffer(3);
1225+
cbuf->readData(0, cbuf->getSizeInBytes(), colmat.ptr());
1226+
colmat.copyTo(colors);
1227+
}
1228+
}
1229+
11581230
}
11591231
}

0 commit comments

Comments
 (0)