Browse Source

better color choices for showCollisions

David Rose 21 years ago
parent
commit
1d8b862db4

+ 1 - 0
panda/src/collide/collisionLine.cxx

@@ -104,6 +104,7 @@ fill_viz_geom() {
   line->set_num_prims(1);
 
   _viz_geom->add_geom(line, get_other_viz_state());
+  _bounds_viz_geom->add_geom(line, get_other_bounds_viz_state());
 }
 
 ////////////////////////////////////////////////////////////////////

+ 2 - 2
panda/src/collide/collisionNode.cxx

@@ -224,7 +224,7 @@ cull_callback(CullTraverser *trav, CullTraverserData &data) {
   Solids::iterator si;
   for (si = _solids.begin(); si != _solids.end(); ++si) {
     CollisionSolid *solid = (*si);
-    PT(PandaNode) node = solid->get_viz(data);
+    PT(PandaNode) node = solid->get_viz(data, false);
     if (node != (PandaNode *)NULL) {
       CullTraverserData next_data(data, node);
 
@@ -246,7 +246,7 @@ cull_callback(CullTraverser *trav, CullTraverserData &data) {
 
     for (si = _solids.begin(); si != _solids.end(); ++si) {
       CollisionSolid *solid = (*si);
-      PT(PandaNode) node = solid->get_viz(data);
+      PT(PandaNode) node = solid->get_viz(data, false);
       if (node != (PandaNode *)NULL) {
         CullTraverserData next_data(data, node);
 

+ 2 - 0
panda/src/collide/collisionPlane.cxx

@@ -334,6 +334,8 @@ fill_viz_geom() {
 
   _viz_geom->add_geom(quad, get_solid_viz_state());
   _viz_geom->add_geom(quad, get_wireframe_viz_state());
+  _bounds_viz_geom->add_geom(quad, get_solid_bounds_viz_state());
+  _bounds_viz_geom->add_geom(quad, get_wireframe_bounds_viz_state());
 }
 
 ////////////////////////////////////////////////////////////////////

+ 19 - 10
panda/src/collide/collisionPolygon.cxx

@@ -275,13 +275,13 @@ get_collision_origin() const {
 //               made visible.
 ////////////////////////////////////////////////////////////////////
 PT(PandaNode) CollisionPolygon::
-get_viz(const CullTraverserData &data) const {
+get_viz(const CullTraverserData &data, bool bounds_only) const {
   const RenderAttrib *cpa_attrib =
     data._state->get_attrib(ClipPlaneAttrib::get_class_type());
   if (cpa_attrib == (const RenderAttrib *)NULL) {
     // Fortunately, the polygon is not clipped.  This is the normal,
     // easy case.
-    return CollisionSolid::get_viz(data);
+    return CollisionSolid::get_viz(data, bounds_only);
   }
 
   if (collide_cat.is_debug()) {
@@ -300,7 +300,7 @@ get_viz(const CullTraverserData &data) const {
   if (apply_clip_plane(new_points, cpa, data._net_transform)) {
     // All points are behind the clip plane; just draw the original
     // polygon.
-    return CollisionSolid::get_viz(data);
+    return CollisionSolid::get_viz(data, bounds_only);
   }
 
   if (new_points.empty()) {
@@ -309,10 +309,15 @@ get_viz(const CullTraverserData &data) const {
   }
 
   // Draw the clipped polygon.
-  PT(GeomNode) geom_node = new GeomNode("viz");
-  draw_polygon(geom_node, new_points);
+  PT(GeomNode) viz_geom_node = new GeomNode("viz");
+  PT(GeomNode) bounds_viz_geom_node = new GeomNode("bounds_viz");
+  draw_polygon(viz_geom_node, bounds_viz_geom_node, new_points);
 
-  return geom_node.p();
+  if (bounds_only) {
+    return bounds_viz_geom_node.p();
+  } else {
+    return viz_geom_node.p();
+  }
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -769,7 +774,7 @@ fill_viz_geom() {
     collide_cat.debug()
       << "Recomputing viz for " << *this << "\n";
   }
-  draw_polygon(_viz_geom, _points);
+  draw_polygon(_viz_geom, _bounds_viz_geom, _points);
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -796,7 +801,8 @@ compute_vectors(Points &points) {
 //               points.
 ////////////////////////////////////////////////////////////////////
 void CollisionPolygon::
-draw_polygon(GeomNode *geom_node, const CollisionPolygon::Points &points) const {
+draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
+             const CollisionPolygon::Points &points) const {
   if (points.size() < 3) {
     if (collide_cat.is_debug()) {
       collide_cat.debug()
@@ -821,8 +827,11 @@ draw_polygon(GeomNode *geom_node, const CollisionPolygon::Points &points) const
   polygon->set_num_prims(1);
   polygon->set_lengths(lengths);
 
-  geom_node->add_geom(polygon, ((CollisionPolygon *)this)->get_solid_viz_state());
-  geom_node->add_geom(polygon, ((CollisionPolygon *)this)->get_wireframe_viz_state());
+  viz_geom_node->add_geom(polygon, ((CollisionPolygon *)this)->get_solid_viz_state());
+  viz_geom_node->add_geom(polygon, ((CollisionPolygon *)this)->get_wireframe_viz_state());
+
+  bounds_viz_geom_node->add_geom(polygon, ((CollisionPolygon *)this)->get_solid_bounds_viz_state());
+  bounds_viz_geom_node->add_geom(polygon, ((CollisionPolygon *)this)->get_wireframe_bounds_viz_state());
 }
 
 

+ 4 - 2
panda/src/collide/collisionPolygon.h

@@ -61,7 +61,8 @@ public:
   virtual void xform(const LMatrix4f &mat);
   virtual LPoint3f get_collision_origin() const;
 
-  virtual PT(PandaNode) get_viz(const CullTraverserData &data) const;
+  virtual PT(PandaNode) get_viz(const CullTraverserData &data,
+                                bool bounds_only) const;
 
   virtual void output(ostream &out) const;
   virtual void write(ostream &out, int indent_level = 0) const;
@@ -94,7 +95,8 @@ private:
   typedef pvector<PointDef> Points;
 
   static void compute_vectors(Points &points);
-  void draw_polygon(GeomNode *geom_node, const Points &points) const;
+  void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
+                    const Points &points) const;
 
   bool point_is_inside(const LPoint2f &p, const Points &points) const;
   float dist_to_polygon(const LPoint2f &p, const Points &points) const;

+ 1 - 0
panda/src/collide/collisionRay.cxx

@@ -176,6 +176,7 @@ fill_viz_geom() {
   line->set_num_prims(1);
 
   _viz_geom->add_geom(line, get_other_viz_state());
+  _bounds_viz_geom->add_geom(line, get_other_bounds_viz_state());
 }
 
 ////////////////////////////////////////////////////////////////////

+ 1 - 0
panda/src/collide/collisionSegment.cxx

@@ -169,6 +169,7 @@ fill_viz_geom() {
   segment->set_num_prims(1);
 
   _viz_geom->add_geom(segment, get_other_viz_state());
+  _bounds_viz_geom->add_geom(segment, get_other_bounds_viz_state());
 }
 
 ////////////////////////////////////////////////////////////////////

+ 118 - 7
panda/src/collide/collisionSolid.cxx

@@ -108,17 +108,24 @@ xform(const LMatrix4f &mat) {
 //               made visible.
 ////////////////////////////////////////////////////////////////////
 PT(PandaNode) CollisionSolid::
-get_viz(const CullTraverserData &) const {
+get_viz(const CullTraverserData &, bool bounds_only) const {
   if ((_flags & F_viz_geom_stale) != 0) {
     if (_viz_geom == (GeomNode *)NULL) {
       ((CollisionSolid *)this)->_viz_geom = new GeomNode("viz");
+      ((CollisionSolid *)this)->_bounds_viz_geom = new GeomNode("bounds_viz");
     } else {
       _viz_geom->remove_all_geoms();
+      _bounds_viz_geom->remove_all_geoms();
     }
     ((CollisionSolid *)this)->fill_viz_geom();
     ((CollisionSolid *)this)->_flags &= ~F_viz_geom_stale;
   }
-  return _viz_geom.p();
+
+  if (bounds_only) {
+    return _bounds_viz_geom.p();
+  } else {
+    return _viz_geom.p();
+  }
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -388,34 +395,138 @@ get_wireframe_viz_state() {
        TransparencyAttrib::make(TransparencyAttrib::M_none));
   }
 
-  if (is_tangible()) {
+  if (!is_tangible()) {
+    static CPT(RenderState) intangible_state = (const RenderState *)NULL;
+    if (intangible_state == (const RenderState *)NULL) {
+      intangible_state = base_state->add_attrib
+        (ColorAttrib::make_flat(Colorf(1.0f, 1.0f, 0.0f, 1.0f)));
+    }
+    return intangible_state;
+
+  } else if (has_effective_normal()) {
+    static CPT(RenderState) fakenormal_state = (const RenderState *)NULL;
+    if (fakenormal_state == (const RenderState *)NULL) {
+      fakenormal_state = base_state->add_attrib
+        (ColorAttrib::make_flat(Colorf(0.0f, 0.0f, 1.0f, 1.0f)));
+    }
+    return fakenormal_state;
+
+  } else {
     static CPT(RenderState) tangible_state = (const RenderState *)NULL;
     if (tangible_state == (const RenderState *)NULL) {
       tangible_state = base_state->add_attrib
         (ColorAttrib::make_flat(Colorf(0.0f, 0.0f, 1.0f, 1.0f)));
     }
     return tangible_state;
+  }
+}
 
-  } else {
+
+////////////////////////////////////////////////////////////////////
+//     Function: CollisionSolid::get_other_viz_state
+//       Access: Protected
+//  Description: Returns a RenderState for rendering collision
+//               visualizations for things that are neither solid nor
+//               exactly wireframe, like rays and segments.
+////////////////////////////////////////////////////////////////////
+CPT(RenderState) CollisionSolid::
+get_other_viz_state() {
+  // Once someone asks for this pointer, we hold its reference count
+  // and never free it.
+  static CPT(RenderState) base_state = (const RenderState *)NULL;
+  if (base_state == (const RenderState *)NULL) {
+    base_state = RenderState::make
+      (CullFaceAttrib::make(CullFaceAttrib::M_cull_clockwise),
+       RenderModeAttrib::make(RenderModeAttrib::M_filled),
+       TransparencyAttrib::make(TransparencyAttrib::M_alpha));
+  }
+
+  // We don't bother to make a distinction here between tangible and
+  // intangible.
+  return base_state;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CollisionSolid::get_solid_bounds_viz_state
+//       Access: Protected
+//  Description: Returns a RenderState for rendering collision
+//               visualizations in solid.  This automatically returns
+//               the appropriate state according to the setting of
+//               _tangible.
+////////////////////////////////////////////////////////////////////
+CPT(RenderState) CollisionSolid::
+get_solid_bounds_viz_state() {
+  // Once someone asks for this pointer, we hold its reference count
+  // and never free it.
+  static CPT(RenderState) base_state = (const RenderState *)NULL;
+  if (base_state == (const RenderState *)NULL) {
+    base_state = RenderState::make
+      (CullFaceAttrib::make(CullFaceAttrib::M_cull_clockwise),
+       RenderModeAttrib::make(RenderModeAttrib::M_filled),
+       TransparencyAttrib::make(TransparencyAttrib::M_alpha));
+  }
+
+  if (!is_tangible()) {
     static CPT(RenderState) intangible_state = (const RenderState *)NULL;
     if (intangible_state == (const RenderState *)NULL) {
       intangible_state = base_state->add_attrib
-        (ColorAttrib::make_flat(Colorf(1.0f, 1.0f, 0.0f, 1.0f)));
+        (ColorAttrib::make_flat(Colorf(1.0f, 1.0f, 0.5f, 0.3f)));
     }
     return intangible_state;
+
+  } else if (has_effective_normal()) {
+    static CPT(RenderState) fakenormal_state = (const RenderState *)NULL;
+    if (fakenormal_state == (const RenderState *)NULL) {
+      fakenormal_state = base_state->add_attrib
+        (ColorAttrib::make_flat(Colorf(0.5f, 0.5f, 1.0f, 0.3f)));
+    }
+    return fakenormal_state;
+
+  } else {
+    static CPT(RenderState) tangible_state = (const RenderState *)NULL;
+    if (tangible_state == (const RenderState *)NULL) {
+      tangible_state = base_state->add_attrib
+        (ColorAttrib::make_flat(Colorf(1.0f, 1.0f, 0.5f, 0.3f)));
+    }
+    return tangible_state;
   }
 }
 
 
 ////////////////////////////////////////////////////////////////////
-//     Function: CollisionSolid::get_other_viz_state
+//     Function: CollisionSolid::get_wireframe_bounds_viz_state
+//       Access: Protected
+//  Description: Returns a RenderState for rendering collision
+//               visualizations in wireframe.  This automatically returns
+//               the appropriate state according to the setting of
+//               _tangible.
+////////////////////////////////////////////////////////////////////
+CPT(RenderState) CollisionSolid::
+get_wireframe_bounds_viz_state() {
+  // Once someone asks for this pointer, we hold its reference count
+  // and never free it.
+  static CPT(RenderState) base_state = (const RenderState *)NULL;
+  if (base_state == (const RenderState *)NULL) {
+    base_state = RenderState::make
+      (CullFaceAttrib::make(CullFaceAttrib::M_cull_none),
+       RenderModeAttrib::make(RenderModeAttrib::M_wireframe),
+       TransparencyAttrib::make(TransparencyAttrib::M_none),
+       ColorAttrib::make_flat(Colorf(1.0f, 0.0f, 0.0f, 1.0f)));
+  }
+
+  return base_state;
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CollisionSolid::get_other_bounds_viz_state
 //       Access: Protected
 //  Description: Returns a RenderState for rendering collision
 //               visualizations for things that are neither solid nor
 //               exactly wireframe, like rays and segments.
 ////////////////////////////////////////////////////////////////////
 CPT(RenderState) CollisionSolid::
-get_other_viz_state() {
+get_other_bounds_viz_state() {
   // Once someone asks for this pointer, we hold its reference count
   // and never free it.
   static CPT(RenderState) base_state = (const RenderState *)NULL;

+ 6 - 1
panda/src/collide/collisionSolid.h

@@ -77,7 +77,8 @@ public:
 
   virtual void xform(const LMatrix4f &mat);
 
-  virtual PT(PandaNode) get_viz(const CullTraverserData &data) const;
+  virtual PT(PandaNode) get_viz(const CullTraverserData &data,
+                                bool bounds_only) const;
 
 PUBLISHED:
   virtual void output(ostream &out) const;
@@ -103,8 +104,12 @@ protected:
   CPT(RenderState) get_solid_viz_state();
   CPT(RenderState) get_wireframe_viz_state();
   CPT(RenderState) get_other_viz_state();
+  CPT(RenderState) get_solid_bounds_viz_state();
+  CPT(RenderState) get_wireframe_bounds_viz_state();
+  CPT(RenderState) get_other_bounds_viz_state();
 
   PT(GeomNode) _viz_geom;
+  PT(GeomNode) _bounds_viz_geom;
 
 private:
   LVector3f _effective_normal;

+ 1 - 0
panda/src/collide/collisionSphere.cxx

@@ -350,6 +350,7 @@ fill_viz_geom() {
   sphere->set_num_prims(num_slices);
 
   _viz_geom->add_geom(sphere, get_solid_viz_state());
+  _bounds_viz_geom->add_geom(sphere, get_solid_bounds_viz_state());
 }
 
 ////////////////////////////////////////////////////////////////////

+ 1 - 1
panda/src/collide/collisionTube.cxx

@@ -436,7 +436,7 @@ fill_viz_geom() {
   tube->set_lengths(lengths);
 
   _viz_geom->add_geom(tube, get_solid_viz_state());
-  //_viz_geom->add_geom(tube, get_wireframe_viz_state());
+  _bounds_viz_geom->add_geom(tube, get_solid_bounds_viz_state());
 }
 
 ////////////////////////////////////////////////////////////////////

+ 6 - 34
panda/src/collide/collisionVisualizer.cxx

@@ -145,19 +145,14 @@ cull_callback(CullTraverser *trav, CullTraverserData &data) {
       // tests that are being made.
       const CollisionSolid *solid = (*si).first;
       const SolidInfo &solid_info = (*si).second;
-      PT(PandaNode) node = solid->get_viz(xform_data);
+      bool was_detected = (solid_info._detected_count > 0);
+      PT(PandaNode) node = solid->get_viz(xform_data, !was_detected);
       if (node != (PandaNode *)NULL) {
         CullTraverserData next_data(xform_data, node);
         
         // We don't want to inherit the render state from above for
-        // these guys.  Instead, we choose the state according to
-        // whether a collision was detected or not.
-        if (solid_info._detected_count > 0) {
-          next_data._state = get_detected_state();
-        } else {
-          next_data._state = get_tested_state();
-        }
-        
+        // these guys.
+        next_data._state = get_viz_state();
         trav->traverse(next_data);
       }
     }
@@ -287,13 +282,13 @@ collision_tested(const CollisionEntry &entry, bool detected) {
 
 
 ////////////////////////////////////////////////////////////////////
-//     Function: CollisionVisualizer::get_detected_state
+//     Function: CollisionVisualizer::get_viz_state
 //       Access: Private
 //  Description: Returns a RenderState suitable for rendering the
 //               collision solids with which a collision was detected.
 ////////////////////////////////////////////////////////////////////
 CPT(RenderState) CollisionVisualizer::
-get_detected_state() {
+get_viz_state() {
   // Once someone asks for this pointer, we hold its reference count
   // and never free it.
   static CPT(RenderState) state = (const RenderState *)NULL;
@@ -305,27 +300,4 @@ get_detected_state() {
   return state;
 }
 
-////////////////////////////////////////////////////////////////////
-//     Function: CollisionVisualizer::get_tested_state
-//       Access: Private
-//  Description: Returns a RenderState suitable for rendering the
-//               collision solids with which a collision was tested,
-//               but no collision was detected..
-////////////////////////////////////////////////////////////////////
-CPT(RenderState) CollisionVisualizer::
-get_tested_state() {
-  // Once someone asks for this pointer, we hold its reference count
-  // and never free it.
-  static CPT(RenderState) state = (const RenderState *)NULL;
-  if (state == (const RenderState *)NULL) {
-    state = RenderState::make
-      (ColorScaleAttrib::make(LVecBase4f(1.0f, 1.0f, 0.5f, 0.5f)),
-       DepthOffsetAttrib::make());
-    state = state->add_attrib
-      (TransparencyAttrib::make(TransparencyAttrib::M_alpha), 1);
-  }
-
-  return state;
-}
-
 #endif  // DO_COLLISION_RECORDING

+ 1 - 2
panda/src/collide/collisionVisualizer.h

@@ -59,8 +59,7 @@ public:
   virtual void collision_tested(const CollisionEntry &entry, bool detected);
 
 private:
-  CPT(RenderState) get_detected_state();
-  CPT(RenderState) get_tested_state();
+  CPT(RenderState) get_viz_state();
 
 private:
   class SolidInfo {