Browse Source

transparency

David Rose 24 years ago
parent
commit
ab47c66154

+ 7 - 8
panda/src/egg2pg/qpeggLoader.cxx

@@ -26,6 +26,7 @@
 #include "texturePool.h"
 #include "texturePool.h"
 #include "billboardAttrib.h"
 #include "billboardAttrib.h"
 #include "cullFaceAttrib.h"
 #include "cullFaceAttrib.h"
+#include "transparencyAttrib.h"
 #include "qpgeomNode.h"
 #include "qpgeomNode.h"
 #include "string_utils.h"
 #include "string_utils.h"
 #include "eggPrimitive.h"
 #include "eggPrimitive.h"
@@ -1044,31 +1045,29 @@ setup_bucket(BuilderBucket &bucket, PandaNode *parent,
     }
     }
   }
   }
 
 
-  /*
   switch (am) {
   switch (am) {
   case EggRenderMode::AM_on:
   case EggRenderMode::AM_on:
   case EggRenderMode::AM_blend:
   case EggRenderMode::AM_blend:
-    bucket._trans.set_transition(new TransparencyTransition(TransparencyProperty::M_alpha));
+    bucket.add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha));
     break;
     break;
 
 
   case EggRenderMode::AM_blend_no_occlude:
   case EggRenderMode::AM_blend_no_occlude:
-    bucket._trans.set_transition(new TransparencyTransition(TransparencyProperty::M_alpha));
-    bucket._trans.set_transition(new DepthWriteTransition(DepthWriteTransition::off()));
+    bucket.add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha));
+    //    bucket.add_attrib(new DepthWriteTransition(DepthWriteTransition::off()));
     break;
     break;
 
 
   case EggRenderMode::AM_ms:
   case EggRenderMode::AM_ms:
-    bucket._trans.set_transition(new TransparencyTransition(TransparencyProperty::M_multisample));
+    bucket.add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_multisample));
     break;
     break;
 
 
   case EggRenderMode::AM_ms_mask:
   case EggRenderMode::AM_ms_mask:
-    bucket._trans.set_transition(new TransparencyTransition(TransparencyProperty::M_multisample_mask));
+    bucket.add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_multisample_mask));
     break;
     break;
 
 
   default:
   default:
-    //    bucket._trans.set_transition(new TransparencyTransition(TransparencyProperty::M_none));
+    bucket.add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_none));
     break;
     break;
   }
   }
-  */
 
 
   /*
   /*
   switch (dwm) {
   switch (dwm) {

+ 59 - 0
panda/src/glgsg/glGraphicsStateGuardian.cxx

@@ -67,6 +67,7 @@
 #include "polygonOffsetTransition.h"
 #include "polygonOffsetTransition.h"
 #include "textureAttrib.h"
 #include "textureAttrib.h"
 #include "cullFaceAttrib.h"
 #include "cullFaceAttrib.h"
+#include "transparencyAttrib.h"
 #include "clockObject.h"
 #include "clockObject.h"
 #include "string_utils.h"
 #include "string_utils.h"
 #include "dcast.h"
 #include "dcast.h"
@@ -3485,6 +3486,64 @@ issue_cull_face(const CullFaceAttrib *attrib) {
   report_errors();
   report_errors();
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: GLGraphicsStateGuardian::issue_transparency
+//       Access: Public, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+void GLGraphicsStateGuardian::
+issue_transparency(const TransparencyAttrib *attrib) {
+  TransparencyAttrib::Mode mode = attrib->get_mode();
+
+  switch (mode) {
+  case TransparencyAttrib::M_none:
+    enable_multisample_alpha_one(false);
+    enable_multisample_alpha_mask(false);
+    enable_blend(false);
+    enable_alpha_test(false);
+    break;
+  case TransparencyAttrib::M_alpha:
+  case TransparencyAttrib::M_alpha_sorted:
+    // Should we really have an "alpha" and an "alpha_sorted" mode,
+    // like Performer does?  (The difference is that "alpha" is with
+    // the write to the depth buffer disabled.)  Or should we just use
+    // the separate depth write transition to control this?  Doing it
+    // implicitly requires a bit more logic here and in the state
+    // management; for now we require the user to explicitly turn off
+    // the depth write.
+    enable_multisample_alpha_one(false);
+    enable_multisample_alpha_mask(false);
+    enable_blend(true);
+    enable_alpha_test(false);
+    call_glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    break;
+  case TransparencyAttrib::M_multisample:
+    enable_multisample_alpha_one(true);
+    enable_multisample_alpha_mask(true);
+    enable_blend(false);
+    enable_alpha_test(false);
+    break;
+  case TransparencyAttrib::M_multisample_mask:
+    enable_multisample_alpha_one(false);
+    enable_multisample_alpha_mask(true);
+    enable_blend(false);
+    enable_alpha_test(false);
+    break;
+  case TransparencyAttrib::M_binary:
+    enable_multisample_alpha_one(false);
+    enable_multisample_alpha_mask(false);
+    enable_blend(false);
+    enable_alpha_test(true);
+    call_glAlphaFunc(GL_EQUAL, 1);
+    break;
+  default:
+    glgsg_cat.error()
+      << "invalid transparency mode " << (int)mode << endl;
+    break;
+  }
+  report_errors();
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: GLGraphicsStateGuardian::wants_normals
 //     Function: GLGraphicsStateGuardian::wants_normals
 //       Access: Public, Virtual
 //       Access: Public, Virtual

+ 1 - 0
panda/src/glgsg/glGraphicsStateGuardian.h

@@ -159,6 +159,7 @@ public:
   virtual void issue_transform(const TransformState *transform);
   virtual void issue_transform(const TransformState *transform);
   virtual void issue_texture(const TextureAttrib *attrib);
   virtual void issue_texture(const TextureAttrib *attrib);
   virtual void issue_cull_face(const CullFaceAttrib *attrib);
   virtual void issue_cull_face(const CullFaceAttrib *attrib);
+  virtual void issue_transparency(const TransparencyAttrib *attrib);
 
 
   virtual bool wants_normals(void) const;
   virtual bool wants_normals(void) const;
   virtual bool wants_texcoords(void) const;
   virtual bool wants_texcoords(void) const;

+ 9 - 3
panda/src/pgraph/Sources.pp

@@ -12,6 +12,7 @@
     colorAttrib.h colorAttrib.I \
     colorAttrib.h colorAttrib.I \
     config_pgraph.h \
     config_pgraph.h \
     cullBin.h cullBin.I \
     cullBin.h cullBin.I \
+    cullBinBackToFront.h cullBinBackToFront.I \
     cullBinManager.h cullBinManager.I \
     cullBinManager.h cullBinManager.I \
     cullBinUnsorted.h cullBinUnsorted.I \
     cullBinUnsorted.h cullBinUnsorted.I \
     cullFaceAttrib.h cullFaceAttrib.I \
     cullFaceAttrib.h cullFaceAttrib.I \
@@ -32,7 +33,8 @@
     renderAttrib.h renderAttrib.I \
     renderAttrib.h renderAttrib.I \
     renderState.h renderState.I \
     renderState.h renderState.I \
     textureAttrib.h textureAttrib.I \
     textureAttrib.h textureAttrib.I \
-    transformState.h transformState.I
+    transformState.h transformState.I \
+    transparencyAttrib.h transparencyAttrib.I
 
 
   #define COMBINED_SOURCES $[TARGET]_composite1.cxx $[TARGET]_composite2.cxx    
   #define COMBINED_SOURCES $[TARGET]_composite1.cxx $[TARGET]_composite2.cxx    
   #define INCLUDED_SOURCES \
   #define INCLUDED_SOURCES \
@@ -42,6 +44,7 @@
     colorAttrib.cxx \
     colorAttrib.cxx \
     config_pgraph.cxx \
     config_pgraph.cxx \
     cullBin.cxx \
     cullBin.cxx \
+    cullBinBackToFront.cxx \
     cullBinManager.cxx \
     cullBinManager.cxx \
     cullBinUnsorted.cxx \
     cullBinUnsorted.cxx \
     cullFaceAttrib.cxx \
     cullFaceAttrib.cxx \
@@ -62,7 +65,8 @@
     renderAttrib.cxx \
     renderAttrib.cxx \
     renderState.cxx \
     renderState.cxx \
     textureAttrib.cxx \
     textureAttrib.cxx \
-    transformState.cxx
+    transformState.cxx \
+    transparencyAttrib.cxx
 
 
   #if $[DONT_COMBINE_PGRAPH]    
   #if $[DONT_COMBINE_PGRAPH]    
     #define SOURCES $[SOURCES] $[INCLUDED_SOURCES]
     #define SOURCES $[SOURCES] $[INCLUDED_SOURCES]
@@ -77,6 +81,7 @@
     colorAttrib.h colorAttrib.I \
     colorAttrib.h colorAttrib.I \
     config_pgraph.h \
     config_pgraph.h \
     cullBin.h cullBin.I \
     cullBin.h cullBin.I \
+    cullBinBackToFront.h cullBinBackToFront.I \
     cullBinManager.h cullBinManager.I \
     cullBinManager.h cullBinManager.I \
     cullBinUnsorted.h cullBinUnsorted.I \
     cullBinUnsorted.h cullBinUnsorted.I \
     cullFaceAttrib.h cullFaceAttrib.I \
     cullFaceAttrib.h cullFaceAttrib.I \
@@ -97,7 +102,8 @@
     renderAttrib.h renderAttrib.I \
     renderAttrib.h renderAttrib.I \
     renderState.h renderState.I \
     renderState.h renderState.I \
     textureAttrib.h textureAttrib.I \
     textureAttrib.h textureAttrib.I \
-    transformState.h transformState.I
+    transformState.h transformState.I \
+    transparencyAttrib.h transparencyAttrib.I
 
 
   #define IGATESCAN all
   #define IGATESCAN all
 
 

+ 5 - 0
panda/src/pgraph/config_pgraph.cxx

@@ -23,6 +23,7 @@
 #include "colorAttrib.h"
 #include "colorAttrib.h"
 #include "cullFaceAttrib.h"
 #include "cullFaceAttrib.h"
 #include "cullBin.h"
 #include "cullBin.h"
+#include "cullBinBackToFront.h"
 #include "cullBinUnsorted.h"
 #include "cullBinUnsorted.h"
 #include "qpgeomNode.h"
 #include "qpgeomNode.h"
 #include "qplensNode.h"
 #include "qplensNode.h"
@@ -34,6 +35,7 @@
 #include "textureAttrib.h"
 #include "textureAttrib.h"
 #include "colorAttrib.h"
 #include "colorAttrib.h"
 #include "transformState.h"
 #include "transformState.h"
+#include "transparencyAttrib.h"
 
 
 #include "dconfig.h"
 #include "dconfig.h"
 
 
@@ -66,6 +68,7 @@ init_libpgraph() {
   ColorAttrib::init_type();
   ColorAttrib::init_type();
   CullFaceAttrib::init_type();
   CullFaceAttrib::init_type();
   CullBin::init_type();
   CullBin::init_type();
+  CullBinBackToFront::init_type();
   CullBinUnsorted::init_type();
   CullBinUnsorted::init_type();
   qpGeomNode::init_type();
   qpGeomNode::init_type();
   qpLensNode::init_type();
   qpLensNode::init_type();
@@ -77,6 +80,7 @@ init_libpgraph() {
   TextureAttrib::init_type();
   TextureAttrib::init_type();
   ColorAttrib::init_type();
   ColorAttrib::init_type();
   TransformState::init_type();
   TransformState::init_type();
+  TransparencyAttrib::init_type();
 
 
   BillboardAttrib::register_with_read_factory();
   BillboardAttrib::register_with_read_factory();
   ColorAttrib::register_with_read_factory();
   ColorAttrib::register_with_read_factory();
@@ -87,4 +91,5 @@ init_libpgraph() {
   TextureAttrib::register_with_read_factory();
   TextureAttrib::register_with_read_factory();
   ColorAttrib::register_with_read_factory();
   ColorAttrib::register_with_read_factory();
   TransformState::register_with_read_factory();
   TransformState::register_with_read_factory();
+  TransparencyAttrib::register_with_read_factory();
 }
 }

+ 56 - 0
panda/src/pgraph/cullBinBackToFront.I

@@ -0,0 +1,56 @@
+// Filename: cullBinBackToFront.I
+// Created by:  drose (28Feb02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullBinBackToFront::GeomData::Constructor
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+INLINE CullBinBackToFront::GeomData::
+GeomData(Geom *geom, const TransformState *transform,
+         const RenderState *state, float dist) :
+  _geom(geom),
+  _transform(transform),
+  _state(state),
+  _dist(dist)
+{
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullBinBackToFront::GeomData::operator <
+//       Access: Public
+//  Description: Specifies the correct sort ordering for these
+//               objects.
+////////////////////////////////////////////////////////////////////
+INLINE bool CullBinBackToFront::GeomData::
+operator < (const GeomData &other) const {
+  return _dist > other._dist;
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullBinBackToFront::Constructor
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+INLINE CullBinBackToFront::
+CullBinBackToFront(GraphicsStateGuardianBase *gsg) :
+  CullBin(gsg)
+{
+}

+ 75 - 0
panda/src/pgraph/cullBinBackToFront.cxx

@@ -0,0 +1,75 @@
+// Filename: cullBinBackToFront.cxx
+// Created by:  drose (28Feb02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "cullBinBackToFront.h"
+#include "graphicsStateGuardianBase.h"
+
+#include <algorithm>
+
+
+TypeHandle CullBinBackToFront::_type_handle;
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullBinBackToFront::add_geom
+//       Access: Public, Virtual
+//  Description: Adds the geom, along with its associated state, to
+//               the bin for rendering.
+////////////////////////////////////////////////////////////////////
+void CullBinBackToFront::
+add_geom(Geom *geom, const TransformState *transform,
+         const RenderState *state) {
+  // Since we don't have bounding volumes yet, for now we'll just use
+  // the origin of the node.  Only accurate for local transforms.
+  const LMatrix4f &mat = transform->get_mat();
+  const LVecBase3f &pos = mat.get_row3(3);
+
+  // Oops!  Don't have compute_distance_to() here either!
+  float dist = -pos[2];
+  _geoms.push_back(GeomData(geom, transform, state, dist));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullBinBackToFront::finish_cull
+//       Access: Public
+//  Description: Called after all the geoms have been added, this
+//               indicates that the cull process is finished for this
+//               frame and gives the bins a chance to do any
+//               post-processing (like sorting) before moving on to
+//               draw.
+////////////////////////////////////////////////////////////////////
+void CullBinBackToFront::
+finish_cull() {
+  sort(_geoms.begin(), _geoms.end());
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullBinBackToFront::draw
+//       Access: Public
+//  Description: Draws all the geoms in the bin, in the appropriate
+//               order.
+////////////////////////////////////////////////////////////////////
+void CullBinBackToFront::
+draw() {
+  Geoms::iterator gi;
+  for (gi = _geoms.begin(); gi != _geoms.end(); ++gi) {
+    GeomData &geom_data = (*gi);
+    _gsg->set_state_and_transform(geom_data._state, geom_data._transform);
+    geom_data._geom->draw(_gsg);
+  }
+}
+

+ 86 - 0
panda/src/pgraph/cullBinBackToFront.h

@@ -0,0 +1,86 @@
+// Filename: cullBinBackToFront.h
+// Created by:  drose (28Feb02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef CULLBINBACKTOFRONT_H
+#define CULLBINBACKTOFRONT_H
+
+#include "pandabase.h"
+
+#include "cullBin.h"
+#include "geom.h"
+#include "transformState.h"
+#include "renderState.h"
+#include "pointerTo.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : CullBinBackToFront
+// Description : A specific kind of CullBin that sorts geometry in
+//               order from furthest to nearest based on the center of
+//               its bounding volume.  This is primarily intended for
+//               rendering transparent and semi-transparent geometry
+//               that must be sorted from back to front.
+////////////////////////////////////////////////////////////////////
+class EXPCL_PANDA CullBinBackToFront : public CullBin {
+public:
+  INLINE CullBinBackToFront(GraphicsStateGuardianBase *gsg);
+
+  virtual void add_geom(Geom *geom, const TransformState *transform,
+                        const RenderState *state);
+  virtual void finish_cull();
+  virtual void draw();
+
+private:
+  class GeomData {
+  public:
+    INLINE GeomData(Geom *geom, const TransformState *transform,
+                    const RenderState *state, float dist);
+    INLINE bool operator < (const GeomData &other) const;
+
+    PT(Geom) _geom;
+    CPT(TransformState) _transform;
+    CPT(RenderState) _state;
+    float _dist;
+  };
+
+  typedef pvector<GeomData> Geoms;
+  Geoms _geoms;
+
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    CullBin::init_type();
+    register_type(_type_handle, "CullBinBackToFront",
+                  CullBin::get_class_type());
+  }
+  virtual TypeHandle get_type() const {
+    return get_class_type();
+  }
+  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
+
+private:
+  static TypeHandle _type_handle;
+};
+
+#include "cullBinBackToFront.I"
+
+#endif
+
+
+  

+ 8 - 1
panda/src/pgraph/cullBinManager.cxx

@@ -17,6 +17,7 @@
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 
 
 #include "cullBinManager.h"
 #include "cullBinManager.h"
+#include "cullBinBackToFront.h"
 #include "cullBinUnsorted.h"
 #include "cullBinUnsorted.h"
 #include "renderState.h"
 #include "renderState.h"
 #include "cullResult.h"
 #include "cullResult.h"
@@ -198,7 +199,13 @@ make_new_bin(int bin_index, GraphicsStateGuardianBase *gsg) {
   nassertr(bin_index >= 0 && bin_index < (int)_bin_definitions.size(), NULL);
   nassertr(bin_index >= 0 && bin_index < (int)_bin_definitions.size(), NULL);
   nassertr(_bin_definitions[bin_index]._in_use, NULL);
   nassertr(_bin_definitions[bin_index]._in_use, NULL);
 
 
-  return new CullBinUnsorted(gsg);
+  switch (_bin_definitions[bin_index]._type) {
+  case BT_back_to_front:
+    return new CullBinBackToFront(gsg);
+
+  default:
+    return new CullBinUnsorted(gsg);
+  }
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////

+ 1 - 0
panda/src/pgraph/pgraph_composite1.cxx

@@ -4,6 +4,7 @@
 #include "colorAttrib.cxx"
 #include "colorAttrib.cxx"
 #include "config_pgraph.cxx"
 #include "config_pgraph.cxx"
 #include "cullBin.cxx"
 #include "cullBin.cxx"
+#include "cullBinBackToFront.cxx"
 #include "cullBinManager.cxx"
 #include "cullBinManager.cxx"
 #include "cullBinUnsorted.cxx"
 #include "cullBinUnsorted.cxx"
 #include "cullFaceAttrib.cxx"
 #include "cullFaceAttrib.cxx"

+ 1 - 0
panda/src/pgraph/pgraph_composite2.cxx

@@ -14,3 +14,4 @@
 #include "test_pgraph.cxx"
 #include "test_pgraph.cxx"
 #include "textureAttrib.cxx"
 #include "textureAttrib.cxx"
 #include "transformState.cxx"
 #include "transformState.cxx"
+#include "transparencyAttrib.cxx"

+ 24 - 1
panda/src/pgraph/renderState.cxx

@@ -18,6 +18,7 @@
 
 
 #include "renderState.h"
 #include "renderState.h"
 #include "billboardAttrib.h"
 #include "billboardAttrib.h"
+#include "transparencyAttrib.h"
 #include "cullBinManager.h"
 #include "cullBinManager.h"
 #include "config_pgraph.h"
 #include "config_pgraph.h"
 #include "bamReader.h"
 #include "bamReader.h"
@@ -865,7 +866,7 @@ determine_billboard() {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void RenderState::
 void RenderState::
 determine_bin_index() {
 determine_bin_index() {
-  string bin_name = "opaque";
+  string bin_name;
 
 
   /*
   /*
   const RenderAttrib *attrib = get_attrib(CullBinAttrib::get_class_type());
   const RenderAttrib *attrib = get_attrib(CullBinAttrib::get_class_type());
@@ -873,6 +874,28 @@ determine_bin_index() {
     const CullBinAttrib *bin_attrib = DCAST(CullBinAttrib, attrib);
     const CullBinAttrib *bin_attrib = DCAST(CullBinAttrib, attrib);
   }
   }
   */
   */
+  {
+    // No explicit bin is specified; put in the in the default bin,
+    // either opaque or transparent, based on the transparency
+    // setting.
+    bin_name = "opaque";
+    const RenderAttrib *attrib = get_attrib(TransparencyAttrib::get_class_type());
+    if (attrib != (const RenderAttrib *)NULL) {
+      const TransparencyAttrib *trans = DCAST(TransparencyAttrib, attrib);
+      switch (trans->get_mode()) {
+      case TransparencyAttrib::M_alpha:
+      case TransparencyAttrib::M_alpha_sorted:
+      case TransparencyAttrib::M_binary:
+        // These transparency modes require special back-to-front sorting.
+        bin_name = "transparent";
+        break;
+
+      default:
+        break;
+      }
+    }
+  }
+
 
 
   CullBinManager *bin_manager = CullBinManager::get_global_ptr();
   CullBinManager *bin_manager = CullBinManager::get_global_ptr();
   _bin_index = bin_manager->find_bin(bin_name);
   _bin_index = bin_manager->find_bin(bin_name);

+ 40 - 0
panda/src/pgraph/transparencyAttrib.I

@@ -0,0 +1,40 @@
+// Filename: transparencyAttrib.I
+// Created by:  drose (28Feb02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::Constructor
+//       Access: Private
+//  Description: Use TransparencyAttrib::make() to construct a new
+//               TransparencyAttrib object.
+////////////////////////////////////////////////////////////////////
+INLINE TransparencyAttrib::
+TransparencyAttrib(TransparencyAttrib::Mode mode) :
+  _mode(mode)
+{
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::get_mode
+//       Access: Published
+//  Description: Returns the transparency mode.
+////////////////////////////////////////////////////////////////////
+INLINE TransparencyAttrib::Mode TransparencyAttrib::
+get_mode() const {
+  return _mode;
+}

+ 177 - 0
panda/src/pgraph/transparencyAttrib.cxx

@@ -0,0 +1,177 @@
+// Filename: transparencyAttrib.cxx
+// Created by:  drose (28Feb02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "transparencyAttrib.h"
+#include "graphicsStateGuardianBase.h"
+#include "dcast.h"
+#include "bamReader.h"
+#include "bamWriter.h"
+#include "datagram.h"
+#include "datagramIterator.h"
+
+TypeHandle TransparencyAttrib::_type_handle;
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::make
+//       Access: Published, Static
+//  Description: Constructs a new TransparencyAttrib object that specifies
+//               how to cull geometry.  By Panda convention, vertices
+//               are ordered counterclockwise when seen from the
+//               front, so the M_cull_clockwise will cull backfacing
+//               polygons.
+////////////////////////////////////////////////////////////////////
+CPT(RenderAttrib) TransparencyAttrib::
+make(TransparencyAttrib::Mode mode) {
+  TransparencyAttrib *attrib = new TransparencyAttrib(mode);
+  return return_new(attrib);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::issue
+//       Access: Public, Virtual
+//  Description: Calls the appropriate method on the indicated GSG
+//               to issue the graphics commands appropriate to the
+//               given attribute.  This is normally called
+//               (indirectly) only from
+//               GraphicsStateGuardian::set_state() or modify_state().
+////////////////////////////////////////////////////////////////////
+void TransparencyAttrib::
+issue(GraphicsStateGuardianBase *gsg) const {
+  gsg->issue_transparency(this);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::output
+//       Access: Public, Virtual
+//  Description: 
+////////////////////////////////////////////////////////////////////
+void TransparencyAttrib::
+output(ostream &out) const {
+  out << get_type() << ":";
+  switch (get_mode()) {
+  case M_none:
+    out << "none";
+
+  case M_alpha:
+    out << "alpha";
+
+  case M_alpha_sorted:
+    out << "alpha sorted";
+
+  case M_multisample:
+    out << "multisample";
+
+  case M_multisample_mask:
+    out << "multisample mask";
+
+  case M_binary:
+    out << "binary";
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::compare_to_impl
+//       Access: Protected, Virtual
+//  Description: Intended to be overridden by derived TransparencyAttrib
+//               types to return a unique number indicating whether
+//               this TransparencyAttrib is equivalent to the other one.
+//
+//               This should return 0 if the two TransparencyAttrib objects
+//               are equivalent, a number less than zero if this one
+//               should be sorted before the other one, and a number
+//               greater than zero otherwise.
+//
+//               This will only be called with two TransparencyAttrib
+//               objects whose get_type() functions return the same.
+////////////////////////////////////////////////////////////////////
+int TransparencyAttrib::
+compare_to_impl(const RenderAttrib *other) const {
+  const TransparencyAttrib *ta;
+  DCAST_INTO_R(ta, other, 0);
+  return (int)_mode - (int)ta->_mode;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::make_default_impl
+//       Access: Protected, Virtual
+//  Description: Intended to be overridden by derived TransparencyAttrib
+//               types to specify what the default property for a
+//               TransparencyAttrib of this type should be.
+//
+//               This should return a newly-allocated TransparencyAttrib of
+//               the same type that corresponds to whatever the
+//               standard default for this kind of TransparencyAttrib is.
+////////////////////////////////////////////////////////////////////
+RenderAttrib *TransparencyAttrib::
+make_default_impl() const {
+  return new TransparencyAttrib;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::register_with_read_factory
+//       Access: Public, Static
+//  Description: Tells the BamReader how to create objects of type
+//               TransparencyAttrib.
+////////////////////////////////////////////////////////////////////
+void TransparencyAttrib::
+register_with_read_factory() {
+  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::write_datagram
+//       Access: Public, Virtual
+//  Description: Writes the contents of this object to the datagram
+//               for shipping out to a Bam file.
+////////////////////////////////////////////////////////////////////
+void TransparencyAttrib::
+write_datagram(BamWriter *manager, Datagram &dg) {
+  RenderAttrib::write_datagram(manager, dg);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::make_from_bam
+//       Access: Protected, Static
+//  Description: This function is called by the BamReader's factory
+//               when a new object of type TransparencyAttrib is encountered
+//               in the Bam file.  It should create the TransparencyAttrib
+//               and extract its information from the file.
+////////////////////////////////////////////////////////////////////
+TypedWritable *TransparencyAttrib::
+make_from_bam(const FactoryParams &params) {
+  TransparencyAttrib *attrib = new TransparencyAttrib;
+  DatagramIterator scan;
+  BamReader *manager;
+
+  parse_params(params, scan, manager);
+  attrib->fillin(scan, manager);
+
+  return new_from_bam(attrib, manager);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TransparencyAttrib::fillin
+//       Access: Protected
+//  Description: This internal function is called by make_from_bam to
+//               read in all of the relevant data from the BamFile for
+//               the new TransparencyAttrib.
+////////////////////////////////////////////////////////////////////
+void TransparencyAttrib::
+fillin(DatagramIterator &scan, BamReader *manager) {
+  RenderAttrib::fillin(scan, manager);
+}

+ 97 - 0
panda/src/pgraph/transparencyAttrib.h

@@ -0,0 +1,97 @@
+// Filename: transparencyAttrib.h
+// Created by:  drose (28Feb02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef TRANSPARENCYATTRIB_H
+#define TRANSPARENCYATTRIB_H
+
+#include "pandabase.h"
+
+#include "renderAttrib.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : TransparencyAttrib
+// Description : This controls the enabling of transparency.  Simply
+//               setting an alpha component to non-1 does not in
+//               itself make an object transparent; you must also
+//               enable transparency mode with a suitable
+//               TransparencyTransition.  Similarly, it is wasteful to
+//               render an object with a TransparencyTransition in
+//               effect unless you actually want it to be at least
+//               partially transparent (and it has alpha components
+//               less than 1).
+////////////////////////////////////////////////////////////////////
+class EXPCL_PANDA TransparencyAttrib : public RenderAttrib {
+PUBLISHED:
+  enum Mode {
+    M_none,             // No transparency in effect.
+    M_alpha,            // Writes to depth buffer of transp objects disabled
+    M_alpha_sorted,     // Assumes transp objects are depth sorted
+    M_multisample,      // Source alpha values modified to 1.0 before writing
+    M_multisample_mask, // Source alpha values not modified
+    M_binary,           // Only writes pixels with alpha = 1.0
+  };
+
+private:
+  INLINE TransparencyAttrib(Mode mode = M_none);
+
+PUBLISHED:
+  static CPT(RenderAttrib) make(Mode mode);
+
+  INLINE Mode get_mode() const;
+
+public:
+  virtual void issue(GraphicsStateGuardianBase *gsg) const;
+  virtual void output(ostream &out) const;
+
+protected:
+  virtual int compare_to_impl(const RenderAttrib *other) const;
+  virtual RenderAttrib *make_default_impl() const;
+
+private:
+  Mode _mode;
+
+public:
+  static void register_with_read_factory();
+  virtual void write_datagram(BamWriter *manager, Datagram &dg);
+
+protected:
+  static TypedWritable *make_from_bam(const FactoryParams &params);
+  void fillin(DatagramIterator &scan, BamReader *manager);
+  
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    RenderAttrib::init_type();
+    register_type(_type_handle, "TransparencyAttrib",
+                  RenderAttrib::get_class_type());
+  }
+  virtual TypeHandle get_type() const {
+    return get_class_type();
+  }
+  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
+
+private:
+  static TypeHandle _type_handle;
+};
+
+#include "transparencyAttrib.I"
+
+#endif
+