Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pgraph/clipPlaneAttrib.cxx

Go to the documentation of this file.
00001 // Filename: clipPlaneAttrib.cxx
00002 // Created by:  drose (11Jul02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "clipPlaneAttrib.h"
00020 #include "pandaNode.h"
00021 #include "graphicsStateGuardianBase.h"
00022 #include "bamReader.h"
00023 #include "bamWriter.h"
00024 #include "datagram.h"
00025 #include "datagramIterator.h"
00026 
00027 TypeHandle ClipPlaneAttrib::_type_handle;
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //     Function: ClipPlaneAttrib::make_all_off
00031 //       Access: Published, Static
00032 //  Description: Constructs a new ClipPlaneAttrib object that turns off
00033 //               all planes (and hence disables planeing).
00034 ////////////////////////////////////////////////////////////////////
00035 CPT(RenderAttrib) ClipPlaneAttrib::
00036 make_all_off() {
00037   ClipPlaneAttrib *attrib = new ClipPlaneAttrib;
00038   attrib->_operation = O_set;
00039   return return_new(attrib);
00040 }
00041 
00042 ////////////////////////////////////////////////////////////////////
00043 //     Function: ClipPlaneAttrib::make
00044 //       Access: Published, Static
00045 //  Description: Constructs a new ClipPlaneAttrib object that turns on (or
00046 //               off, according to op) the indicate plane(s).
00047 ////////////////////////////////////////////////////////////////////
00048 CPT(RenderAttrib) ClipPlaneAttrib::
00049 make(ClipPlaneAttrib::Operation op, PlaneNode *plane) {
00050   ClipPlaneAttrib *attrib = new ClipPlaneAttrib;
00051   attrib->_operation = op;
00052   attrib->_planes.push_back(plane);
00053   return return_new(attrib);
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function: ClipPlaneAttrib::make
00058 //       Access: Published, Static
00059 //  Description: Constructs a new ClipPlaneAttrib object that turns on (or
00060 //               off, according to op) the indicate plane(s).
00061 ////////////////////////////////////////////////////////////////////
00062 CPT(RenderAttrib) ClipPlaneAttrib::
00063 make(ClipPlaneAttrib::Operation op, PlaneNode *plane1, PlaneNode *plane2) {
00064   ClipPlaneAttrib *attrib = new ClipPlaneAttrib;
00065   attrib->_operation = op;
00066   attrib->_planes.push_back(plane1);
00067   attrib->_planes.push_back(plane2);
00068 
00069   attrib->_planes.sort();
00070   return return_new(attrib);
00071 }
00072 
00073 ////////////////////////////////////////////////////////////////////
00074 //     Function: ClipPlaneAttrib::make
00075 //       Access: Published, Static
00076 //  Description: Constructs a new ClipPlaneAttrib object that turns on (or
00077 //               off, according to op) the indicate plane(s).
00078 ////////////////////////////////////////////////////////////////////
00079 CPT(RenderAttrib) ClipPlaneAttrib::
00080 make(ClipPlaneAttrib::Operation op, PlaneNode *plane1, PlaneNode *plane2,
00081      PlaneNode *plane3) {
00082   ClipPlaneAttrib *attrib = new ClipPlaneAttrib;
00083   attrib->_operation = op;
00084   attrib->_planes.push_back(plane1);
00085   attrib->_planes.push_back(plane2);
00086   attrib->_planes.push_back(plane3);
00087 
00088   attrib->_planes.sort();
00089   return return_new(attrib);
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////
00093 //     Function: ClipPlaneAttrib::make
00094 //       Access: Published, Static
00095 //  Description: Constructs a new ClipPlaneAttrib object that turns on (or
00096 //               off, according to op) the indicate plane(s).
00097 ////////////////////////////////////////////////////////////////////
00098 CPT(RenderAttrib) ClipPlaneAttrib::
00099 make(ClipPlaneAttrib::Operation op, PlaneNode *plane1, PlaneNode *plane2,
00100      PlaneNode *plane3, PlaneNode *plane4) {
00101   ClipPlaneAttrib *attrib = new ClipPlaneAttrib;
00102   attrib->_operation = op;
00103   attrib->_planes.push_back(plane1);
00104   attrib->_planes.push_back(plane2);
00105   attrib->_planes.push_back(plane3);
00106   attrib->_planes.push_back(plane4);
00107 
00108   attrib->_planes.sort();
00109   return return_new(attrib);
00110 }
00111 
00112 ////////////////////////////////////////////////////////////////////
00113 //     Function: ClipPlaneAttrib::has_plane
00114 //       Access: Published
00115 //  Description: Returns true if the indicated plane is listed in the
00116 //               attrib, false otherwise.
00117 ////////////////////////////////////////////////////////////////////
00118 bool ClipPlaneAttrib::
00119 has_plane(PlaneNode *plane) const {
00120   return _planes.find(plane) != _planes.end();
00121 }
00122 
00123 ////////////////////////////////////////////////////////////////////
00124 //     Function: ClipPlaneAttrib::issue
00125 //       Access: Public, Virtual
00126 //  Description: Calls the appropriate method on the indicated GSG
00127 //               to issue the graphics commands appropriate to the
00128 //               given attribute.  This is normally called
00129 //               (indirectly) only from
00130 //               GraphicsStateGuardian::set_state() or modify_state().
00131 ////////////////////////////////////////////////////////////////////
00132 void ClipPlaneAttrib::
00133 issue(GraphicsStateGuardianBase *gsg) const {
00134   gsg->issue_clip_plane(this);
00135 }
00136 
00137 ////////////////////////////////////////////////////////////////////
00138 //     Function: ClipPlaneAttrib::output
00139 //       Access: Public, Virtual
00140 //  Description: 
00141 ////////////////////////////////////////////////////////////////////
00142 void ClipPlaneAttrib::
00143 output(ostream &out) const {
00144   out << get_type() << ":";
00145   if (_operation == O_set && _planes.empty()) {
00146     out << "all off";
00147   } else {
00148     switch (_operation) {
00149     case O_set:
00150       out << "set";
00151       break;
00152     case O_add:
00153       out << "add";
00154       break;
00155     case O_remove:
00156       out << "remove";
00157       break;
00158     }
00159 
00160     Planes::const_iterator li;
00161     for (li = _planes.begin(); li != _planes.end(); ++li) {
00162       PlaneNode *plane = (*li);
00163       out << " " << *plane;
00164     }
00165   }
00166 }
00167 
00168 ////////////////////////////////////////////////////////////////////
00169 //     Function: ClipPlaneAttrib::compare_to_impl
00170 //       Access: Protected, Virtual
00171 //  Description: Intended to be overridden by derived ClipPlaneAttrib
00172 //               types to return a unique number indicating whether
00173 //               this ClipPlaneAttrib is equivalent to the other one.
00174 //
00175 //               This should return 0 if the two ClipPlaneAttrib objects
00176 //               are equivalent, a number less than zero if this one
00177 //               should be sorted before the other one, and a number
00178 //               greater than zero otherwise.
00179 //
00180 //               This will only be called with two ClipPlaneAttrib
00181 //               objects whose get_type() functions return the same.
00182 ////////////////////////////////////////////////////////////////////
00183 int ClipPlaneAttrib::
00184 compare_to_impl(const RenderAttrib *other) const {
00185   const ClipPlaneAttrib *ta;
00186   DCAST_INTO_R(ta, other, 0);
00187 
00188   if (_operation != ta->_operation) {
00189     return (int)_operation - (int)ta->_operation;
00190   }
00191 
00192   Planes::const_iterator li = _planes.begin();
00193   Planes::const_iterator oli = ta->_planes.begin();
00194 
00195   while (li != _planes.end() && oli != ta->_planes.end()) {
00196     PlaneNode *plane = (*li);
00197     PlaneNode *other_plane = (*oli);
00198 
00199     if (plane != other_plane) {
00200       return plane < other_plane ? -1 : 1;
00201     }
00202 
00203     ++li;
00204     ++oli;
00205   }
00206 
00207   if (li != _planes.end()) {
00208     return 1;
00209   }
00210   if (oli != ta->_planes.end()) {
00211     return -1;
00212   }
00213   
00214   return 0;
00215 }
00216 
00217 ////////////////////////////////////////////////////////////////////
00218 //     Function: ClipPlaneAttrib::compose_impl
00219 //       Access: Protected, Virtual
00220 //  Description: Intended to be overridden by derived RenderAttrib
00221 //               types to specify how two consecutive RenderAttrib
00222 //               objects of the same type interact.
00223 //
00224 //               This should return the result of applying the other
00225 //               RenderAttrib to a node in the scene graph below this
00226 //               RenderAttrib, which was already applied.  In most
00227 //               cases, the result is the same as the other
00228 //               RenderAttrib (that is, a subsequent RenderAttrib
00229 //               completely replaces the preceding one).  On the other
00230 //               hand, some kinds of RenderAttrib (for instance,
00231 //               ColorTransformAttrib) might combine in meaningful
00232 //               ways.
00233 ////////////////////////////////////////////////////////////////////
00234 CPT(RenderAttrib) ClipPlaneAttrib::
00235 compose_impl(const RenderAttrib *other) const {
00236   const ClipPlaneAttrib *ta;
00237   DCAST_INTO_R(ta, other, 0);
00238 
00239   if (ta->_operation == O_set) {
00240     // If the other type is O_set, it doesn't matter what we are.
00241     return ta;
00242   }
00243 
00244   if (_operation == ta->_operation) {
00245     // If the operation types match, the composition is simply the
00246     // union.
00247     return do_add(ta, _operation);
00248 
00249   } else if (ta->_operation == O_remove) {
00250     // If the other operation type is remove, and our type is add or
00251     // set, then remove.
00252     return do_remove(ta, _operation);
00253 
00254   } else if (_operation == O_remove) {
00255     // If our type is remove, then the other one wins.
00256     return ta;
00257 
00258   } else {
00259     // Otherwise, the result is the union.
00260     return do_add(ta, _operation);
00261   }
00262 }
00263 
00264 ////////////////////////////////////////////////////////////////////
00265 //     Function: ClipPlaneAttrib::invert_compose_impl
00266 //       Access: Protected, Virtual
00267 //  Description: Intended to be overridden by derived RenderAttrib
00268 //               types to specify how two consecutive RenderAttrib
00269 //               objects of the same type interact.
00270 //
00271 //               See invert_compose() and compose_impl().
00272 ////////////////////////////////////////////////////////////////////
00273 CPT(RenderAttrib) ClipPlaneAttrib::
00274 invert_compose_impl(const RenderAttrib *other) const {
00275   // I think in this case the other attrib always wins.  Maybe this
00276   // needs a bit more thought.  It's hard to imagine that it's even
00277   // important to compute this properly.
00278   return other;
00279 }
00280 
00281 ////////////////////////////////////////////////////////////////////
00282 //     Function: ClipPlaneAttrib::make_default_impl
00283 //       Access: Protected, Virtual
00284 //  Description: Intended to be overridden by derived ClipPlaneAttrib
00285 //               types to specify what the default property for a
00286 //               ClipPlaneAttrib of this type should be.
00287 //
00288 //               This should return a newly-allocated ClipPlaneAttrib of
00289 //               the same type that corresponds to whatever the
00290 //               standard default for this kind of ClipPlaneAttrib is.
00291 ////////////////////////////////////////////////////////////////////
00292 RenderAttrib *ClipPlaneAttrib::
00293 make_default_impl() const {
00294   return new ClipPlaneAttrib;
00295 }
00296 
00297 ////////////////////////////////////////////////////////////////////
00298 //     Function: ClipPlaneAttrib::do_add
00299 //       Access: Private
00300 //  Description: Returns a new ClipPlaneAttrib that represents all the
00301 //               planes of this attrib, with those of the other one
00302 //               added in.
00303 ////////////////////////////////////////////////////////////////////
00304 CPT(RenderAttrib) ClipPlaneAttrib::
00305 do_add(const ClipPlaneAttrib *other, ClipPlaneAttrib::Operation op) const {
00306   Planes::const_iterator ai = _planes.begin();
00307   Planes::const_iterator bi = other->_planes.begin();
00308 
00309   // Create a new ClipPlaneAttrib that will hold the result.
00310   ClipPlaneAttrib *new_attrib = new ClipPlaneAttrib;
00311   new_attrib->_operation = op;
00312   back_insert_iterator<Planes> result = 
00313     back_inserter(new_attrib->_planes);
00314 
00315   while (ai != _planes.end() && bi != other->_planes.end()) {
00316     if ((*ai) < (*bi)) {
00317       // Here is a plane that we have in the original, which is not
00318       // present in the secondary.
00319       *result = *ai;
00320       ++ai;
00321       ++result;
00322     } else if ((*bi) < (*ai)) {
00323       // Here is a new plane we have in the secondary, that was not
00324       // present in the original.
00325       *result = *bi;
00326       ++bi;
00327       ++result;
00328     } else {
00329       // Here is a plane we have in both.
00330       *result = *ai;
00331       ++ai;
00332       ++bi;
00333       ++result;
00334     }
00335   }
00336 
00337   while (ai != _planes.end()) {
00338     *result = *ai;
00339     ++ai;
00340     ++result;
00341   }
00342 
00343   while (bi != other->_planes.end()) {
00344     *result = *bi;
00345     ++bi;
00346     ++result;
00347   }
00348 
00349   return return_new(new_attrib);
00350 }
00351 
00352 ////////////////////////////////////////////////////////////////////
00353 //     Function: ClipPlaneAttrib::do_remove
00354 //       Access: Private
00355 //  Description: Returns a new ClipPlaneAttrib that represents all the
00356 //               planes of this attrib, with those of the other one
00357 //               removed.
00358 ////////////////////////////////////////////////////////////////////
00359 CPT(RenderAttrib) ClipPlaneAttrib::
00360 do_remove(const ClipPlaneAttrib *other, ClipPlaneAttrib::Operation op) const {
00361   Planes::const_iterator ai = _planes.begin();
00362   Planes::const_iterator bi = other->_planes.begin();
00363 
00364   // Create a new ClipPlaneAttrib that will hold the result.
00365   ClipPlaneAttrib *new_attrib = new ClipPlaneAttrib;
00366   new_attrib->_operation = op;
00367   back_insert_iterator<Planes> result = 
00368     back_inserter(new_attrib->_planes);
00369 
00370   while (ai != _planes.end() && bi != other->_planes.end()) {
00371     if ((*ai) < (*bi)) {
00372       // Here is a plane that we have in the original, which is
00373       // not present in the secondary.  Keep it.
00374       *result = *ai;
00375       ++ai;
00376       ++result;
00377     } else if ((*bi) < (*ai)) {
00378       // Here is a new plane we have in the secondary, that was
00379       // not present in the original.  Ignore it.
00380       ++bi;
00381     } else {
00382       // Here is a plane we have in both.  Drop it.
00383       ++ai;
00384       ++bi;
00385     }
00386   }
00387 
00388   while (ai != _planes.end()) {
00389     *result = *ai;
00390     ++ai;
00391     ++result;
00392   }
00393 
00394   return return_new(new_attrib);
00395 }
00396 
00397 ////////////////////////////////////////////////////////////////////
00398 //     Function: ClipPlaneAttrib::register_with_read_factory
00399 //       Access: Public, Static
00400 //  Description: Tells the BamReader how to create objects of type
00401 //               ClipPlaneAttrib.
00402 ////////////////////////////////////////////////////////////////////
00403 void ClipPlaneAttrib::
00404 register_with_read_factory() {
00405   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00406 }
00407 
00408 ////////////////////////////////////////////////////////////////////
00409 //     Function: ClipPlaneAttrib::write_datagram
00410 //       Access: Public, Virtual
00411 //  Description: Writes the contents of this object to the datagram
00412 //               for shipping out to a Bam file.
00413 ////////////////////////////////////////////////////////////////////
00414 void ClipPlaneAttrib::
00415 write_datagram(BamWriter *manager, Datagram &dg) {
00416   RenderAttrib::write_datagram(manager, dg);
00417 
00418   dg.add_int8((int)_operation);
00419   PN_uint16 num_planes = _planes.size();
00420   nassertv(num_planes == _planes.size());
00421   dg.add_uint16(num_planes);
00422 
00423   Planes::const_iterator li;
00424   for (li = _planes.begin(); li != _planes.end(); ++li) {
00425     PlaneNode *plane = (*li);
00426     manager->write_pointer(dg, plane);
00427   }
00428 }
00429 
00430 ////////////////////////////////////////////////////////////////////
00431 //     Function: ClipPlaneAttrib::complete_pointers
00432 //       Access: Public, Virtual
00433 //  Description: Receives an array of pointers, one for each time
00434 //               manager->read_pointer() was called in fillin().
00435 //               Returns the number of pointers processed.
00436 ////////////////////////////////////////////////////////////////////
00437 int ClipPlaneAttrib::
00438 complete_pointers(TypedWritable **p_list, BamReader *manager) {
00439   int pi = RenderAttrib::complete_pointers(p_list, manager);
00440 
00441   Planes::iterator li;
00442   for (li = _planes.begin(); li != _planes.end(); ++li) {
00443     PlaneNode *node;
00444     DCAST_INTO_R(node, p_list[pi++], pi);
00445     (*li) = node;
00446   }
00447 
00448   return pi;
00449 }
00450 
00451 ////////////////////////////////////////////////////////////////////
00452 //     Function: ClipPlaneAttrib::make_from_bam
00453 //       Access: Protected, Static
00454 //  Description: This function is called by the BamReader's factory
00455 //               when a new object of type ClipPlaneAttrib is encountered
00456 //               in the Bam file.  It should create the ClipPlaneAttrib
00457 //               and extract its information from the file.
00458 ////////////////////////////////////////////////////////////////////
00459 TypedWritable *ClipPlaneAttrib::
00460 make_from_bam(const FactoryParams &params) {
00461   ClipPlaneAttrib *attrib = new ClipPlaneAttrib;
00462   DatagramIterator scan;
00463   BamReader *manager;
00464 
00465   parse_params(params, scan, manager);
00466   attrib->fillin(scan, manager);
00467 
00468   return attrib;
00469 }
00470 
00471 ////////////////////////////////////////////////////////////////////
00472 //     Function: ClipPlaneAttrib::fillin
00473 //       Access: Protected
00474 //  Description: This internal function is called by make_from_bam to
00475 //               read in all of the relevant data from the BamFile for
00476 //               the new ClipPlaneAttrib.
00477 ////////////////////////////////////////////////////////////////////
00478 void ClipPlaneAttrib::
00479 fillin(DatagramIterator &scan, BamReader *manager) {
00480   RenderAttrib::fillin(scan, manager);
00481 
00482   _operation = (Operation)scan.get_int8();
00483   int num_planes = scan.get_uint16();
00484 
00485   for (int i = 0; i < num_planes; i++) {
00486     manager->read_pointer(scan);
00487     _planes.push_back(NULL);
00488   }
00489 }

Generated on Fri May 2 00:41:08 2003 for Panda by doxygen1.3