Browse Source

some fixes to make the portal node loaded as portal nodes

Asad M. Zaman 21 years ago
parent
commit
be718ced33

+ 3 - 1
panda/src/pgraph/config_pgraph.cxx

@@ -62,6 +62,7 @@
 #include "pandaNode.h"
 #include "planeNode.h"
 #include "pointLight.h"
+#include "portalNode.h"
 #include "portalClipper.h"
 #include "renderAttrib.h"
 #include "renderEffect.h"
@@ -208,6 +209,7 @@ init_libpgraph() {
   PandaNode::init_type();
   PlaneNode::init_type();
   PointLight::init_type();
+  PortalNode::init_type();
   PortalClipper::init_type();
   RenderAttrib::init_type();
   RenderEffect::init_type();
@@ -262,7 +264,7 @@ init_libpgraph() {
   PandaNode::register_with_read_factory();
   PlaneNode::register_with_read_factory();
   PointLight::register_with_read_factory();
-  //PortalClipper::register_with_read_factory();
+  PortalNode::register_with_read_factory();
   RenderEffects::register_with_read_factory();
   RenderModeAttrib::register_with_read_factory();
   RenderState::register_with_read_factory();

+ 20 - 24
panda/src/pgraph/cullTraverser.cxx

@@ -77,7 +77,7 @@ traverse(const NodePath &root, bool python_cull_control) {
   nassertv(_cull_handler != (CullHandler *)NULL);
   nassertv(_scene_setup != (SceneSetup *)NULL);
 
-  if (1) {
+  if (allow_portal_cull || python_cull_control) {
     // This _view_frustum is in cull_center space
     PT(GeometricBoundingVolume) vf = _view_frustum;
     pgraph_cat.debug() << "_view_frustum is " << *_view_frustum << "\n";
@@ -93,41 +93,37 @@ traverse(const NodePath &root, bool python_cull_control) {
       
     // This local_frustum is in camera space
     PortalClipper portal_viewer(local_frustum, _scene_setup);
-    if (allow_portal_cull || python_cull_control) {
-      portal_viewer.draw_camera_frustum();
-    }
+    portal_viewer.draw_camera_frustum();
 
-    if (allow_portal_cull || python_cull_control) {
-      // for each portal draw its frustum
-      for (int portal_idx=1; portal_idx<2; ++portal_idx) {
-        PT(BoundingVolume) reduced_frustum;
+    // for each portal draw its frustum
+    for (int portal_idx=1; portal_idx<2; ++portal_idx) {
+      PT(BoundingVolume) reduced_frustum;
+      
+      portal_viewer.prepare_portal(portal_idx);
+      portal_viewer.clip_portal(portal_idx);
+      if ((reduced_frustum = portal_viewer.get_reduced_frustum(portal_idx))) {
+        // This reduced frustum is in camera space
+        pgraph_cat.debug() << "got reduced frustum " << reduced_frustum << endl;
+        vf = DCAST(GeometricBoundingVolume, reduced_frustum);
         
-        portal_viewer.prepare_portal(portal_idx);
-        portal_viewer.clip_portal(portal_idx);
-        if ((reduced_frustum = portal_viewer.get_reduced_frustum(portal_idx))) {
-          // This reduced frustum is in camera space
-          pgraph_cat.debug() << "got reduced frustum " << reduced_frustum << endl;
-          vf = DCAST(GeometricBoundingVolume, reduced_frustum);
-          
-          // trasform it to cull_center space
-          CPT(TransformState) cull_center_transform = 
-            _scene_setup->get_cull_center().get_transform(_scene_setup->get_scene_root());
-          vf->xform(cull_center_transform->get_mat());
-        }
+        // trasform it to cull_center space
+        CPT(TransformState) cull_center_transform = 
+          _scene_setup->get_cull_center().get_transform(_scene_setup->get_scene_root());
+        vf->xform(cull_center_transform->get_mat());
       }
     }
     pgraph_cat.debug() << "vf is " << *vf << "\n";
-
+    
     CullTraverserData data(root, get_render_transform(),
                            TransformState::make_identity(),
                            _initial_state, _view_frustum, 
                            vf, _guard_band);
-
+    
     traverse(data);
     
     // finally add the lines to be drawn
     portal_viewer.draw_lines();
-      
+    
     // Render the frustum relative to the cull center.
     NodePath cull_center = _scene_setup->get_cull_center();
     CPT(TransformState) transform = cull_center.get_transform(root);
@@ -167,7 +163,7 @@ traverse(CullTraverserData &data) {
     // let me see the names, curious
     unsigned int loc = node->get_name().find("pTypeArchway");
     if (loc != string::npos) {
-      node->output(pgraph_cat.debug());
+      node->output(pgraph_cat.spam());
       pgraph_cat.spam() << endl;
       if (data._reduced_frustum) {
         pgraph_cat.debug() << "setting reduced frustum to this node\n";

+ 17 - 0
panda/src/pgraph/portalClipper.I

@@ -111,8 +111,25 @@ is_facing_camera()
 {
   Planef portal_plane(_coords[0], _coords[1], _coords[2]);
   Planef camera_plane(_hex_frustum->get_point(4), _hex_frustum->get_point(5), _hex_frustum->get_point(6));
+#if 0
+  // use the camera's near plane to calculate direction
   pgraph_cat.debug() << portal_plane.get_normal() << "; " << -camera_plane.get_normal() << endl;
   float direction = portal_plane.get_normal().dot(-camera_plane.get_normal());
   pgraph_cat.debug() << "Found direction of " << direction << endl;
   return (direction < _FACING_THRESHOLD);
+#else
+  // use the center of camera's near plane to the center of the
+  // portal_plane to calulate the direction
+  LPoint3f portal_center = (_coords[0] + _coords[1] + _coords[2] + _coords[3]) / 4;
+  LPoint3f camera_center = (_hex_frustum->get_point(4) + _hex_frustum->get_point(5) + _hex_frustum->get_point(6) + _hex_frustum->get_point(7)) / 4;
+  LVector3f camera_to_portal = portal_center - camera_center;
+  camera_to_portal.normalize();
+  pgraph_cat.debug() << "portal_center " << portal_center << "; camera_center " << camera_center << "; camera_to_portal " << camera_to_portal << endl;
+  float direction = camera_plane.get_normal().dot(camera_to_portal);
+  _color = Colorf(0,1,0,1);
+  move_to(camera_center);
+  draw_to(portal_center);
+#endif
+  pgraph_cat.debug() << "Found direction of " << direction << endl;
+  return (direction < _FACING_THRESHOLD);
 }

+ 33 - 54
panda/src/pgraph/portalClipper.cxx

@@ -236,6 +236,7 @@ prepare_portal(int idx)
   pgraph_cat.debug() << "creating portal clipper " << idx << endl;
 
   // walk the portal
+  _num_vert = 0;
   sprintf(portal_name, "**/portal%d", idx);
   NodePath portal_nodepath = _scene_setup->get_scene_root().find(portal_name);
   if (!portal_nodepath.is_empty()) {
@@ -255,63 +256,38 @@ prepare_portal(int idx)
     pgraph_cat.debug() << cmat << endl;
     
     // Get the geometry from the portal    
-    PandaNode *portal_node = portal_nodepath.node();
-    GeomNode *portal_geom = DCAST(GeomNode, portal_node);
+    PandaNode *node = portal_nodepath.node();
+    if (node->is_of_type(PortalNode::get_class_type())) {
+      PortalNode *portal_node = DCAST(PortalNode, node);
+      pgraph_cat.debug() << *portal_node << endl;
     
-    //portal_geom->write_verbose(pgraph_cat.debug(false), 0);
-    
-    int num_geoms = portal_geom->get_num_geoms();
-    pgraph_cat.debug() << "num geometry in portal " << num_geoms << endl;
-    
-    PTA_ushort index;
-    PT(Geom) geom = portal_geom->get_geom(0);
-    _num_vert = geom->get_num_vertices();
-    PTA_Vertexf coords;
-    geom->get_coords(coords, index);
-
-    /*    
-    pgraph_cat.debug() << "before transformation to camera space" << endl;
-    pgraph_cat.debug() << coords[0] << endl;
-    pgraph_cat.debug() << coords[1] << endl;
-    pgraph_cat.debug() << coords[2] << endl;
-    pgraph_cat.debug() << coords[3] << endl;
-    */
-    
-    _coords[0] = coords[0]*cmat;
-    _coords[1] = coords[1]*cmat;
-    _coords[2] = coords[3]*cmat;  // flip with 3rd vertex
-    _coords[3] = coords[2]*cmat;  // flip with 2nd vertex
-    
-    /*
-    pgraph_cat.debug() << "after transformation to camera space" << endl;
-    pgraph_cat.debug() << _coords[0] << endl;
-    pgraph_cat.debug() << _coords[1] << endl;
-    pgraph_cat.debug() << _coords[2] << endl;
-    pgraph_cat.debug() << _coords[3] << endl;
-    */
-    
-    //geom->write_verbose(pgraph_cat.debug(false), 0);
-
-    // check if facing camera
-    if (is_facing_camera()) {
-    
-      // ok, now lets add the near plane to this portal
-      _color = Colorf(1,0,0,1);
-      move_to(_coords[0]);
-      draw_to(_coords[1]);
-      draw_to(_coords[2]);
-      draw_to(_coords[3]);
-      draw_to(_coords[0]);
-    
-      pgraph_cat.debug() << "assembled portal" << idx << " frustum points" << endl;
-    }
-    else {
-      _num_vert = 0;
+      _coords[0] = portal_node->get_vertex(0)*cmat;
+      _coords[1] = portal_node->get_vertex(1)*cmat;
+      _coords[2] = portal_node->get_vertex(2)*cmat;
+      _coords[3] = portal_node->get_vertex(3)*cmat;
+      
+      pgraph_cat.debug() << "after transformation to camera space" << endl;
+      pgraph_cat.debug() << _coords[0] << endl;
+      pgraph_cat.debug() << _coords[1] << endl;
+      pgraph_cat.debug() << _coords[2] << endl;
+      pgraph_cat.debug() << _coords[3] << endl;
+      
+      // check if facing camera
+      if (is_facing_camera()) {
+        
+        // ok, now lets add the near plane to this portal
+        _color = Colorf(1,0,0,1);
+        move_to(_coords[0]);
+        draw_to(_coords[1]);
+        draw_to(_coords[2]);
+        draw_to(_coords[3]);
+        draw_to(_coords[0]);
+        
+        pgraph_cat.debug() << "assembled portal" << idx << " frustum points" << endl;
+        _num_vert = portal_node->get_num_vertices();
+      }
     }
   }
-  else {
-    _num_vert = 0;
-  }
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -325,6 +301,9 @@ clip_portal(int idx)
 {
   int num_planes = _hex_frustum->get_num_planes();
 
+  if (!_num_vert)
+    return;
+
   /*
   pgraph_cat.debug() << "Number of planes " << num_planes << endl;
 

+ 18 - 0
panda/src/pgraph/portalNode.I

@@ -167,3 +167,21 @@ get_vertex(int n) const {
   nassertr(n >= 0 && n < (int)_vertices.size(), LPoint3f::zero());
   return _vertices[n];
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: PortalNode::set_zone
+//       Access: Published
+//  Description: Sets the zone that this portal belongs to
+////////////////////////////////////////////////////////////////////
+INLINE void PortalNode::set_zone(PandaNode *zone) {
+  _zone_node = zone;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PortalNode::set_zone
+//       Access: Published
+//  Description: Sets the zone that this portal belongs to
+////////////////////////////////////////////////////////////////////
+INLINE PandaNode *PortalNode::get_zone() const {
+  return _zone_node;
+}

+ 17 - 0
panda/src/pgraph/portalNode.cxx

@@ -276,6 +276,13 @@ register_with_read_factory() {
 void PortalNode::
 write_datagram(BamWriter *manager, Datagram &dg) {
   PandaNode::write_datagram(manager, dg);
+
+  dg.add_uint16(_vertices.size());
+  for (Vertices::const_iterator vi = _vertices.begin();
+       vi != _vertices.end();
+       ++vi) {
+    (*vi).write_datagram(dg);
+  }
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -323,7 +330,17 @@ void PortalNode::
 fillin(DatagramIterator &scan, BamReader *manager) {
   PandaNode::fillin(scan, manager);
 
+  int num_vertices = scan.get_uint16();
+  _vertices.reserve(num_vertices);
+  for (int i = 0; i < num_vertices; i++) {
+    LPoint3f vertex;
+    vertex.read_datagram(scan);
+    _vertices.push_back(vertex);
+  }
+
+  /*
   _from_portal_mask.set_word(scan.get_uint32());
   _into_portal_mask.set_word(scan.get_uint32());
   _flags = scan.get_uint8();
+  */
 }

+ 5 - 0
panda/src/pgraph/portalNode.h

@@ -68,6 +68,9 @@ PUBLISHED:
   INLINE int get_num_vertices() const;
   INLINE const LPoint3f &get_vertex(int n) const;
 
+  INLINE void set_zone(PandaNode *zone);
+  INLINE PandaNode *get_zone() const;
+
 protected:
   virtual BoundingVolume *recompute_bound();
   virtual BoundingVolume *recompute_internal_bound();
@@ -90,6 +93,8 @@ private:
   typedef pvector<LPoint3f> Vertices;
   Vertices _vertices;
 
+  PT(PandaNode) _zone_node;  // This is the zone it belongs to
+
 public:
   static void register_with_read_factory();
   virtual void write_datagram(BamWriter *manager, Datagram &dg);