From 4a24b121b0d6d403ee01bb2939b383d66661c339 Mon Sep 17 00:00:00 2001 From: "Asad M. Zaman" Date: Tue, 18 May 2004 00:50:43 +0000 Subject: [PATCH] python toggle switch allows the portal culling to be on/off --- panda/src/display/graphicsEngine.I | 25 ++++++++++++++++ panda/src/display/graphicsEngine.cxx | 5 ++-- panda/src/display/graphicsEngine.h | 4 +++ panda/src/pgraph/Sources.pp | 3 ++ panda/src/pgraph/cullTraverser.cxx | 41 +++++++++++++++----------- panda/src/pgraph/cullTraverser.h | 2 +- panda/src/pgraph/pgraph_composite2.cxx | 1 + panda/src/pgraph/portalClipper.I | 10 +++---- panda/src/pgraph/portalClipper.h | 4 +++ 9 files changed, 70 insertions(+), 25 deletions(-) diff --git a/panda/src/display/graphicsEngine.I b/panda/src/display/graphicsEngine.I index a7078dbac6..c54be03233 100644 --- a/panda/src/display/graphicsEngine.I +++ b/panda/src/display/graphicsEngine.I @@ -53,6 +53,31 @@ get_auto_flip() const { return _auto_flip; } +//////////////////////////////////////////////////////////////////// +// Function: GraphicsEngine::set_portal_cull +// Access: Published +// Description: Set this flag true to indicate the GraphicsEngine +// should start portal culling +//////////////////////////////////////////////////////////////////// +INLINE void GraphicsEngine:: +set_portal_cull(bool value) { + // We don't bother with the mutex here. It's just a bool, after + // all. + _portal_enabled = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: GraphicsEngine::get_portal_cull +// Access: Published +// Description: Returns the current setting for the portal culling flag. +//////////////////////////////////////////////////////////////////// +INLINE bool GraphicsEngine:: +get_portal_cull() const { + // We don't bother with the mutex here. It's just a bool, after + // all. + return _portal_enabled; +} + //////////////////////////////////////////////////////////////////// // Function: GraphicsEngine::make_gsg // Access: Published diff --git a/panda/src/display/graphicsEngine.cxx b/panda/src/display/graphicsEngine.cxx index 426feb6106..6e895ab51f 100644 --- a/panda/src/display/graphicsEngine.cxx +++ b/panda/src/display/graphicsEngine.cxx @@ -87,6 +87,7 @@ GraphicsEngine(Pipeline *pipeline) : << "Using threading model " << _threading_model << "\n"; } _auto_flip = auto_flip; + _portal_enabled = false; _flip_state = FS_flip; } @@ -1026,7 +1027,7 @@ do_cull(CullHandler *cull_handler, SceneSetup *scene_setup, trav.set_depth_offset_decals(gsg->depth_offset_decals()); trav.set_scene(scene_setup); trav.set_camera_mask(scene_setup->get_camera_node()->get_camera_mask()); - + if (view_frustum_cull) { // If we're to be performing view-frustum culling, determine the // bounding volume associated with the current viewing frustum. @@ -1049,7 +1050,7 @@ do_cull(CullHandler *cull_handler, SceneSetup *scene_setup, } } - trav.traverse(scene_setup->get_scene_root()); + trav.traverse(scene_setup->get_scene_root(), get_portal_cull()); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/display/graphicsEngine.h b/panda/src/display/graphicsEngine.h index 1c324242ce..51f0eae5b8 100644 --- a/panda/src/display/graphicsEngine.h +++ b/panda/src/display/graphicsEngine.h @@ -69,6 +69,9 @@ PUBLISHED: INLINE void set_auto_flip(bool auto_flip); INLINE bool get_auto_flip() const; + INLINE void set_portal_cull(bool value); + INLINE bool get_portal_cull() const; + INLINE PT(GraphicsStateGuardian) make_gsg(GraphicsPipe *pipe); PT(GraphicsStateGuardian) make_gsg(GraphicsPipe *pipe, const FrameBufferProperties &properties, @@ -226,6 +229,7 @@ private: FrameBufferProperties _frame_buffer_properties; GraphicsThreadingModel _threading_model; bool _auto_flip; + bool _portal_enabled; //toggle to portal culling on/off enum FlipState { FS_draw, // Still drawing. diff --git a/panda/src/pgraph/Sources.pp b/panda/src/pgraph/Sources.pp index 4d331cb1da..7099a04518 100644 --- a/panda/src/pgraph/Sources.pp +++ b/panda/src/pgraph/Sources.pp @@ -69,6 +69,7 @@ pandaNode.I pandaNode.h \ planeNode.I planeNode.h \ pointLight.I pointLight.h \ + portalNode.I portalNode.h \ portalClipper.I portalClipper.h \ renderAttrib.I renderAttrib.h \ renderEffect.I renderEffect.h \ @@ -154,6 +155,7 @@ pandaNode.cxx \ planeNode.cxx \ pointLight.cxx \ + portalNode.cxx \ portalClipper.cxx \ renderAttrib.cxx \ renderEffect.cxx \ @@ -235,6 +237,7 @@ pandaNode.I pandaNode.h \ planeNode.I planeNode.h \ pointLight.I pointLight.h \ + portalNode.I portalNode.h \ portalClipper.I portalClipper.h \ renderAttrib.I renderAttrib.h \ renderEffect.I renderEffect.h \ diff --git a/panda/src/pgraph/cullTraverser.cxx b/panda/src/pgraph/cullTraverser.cxx index a4dafb1114..d3f57b9ef2 100644 --- a/panda/src/pgraph/cullTraverser.cxx +++ b/panda/src/pgraph/cullTraverser.cxx @@ -73,13 +73,14 @@ CullTraverser(const CullTraverser ©) : // Description: Begins the traversal from the indicated node. //////////////////////////////////////////////////////////////////// void CullTraverser:: -traverse(const NodePath &root) { +traverse(const NodePath &root, bool python_cull_control) { nassertv(_cull_handler != (CullHandler *)NULL); nassertv(_scene_setup != (SceneSetup *)NULL); - if (allow_portal_cull) { + if (1) { + // This _view_frustum is in cull_center space PT(GeometricBoundingVolume) vf = _view_frustum; - pgraph_cat.spam() << "_view_frustum is " << *_view_frustum << "\n"; + pgraph_cat.debug() << "_view_frustum is " << *_view_frustum << "\n"; GeometricBoundingVolume *local_frustum = NULL; PT(BoundingVolume) bv = _scene_setup->get_lens()->make_bounds(); @@ -88,26 +89,32 @@ traverse(const NodePath &root) { local_frustum = DCAST(GeometricBoundingVolume, bv); } - pgraph_cat.spam() << "local_frustum is " << *local_frustum << "\n"; + pgraph_cat.debug() << "local_frustum is " << *local_frustum << "\n"; + // This local_frustum is in camera space PortalClipper portal_viewer(local_frustum, _scene_setup); portal_viewer.draw_camera_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))) { - pgraph_cat.debug() << "got reduced frustum " << reduced_frustum << endl; - vf = DCAST(GeometricBoundingVolume, reduced_frustum); - CPT(TransformState) cull_center_transform = - _scene_setup->get_cull_center().get_transform(_scene_setup->get_scene_root()); - vf->xform(cull_center_transform->get_mat()); + 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; + + 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()); + } } } - pgraph_cat.spam() << "vf is " << *vf << "\n"; + pgraph_cat.debug() << "vf is " << *vf << "\n"; CullTraverserData data(root, get_render_transform(), TransformState::make_identity(), diff --git a/panda/src/pgraph/cullTraverser.h b/panda/src/pgraph/cullTraverser.h index 40444918eb..e76b524e7f 100644 --- a/panda/src/pgraph/cullTraverser.h +++ b/panda/src/pgraph/cullTraverser.h @@ -75,7 +75,7 @@ public: INLINE void set_cull_handler(CullHandler *cull_handler); INLINE CullHandler *get_cull_handler() const; - void traverse(const NodePath &root); + void traverse(const NodePath &root, bool python_cull_control=false); void traverse(CullTraverserData &data); void traverse_below(CullTraverserData &data); diff --git a/panda/src/pgraph/pgraph_composite2.cxx b/panda/src/pgraph/pgraph_composite2.cxx index 4305f840d3..4d5169e189 100644 --- a/panda/src/pgraph/pgraph_composite2.cxx +++ b/panda/src/pgraph/pgraph_composite2.cxx @@ -18,6 +18,7 @@ #include "pandaNode.cxx" #include "planeNode.cxx" #include "pointLight.cxx" +#include "portalNode.cxx" #include "portalClipper.cxx" #include "renderAttrib.cxx" #include "renderEffect.cxx" diff --git a/panda/src/pgraph/portalClipper.I b/panda/src/pgraph/portalClipper.I index 048b6ecd59..97403b445f 100755 --- a/panda/src/pgraph/portalClipper.I +++ b/panda/src/pgraph/portalClipper.I @@ -94,7 +94,7 @@ draw_to(float x, float y, float z) { // Description: Draw the current camera frustum in white color // //////////////////////////////////////////////////////////////////// -void PortalClipper:: +INLINE void PortalClipper:: draw_camera_frustum() { _color = Colorf(1,1,1,1); @@ -107,13 +107,13 @@ draw_camera_frustum() // Description: checks if the _coords that forms the plane is // facing the camera //////////////////////////////////////////////////////////////////// -bool PortalClipper:: +INLINE bool PortalClipper:: 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)); - - float direction = portal_plane.get_normal().dot(camera_plane.get_normal()); + 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 > 0); + return (direction < _FACING_THRESHOLD); } diff --git a/panda/src/pgraph/portalClipper.h b/panda/src/pgraph/portalClipper.h index fbac39ce69..ce4ff2f0c2 100755 --- a/panda/src/pgraph/portalClipper.h +++ b/panda/src/pgraph/portalClipper.h @@ -44,6 +44,10 @@ class CullTraverserData; class CullableObject; class NodePath; +//#define _FACING_THRESHOLD -0.7 //about 45 degrees with the camera +//#define _FACING_THRESHOLD -0.5 //about 60 degrees with the camera +#define _FACING_THRESHOLD 0.0 //about 90 degrees with the camera + //////////////////////////////////////////////////////////////////// // Class : PortalClipper // Description : This object performs a depth-first traversal of the