From c3b2cefc72133f41540904986f002b4554586ef7 Mon Sep 17 00:00:00 2001 From: "Asad M. Zaman" Date: Wed, 2 Jun 2004 17:04:38 +0000 Subject: [PATCH] save as of june 1st --- panda/src/pgraph/cullTraverser.cxx | 72 +++++++++++------ panda/src/pgraph/cullTraverser.h | 4 +- panda/src/pgraph/cullTraverserData.I | 1 - panda/src/pgraph/cullTraverserData.cxx | 2 +- panda/src/pgraph/portalClipper.cxx | 103 ++++++++++++------------- panda/src/pgraph/portalClipper.h | 6 +- panda/src/pgraph/portalNode.I | 47 +++++++++-- panda/src/pgraph/portalNode.cxx | 36 +++++---- panda/src/pgraph/portalNode.h | 15 +++- 9 files changed, 178 insertions(+), 108 deletions(-) diff --git a/panda/src/pgraph/cullTraverser.cxx b/panda/src/pgraph/cullTraverser.cxx index 0f43d09c1c..7296aa640f 100644 --- a/panda/src/pgraph/cullTraverser.cxx +++ b/panda/src/pgraph/cullTraverser.cxx @@ -95,31 +95,31 @@ traverse(const NodePath &root, bool python_cull_control) { PortalClipper portal_viewer(local_frustum, _scene_setup); portal_viewer.draw_camera_frustum(); - // for each portal draw its frustum - for (int portal_idx=2; portal_idx<3; ++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); +#if 0 + PT(BoundingVolume) reduced_frustum; + + // right now it is returning only one reduced frustum + portal_viewer.prepare_portal(); + //portal_viewer.clip_portal(portal_idx); + if ((reduced_frustum = portal_viewer.get_reduced_frustum())) { + // 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"; +#endif CullTraverserData data(root, get_render_transform(), TransformState::make_identity(), _initial_state, _view_frustum, vf, _guard_band); - traverse(data); + traverse(data, &portal_viewer); // finally add the lines to be drawn portal_viewer.draw_lines(); @@ -132,6 +132,8 @@ traverse(const NodePath &root, bool python_cull_control) { my_data._render_transform = my_data._render_transform->compose(transform); traverse(my_data); pgraph_cat.debug() << "finished portal culling\n"; + pgraph_cat.debug() << "**********end*********\n"; + } else { CullTraverserData data(root, get_render_transform(), TransformState::make_identity(), @@ -150,7 +152,7 @@ traverse(const NodePath &root, bool python_cull_control) { // has not yet been converted into the node's space. //////////////////////////////////////////////////////////////////// void CullTraverser:: -traverse(CullTraverserData &data) { +traverse(CullTraverserData &data, PortalClipper *portal_viewer) { // Most nodes will have no transform or state, and will not // contain decals or require a special cull callback. As an // optimization, we should tag nodes with these properties as @@ -159,7 +161,8 @@ traverse(CullTraverserData &data) { if (data.is_in_view(_camera_mask)) { PandaNode *node = data.node(); pgraph_cat.spam() << "\n" << data._node_path << "\n"; - + +#if 0 // let me see the names, curious unsigned int loc = node->get_name().find("pTypeArchway"); if (loc != string::npos) { @@ -177,6 +180,31 @@ traverse(CullTraverserData &data) { data._reduced_frustum = NULL; } } +#else + if (node->is_of_type(PortalNode::get_class_type())) { + PortalNode *portal_node = DCAST(PortalNode, node); + if (portal_node->is_visible()) { + pgraph_cat.debug() << "portal node visible " << *portal_node << endl; + PT(GeometricBoundingVolume) vf = _view_frustum; + PT(BoundingVolume) reduced_frustum; + + // right now it is returning only one reduced frustum + portal_viewer->prepare_portal(data._node_path.get_node_path()); + //portal_viewer->clip_portal(data._node_path.get_node_path()); + if ((reduced_frustum = portal_viewer->get_reduced_frustum(data._node_path.get_node_path()))) { + // 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()); + } + pgraph_cat.debug() << "vf is " << *vf << "\n"; + } + } +#endif const RenderEffects *node_effects = node->get_effects(); if (node_effects->has_show_bounds()) { @@ -203,7 +231,7 @@ traverse(CullTraverserData &data) { } } - traverse_below(data); + traverse_below(data, portal_viewer); } } @@ -215,7 +243,7 @@ traverse(CullTraverserData &data) { // node's space. //////////////////////////////////////////////////////////////////// void CullTraverser:: -traverse_below(CullTraverserData &data) { +traverse_below(CullTraverserData &data, PortalClipper *portal_viewer) { _nodes_pcollector.add_level(1); PandaNode *node = data.node(); @@ -262,14 +290,14 @@ traverse_below(CullTraverserData &data) { int i = node->get_first_visible_child(); while (i < num_children) { CullTraverserData next_data(data, node->get_child(i)); - traverse(next_data); + traverse(next_data, portal_viewer); i = node->get_next_visible_child(i); } } else { for (int i = 0; i < num_children; i++) { CullTraverserData next_data(data, node->get_child(i)); - traverse(next_data); + traverse(next_data, portal_viewer); } } } diff --git a/panda/src/pgraph/cullTraverser.h b/panda/src/pgraph/cullTraverser.h index e76b524e7f..fcfc0b68e0 100644 --- a/panda/src/pgraph/cullTraverser.h +++ b/panda/src/pgraph/cullTraverser.h @@ -76,8 +76,8 @@ public: INLINE CullHandler *get_cull_handler() const; void traverse(const NodePath &root, bool python_cull_control=false); - void traverse(CullTraverserData &data); - void traverse_below(CullTraverserData &data); + void traverse(CullTraverserData &data, PortalClipper *portal_viewer=NULL); + void traverse_below(CullTraverserData &data, PortalClipper *portal_viewer=NULL); public: // Statistics diff --git a/panda/src/pgraph/cullTraverserData.I b/panda/src/pgraph/cullTraverserData.I index f3442bb826..063ef9e634 100644 --- a/panda/src/pgraph/cullTraverserData.I +++ b/panda/src/pgraph/cullTraverserData.I @@ -16,7 +16,6 @@ // //////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////// // Function: CullTraverserData::Constructor // Access: Public diff --git a/panda/src/pgraph/cullTraverserData.cxx b/panda/src/pgraph/cullTraverserData.cxx index ef1dcf30cd..243c5c51b0 100644 --- a/panda/src/pgraph/cullTraverserData.cxx +++ b/panda/src/pgraph/cullTraverserData.cxx @@ -18,9 +18,9 @@ #include "cullTraverserData.h" #include "cullTraverser.h" -#include "config_pgraph.h" #include "pandaNode.h" #include "colorAttrib.h" +#include "config_pgraph.h" #include "textureAttrib.h" #include "renderModeAttrib.h" #include "billboardEffect.h" diff --git a/panda/src/pgraph/portalClipper.cxx b/panda/src/pgraph/portalClipper.cxx index 16298301f8..238613600c 100755 --- a/panda/src/pgraph/portalClipper.cxx +++ b/panda/src/pgraph/portalClipper.cxx @@ -227,66 +227,59 @@ draw_lines() // for now. More functionalities coming up //////////////////////////////////////////////////////////////////// void PortalClipper:: -prepare_portal(int idx) +prepare_portal(NodePath &node_path) { SegmentList segs; - char portal_name[128]; - // print some messages to see if i am getting to this part - pgraph_cat.debug() << "creating portal clipper " << idx << endl; + // Get the Portal Node from this node_path + PandaNode *node = node_path.node(); + PortalNode *portal_node = NULL; + if (node->is_of_type(PortalNode::get_class_type())) + portal_node = DCAST(PortalNode, node); // 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()) { - pgraph_cat.debug() << "portal nodepath " << portal_nodepath << endl; + + // Get the geometry from the portal + pgraph_cat.debug() << *portal_node << endl; + + /* + // Get the World transformation matrix + CPT(TransformState) wtransform = portal_nodepath.get_transform(_scene_setup->get_scene_root()); + LMatrix4f wmat = wtransform->get_mat(); + pgraph_cat.debug() << wmat << endl; + */ + + // Get the camera transformation matrix + CPT(TransformState) ctransform = node_path.get_transform(_scene_setup->get_cull_center()); + //CPT(TransformState) ctransform = node_path.get_transform(_scene_setup->get_camera_path()); + LMatrix4f cmat = ctransform->get_mat(); + pgraph_cat.debug() << cmat << endl; + + _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()) { - /* - // Get the World transformation matrix - CPT(TransformState) wtransform = portal_nodepath.get_transform(_scene_setup->get_scene_root()); - LMatrix4f wmat = wtransform->get_mat(); - pgraph_cat.debug() << wmat << endl; - */ + // 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]); - // Get the camera transformation matrix - CPT(TransformState) ctransform = portal_nodepath.get_transform(_scene_setup->get_cull_center()); - //CPT(TransformState) ctransform = portal_nodepath.get_transform(_scene_setup->get_camera_path()); - LMatrix4f cmat = ctransform->get_mat(); - pgraph_cat.debug() << cmat << endl; - - // Get the geometry from the portal - 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; - - _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(); - } - } + pgraph_cat.debug() << "assembled " << portal_node->get_name() << ": frustum points" << endl; + _num_vert = portal_node->get_num_vertices(); } } @@ -297,7 +290,7 @@ prepare_portal(int idx) // and form the new planes of the reduced view frustum //////////////////////////////////////////////////////////////////// void PortalClipper:: -clip_portal(int idx) +clip_portal(NodePath &node_path) { int num_planes = _hex_frustum->get_num_planes(); @@ -338,7 +331,7 @@ clip_portal(int idx) // fill in the new frustum. Return true if success //////////////////////////////////////////////////////////////////// PT(BoundingVolume) PortalClipper:: -get_reduced_frustum(int idx) +get_reduced_frustum(NodePath &node_path) { int num_planes = 6; LPoint3f intersect_points[4]; @@ -416,7 +409,7 @@ get_reduced_frustum(int idx) visible = false; if (!visible) { - pgraph_cat.debug() << "portal" << idx << " is not visible from current camera look at" << endl; + pgraph_cat.debug() << "portal is not visible from current camera look at" << endl; return NULL; } diff --git a/panda/src/pgraph/portalClipper.h b/panda/src/pgraph/portalClipper.h index ce4ff2f0c2..eb716fe2c9 100755 --- a/panda/src/pgraph/portalClipper.h +++ b/panda/src/pgraph/portalClipper.h @@ -62,11 +62,11 @@ public: ~PortalClipper(); INLINE bool is_facing_camera(); - void prepare_portal(int idx); + void prepare_portal(NodePath &node_path); - void clip_portal(int idx); + void clip_portal(NodePath &node_path); - PT(BoundingVolume) get_reduced_frustum(int idx); + PT(BoundingVolume) get_reduced_frustum(NodePath &node_path); void draw_lines(); INLINE void draw_camera_frustum(); diff --git a/panda/src/pgraph/portalNode.I b/panda/src/pgraph/portalNode.I index e51a7c8615..3ee52f7448 100755 --- a/panda/src/pgraph/portalNode.I +++ b/panda/src/pgraph/portalNode.I @@ -169,19 +169,54 @@ get_vertex(int n) const { } //////////////////////////////////////////////////////////////////// -// Function: PortalNode::set_zone +// Function: PortalNode::set_zone_in // Access: Published // Description: Sets the zone that this portal belongs to //////////////////////////////////////////////////////////////////// -INLINE void PortalNode::set_zone(PandaNode *zone) { - _zone_node = zone; +INLINE void PortalNode::set_zone_in(PandaNode *zone) { + _zone_in = zone; } //////////////////////////////////////////////////////////////////// -// Function: PortalNode::set_zone +// Function: PortalNode::get_zone_in // Access: Published // Description: Sets the zone that this portal belongs to //////////////////////////////////////////////////////////////////// -INLINE PandaNode *PortalNode::get_zone() const { - return _zone_node; +INLINE PandaNode *PortalNode::get_zone_in() const { + return _zone_in; +} +//////////////////////////////////////////////////////////////////// +// Function: PortalNode::set_zone_out +// Access: Published +// Description: Sets the zone that this portal leads out to +//////////////////////////////////////////////////////////////////// +INLINE void PortalNode::set_zone_out(PandaNode *zone) { + _zone_out = zone; +} + +//////////////////////////////////////////////////////////////////// +// Function: PortalNode::get_zone_out +// Access: Published +// Description: Sets the zone that this portal leads out to +//////////////////////////////////////////////////////////////////// +INLINE PandaNode *PortalNode::get_zone_out() const { + return _zone_out; +} + +//////////////////////////////////////////////////////////////////// +// Function: PortalNode::set_visible +// Access: Published +// Description: Python sets this based on curent camera zone +//////////////////////////////////////////////////////////////////// +INLINE void PortalNode::set_visible(bool value) { + _visible = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: PortalNode::is_visible +// Access: Published +// Description: Is this portal visible from current camera zone +//////////////////////////////////////////////////////////////////// +INLINE bool PortalNode::is_visible() { + return _visible; } diff --git a/panda/src/pgraph/portalNode.cxx b/panda/src/pgraph/portalNode.cxx index f3942752fb..2f877e7acb 100755 --- a/panda/src/pgraph/portalNode.cxx +++ b/panda/src/pgraph/portalNode.cxx @@ -45,8 +45,9 @@ PortalNode(const string &name) : _into_portal_mask(PortalMask::all_on()), _flags(0) { - // PortalNodes are hidden by default. - set_draw_mask(DrawMask::all_off()); + _zone_in = NULL; + _zone_out = NULL; + _visible = false; } //////////////////////////////////////////////////////////////////// @@ -61,6 +62,9 @@ PortalNode(const PortalNode ©) : _into_portal_mask(copy._into_portal_mask), _flags(copy._flags) { + _zone_in = NULL; + _zone_out = NULL; + _visible = false; } //////////////////////////////////////////////////////////////////// @@ -154,7 +158,7 @@ combine_with(PandaNode *other) { //////////////////////////////////////////////////////////////////// bool PortalNode:: has_cull_callback() const { - return true; + return false; } //////////////////////////////////////////////////////////////////// @@ -162,24 +166,26 @@ has_cull_callback() const { // Access: Public, Virtual // Description: If has_cull_callback() returns true, this function // will be called during the cull traversal to perform -// any additional operations that should be performed at -// cull time. This may include additional manipulation -// of render state or additional visible/invisible -// decisions, or any other arbitrary operation. -// -// By the time this function is called, the node has -// already passed the bounding-volume test for the -// viewing frustum, and the node's transform and state -// have already been applied to the indicated -// CullTraverserData object. +// reduced frustum culling. Basically, once the scenegraph +// comes across a portal node, it calculates a CulltraverserData +// with which zone, this portal leads out to and the new frustum. +// Then it traverses that child // // The return value is true if this node should be // visible, or false if it should be culled. //////////////////////////////////////////////////////////////////// bool PortalNode:: cull_callback(CullTraverser *trav, CullTraverserData &data) { - // Append our portal vizzes to the drawing, even though they're - // not actually part of the scene graph. + + // Calculate the reduced frustum for this portal + + CullTraverserData next_data(data, _zone_out); + + // We don't want to inherit the render state from above for these + // guys. + next_data._state = RenderState::make_empty(); + trav->traverse(next_data); + // Now carry on to render our child nodes. return true; } diff --git a/panda/src/pgraph/portalNode.h b/panda/src/pgraph/portalNode.h index 0852c76eae..03f0c745c6 100755 --- a/panda/src/pgraph/portalNode.h +++ b/panda/src/pgraph/portalNode.h @@ -68,8 +68,14 @@ 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; + INLINE void set_zone_in(PandaNode *zone); + INLINE PandaNode *get_zone_in() const; + + INLINE void set_zone_out(PandaNode *zone); + INLINE PandaNode *get_zone_out() const; + + INLINE void set_visible(bool value); + INLINE bool is_visible(); protected: virtual BoundingVolume *recompute_bound(); @@ -93,7 +99,10 @@ private: typedef pvector Vertices; Vertices _vertices; - PT(PandaNode) _zone_node; // This is the zone it belongs to + PT(PandaNode) _zone_in; // This is the zone it resides in + PT(PandaNode) _zone_out; // This is the zone it leads out to + + bool _visible; public: static void register_with_read_factory();