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 odetrimeshdata_cat.debug() << get_class_type() <<
"::remove_data(" << data->get_id() <<
")" <<
"\n";
59 nassertv(_tri_mesh_data_map != (TriMeshDataMap *)NULL);
60 TriMeshDataMap::iterator iter;
62 for (iter = _tri_mesh_data_map->begin();
63 iter != _tri_mesh_data_map->end();
65 if ( iter->second == data ) {
70 while (iter != _tri_mesh_data_map->end()) {
71 _tri_mesh_data_map->erase(iter);
73 for (iter = _tri_mesh_data_map->begin();
74 iter != _tri_mesh_data_map->end();
76 if ( iter->second == data ) {
88 OdeTriMeshData(
const NodePath& model,
bool use_normals) :
89 _id(dGeomTriMeshDataCreate()),
95 odetrimeshdata_cat.debug() << get_type() <<
"(" << _id <<
")" <<
"\n";
97 process_model(model, use_normals);
99 write_faces(odetrimeshdata_cat.debug());
102 build_single(_vertices,
sizeof(StridedVertex), _num_vertices,
103 _faces, _num_faces * 3,
sizeof(StridedTri));
105 build_single1(_vertices,
sizeof(StridedVertex), _num_vertices,
106 _faces, _num_faces * 3,
sizeof(StridedTri),
120 odetrimeshdata_cat.debug() <<
"~" << get_type() <<
"(" << _id <<
")" <<
"\n";
122 if (_vertices != 0) {
123 PANDA_FREE_ARRAY(_vertices);
128 PANDA_FREE_ARRAY(_faces);
135 PANDA_FREE_ARRAY(_normals);
139 void OdeTriMeshData::
141 odetrimeshdata_cat.debug() << get_type() <<
"::destroy(" << _id <<
")" <<
"\n";
143 dGeomTriMeshDataDestroy(_id);
150 void OdeTriMeshData::
154 void OdeTriMeshData::
155 process_model(
const NodePath& model,
bool &use_normals) {
157 ostream &out = odetrimeshdata_cat.debug();
158 out <<
"process_model(" << model <<
")" <<
"\n";
161 if (model.node()->get_type() == GeomNode::get_class_type()) {
166 analyze((
GeomNode*)geomNodePaths[i].node());
169 odetrimeshdata_cat.debug() <<
"Found " << _num_vertices <<
" vertices.\n";
170 odetrimeshdata_cat.debug() <<
"Found " << _num_faces <<
" faces.\n";
172 _vertices = (StridedVertex *)PANDA_MALLOC_ARRAY(_num_vertices *
sizeof(StridedVertex));
173 _faces = (StridedTri *)PANDA_MALLOC_ARRAY(_num_faces *
sizeof(StridedTri));
175 _num_vertices = 0, _num_faces = 0;
178 process_geom_node((
GeomNode*)geomNodePaths[i].node());
179 odetrimeshdata_cat.debug() <<
"_num_vertices now at " << _num_vertices <<
"\n";
182 odetrimeshdata_cat.debug() <<
"Filled " << _num_faces <<
" triangles(" \
183 << _num_vertices <<
" vertices)\n";
186 void OdeTriMeshData::
187 process_geom_node(
const GeomNode *geomNode) {
188 ostream &out = odetrimeshdata_cat.debug();
189 out.width(2); out <<
"" <<
"process_geom_node(" << *geomNode <<
")" <<
"\n";
190 for (
int i = 0; i < geomNode->get_num_geoms(); ++i) {
191 process_geom(geomNode->get_geom(i));
195 void OdeTriMeshData::
196 process_geom(
const Geom *geom) {
197 ostream &out = odetrimeshdata_cat.debug();
198 out.width(4); out <<
"" <<
"process_geom(" << *geom <<
")" <<
"\n";
199 if (geom->get_primitive_type() != Geom::PT_polygons) {
205 for (
int i = 0; i < geom->get_num_primitives(); ++i) {
206 process_primitive(geom->get_primitive(i), vData);
210 void OdeTriMeshData::
218 ostream &out = odetrimeshdata_cat.debug();
219 out.width(6); out << "" << "process_primitive(" << *dPrimitive << ")" << "\n";
222 if (dPrimitive->get_type() ==
GeomTriangles::get_class_type()) {
223 for (
int i = 0; i < dPrimitive->get_num_primitives(); i++, _num_faces++) {
225 int e = dPrimitive->get_primitive_end(i);
226 out.width(8); out <<
"" <<
"primitive " << i <<
":" <<
"\n";
227 for (
int j = s, m = 0; j < e; j++, m++, _num_vertices++) {
228 int vRowIndex = dPrimitive->get_vertex(j);
229 vReader.set_row_unsafe(vRowIndex);
230 nReader.set_row_unsafe(vRowIndex);
231 vertex = vReader.get_data3f();
233 _faces[_num_faces].Indices[m] = _num_vertices;
235 _vertices[_num_vertices].Vertex[0] = vertex[0];
236 _vertices[_num_vertices].Vertex[1] = vertex[1];
237 _vertices[_num_vertices].Vertex[2] = vertex[2];
239 out.width(10); out <<
"" <<
"vertex " << j <<
" has: pos(" \
240 << vertex <<
") normal(" <<
"normal" <<
")" <<
"\n";
243 }
else if (dPrimitive->get_type() == GeomTristrips::get_class_type()){
244 for (
int i = 0; i < dPrimitive->get_num_primitives(); i++, _num_faces++) {
245 int s = dPrimitive->get_primitive_start(i);
246 int e = dPrimitive->get_primitive_end(i);
247 out.width(8); out <<
"" <<
"primitive " << i <<
":" <<
"\n";
248 for (
int j = s, m = 0; j < e; j++, m++, _num_vertices++) {
249 int vRowIndex = dPrimitive->get_vertex(j);
250 vReader.set_row_unsafe(vRowIndex);
251 nReader.set_row_unsafe(vRowIndex);
252 vertex = vReader.get_data3f();
255 _vertices[_num_vertices].Vertex[0] = vertex[0];
256 _vertices[_num_vertices].Vertex[1] = vertex[1];
257 _vertices[_num_vertices].Vertex[2] = vertex[2];
258 out.width(10); out <<
"" <<
"vertex " << j <<
" has: pos(" \
259 << vertex <<
") normal(" <<
"normal" <<
")" <<
"\n";
261 _faces[_num_faces].Indices[m] = _num_vertices;
265 _faces[_num_faces].Indices[0] = _num_vertices-1;
266 _faces[_num_faces].Indices[1] = _num_vertices-2;
267 _faces[_num_faces].Indices[2] = _num_vertices;
269 _faces[_num_faces].Indices[0] = _num_vertices-2;
270 _faces[_num_faces].Indices[1] = _num_vertices-1;
271 _faces[_num_faces].Indices[2] = _num_vertices;
279 void OdeTriMeshData::
282 analyze(geomNode->get_geom(i));
286 void OdeTriMeshData::
287 analyze(
const Geom *geom) {
293 analyze(geom->get_primitive(i));
297 void OdeTriMeshData::
305 void OdeTriMeshData::
306 write_faces(ostream &out)
const {
308 for (
unsigned int i = 0; i < _num_faces; ++i) {
309 out.width(2); out <<
"Face " << i <<
":\n";
310 for (
int j = 0; j < 3; ++j) {
312 out <<
"(" << _vertices[_faces[i].Indices[j]].Vertex[0] \
313 <<
", " << _vertices[_faces[i].Indices[j]].Vertex[1] \
314 <<
", " << _vertices[_faces[i].Indices[j]].Vertex[2] <<
")\n" ;
319 void OdeTriMeshData::
320 write(ostream &out,
unsigned int indent)
const {
321 out.width(indent); out <<
"" << get_type() <<
"(id = " << _id <<
") : " \
322 <<
"" <<
"Vertices: " << (_id ? _num_vertices : 0) <<
", " \
323 <<
"" <<
"Triangles: " << (_id ? _num_faces : 0);
int get_primitive_start(int n) const
Returns the element within the _vertices list at which the nth primitive starts.
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.
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_paths() const
Returns the number of NodePaths in the collection.
int get_primitive_num_vertices(int n) const
Returns the number of vertices used by the nth primitive.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
Defines a series of disconnected triangles.
int get_primitive_num_faces(int n) const
Returns the number of triangles or other fundamental type (such as line segments) represented by the ...
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 any NodePath in this collection and can be ...
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.
int get_num_geoms() const
Returns the number of geoms in the node.
int get_num_primitives() const
Returns the number of individual primitives stored within this object.
int get_num_primitives() const
Returns the number of GeomPrimitive objects stored within the Geom, each of which represents a number...
This is a set of zero or more NodePaths.