@@ -102,6 +102,92 @@ void createPointCloudMesh(const String& name, InputArray vertices, InputArray co
102
102
mesh->_setBounds (bounds);
103
103
}
104
104
105
+ void createTriangleMesh (const String& name, InputArray vertices, InputArray normals, InputArray indices)
106
+ {
107
+ CV_CheckTypeEQ (vertices.type (), CV_32FC3, " vertices type must be Vec3f" );
108
+ CV_Assert (vertices.isContinuous ());
109
+
110
+ if (!normals.empty ())
111
+ {
112
+ CV_CheckTypeEQ (normals.type (), CV_32FC3, " normals type must be Vec3f" );
113
+ CV_Assert (normals.isContinuous ());
114
+ CV_Assert (normals.size () == vertices.size ());
115
+ }
116
+ if (!indices.empty ())
117
+ {
118
+ CV_CheckTypeEQ (indices.type (), CV_32S, " indices type must be int" );
119
+ CV_Assert (indices.isContinuous ());
120
+ }
121
+
122
+ // default material
123
+ auto mat = MaterialManager::getSingleton ().create (name, RESOURCEGROUP_NAME);
124
+
125
+ // mesh
126
+ MeshPtr mesh = MeshManager::getSingleton ().createManual (name, RESOURCEGROUP_NAME);
127
+ SubMesh* sub = mesh->createSubMesh ();
128
+ sub->useSharedVertices = true ;
129
+ sub->operationType = RenderOperation::OT_TRIANGLE_LIST;
130
+ sub->setMaterialName (name);
131
+
132
+ int n = vertices.rows ();
133
+
134
+ mesh->sharedVertexData = new VertexData ();
135
+ mesh->sharedVertexData ->vertexCount = n;
136
+ VertexDeclaration* decl = mesh->sharedVertexData ->vertexDeclaration ;
137
+
138
+ // vertex data
139
+ HardwareBufferManager& hbm = HardwareBufferManager::getSingleton ();
140
+
141
+ Mat _vertices = vertices.getMat ();
142
+
143
+ int source = 0 ;
144
+ HardwareVertexBufferSharedPtr hwbuf;
145
+
146
+ decl->addElement (source, 0 , VET_FLOAT3, VES_POSITION);
147
+ hwbuf = hbm.createVertexBuffer (decl->getVertexSize (source), n, HardwareBuffer::HBU_STATIC_WRITE_ONLY);
148
+ hwbuf->writeData (0 , hwbuf->getSizeInBytes (), _vertices.ptr (), true );
149
+ mesh->sharedVertexData ->vertexBufferBinding ->setBinding (source, hwbuf);
150
+
151
+ // normals
152
+ if (!normals.empty ())
153
+ {
154
+ source += 1 ;
155
+
156
+ Mat _normals = normals.getMat ();
157
+ decl->addElement (source, 0 , VET_FLOAT3, VES_NORMAL);
158
+ hwbuf =
159
+ hbm.createVertexBuffer (decl->getVertexSize (source), n, HardwareBuffer::HBU_STATIC_WRITE_ONLY);
160
+ hwbuf->writeData (0 , hwbuf->getSizeInBytes (), _normals.ptr (), true );
161
+ mesh->sharedVertexData ->vertexBufferBinding ->setBinding (source, hwbuf);
162
+ }
163
+ else
164
+ {
165
+ mat->setLightingEnabled (false );
166
+ }
167
+
168
+ // indices
169
+ if (!indices.empty ())
170
+ {
171
+ Mat _indices = indices.getMat ();
172
+
173
+ HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton ().createIndexBuffer (
174
+ HardwareIndexBuffer::IT_32BIT, indices.total (), HardwareBuffer::HBU_STATIC_WRITE_ONLY);
175
+ ibuf->writeData (0 , ibuf->getSizeInBytes (), _indices.ptr (), true );
176
+
177
+ sub->indexData ->indexBuffer = ibuf;
178
+ sub->indexData ->indexStart = 0 ;
179
+ sub->indexData ->indexCount = indices.total ();
180
+ }
181
+
182
+ AxisAlignedBox bounds (AxisAlignedBox::EXTENT_NULL);
183
+ for (int i = 0 ; i < n; i++)
184
+ {
185
+ Vec3f v = _vertices.at <Vec3f>(i);
186
+ bounds.merge (Vector3 (v[0 ], v[1 ], v[2 ]));
187
+ }
188
+ mesh->_setBounds (bounds);
189
+ }
190
+
105
191
void createGridMesh (const String& name, const Size2f& size, const Size& segments)
106
192
{
107
193
CV_Assert_N (_app, !segments.empty ());
0 commit comments