Panda3D
|
00001 // Filename: linearNoiseForce.cxx 00002 // Created by: charles (13Jun00) 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 <stdlib.h> 00016 #include <math.h> 00017 00018 #include "linearNoiseForce.h" 00019 00020 // declare the statics 00021 00022 ConfigVariableInt LinearNoiseForce::_random_seed 00023 ("default_noise_force_seed", 665); 00024 00025 bool LinearNoiseForce::_initialized = false; 00026 unsigned char LinearNoiseForce::_prn_table[256]; 00027 LVector3 LinearNoiseForce::_gradient_table[256]; 00028 00029 TypeHandle LinearNoiseForce::_type_handle; 00030 00031 //////////////////////////////////////////////////////////////////// 00032 // Function : InitNoiseTables 00033 // Access : Public 00034 // Description : One-time config function, sets up the PRN 00035 // lattice. 00036 //////////////////////////////////////////////////////////////////// 00037 void LinearNoiseForce:: 00038 init_noise_tables() { 00039 // since this is a repeatable noise function, we always want 00040 // to init with the same seed. 00041 srand(_random_seed); 00042 00043 LVector3 *gtable = _gradient_table; 00044 for (int i = 0; i < 256; ++i) { 00045 // fill the 1d array 00046 _prn_table[i] = rand() & 255; 00047 00048 // now fill the gradient array 00049 *gtable++ = random_unit_vector(); 00050 } 00051 } 00052 00053 //////////////////////////////////////////////////////////////////// 00054 // Function : LinearNoiseForce 00055 // Access : Public 00056 // Description : constructor 00057 //////////////////////////////////////////////////////////////////// 00058 LinearNoiseForce:: 00059 LinearNoiseForce(PN_stdfloat a, bool mass) : 00060 LinearRandomForce(a, mass) { 00061 if (_initialized == false) { 00062 init_noise_tables(); 00063 _initialized = true; 00064 } 00065 } 00066 00067 //////////////////////////////////////////////////////////////////// 00068 // Function : LinearNoiseForce 00069 // Access : Public 00070 // Description : copy constructor 00071 //////////////////////////////////////////////////////////////////// 00072 LinearNoiseForce:: 00073 LinearNoiseForce(const LinearNoiseForce ©) : 00074 LinearRandomForce(copy) { 00075 } 00076 00077 //////////////////////////////////////////////////////////////////// 00078 // Function : ~LinearNoiseForce 00079 // Access : Public 00080 // Description : destructor 00081 //////////////////////////////////////////////////////////////////// 00082 LinearNoiseForce:: 00083 ~LinearNoiseForce() { 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function : make_copy 00088 // Access : Public 00089 // Description : copier 00090 //////////////////////////////////////////////////////////////////// 00091 LinearForce *LinearNoiseForce:: 00092 make_copy() { 00093 return new LinearNoiseForce(*this); 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function : get_child_vector 00098 // Access : Public 00099 // Description : Returns the noise value based on the object's 00100 // position. 00101 //////////////////////////////////////////////////////////////////// 00102 LVector3 LinearNoiseForce:: 00103 get_child_vector(const PhysicsObject *po) { 00104 LPoint3 p = po->get_position(); 00105 00106 // get all of the components 00107 int int_x, int_y, int_z; 00108 PN_stdfloat frac_x, frac_y, frac_z; 00109 00110 int_x = (int) p[0]; 00111 frac_x = p[0] - int_x; 00112 00113 int_y = (int) p[1]; 00114 frac_y = p[1] - int_y; 00115 00116 int_z = (int) p[2]; 00117 frac_z = p[2] - int_z; 00118 00119 // apply the cubic smoother to the fractional values 00120 PN_stdfloat cubic_x, cubic_y, cubic_z; 00121 00122 cubic_x = cubic_step(frac_x); 00123 cubic_y = cubic_step(frac_y); 00124 cubic_z = cubic_step(frac_z); 00125 00126 // trilinear interpolation into the cube (over, in, down) 00127 LVector3 temp0, temp1, temp2, temp3; 00128 00129 // x direction 00130 temp0 = vlerp(cubic_x, get_lattice_entry(p), 00131 get_lattice_entry(p[0] + 1.0f, p[1], p[2])); 00132 00133 temp1 = vlerp(cubic_x, get_lattice_entry(p[0], p[1], p[2] + 1.0f), 00134 get_lattice_entry(p[0] + 1.0f, p[1], p[2] + 1.0f)); 00135 00136 temp2 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2]), 00137 get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2])); 00138 00139 temp3 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2] + 1.0f), 00140 get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2] + 1.0f)); 00141 00142 // y direction 00143 temp0 = vlerp(cubic_z, temp0, temp1); 00144 temp1 = vlerp(cubic_z, temp2, temp3); 00145 00146 // z direction 00147 return vlerp(cubic_y, temp0, temp1); 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function : output 00152 // Access : Public 00153 // Description : Write a string representation of this instance to 00154 // <out>. 00155 //////////////////////////////////////////////////////////////////// 00156 void LinearNoiseForce:: 00157 output(ostream &out) const { 00158 #ifndef NDEBUG //[ 00159 out<<""<<"LinearNoiseForce"; 00160 #endif //] NDEBUG 00161 } 00162 00163 //////////////////////////////////////////////////////////////////// 00164 // Function : write 00165 // Access : Public 00166 // Description : Write a string representation of this instance to 00167 // <out>. 00168 //////////////////////////////////////////////////////////////////// 00169 void LinearNoiseForce:: 00170 write(ostream &out, unsigned int indent) const { 00171 #ifndef NDEBUG //[ 00172 out.width(indent); 00173 out<<""<<"LinearNoiseForce:"; 00174 out.width(indent+2); out<<""; out<<"_random_seed"<<_random_seed<<" (class)\n"; 00175 out.width(indent+2); out<<""; out<<"_initialized"<<_initialized<<" (class)\n"; 00176 out.width(indent+2); out<<""; out<<"_gradient_table"<<_gradient_table<<" (class)\n"; 00177 out.width(indent+2); out<<""; out<<"_prn_table"<<_prn_table<<" (class)\n"; 00178 LinearRandomForce::write(out, indent+2); 00179 #endif //] NDEBUG 00180 }