Panda3D
 All Classes Functions Variables Enumerations
stencilAttrib.cxx
00001 // Filename: stencilAttrib.cxx
00002 // Created by:  aignacio (18May06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "stencilAttrib.h"
00016 #include "graphicsStateGuardianBase.h"
00017 #include "dcast.h"
00018 #include "bamReader.h"
00019 #include "bamWriter.h"
00020 #include "datagram.h"
00021 #include "datagramIterator.h"
00022 
00023 TypeHandle StencilAttrib::_type_handle;
00024 int StencilAttrib::_attrib_slot;
00025 
00026 const char *StencilAttrib::
00027 stencil_render_state_name_array [StencilAttrib::SRS_total] =
00028 {
00029   "SRS_front_enable",
00030   "SRS_back_enable",
00031 
00032   "SRS_front_comparison_function",
00033   "SRS_front_stencil_fail_operation",
00034   "SRS_front_stencil_pass_z_fail_operation",
00035   "SRS_front_stencil_pass_z_pass_operation",
00036 
00037   "SRS_reference",
00038   "SRS_read_mask",
00039   "SRS_write_mask",
00040 
00041   "SRS_back_comparison_function",
00042   "SRS_back_stencil_fail_operation",
00043   "SRS_back_stencil_pass_z_fail_operation",
00044   "SRS_back_stencil_pass_z_pass_operation",
00045 
00046   "SRS_clear",
00047   "SRS_clear_value",
00048 };
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: StencilAttrib::Constructor
00052 //       Access: Private
00053 //  Description: Use StencilAttrib::make() to construct a new
00054 //               StencilAttrib object.
00055 ////////////////////////////////////////////////////////////////////
00056 StencilAttrib::
00057 StencilAttrib() {
00058 
00059   _stencil_render_states [SRS_front_enable] = 0;
00060   _stencil_render_states [SRS_back_enable] = 0;
00061 
00062   _stencil_render_states [SRS_front_comparison_function] = SCF_always;
00063   _stencil_render_states [SRS_front_stencil_fail_operation] = SO_keep;
00064   _stencil_render_states [SRS_front_stencil_pass_z_fail_operation] = SO_keep;
00065   _stencil_render_states [SRS_front_stencil_pass_z_pass_operation] = SO_keep;
00066 
00067   _stencil_render_states [SRS_reference] = 0;
00068   _stencil_render_states [SRS_read_mask] = ~0;
00069   _stencil_render_states [SRS_write_mask] = ~0;
00070 
00071   _stencil_render_states [SRS_back_comparison_function] = SCF_always;
00072   _stencil_render_states [SRS_back_stencil_fail_operation] = SO_keep;
00073   _stencil_render_states [SRS_back_stencil_pass_z_fail_operation] = SO_keep;
00074   _stencil_render_states [SRS_back_stencil_pass_z_pass_operation] = SO_keep;
00075 
00076   _stencil_render_states [SRS_clear] = 0;
00077   _stencil_render_states [SRS_clear_value] = 0;
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: StencilAttrib::make_off
00082 //       Access: Published, Static
00083 //  Description: Constructs a StencilAttrib that has stenciling 
00084 //               turned off.
00085 ////////////////////////////////////////////////////////////////////
00086 CPT(RenderAttrib) StencilAttrib::
00087 make_off()
00088 {
00089   StencilAttrib *attrib = new StencilAttrib;
00090 
00091   return return_new(attrib);
00092 }
00093 
00094 ////////////////////////////////////////////////////////////////////
00095 //     Function: StencilAttrib::make_default
00096 //       Access: Published, Static
00097 //  Description: Returns a RenderAttrib that corresponds to whatever
00098 //               the standard default properties for render attributes
00099 //               of this type ought to be.
00100 ////////////////////////////////////////////////////////////////////
00101 CPT(RenderAttrib) StencilAttrib::
00102 make_default() {
00103   return return_new(new StencilAttrib);
00104 }
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: StencilAttrib::make
00108 //       Access: Published, Static
00109 //  Description: Constructs a front face StencilAttrib.
00110 ////////////////////////////////////////////////////////////////////
00111 CPT(RenderAttrib) StencilAttrib::
00112 make(
00113   unsigned int front_enable,
00114   unsigned int front_comparison_function,
00115   unsigned int stencil_fail_operation,
00116   unsigned int stencil_pass_z_fail_operation,
00117   unsigned int front_stencil_pass_z_pass_operation,
00118   unsigned int reference,
00119   unsigned int read_mask,
00120   unsigned int write_mask)
00121 {
00122   StencilAttrib *attrib = new StencilAttrib;
00123 
00124   attrib->_stencil_render_states [SRS_front_enable] = front_enable;
00125   attrib->_stencil_render_states [SRS_back_enable] = 0;
00126 
00127   attrib->_stencil_render_states [SRS_front_comparison_function] = front_comparison_function;
00128   attrib->_stencil_render_states [SRS_front_stencil_fail_operation] = stencil_fail_operation;
00129   attrib->_stencil_render_states [SRS_front_stencil_pass_z_fail_operation] = stencil_pass_z_fail_operation;
00130   attrib->_stencil_render_states [SRS_front_stencil_pass_z_pass_operation] = front_stencil_pass_z_pass_operation;
00131 
00132   attrib->_stencil_render_states [SRS_reference] = reference;
00133   attrib->_stencil_render_states [SRS_read_mask] = read_mask;
00134   attrib->_stencil_render_states [SRS_write_mask] = write_mask;
00135 
00136   attrib->_stencil_render_states [SRS_back_comparison_function] = SCF_always;
00137   attrib->_stencil_render_states [SRS_back_stencil_fail_operation] = SO_keep;
00138   attrib->_stencil_render_states [SRS_back_stencil_pass_z_fail_operation] = SO_keep;
00139   attrib->_stencil_render_states [SRS_back_stencil_pass_z_pass_operation] = SO_keep;
00140 
00141   return return_new(attrib);
00142 }
00143 
00144 ////////////////////////////////////////////////////////////////////
00145 //     Function: StencilAttrib::make_2_sided
00146 //       Access: Published, Static
00147 //  Description: Constructs a two-sided StencilAttrib.
00148 ////////////////////////////////////////////////////////////////////
00149 CPT(RenderAttrib) StencilAttrib::
00150 make_2_sided(
00151   unsigned int front_enable,
00152   unsigned int back_enable,
00153   unsigned int front_comparison_function,
00154   unsigned int stencil_fail_operation,
00155   unsigned int stencil_pass_z_fail_operation,
00156   unsigned int front_stencil_pass_z_pass_operation,
00157   unsigned int reference,
00158   unsigned int read_mask,
00159   unsigned int write_mask,
00160   unsigned int back_comparison_function,
00161   unsigned int back_stencil_fail_operation,
00162   unsigned int back_stencil_pass_z_fail_operation,
00163   unsigned int back_stencil_pass_z_pass_operation)
00164 {
00165   StencilAttrib *attrib = new StencilAttrib;
00166 
00167   attrib->_stencil_render_states [SRS_front_enable] = front_enable;
00168   attrib->_stencil_render_states [SRS_back_enable] = back_enable;
00169 
00170   attrib->_stencil_render_states [SRS_front_comparison_function] = front_comparison_function;
00171   attrib->_stencil_render_states [SRS_front_stencil_fail_operation] = stencil_fail_operation;
00172   attrib->_stencil_render_states [SRS_front_stencil_pass_z_fail_operation] = stencil_pass_z_fail_operation;
00173   attrib->_stencil_render_states [SRS_front_stencil_pass_z_pass_operation] = front_stencil_pass_z_pass_operation;
00174 
00175   attrib->_stencil_render_states [SRS_reference] = reference;
00176   attrib->_stencil_render_states [SRS_read_mask] = read_mask;
00177   attrib->_stencil_render_states [SRS_write_mask] = write_mask;
00178 
00179   attrib->_stencil_render_states [SRS_back_comparison_function] = back_comparison_function;
00180   attrib->_stencil_render_states [SRS_back_stencil_fail_operation] = back_stencil_fail_operation;
00181   attrib->_stencil_render_states [SRS_back_stencil_pass_z_fail_operation] = back_stencil_pass_z_fail_operation;
00182   attrib->_stencil_render_states [SRS_back_stencil_pass_z_pass_operation] = back_stencil_pass_z_pass_operation;
00183 
00184   return return_new(attrib);
00185 }
00186 
00187 ////////////////////////////////////////////////////////////////////
00188 //     Function: StencilAttrib::make
00189 //       Access: Published, Static
00190 //  Description: Constructs a front face StencilAttrib.
00191 ////////////////////////////////////////////////////////////////////
00192 CPT(RenderAttrib) StencilAttrib::
00193 make_with_clear(
00194   unsigned int front_enable,
00195   unsigned int front_comparison_function,
00196   unsigned int stencil_fail_operation,
00197   unsigned int stencil_pass_z_fail_operation,
00198   unsigned int front_stencil_pass_z_pass_operation,
00199   unsigned int reference,
00200   unsigned int read_mask,
00201   unsigned int write_mask,
00202   unsigned int clear,
00203   unsigned int clear_value)
00204 {
00205   StencilAttrib *attrib = new StencilAttrib;
00206 
00207   attrib->_stencil_render_states [SRS_front_enable] = front_enable;
00208   attrib->_stencil_render_states [SRS_back_enable] = 0;
00209 
00210   attrib->_stencil_render_states [SRS_front_comparison_function] = front_comparison_function;
00211   attrib->_stencil_render_states [SRS_front_stencil_fail_operation] = stencil_fail_operation;
00212   attrib->_stencil_render_states [SRS_front_stencil_pass_z_fail_operation] = stencil_pass_z_fail_operation;
00213   attrib->_stencil_render_states [SRS_front_stencil_pass_z_pass_operation] = front_stencil_pass_z_pass_operation;
00214 
00215   attrib->_stencil_render_states [SRS_reference] = reference;
00216   attrib->_stencil_render_states [SRS_read_mask] = read_mask;
00217   attrib->_stencil_render_states [SRS_write_mask] = write_mask;
00218 
00219   attrib->_stencil_render_states [SRS_back_comparison_function] = SCF_always;
00220   attrib->_stencil_render_states [SRS_back_stencil_fail_operation] = SO_keep;
00221   attrib->_stencil_render_states [SRS_back_stencil_pass_z_fail_operation] = SO_keep;
00222   attrib->_stencil_render_states [SRS_back_stencil_pass_z_pass_operation] = SO_keep;
00223 
00224   attrib->_stencil_render_states [SRS_clear] = clear;
00225   attrib->_stencil_render_states [SRS_clear_value] = clear_value;
00226 
00227   return return_new(attrib);
00228 }
00229 
00230 ////////////////////////////////////////////////////////////////////
00231 //     Function: StencilAttrib::make_2_sided
00232 //       Access: Published, Static
00233 //  Description: Constructs a two-sided StencilAttrib.
00234 ////////////////////////////////////////////////////////////////////
00235 CPT(RenderAttrib) StencilAttrib::
00236 make_2_sided_with_clear(
00237   unsigned int front_enable,
00238   unsigned int back_enable,
00239   unsigned int front_comparison_function,
00240   unsigned int stencil_fail_operation,
00241   unsigned int stencil_pass_z_fail_operation,
00242   unsigned int front_stencil_pass_z_pass_operation,
00243   unsigned int reference,
00244   unsigned int read_mask,
00245   unsigned int write_mask,
00246   unsigned int back_comparison_function,
00247   unsigned int back_stencil_fail_operation,
00248   unsigned int back_stencil_pass_z_fail_operation,
00249   unsigned int back_stencil_pass_z_pass_operation,
00250   unsigned int clear,
00251   unsigned int clear_value)
00252 {
00253   StencilAttrib *attrib = new StencilAttrib;
00254 
00255   attrib->_stencil_render_states [SRS_front_enable] = front_enable;
00256   attrib->_stencil_render_states [SRS_back_enable] = back_enable;
00257 
00258   attrib->_stencil_render_states [SRS_front_comparison_function] = front_comparison_function;
00259   attrib->_stencil_render_states [SRS_front_stencil_fail_operation] = stencil_fail_operation;
00260   attrib->_stencil_render_states [SRS_front_stencil_pass_z_fail_operation] = stencil_pass_z_fail_operation;
00261   attrib->_stencil_render_states [SRS_front_stencil_pass_z_pass_operation] = front_stencil_pass_z_pass_operation;
00262 
00263   attrib->_stencil_render_states [SRS_reference] = reference;
00264   attrib->_stencil_render_states [SRS_read_mask] = read_mask;
00265   attrib->_stencil_render_states [SRS_write_mask] = write_mask;
00266 
00267   attrib->_stencil_render_states [SRS_back_comparison_function] = back_comparison_function;
00268   attrib->_stencil_render_states [SRS_back_stencil_fail_operation] = back_stencil_fail_operation;
00269   attrib->_stencil_render_states [SRS_back_stencil_pass_z_fail_operation] = back_stencil_pass_z_fail_operation;
00270   attrib->_stencil_render_states [SRS_back_stencil_pass_z_pass_operation] = back_stencil_pass_z_pass_operation;
00271 
00272   attrib->_stencil_render_states [SRS_clear] = clear;
00273   attrib->_stencil_render_states [SRS_clear_value] = clear_value;
00274 
00275   return return_new(attrib);
00276 }
00277 
00278 ////////////////////////////////////////////////////////////////////
00279 //     Function: StencilAttrib::output
00280 //       Access: Public, Virtual
00281 //  Description:
00282 ////////////////////////////////////////////////////////////////////
00283 void StencilAttrib::
00284 output(ostream &out) const {
00285 
00286   int index;
00287   for (index = 0; index < SRS_total; index++) {
00288     out
00289       << "(" << stencil_render_state_name_array [index]
00290       << ", " << _stencil_render_states [index] << ")";
00291   }
00292 }
00293 
00294 ////////////////////////////////////////////////////////////////////
00295 //     Function: StencilAttrib::compare_to_impl
00296 //       Access: Protected, Virtual
00297 //  Description: Intended to be overridden by derived StencilAttrib
00298 //               types to return a unique number indicating whether
00299 //               this StencilAttrib is equivalent to the other one.
00300 //
00301 //               This should return 0 if the two StencilAttrib objects
00302 //               are equivalent, a number less than zero if this one
00303 //               should be sorted before the other one, and a number
00304 //               greater than zero otherwise.
00305 //
00306 //               This will only be called with two StencilAttrib
00307 //               objects whose get_type() functions return the same.
00308 ////////////////////////////////////////////////////////////////////
00309 int StencilAttrib::
00310 compare_to_impl(const RenderAttrib *other) const {
00311   const StencilAttrib *sa;
00312   DCAST_INTO_R(sa, other, 0);
00313 
00314   int a;
00315   int b;
00316   int index;
00317   int compare_result = 0;
00318 
00319   for (index = 0; index < SRS_total; index++) {
00320     a = (int) sa -> _stencil_render_states [index];
00321     b = (int) _stencil_render_states [index];
00322     compare_result = (a - b);
00323     if (compare_result) {
00324       break;
00325     }
00326   }
00327 
00328   return compare_result;
00329 }
00330 
00331 ////////////////////////////////////////////////////////////////////
00332 //     Function: StencilAttrib::get_hash_impl
00333 //       Access: Protected, Virtual
00334 //  Description: Intended to be overridden by derived RenderAttrib
00335 //               types to return a unique hash for these particular
00336 //               properties.  RenderAttribs that compare the same with
00337 //               compare_to_impl(), above, should return the same
00338 //               hash; RenderAttribs that compare differently should
00339 //               return a different hash.
00340 ////////////////////////////////////////////////////////////////////
00341 size_t StencilAttrib::
00342 get_hash_impl() const {
00343   size_t hash = 0;
00344   for (int index = 0; index < SRS_total; index++) {
00345     hash = int_hash::add_hash(hash, (int)_stencil_render_states[index]);
00346   }
00347   return hash;
00348 }
00349 
00350 ////////////////////////////////////////////////////////////////////
00351 //     Function: StencilAttrib::register_with_read_factory
00352 //       Access: Public, Static
00353 //  Description: Tells the BamReader how to create objects of type
00354 //               StencilAttrib.
00355 ////////////////////////////////////////////////////////////////////
00356 void StencilAttrib::
00357 register_with_read_factory() {
00358   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00359 }
00360 
00361 ////////////////////////////////////////////////////////////////////
00362 //     Function: StencilAttrib::write_datagram
00363 //       Access: Public, Virtual
00364 //  Description: Writes the contents of this object to the datagram
00365 //               for shipping out to a Bam file.
00366 ////////////////////////////////////////////////////////////////////
00367 void StencilAttrib::
00368 write_datagram(BamWriter *manager, Datagram &dg) {
00369   RenderAttrib::write_datagram(manager, dg);
00370 
00371   int index;
00372   for (index = 0; index < SRS_total; index++) {
00373     dg.add_int32(_stencil_render_states [index]);
00374   }
00375 }
00376 
00377 ////////////////////////////////////////////////////////////////////
00378 //     Function: StencilAttrib::make_from_bam
00379 //       Access: Protected, Static
00380 //  Description: This function is called by the BamReader's factory
00381 //               when a new object of type StencilAttrib is encountered
00382 //               in the Bam file.  It should create the StencilAttrib
00383 //               and extract its information from the file.
00384 ////////////////////////////////////////////////////////////////////
00385 TypedWritable *StencilAttrib::
00386 make_from_bam(const FactoryParams &params) {
00387   StencilAttrib *attrib = new StencilAttrib;
00388   DatagramIterator scan;
00389   BamReader *manager;
00390 
00391   parse_params(params, scan, manager);
00392   attrib->fillin(scan, manager);
00393 
00394   return attrib;
00395 }
00396 
00397 ////////////////////////////////////////////////////////////////////
00398 //     Function: StencilAttrib::fillin
00399 //       Access: Protected
00400 //  Description: This internal function is called by make_from_bam to
00401 //               read in all of the relevant data from the BamFile for
00402 //               the new StencilAttrib.
00403 ////////////////////////////////////////////////////////////////////
00404 void StencilAttrib::
00405 fillin(DatagramIterator &scan, BamReader *manager) {
00406   RenderAttrib::fillin(scan, manager);
00407 
00408   int index;
00409   for (index = 0; index < SRS_total; index++) {
00410     _stencil_render_states [index] = scan.get_int32();
00411   }
00412 }
 All Classes Functions Variables Enumerations