Panda3D
|
00001 // Filename: bulletDebugNode.cxx 00002 // Created by: enn0x (23Jan10) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "bulletDebugNode.h" 00016 00017 #include "geomLines.h" 00018 #include "geomVertexData.h" 00019 #include "geomTriangles.h" 00020 #include "geomVertexFormat.h" 00021 #include "geomVertexWriter.h" 00022 #include "omniBoundingVolume.h" 00023 00024 TypeHandle BulletDebugNode::_type_handle; 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: BulletDebugNode::Constructor 00028 // Access: Published 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 BulletDebugNode:: 00032 BulletDebugNode(const char *name) : GeomNode(name) { 00033 00034 _wireframe = true; 00035 _constraints = true; 00036 _bounds = false; 00037 _drawer._normals = true; 00038 00039 CPT (BoundingVolume) bounds = new OmniBoundingVolume(); 00040 set_bounds(bounds); 00041 set_final(true); 00042 set_overall_hidden(true); 00043 00044 // Lines 00045 { 00046 PT(GeomVertexData) vdata; 00047 PT(Geom) geom; 00048 PT(GeomLines) prim; 00049 00050 vdata = new GeomVertexData("", GeomVertexFormat::get_v3c4(), Geom::UH_stream); 00051 00052 prim = new GeomLines(Geom::UH_stream); 00053 prim->set_shade_model(Geom::SM_uniform); 00054 00055 geom = new Geom(vdata); 00056 geom->add_primitive(prim); 00057 00058 add_geom(geom); 00059 } 00060 00061 // Triangles 00062 { 00063 PT(GeomVertexData) vdata; 00064 PT(Geom) geom; 00065 PT(GeomTriangles) prim; 00066 00067 vdata = new GeomVertexData("", GeomVertexFormat::get_v3c4(), Geom::UH_stream); 00068 00069 prim = new GeomTriangles(Geom::UH_stream); 00070 prim->set_shade_model(Geom::SM_uniform); 00071 00072 geom = new Geom(vdata); 00073 geom->add_primitive(prim); 00074 00075 add_geom(geom); 00076 } 00077 } 00078 00079 //////////////////////////////////////////////////////////////////// 00080 // Function: BulletDebugNode::safe_to_flatten 00081 // Access: Public, Virtual 00082 // Description: Returns true if it is generally safe to flatten out 00083 // this particular kind of Node by duplicating 00084 // instances, false otherwise (for instance, a Camera 00085 // cannot be safely flattened, because the Camera 00086 // pointer itself is meaningful). 00087 //////////////////////////////////////////////////////////////////// 00088 bool BulletDebugNode:: 00089 safe_to_flatten() const { 00090 00091 return false; 00092 } 00093 00094 //////////////////////////////////////////////////////////////////// 00095 // Function: BulletDebugNode::safe_to_transform 00096 // Access: Public, Virtual 00097 // Description: Returns true if it is generally safe to transform 00098 // this particular kind of Node by calling the xform() 00099 // method, false otherwise. For instance, it's usually 00100 // a bad idea to attempt to xform a Character. 00101 //////////////////////////////////////////////////////////////////// 00102 bool BulletDebugNode:: 00103 safe_to_transform() const { 00104 00105 return false; 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: BulletDebugNode::safe_to_modify_transform 00110 // Access: Public, Virtual 00111 // Description: Returns true if it is safe to automatically adjust 00112 // the transform on this kind of node. Usually, this is 00113 // only a bad idea if the user expects to find a 00114 // particular transform on the node. 00115 // 00116 // ModelNodes with the preserve_transform flag set are 00117 // presently the only kinds of nodes that should not 00118 // have their transform even adjusted. 00119 //////////////////////////////////////////////////////////////////// 00120 bool BulletDebugNode:: 00121 safe_to_modify_transform() const { 00122 00123 return false; 00124 } 00125 00126 //////////////////////////////////////////////////////////////////// 00127 // Function: BulletDebugNode::safe_to_combine 00128 // Access: Public, Virtual 00129 // Description: Returns true if it is generally safe to combine this 00130 // particular kind of PandaNode with other kinds of 00131 // PandaNodes of compatible type, adding children or 00132 // whatever. For instance, an LODNode should not be 00133 // combined with any other PandaNode, because its set of 00134 // children is meaningful. 00135 //////////////////////////////////////////////////////////////////// 00136 bool BulletDebugNode:: 00137 safe_to_combine() const { 00138 00139 return false; 00140 } 00141 00142 //////////////////////////////////////////////////////////////////// 00143 // Function: BulletDebugNode::safe_to_combine_children 00144 // Access: Public, Virtual 00145 // Description: Returns true if it is generally safe to combine the 00146 // children of this PandaNode with each other. For 00147 // instance, an LODNode's children should not be 00148 // combined with each other, because the set of children 00149 // is meaningful. 00150 //////////////////////////////////////////////////////////////////// 00151 bool BulletDebugNode:: 00152 safe_to_combine_children() const { 00153 00154 return false; 00155 } 00156 00157 //////////////////////////////////////////////////////////////////// 00158 // Function: BulletDebugNode::safe_to_flatten_below 00159 // Access: Public, Virtual 00160 // Description: Returns true if a flatten operation may safely 00161 // continue past this node, or false if nodes below this 00162 // node may not be molested. 00163 //////////////////////////////////////////////////////////////////// 00164 bool BulletDebugNode:: 00165 safe_to_flatten_below() const { 00166 00167 return false; 00168 } 00169 00170 //////////////////////////////////////////////////////////////////// 00171 // Function: BulletDebugNode::draw_mask_changed 00172 // Access: Published 00173 // Description: 00174 //////////////////////////////////////////////////////////////////// 00175 void BulletDebugNode:: 00176 draw_mask_changed() { 00177 00178 if (is_overall_hidden()) { 00179 _drawer.setDebugMode(DebugDraw::DBG_NoDebug); 00180 } 00181 else { 00182 int mode = DebugDraw::DBG_DrawText | 00183 DebugDraw::DBG_DrawFeaturesText | 00184 DebugDraw::DBG_DrawContactPoints; 00185 00186 if (_wireframe) { 00187 mode |= DebugDraw::DBG_DrawWireframe; 00188 } 00189 00190 if (_constraints) { 00191 mode |= DebugDraw::DBG_DrawConstraints; 00192 mode |= DebugDraw::DBG_DrawConstraintLimits; 00193 } 00194 00195 if (_bounds) { 00196 mode |= DebugDraw::DBG_DrawAabb; 00197 } 00198 00199 _drawer.setDebugMode(mode); 00200 } 00201 } 00202 00203 //////////////////////////////////////////////////////////////////// 00204 // Function: BulletDebugNode::sync_b2p 00205 // Access: Private 00206 // Description: 00207 //////////////////////////////////////////////////////////////////// 00208 void BulletDebugNode:: 00209 sync_b2p(btDynamicsWorld *world) { 00210 00211 if (is_overall_hidden()) return; 00212 00213 nassertv(get_num_geoms() == 2); 00214 00215 // Collect debug geometry data 00216 _drawer._lines.clear(); 00217 _drawer._triangles.clear(); 00218 00219 world->debugDrawWorld(); 00220 00221 // Get inverse of this node's net transform 00222 NodePath np = NodePath::any_path((PandaNode *)this); 00223 LMatrix4 m = np.get_net_transform()->get_mat(); 00224 m.invert_in_place(); 00225 00226 // Render lines 00227 { 00228 PT(GeomVertexData) vdata; 00229 PT(Geom) geom; 00230 PT(GeomLines) prim; 00231 00232 vdata = new GeomVertexData("", GeomVertexFormat::get_v3c4(), Geom::UH_stream); 00233 00234 prim = new GeomLines(Geom::UH_stream); 00235 prim->set_shade_model(Geom::SM_uniform); 00236 00237 GeomVertexWriter vwriter = GeomVertexWriter(vdata, InternalName::get_vertex()); 00238 GeomVertexWriter cwriter = GeomVertexWriter(vdata, InternalName::get_color()); 00239 00240 int v = 0; 00241 00242 pvector<Line>::const_iterator lit; 00243 for (lit = _drawer._lines.begin(); lit != _drawer._lines.end(); lit++) { 00244 Line line = *lit; 00245 00246 vwriter.add_data3(m.xform_point(line._p0)); 00247 vwriter.add_data3(m.xform_point(line._p1)); 00248 cwriter.add_data4(LVecBase4(line._color)); 00249 cwriter.add_data4(LVecBase4(line._color)); 00250 00251 prim->add_vertex(v++); 00252 prim->add_vertex(v++); 00253 prim->close_primitive(); 00254 } 00255 00256 geom = new Geom(vdata); 00257 geom->add_primitive(prim); 00258 00259 set_geom(0, geom); 00260 } 00261 00262 // Render triangles 00263 { 00264 PT(GeomVertexData) vdata; 00265 PT(Geom) geom; 00266 PT(GeomTriangles) prim; 00267 00268 vdata = new GeomVertexData("", GeomVertexFormat::get_v3c4(), Geom::UH_stream); 00269 00270 prim = new GeomTriangles(Geom::UH_stream); 00271 prim->set_shade_model(Geom::SM_uniform); 00272 00273 GeomVertexWriter vwriter = GeomVertexWriter(vdata, InternalName::get_vertex()); 00274 GeomVertexWriter cwriter = GeomVertexWriter(vdata, InternalName::get_color()); 00275 00276 int v = 0; 00277 00278 pvector<Triangle>::const_iterator tit; 00279 for (tit = _drawer._triangles.begin(); tit != _drawer._triangles.end(); tit++) { 00280 Triangle tri = *tit; 00281 00282 vwriter.add_data3(m.xform_point(tri._p0)); 00283 vwriter.add_data3(m.xform_point(tri._p1)); 00284 vwriter.add_data3(m.xform_point(tri._p2)); 00285 cwriter.add_data4(LVecBase4(tri._color)); 00286 cwriter.add_data4(LVecBase4(tri._color)); 00287 cwriter.add_data4(LVecBase4(tri._color)); 00288 00289 prim->add_vertex(v++); 00290 prim->add_vertex(v++); 00291 prim->add_vertex(v++); 00292 prim->close_primitive(); 00293 } 00294 00295 geom = new Geom(vdata); 00296 geom->add_primitive(prim); 00297 00298 set_geom(1, geom); 00299 } 00300 } 00301 00302 //////////////////////////////////////////////////////////////////// 00303 // Function: BulletDebugNode::DebugDraw::setDebugMode 00304 // Access: Public 00305 // Description: 00306 //////////////////////////////////////////////////////////////////// 00307 void BulletDebugNode::DebugDraw:: 00308 setDebugMode(int mode) { 00309 00310 _mode = mode; 00311 } 00312 00313 //////////////////////////////////////////////////////////////////// 00314 // Function: BulletDebugNode::DebugDraw::getDebugMode 00315 // Access: Public 00316 // Description: 00317 //////////////////////////////////////////////////////////////////// 00318 int BulletDebugNode::DebugDraw:: 00319 getDebugMode() const { 00320 00321 return _mode; 00322 } 00323 00324 //////////////////////////////////////////////////////////////////// 00325 // Function: BulletDebugNode::DebugDraw::reportErrorWarning 00326 // Access: Public 00327 // Description: 00328 //////////////////////////////////////////////////////////////////// 00329 void BulletDebugNode::DebugDraw:: 00330 reportErrorWarning(const char *warning) { 00331 00332 bullet_cat.error() << warning << endl; 00333 } 00334 00335 //////////////////////////////////////////////////////////////////// 00336 // Function: BulletDebugNode::DebugDraw::drawLine 00337 // Access: Public 00338 // Description: 00339 //////////////////////////////////////////////////////////////////// 00340 void BulletDebugNode::DebugDraw:: 00341 drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color) { 00342 00343 PN_stdfloat r = color.getX(); 00344 PN_stdfloat g = color.getY(); 00345 PN_stdfloat b = color.getZ(); 00346 00347 // Hack to get rid of triangle normals. The hack is based on the 00348 // assumption that only normals are drawn in yellow. 00349 if (_normals==false && r==1.0f && g==1.0f && b==0.0f) return; 00350 00351 Line line; 00352 00353 line._p0 = LVecBase3((PN_stdfloat)from.getX(), 00354 (PN_stdfloat)from.getY(), 00355 (PN_stdfloat)from.getZ()); 00356 line._p1 = LVecBase3((PN_stdfloat)to.getX(), 00357 (PN_stdfloat)to.getY(), 00358 (PN_stdfloat)to.getZ()); 00359 line._color = UnalignedLVecBase4((PN_stdfloat)r, 00360 (PN_stdfloat)g, 00361 (PN_stdfloat)b, 1.0f); 00362 00363 _lines.push_back(line); 00364 } 00365 00366 //////////////////////////////////////////////////////////////////// 00367 // Function: BulletDebugNode::DebugDraw::drawTriangle 00368 // Access: Public 00369 // Description: 00370 //////////////////////////////////////////////////////////////////// 00371 void BulletDebugNode::DebugDraw:: 00372 drawTriangle(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2, const btVector3 &color, btScalar) { 00373 00374 btScalar r = color.getX(); 00375 btScalar g = color.getY(); 00376 btScalar b = color.getZ(); 00377 00378 Triangle tri; 00379 00380 tri._p0 = LVecBase3((PN_stdfloat)v0.getX(), 00381 (PN_stdfloat)v0.getY(), 00382 (PN_stdfloat)v0.getZ()); 00383 00384 tri._p1 = LVecBase3((PN_stdfloat)v1.getX(), 00385 (PN_stdfloat)v1.getY(), 00386 (PN_stdfloat)v1.getZ()); 00387 00388 tri._p2 = LVecBase3((PN_stdfloat)v2.getX(), 00389 (PN_stdfloat)v2.getY(), 00390 (PN_stdfloat)v2.getZ()); 00391 00392 tri._color = UnalignedLVecBase4((PN_stdfloat)r, 00393 (PN_stdfloat)g, 00394 (PN_stdfloat)b, 1.0f); 00395 00396 _triangles.push_back(tri); 00397 00398 00399 // Draw the triangle's normal 00400 /* 00401 btVector3 x1 = v1 - v0; 00402 btVector3 x2 = v2 - v0; 00403 btVector3 normal = v1.cross(v2).normalize(); 00404 00405 btVector3 from = (v0 + v1 + v2) * 0.3333; 00406 btVector3 to = from + normal; 00407 drawLine(from, to, color); 00408 */ 00409 } 00410 00411 //////////////////////////////////////////////////////////////////// 00412 // Function: BulletDebugNode::DebugDraw::drawTriangle 00413 // Access: Public 00414 // Description: 00415 //////////////////////////////////////////////////////////////////// 00416 void BulletDebugNode::DebugDraw:: 00417 drawTriangle(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2, const btVector3 &n0, const btVector3 &n1, const btVector3 &n2, const btVector3 &color, btScalar alpha) { 00418 00419 bullet_cat.debug() << "drawTriangle(2) - not yet implemented!" << endl; 00420 } 00421 00422 //////////////////////////////////////////////////////////////////// 00423 // Function: BulletDebugNode::DebugDraw::drawContactPoint 00424 // Access: Public 00425 // Description: 00426 //////////////////////////////////////////////////////////////////// 00427 void BulletDebugNode::DebugDraw:: 00428 drawContactPoint(const btVector3 &point, const btVector3 &normal, btScalar distance, int lifetime, const btVector3 &color) { 00429 00430 const btVector3 to = point + normal * distance; 00431 const btVector3 &from = point; 00432 00433 drawLine(from, to, color); 00434 } 00435 00436 //////////////////////////////////////////////////////////////////// 00437 // Function: BulletDebugNode::DebugDraw::draw3dText 00438 // Access: Public 00439 // Description: 00440 //////////////////////////////////////////////////////////////////// 00441 void BulletDebugNode::DebugDraw:: 00442 draw3dText(const btVector3 &location, const char *text) { 00443 00444 bullet_cat.debug() << "draw3dText - not yet implemented!" << endl; 00445 } 00446 00447 //////////////////////////////////////////////////////////////////// 00448 // Function: BulletDebugNode::DebugDraw::drawSphere 00449 // Access: Public 00450 // Description: 00451 //////////////////////////////////////////////////////////////////// 00452 void BulletDebugNode::DebugDraw:: 00453 drawSphere(btScalar radius, const btTransform &transform, const btVector3 &color) { 00454 00455 btVector3 center = transform.getOrigin(); 00456 00457 const btVector3 xoffs = transform.getBasis() * btVector3(1, 0, 0); 00458 const btVector3 yoffs = transform.getBasis() * btVector3(0, 1, 0); 00459 const btVector3 zoffs = transform.getBasis() * btVector3(0, 0, 1); 00460 00461 drawArc(center, xoffs, yoffs, radius, radius, 0, SIMD_2_PI, color, false, 10.0); 00462 drawArc(center, yoffs, zoffs, radius, radius, 0, SIMD_2_PI, color, false, 10.0); 00463 drawArc(center, zoffs, xoffs, radius, radius, 0, SIMD_2_PI, color, false, 10.0); 00464 } 00465