00001 // Filename: lightLensNode.cxx 00002 // Created by: drose (26Mar02) 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 "lightLensNode.h" 00016 #include "bamWriter.h" 00017 #include "bamReader.h" 00018 #include "datagram.h" 00019 #include "datagramIterator.h" 00020 #include "renderState.h" 00021 #include "cullFaceAttrib.h" 00022 #include "colorWriteAttrib.h" 00023 00024 TypeHandle LightLensNode::_type_handle; 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: LightLensNode::Constructor 00028 // Access: Published 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 LightLensNode:: 00032 LightLensNode(const string &name, Lens *lens) : 00033 Camera(name, lens) 00034 { 00035 set_active(false); 00036 _shadow_caster = false; 00037 _sb_xsize = 512; 00038 _sb_ysize = 512; 00039 _sb_sort = -10; 00040 // Backface culling helps eliminating artifacts. 00041 set_initial_state(RenderState::make(CullFaceAttrib::make_reverse(), 00042 ColorWriteAttrib::make(ColorWriteAttrib::C_off))); 00043 } 00044 00045 //////////////////////////////////////////////////////////////////// 00046 // Function: LightLensNode::Destructor 00047 // Access: Published 00048 // Description: 00049 //////////////////////////////////////////////////////////////////// 00050 LightLensNode:: 00051 ~LightLensNode() { 00052 set_active(false); 00053 clear_shadow_buffers(); 00054 } 00055 00056 //////////////////////////////////////////////////////////////////// 00057 // Function: LightLensNode::Copy Constructor 00058 // Access: Protected 00059 // Description: 00060 //////////////////////////////////////////////////////////////////// 00061 LightLensNode:: 00062 LightLensNode(const LightLensNode ©) : 00063 Light(copy), 00064 Camera(copy) 00065 { 00066 _shadow_caster = false; 00067 _sb_xsize = 512; 00068 _sb_ysize = 512; 00069 _sb_sort = -10; 00070 // Backface culling helps eliminating artifacts. 00071 set_initial_state(RenderState::make(CullFaceAttrib::make_reverse(), 00072 ColorWriteAttrib::make(ColorWriteAttrib::C_off))); 00073 } 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: LightLensNode::clear_shadow_buffers 00077 // Access: Protected 00078 // Description: Clears the shadow buffers, meaning they will be 00079 // automatically recreated when the Shader Generator 00080 // needs them. 00081 //////////////////////////////////////////////////////////////////// 00082 void LightLensNode:: 00083 clear_shadow_buffers() { 00084 ShadowBuffers::iterator it; 00085 for(it = _sbuffers.begin(); it != _sbuffers.end(); ++it) { 00086 (*it).first->remove_window((*it).second); 00087 } 00088 _sbuffers.clear(); 00089 } 00090 00091 //////////////////////////////////////////////////////////////////// 00092 // Function: LightLensNode::as_node 00093 // Access: Published, Virtual 00094 // Description: Returns the Light object upcast to a PandaNode. 00095 //////////////////////////////////////////////////////////////////// 00096 PandaNode *LightLensNode:: 00097 as_node() { 00098 return this; 00099 } 00100 00101 //////////////////////////////////////////////////////////////////// 00102 // Function: LightLensNode::as_light 00103 // Access: Public, Virtual 00104 // Description: Cross-casts the node to a Light pointer, if it is one 00105 // of the four kinds of Light nodes, or returns NULL if 00106 // it is not. 00107 //////////////////////////////////////////////////////////////////// 00108 Light *LightLensNode:: 00109 as_light() { 00110 return this; 00111 } 00112 00113 //////////////////////////////////////////////////////////////////// 00114 // Function: LightLensNode::output 00115 // Access: Public, Virtual 00116 // Description: 00117 //////////////////////////////////////////////////////////////////// 00118 void LightLensNode:: 00119 output(ostream &out) const { 00120 LensNode::output(out); 00121 } 00122 00123 //////////////////////////////////////////////////////////////////// 00124 // Function: LightLensNode::write 00125 // Access: Public, Virtual 00126 // Description: 00127 //////////////////////////////////////////////////////////////////// 00128 void LightLensNode:: 00129 write(ostream &out, int indent_level) const { 00130 LensNode::write(out, indent_level); 00131 } 00132 00133 //////////////////////////////////////////////////////////////////// 00134 // Function: LightLensNode::write_datagram 00135 // Access: Public, Virtual 00136 // Description: Writes the contents of this object to the datagram 00137 // for shipping out to a Bam file. 00138 //////////////////////////////////////////////////////////////////// 00139 void LightLensNode:: 00140 write_datagram(BamWriter *manager, Datagram &dg) { 00141 Camera::write_datagram(manager, dg); 00142 Light::write_datagram(manager, dg); 00143 00144 dg.add_bool(_shadow_caster); 00145 dg.add_int32(_sb_xsize); 00146 dg.add_int32(_sb_ysize); 00147 dg.add_int32(_sb_sort); 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: LightLensNode::fillin 00152 // Access: Protected 00153 // Description: This internal function is called by make_from_bam to 00154 // read in all of the relevant data from the BamFile for 00155 // the new LightLensNode. 00156 //////////////////////////////////////////////////////////////////// 00157 void LightLensNode:: 00158 fillin(DatagramIterator &scan, BamReader *manager) { 00159 Camera::fillin(scan, manager); 00160 Light::fillin(scan, manager); 00161 00162 bool shadow_caster = scan.get_bool(); 00163 int sb_xsize = scan.get_int32(); 00164 int sb_ysize = scan.get_int32(); 00165 int sb_sort = scan.get_int32(); 00166 set_shadow_caster(shadow_caster, sb_xsize, sb_ysize, sb_sort); 00167 } 00168