14 #include "config_ode.h" 15 #include "odeTriMeshData.h" 22 odetrimeshdata_cat.debug() << get_class_type() <<
"::link_data(" <<
id <<
"->" << data <<
")" <<
"\n";
23 get_tri_mesh_data_map()[id] = data;
27 get_data(dGeomID
id) {
28 const TriMeshDataMap &data_map = get_tri_mesh_data_map();
29 TriMeshDataMap::const_iterator iter = data_map.find(
id);
30 if (iter != data_map.end()) {
37 unlink_data(dGeomID
id) {
38 odetrimeshdata_cat.debug() << get_class_type() <<
"::unlink_data(" <<
id <<
")" <<
"\n";
39 nassertv(_tri_mesh_data_map != (TriMeshDataMap *)NULL);
40 TriMeshDataMap::iterator iter = _tri_mesh_data_map->find(
id);
41 if (iter != _tri_mesh_data_map->end()) {
42 _tri_mesh_data_map->erase(iter);
47 print_data(
const string &marker) {
48 odetrimeshdata_cat.debug() << get_class_type() <<
"::print_data(" << marker <<
")\n";
49 const TriMeshDataMap &data_map = get_tri_mesh_data_map();
50 TriMeshDataMap::const_iterator iter = data_map.begin();
51 for (;iter != data_map.end(); ++iter) {
52 odetrimeshdata_cat.debug() <<
"\t" << iter->first <<
" : " << iter->second <<
"\n";
58 if (odetrimeshdata_cat.is_debug()) {
59 odetrimeshdata_cat.debug()
60 << get_class_type() <<
"::remove_data(" << data->get_id() <<
")" <<
"\n";
62 if (_tri_mesh_data_map == (TriMeshDataMap *)NULL) {
66 TriMeshDataMap::iterator iter;
67 for (iter = _tri_mesh_data_map->begin();
68 iter != _tri_mesh_data_map->end();
70 if (iter->second == data) {
75 while (iter != _tri_mesh_data_map->end()) {
76 _tri_mesh_data_map->erase(iter);
78 for (iter = _tri_mesh_data_map->begin();
79 iter != _tri_mesh_data_map->end();
81 if (iter->second == data) {
93 OdeTriMeshData(
const NodePath& model,
bool use_normals) :
94 _id(dGeomTriMeshDataCreate()),
100 odetrimeshdata_cat.debug() << get_type() <<
"(" << _id <<
")" <<
"\n";
102 process_model(model, use_normals);
104 write_faces(odetrimeshdata_cat.debug());
107 build_single(_vertices,
sizeof(StridedVertex), _num_vertices,
108 _faces, _num_faces * 3,
sizeof(StridedTri));
110 build_single1(_vertices,
sizeof(StridedVertex), _num_vertices,
111 _faces, _num_faces * 3,
sizeof(StridedTri),
125 odetrimeshdata_cat.debug() <<
"~" << get_type() <<
"(" << _id <<
")" <<
"\n";
127 if (_vertices != 0) {
128 PANDA_FREE_ARRAY(_vertices);
133 PANDA_FREE_ARRAY(_faces);
140 PANDA_FREE_ARRAY(_normals);
144 void OdeTriMeshData::
146 odetrimeshdata_cat.debug() << get_type() <<
"::destroy(" << _id <<
")" <<
"\n";
148 dGeomTriMeshDataDestroy(_id);
155 void OdeTriMeshData::
159 void OdeTriMeshData::
160 process_model(
const NodePath& model,
bool &use_normals) {
162 if (odetrimeshdata_cat.is_debug()) {
163 odetrimeshdata_cat.debug()
164 <<
"process_model(" << model <<
")" <<
"\n";
171 if (model.
node()->get_type() == GeomNode::get_class_type()) {
176 analyze((
GeomNode*)geomNodePaths[i].node());
179 odetrimeshdata_cat.debug() <<
"Found " << _num_vertices <<
" vertices.\n";
180 odetrimeshdata_cat.debug() <<
"Found " << _num_faces <<
" faces.\n";
182 _vertices = (StridedVertex *)PANDA_MALLOC_ARRAY(_num_vertices *
sizeof(StridedVertex));
183 _faces = (StridedTri *)PANDA_MALLOC_ARRAY(_num_faces *
sizeof(StridedTri));
185 _num_vertices = 0, _num_faces = 0;
188 process_geom_node((
GeomNode*)geomNodePaths[i].node());
189 odetrimeshdata_cat.debug() <<
"_num_vertices now at " << _num_vertices <<
"\n";
192 odetrimeshdata_cat.debug() <<
"Filled " << _num_faces <<
" triangles(" \
193 << _num_vertices <<
" vertices)\n";
196 void OdeTriMeshData::
197 process_geom_node(
const GeomNode *geomNode) {
198 ostream &out = odetrimeshdata_cat.debug();
199 out.width(2); out <<
"" <<
"process_geom_node(" << *geomNode <<
")" <<
"\n";
200 for (
int i = 0; i < geomNode->get_num_geoms(); ++i) {
201 process_geom(geomNode->get_geom(i));
205 void OdeTriMeshData::
206 process_geom(
const Geom *geom) {
207 ostream &out = odetrimeshdata_cat.debug();
208 out.width(4); out <<
"" <<
"process_geom(" << *geom <<
")" <<
"\n";
209 if (geom->get_primitive_type() != Geom::PT_polygons) {
215 for (
int i = 0; i < geom->get_num_primitives(); ++i) {
216 process_primitive(geom->get_primitive(i), vData);
220 void OdeTriMeshData::
228 ostream &out = odetrimeshdata_cat.debug();
229 out.width(6); out <<
"" <<
"process_primitive(" << *dPrimitive <<
")" <<
"\n";
232 if (dPrimitive->get_type() == GeomTriangles::get_class_type()) {
233 for (
int i = 0; i < dPrimitive->get_num_primitives(); i++, _num_faces++) {
235 int e = dPrimitive->get_primitive_end(i);
236 out.width(8); out <<
"" <<
"primitive " << i <<
":" <<
"\n";
237 for (
int j = s, m = 0; j < e; j++, m++, _num_vertices++) {
238 int vRowIndex = dPrimitive->get_vertex(j);
239 vReader.set_row_unsafe(vRowIndex);
240 nReader.set_row_unsafe(vRowIndex);
241 vertex = vReader.get_data3f();
243 _faces[_num_faces].Indices[m] = _num_vertices;
245 _vertices[_num_vertices].Vertex[0] = vertex[0];
246 _vertices[_num_vertices].Vertex[1] = vertex[1];
247 _vertices[_num_vertices].Vertex[2] = vertex[2];
249 out.width(10); out <<
"" <<
"vertex " << j <<
" has: pos(" \
250 << vertex <<
") normal(" <<
"normal" <<
")" <<
"\n";
253 }
else if (dPrimitive->get_type() == GeomTristrips::get_class_type()){
254 for (
int i = 0; i < dPrimitive->get_num_primitives(); i++, _num_faces++) {
255 int s = dPrimitive->get_primitive_start(i);
256 int e = dPrimitive->get_primitive_end(i);
257 out.width(8); out <<
"" <<
"primitive " << i <<
":" <<
"\n";
258 for (
int j = s, m = 0; j < e; j++, m++, _num_vertices++) {
259 int vRowIndex = dPrimitive->get_vertex(j);
260 vReader.set_row_unsafe(vRowIndex);
261 nReader.set_row_unsafe(vRowIndex);
262 vertex = vReader.get_data3f();
265 _vertices[_num_vertices].Vertex[0] = vertex[0];
266 _vertices[_num_vertices].Vertex[1] = vertex[1];
267 _vertices[_num_vertices].Vertex[2] = vertex[2];
268 out.width(10); out <<
"" <<
"vertex " << j <<
" has: pos(" \
269 << vertex <<
") normal(" <<
"normal" <<
")" <<
"\n";
271 _faces[_num_faces].Indices[m] = _num_vertices;
275 _faces[_num_faces].Indices[0] = _num_vertices-1;
276 _faces[_num_faces].Indices[1] = _num_vertices-2;
277 _faces[_num_faces].Indices[2] = _num_vertices;
279 _faces[_num_faces].Indices[0] = _num_vertices-2;
280 _faces[_num_faces].Indices[1] = _num_vertices-1;
281 _faces[_num_faces].Indices[2] = _num_vertices;
289 void OdeTriMeshData::
292 analyze(geomNode->get_geom(i));
296 void OdeTriMeshData::
297 analyze(
const Geom *geom) {
302 for (
int i = 0; i < geom->get_num_primitives(); ++i) {
303 analyze(geom->get_primitive(i));
307 void OdeTriMeshData::
315 void OdeTriMeshData::
316 write_faces(ostream &out)
const {
318 for (
unsigned int i = 0; i < _num_faces; ++i) {
319 out.width(2); out <<
"Face " << i <<
":\n";
320 for (
int j = 0; j < 3; ++j) {
322 out <<
"(" << _vertices[_faces[i].Indices[j]].Vertex[0] \
323 <<
", " << _vertices[_faces[i].Indices[j]].Vertex[1] \
324 <<
", " << _vertices[_faces[i].Indices[j]].Vertex[2] <<
")\n" ;
329 void OdeTriMeshData::
330 write(ostream &out,
unsigned int indent)
const {
331 out.width(indent); out <<
"" << get_type() <<
"(id = " << _id <<
") : " \
332 <<
"" <<
"Vertices: " << (_id ? _num_vertices : 0) <<
", " \
333 <<
"" <<
"Triangles: " << (_id ? _num_faces : 0);
int get_primitive_num_faces(int n) const
Returns the number of triangles or other fundamental type (such as line segments) represented by the ...
This is the base class for all three-component vectors and points.
This is our own Panda specialization on the default STL map.
void add_path(const NodePath &node_path)
Adds a new NodePath to the collection.
int get_primitive_num_vertices(int n) const
Returns the number of vertices used by the nth primitive.
bool is_empty() const
Returns true if the NodePath contains no nodes.
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
PrimitiveType get_primitive_type() const
Returns the fundamental primitive type that is common to all GeomPrimitives added within the Geom...
int get_num_geoms() const
Returns the number of geoms in the node.
int get_primitive_start(int n) const
Returns the element within the _vertices list at which the nth primitive starts.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
int get_num_paths() const
Returns the number of NodePaths in the collection.
int get_num_primitives() const
Returns the number of individual primitives stored within this object.
PandaNode * node() const
Returns the referenced node of the path.
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
TypeHandle is the identifier used to differentiate C++ class types.
NodePathCollection find_all_matches(const string &path) const
Returns the complete set of all NodePaths that begin with this NodePath and can be extended by path...
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
A node that holds Geom objects, renderable pieces of geometry.
This is a set of zero or more NodePaths.