@@ -1155,5 +1155,77 @@ void updateTexture(const String& name, InputArray image)
1155
1155
CV_Assert (tex);
1156
1156
_createTexture (name, image.getMat ());
1157
1157
}
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
+
1158
1230
}
1159
1231
}
0 commit comments