00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <UT/UT_DSOVersion.h>
00029 #include <UT/UT_Color.h>
00030 #include <GEO/GEO_PrimPart.h>
00031 #include <GEO/GEO_Point.h>
00032 #include <GU/GU_Detail.h>
00033 #include <PRM/PRM_Include.h>
00034 #include <OP/OP_Operator.h>
00035 #include <OP/OP_OperatorTable.h>
00036 #include "POP_SpotLight.h"
00037
00038
00039
00040 using namespace HDK_Sample;
00041
00042 static PRM_Name names[] =
00043 {
00044 PRM_Name("center", "Center"),
00045 PRM_Name(0)
00046 };
00047
00048 PRM_Template
00049 POP_SpotLight::myTemplateList[] =
00050 {
00051 PRM_Template(PRM_FLT_J, 1, &POPactivateName, PRMoneDefaults, 0,
00052 &PRMunitRange),
00053 PRM_Template(PRM_STRING, 1, &POPsourceName, 0,
00054 &POP_Node::pointGroupMenu),
00055 PRM_Template(PRM_XYZ_J, 3, &names[0]),
00056 PRM_Template()
00057 };
00058
00059 OP_TemplatePair
00060 POP_SpotLight::myTemplatePair (myTemplateList, &POP_LocalVar::myTemplatePair);
00061
00062 OP_VariablePair
00063 POP_SpotLight::myVariablePair (0, &POP_LocalVar::myVariablePair);
00064
00065
00066
00067 void
00068 newPopOperator (OP_OperatorTable* table)
00069 {
00070 table->addOperator(
00071 new OP_Operator("hdk_spotlight",
00072 "SpotLight",
00073 POP_SpotLight::myConstructor,
00074 &POP_SpotLight::myTemplatePair,
00075 1,
00076 1,
00077 &POP_SpotLight::myVariablePair));
00078 }
00079
00080
00081
00082 OP_Node*
00083 POP_SpotLight::myConstructor (OP_Network* net, const char* name,
00084 OP_Operator* entry)
00085 {
00086 return new POP_SpotLight(net, name, entry);
00087 }
00088
00089 int *POP_SpotLight::myIndirect = 0;
00090
00091 POP_SpotLight::POP_SpotLight (OP_Network* net, const char* name,
00092 OP_Operator* entry)
00093 :POP_LocalVar (net, name, entry)
00094 {
00095 if (!myIndirect)
00096 myIndirect = allocIndirect(sizeof(myTemplateList)/sizeof(PRM_Template));
00097 }
00098
00099 POP_SpotLight::~POP_SpotLight (void)
00100 {
00101 }
00102
00103 OP_ERROR
00104 POP_SpotLight::cookPop (OP_Context& context)
00105 {
00106 POP_ContextData* data = (POP_ContextData*) context.getData();
00107 float t = context.myTime;
00108 int thread = context.getThread();
00109 GEO_PrimParticle* part;
00110 GEO_ParticleVertex* pvtx;
00111 POP_FParam centerx;
00112 POP_FParam centery;
00113 POP_FParam centerz;
00114 UT_String sourceName;
00115 const GB_PointGroup *sourceGroup = NULL;
00116
00117
00118 if (lockInputs(context) >= UT_ERROR_ABORT)
00119 return(error());
00120
00121 setupDynamicVars(data);
00122
00123
00124 if (buildParticleList(context) >= UT_ERROR_ABORT)
00125 goto done;
00126
00127
00128
00129 if (data->isGuideOnly())
00130 goto done;
00131
00132
00133 if (!checkActivation(data, (POP_FParam) &POP_SpotLight::ACTIVATE))
00134 goto done;
00135
00136
00137 SOURCE(sourceName);
00138 if (sourceName.isstring())
00139 {
00140 sourceGroup = parsePointGroups((const char*) sourceName,
00141 data->getDetail());
00142 if (!sourceGroup)
00143 {
00144 addError(POP_BAD_GROUP, sourceName);
00145 goto done;
00146 }
00147 }
00148
00149
00150 setupVars(data, sourceGroup);
00151
00152
00153
00154
00155 POP_FCACHE(centerx, CENTERX, getCenterX, myCenterX, POP_SpotLight);
00156 POP_FCACHE(centery, CENTERY, getCenterY, myCenterY, POP_SpotLight);
00157 POP_FCACHE(centerz, CENTERZ, getCenterZ, myCenterZ, POP_SpotLight);
00158
00159 if (error() >= UT_ERROR_ABORT)
00160 goto done;
00161
00162
00163
00164
00165 myCurrIter = 0;
00166
00167
00168
00169 if (sourceGroup)
00170 {
00171 FOR_ALL_GROUP_POINTS(data->getDetail(), sourceGroup, myCurrPt)
00172 {
00173 changePoint(myCurrPt, data, t, centerx, centery, centerz);
00174 myCurrIter++;
00175 }
00176 }
00177 else
00178 {
00179
00180
00181 for (part = myParticleList.iterateInit() ;
00182 part ; part = myParticleList.iterateNext())
00183 {
00184 for (pvtx = part->iterateInit() ; pvtx ; pvtx = pvtx->next)
00185 {
00186 myCurrPt = pvtx->getPt();
00187 changePoint(myCurrPt, data, t, centerx, centery, centerz);
00188 myCurrIter++;
00189 }
00190 }
00191 }
00192
00193 done:
00194
00195 cleanupDynamicVars();
00196
00197 unlockInputs();
00198
00199
00200 myCurrPt = NULL;
00201
00202 return error();
00203 }
00204
00205 void
00206 POP_SpotLight::changePoint (GEO_Point* ppt, POP_ContextData* data, float t,
00207 POP_FParam centerx, POP_FParam centery,
00208 POP_FParam centerz)
00209 {
00210 UT_Vector3 center;
00211 UT_Vector3* color;
00212 UT_Vector3 d;
00213 UT_Vector3 p;
00214 UT_Color HSVtoRGB(UT_HSV);
00215 UT_Color RGBtoHSV(UT_RGB);
00216 float r, g, b;
00217 float h, s, v;
00218 float d2;
00219
00220
00221
00222
00223
00224 center.assign(POP_PEVAL(centerx), POP_PEVAL(centery), POP_PEVAL(centerz));
00225
00226
00227 p = ppt->getPos();
00228 d = p - center;
00229
00230
00231 color = ppt->castAttribData<UT_Vector3>(data->getDiffuseOffset());
00232
00233
00234 RGBtoHSV.setValue(color->x(), color->y(), color->z());
00235 RGBtoHSV.getHSV(&h, &s, &v);
00236
00237
00238 d2 = d.length2();
00239 v = UTequalZero(d2) ? 1.0f : 1.0f / d.length2();
00240
00241
00242 if (v > 1.0f)
00243 v = 1.0f;
00244 HSVtoRGB.setValue(h, s, v);
00245 HSVtoRGB.getRGB(&r, &g, &b);
00246
00247 color->assign(r, g, b);
00248 }
00249
00250 void
00251 POP_SpotLight::addAttrib (void* userdata)
00252 {
00253 POP_ContextData* data = (POP_ContextData*) userdata;
00254
00255
00256 addDiffuseAttrib(data);
00257 }