00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef debug
00022 #undef debug
00023 #endif
00024
00025
00026 #include <GB/GB_ExtraMacros.h>
00027 #include <GB/GB_AttributeDefines.h>
00028 #include <GEO/GEO_AttributeHandle.h>
00029 #include <GEO/GEO_Point.h>
00030 #include <GU/GU_Detail.h>
00031 #include <GU/GU_PrimPoly.h>
00032
00033 #include <SYS/SYS_Math.h>
00034 #include <UT/UT_XformOrder.h>
00035 #include <UT/UT_Vector3.h>
00036 #include <UT/UT_Vector4.h>
00037 #include <UT/UT_Matrix3.h>
00038 #include <UT/UT_Matrix4.h>
00039
00040 #include <UT/UT_DSOVersion.h>
00041 #include <time.h>
00042
00043 #include "VRAY_StampFile.h"
00044
00045
00046
00047 inline fpreal remapValue(fpreal minV, fpreal maxV, fpreal value)
00048 {
00049 return (minV +(maxV- minV)*value);
00050 }
00051
00052
00053
00054
00055
00056
00057 class vray_InstanceFile : public VRAY_Procedural
00058 {
00059 public:
00060 vray_InstanceFile(UT_Vector3 pCenter, fpreal pSize, int pSizeRandom,
00061 UT_Vector3 pNormal, GU_Detail *pCFileGDP, UT_Vector3 pPointCd)
00062 : m_InstCenter(pCenter), m_InstSizeFactor(pSize), m_InstSizeRandomPerc(pSizeRandom),
00063 m_mOrientUp(pNormal), m_InstFileGDP(pCFileGDP),m_InstDiffColor(pPointCd)
00064 {
00065 fpreal doubleSize=m_InstSizeFactor*2;
00066 m_InstBox.expandBounds(doubleSize, doubleSize, doubleSize);
00067 }
00068 virtual const char *getClassName();
00069 virtual void getBoundingBox(UT_BoundingBox &box);
00070 virtual int initialize(const UT_BoundingBox *);
00071 virtual void render();
00072
00073 private:
00074 int m_InstSizeRandomPerc;
00075 fpreal m_InstSizeFactor;
00076 UT_BoundingBox m_InstBox;
00077 GU_Detail *m_InstFileGDP;
00078 UT_Vector3 m_InstDiffColor, m_InstCenter, m_mOrientUp;
00079 };
00080
00081 static VRAY_ProceduralArg theArgs[] =
00082 {
00083 VRAY_ProceduralArg("minbound", "real", "-1 -1 -1"),
00084 VRAY_ProceduralArg("maxbound", "real", "1 1 1"),
00085 VRAY_ProceduralArg("geoscale", "real", "1"),
00086 VRAY_ProceduralArg("randscale", "int", "1"),
00087 VRAY_ProceduralArg("geofile", "string", ""),
00088 VRAY_ProceduralArg("blurfile", "string", ""),
00089 VRAY_ProceduralArg("velocityblur", "int", "1"),
00090 VRAY_ProceduralArg("shutter", "real", "1"),
00091 VRAY_ProceduralArg()
00092 };
00093
00094 VRAY_Procedural * allocProcedural(const char *)
00095 {
00096 return new VRAY_StampFile();
00097 }
00098
00099 const VRAY_ProceduralArg * getProceduralArgs(const char *)
00100 {
00101 return theArgs;
00102 }
00103
00104 VRAY_StampFile::VRAY_StampFile()
00105 {
00106 m_Box.initBounds(0, 0, 0);
00107 }
00108
00109 VRAY_StampFile::~VRAY_StampFile()
00110 {}
00111
00112 const char * VRAY_StampFile::getClassName()
00113 {
00114 return "VRAY_StampFile";
00115 }
00116
00117 int VRAY_StampFile::initialize(const UT_BoundingBox *)
00118 {
00119 fpreal val[3]={-1., -1., -1.};
00120 import("minbound", val, 3);
00121 m_Box.initBounds(val[0], val[1], val[2]);
00122
00123 val[0] = val[1] = val[2] = 1;
00124 import("maxbound", val, 3);
00125 m_Box.enlargeBounds(val[0], val[1], val[2]);
00126
00127 if (!import("geoscale", &m_GeoSizeFactor, 1))
00128 m_GeoSizeFactor = 1;
00129 if (!import("randscale", &m_SizeRandomPerc, 1))
00130 m_SizeRandomPerc = 0;
00131
00132 import("geofile", m_File);
00133 import("blurfile", m_BlurFile);
00134
00135 return 1;
00136 }
00137
00138 void VRAY_StampFile::getBoundingBox(UT_BoundingBox &box)
00139 {
00140 box = m_Box;
00141 box.enlargeBounds(0, m_GeoSizeFactor);
00142 }
00143
00144 void VRAY_StampFile::render()
00145 {
00146 UT_Vector3 pointCd(-1., -1. , -1.);
00147 UT_Vector4 pValue, nValue;
00148
00149 GEO_AttributeHandle pHandle, nHandle;
00150 const GEO_Point * ppt=NULL;
00151 GU_Detail *pGuDetail;
00152
00153 void *handle = queryObject(NULL);
00154 handle = queryObject(m_Object);
00155 if (!handle)
00156 {
00157 fprintf(stderr, "VRAY_StampFile: Unable to find handle for %s\n", (const char *)m_Object);
00158 return;
00159 }
00160 const GU_Detail *sgdp;
00161 sgdp = queryGeometry(handle, 0);
00162 if (!sgdp)
00163 {
00164 fprintf(stderr, "VRAY_StampFile: There's no geometry for %s\n", (const char *)m_Object);
00165 return;
00166 }
00167
00168 pGuDetail = allocateGeometry();
00169
00170 if (pGuDetail->load(m_File, 0) < 0)
00171 {
00172 fprintf(stderr, "VRAY_StampFile: Unable to load geometry: '%s'\n",(const char *)m_File);
00173 freeGeometry(pGuDetail);
00174 return;
00175 }
00176 int npts = sgdp->points().entries();
00177
00178 #ifdef debug
00179 std::cout <<" parent npts: "<< npts <<std::endl;
00180 #endif
00181 pHandle = sgdp->getAttribute(GEO_POINT_DICT, "P");
00182 if (!pHandle.isAttributeValid())
00183 {
00184 fprintf(stderr, "VRAY_StampFile: no points in geometry '%s'\n",(const char *)m_File);
00185 return;
00186 }
00187 nHandle = sgdp->getAttribute(GEO_POINT_DICT, "N");
00188 int pCdIndex=sgdp->findDiffuseAttribute ( GEO_POINT_DICT );
00189 m_Box.enlargeBounds(m_GeoSizeFactor, m_GeoSizeFactor, m_GeoSizeFactor);
00190
00191 for (int i = 0; i < npts; i++)
00192 {
00193 ppt=(sgdp->points()(i));
00194 pHandle.setElement(ppt);
00195 nHandle.setElement(ppt);
00196 if (pCdIndex >= 0)
00197 pointCd= *ppt->castAttribData<UT_Vector3> (pCdIndex);
00198 pValue = pHandle.getV3();
00199 nValue = nHandle.getV3();
00200
00201 openProceduralObject();
00202 addProcedural( new vray_InstanceFile(pValue, m_GeoSizeFactor, m_SizeRandomPerc, nValue, pGuDetail, pointCd) );
00203 closeObject();
00204 }
00205 }
00206
00207
00208 const char * vray_InstanceFile::getClassName()
00209 {
00210 return "vray_InstanceFile";
00211 }
00212
00213
00214 int vray_InstanceFile::initialize(const UT_BoundingBox *)
00215 {
00216 fprintf(stderr, "VRAY_StampFile: This method should never be called\n");
00217 return 0;
00218 }
00219
00220 void vray_InstanceFile::getBoundingBox(UT_BoundingBox &box)
00221 {
00222 box.initBounds(m_InstCenter);
00223 box.enlargeBounds(0, m_InstSizeFactor);
00224 }
00225
00226
00227 void vray_InstanceFile::render()
00228 {
00229 GU_Detail gdp(m_InstFileGDP);
00230 GU_Detail *instGdp=NULL;
00231 GEO_Primitive *prim=NULL;
00232
00233 UT_Matrix4 rXform(1.0);
00234 UT_Matrix4 instXform(1.0);
00235 UT_Matrix3 rotXform(1.0);
00236 UT_Vector3 *iCd=NULL;
00237 instGdp = allocateGeometry();
00238
00239
00240 int pCdIndex = gdp.addDiffuseAttribute( GEO_PRIMITIVE_DICT );
00241 #ifdef debug
00242 std::cout<<"rgb: " << m_InstDiffColor[0] <<" "<<m_InstDiffColor[1] << " "<<m_InstDiffColor[2]<< std::endl;
00243 #endif
00244 if (pCdIndex >=0)
00245 {
00246 if (m_InstDiffColor[0] < 0)
00247 {
00248
00249 unsigned int seed=static_cast <unsigned>( time(NULL)/m_InstCenter.x()+ 1000*m_InstCenter.z());
00250 m_InstDiffColor[0]=SYSfastRandom(seed);
00251 m_InstDiffColor[1]=SYSfastRandom(seed);
00252 m_InstDiffColor[2]=SYSfastRandom(seed);
00253 }
00254
00255 FOR_ALL_PRIMITIVES(&gdp, prim)
00256 {
00257 iCd = prim->castAttribData<UT_Vector3> ( pCdIndex);
00258 iCd->assign(m_InstDiffColor[0], m_InstDiffColor[1], m_InstDiffColor[2]);
00259 }
00260 }
00261
00262
00263 if (m_InstSizeRandomPerc >0)
00264 {
00265 unsigned int seed=static_cast <unsigned>( time(NULL)*m_InstCenter.y()-m_InstCenter.x()*1000 );
00266 fpreal dsx=SYSfastRandom(seed);
00267 fpreal dsy=SYSfastRandom(seed);
00268 fpreal dsz=SYSfastRandom(seed);
00269
00270 dsx=remapValue(-m_InstSizeRandomPerc, m_InstSizeRandomPerc, dsx);
00271 dsy=remapValue(-m_InstSizeRandomPerc, m_InstSizeRandomPerc, dsy);
00272 dsz=remapValue(-m_InstSizeRandomPerc, m_InstSizeRandomPerc, dsz);
00273 instXform.scale(m_InstSizeFactor+dsx/100, m_InstSizeFactor+dsy/100, m_InstSizeFactor+dsz/100);
00274 }
00275 else
00276 instXform.scale(m_InstSizeFactor,m_InstSizeFactor,m_InstSizeFactor);
00277
00278 m_mOrientUp.normalize();
00279 UT_Vector3 iUp = UT_Vector3(0,1,0);
00280 rotXform.orient(iUp, m_mOrientUp);
00281 UT_Matrix4 tXform(1.0);
00282 tXform=rotXform;
00283 instXform=instXform*tXform;
00284
00285 #ifdef debug
00286 std::cout << "myDir:"<< time(NULL) <<std::endl <<std::endl;
00287 #endif
00288
00289 instXform.translate(m_InstCenter.x(), m_InstCenter.y(), m_InstCenter.z() );
00290 gdp.transform(instXform);
00291
00292 instGdp->merge(gdp);
00293 #ifdef debug
00294 int inpts = instGdp->points().entries();
00295 int npts = gdp.points().entries();
00296 std::cout << m_InstSizeFactor<<" child npts: "<< npts<<" : "<<inpts << " m_FileGDP: " <<std::endl;
00297 #endif
00298
00299
00300 openGeometryObject();
00301 addGeometry(instGdp, 0);
00302 closeObject();
00303 }