1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/*******************************************************************************
* photonshootingtask.cpp
*
* This module implements Photon Mapping.
*
* Author: Nathan Kopp
*
* ---------------------------------------------------------------------------
* Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
* Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
*
* POV-Ray is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* POV-Ray is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------------
* POV-Ray is based on the popular DKB raytracer version 2.12.
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
* ---------------------------------------------------------------------------
* $File: //depot/public/povray/3.x/source/backend/lighting/photonshootingtask.cpp $
* $Revision: #1 $
* $Change: 6069 $
* $DateTime: 2013/11/06 11:59:40 $
* $Author: chrisc $
*******************************************************************************/
// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"
#include "base/povms.h"
#include "base/povmsgid.h"
#include "backend/math/vector.h"
#include "backend/math/matrices.h"
#include "backend/scene/objects.h"
#include "backend/shape/csg.h"
#include "backend/support/octree.h"
#include "backend/bounding/bbox.h"
#include "backend/scene/threaddata.h"
#include "backend/scene/scene.h"
#include "backend/scene/view.h"
#include "backend/support/msgutil.h"
#include "backend/lighting/point.h"
#include "backend/lighting/photonshootingtask.h"
#include "lightgrp.h"
#include <algorithm>
// this must be the last file included
#include "base/povdebug.h"
namespace pov
{
PhotonShootingTask::PhotonShootingTask(ViewData *vd, PhotonShootingStrategy* strategy) :
RenderTask(vd),
trace(vd->GetSceneData(), GetViewDataPtr(), vd->GetSceneData()->photonSettings.Max_Trace_Level,
vd->GetSceneData()->photonSettings.adcBailout, vd->GetQualityFeatureFlags(), cooperate),
messageFactory(10, 370, "Photon", vd->GetSceneData()->backendAddress, vd->GetSceneData()->frontendAddress, vd->GetSceneData()->sceneId, 0), // TODO FIXME - Values need to come from the correct place!
rands(0.0, 1.0, 32768),
randgen(&rands),
strategy(strategy),
cooperate(*this),
maxTraceLevel(vd->GetSceneData()->photonSettings.Max_Trace_Level),
adcBailout(vd->GetSceneData()->photonSettings.adcBailout)
{
}
PhotonShootingTask::~PhotonShootingTask()
{
}
PhotonMap* PhotonShootingTask::getMediaPhotonMap()
{
return GetViewDataPtr()->mediaPhotonMap;
}
PhotonMap* PhotonShootingTask::getSurfacePhotonMap()
{
return GetViewDataPtr()->surfacePhotonMap;
}
void PhotonShootingTask::SendProgress(void)
{
if (timer.ElapsedRealTime() > 1000)
{
// TODO FIXME PHOTONS
// with multiple threads shooting photons, the stats messages get confusing on the front-end.
// this is because each thread sends its own count, and so varying numbers get displayed.
// the totals should be combined and sent from a single thread.
timer.Reset();
POVMS_Object obj(kPOVObjectClass_PhotonProgress);
obj.SetInt(kPOVAttrib_CurrentPhotonCount, GetViewDataPtr()->surfacePhotonMap->numPhotons + GetViewDataPtr()->mediaPhotonMap->numPhotons);
RenderBackend::SendViewOutput(GetViewData()->GetViewId(), GetSceneData()->frontendAddress, kPOVMsgIdent_Progress, obj);
}
}
void PhotonShootingTask::Run()
{
// quit right away if photons not enabled
if (!GetSceneData()->photonSettings.photonsEnabled) return;
Cooperate();
PhotonShootingUnit* unit = strategy->getNextUnit();
while(unit)
{
//ShootPhotonsAtObject(unit->lightAndObject.target, unit->lightAndObject.light);
ShootPhotonsAtObject(unit->lightAndObject);
unit = strategy->getNextUnit();
}
// good idea to make sure all warnings and errors arrive frontend now [trf]
SendProgress();
Cooperate();
}
void PhotonShootingTask::Stopped()
{
// nothing to do for now [trf]
}
void PhotonShootingTask::Finish()
{
GetViewDataPtr()->timeType = SceneThreadData::kPhotonTime;
GetViewDataPtr()->realTime = ConsumedRealTime();
GetViewDataPtr()->cpuTime = ConsumedCPUTime();
}
void PhotonShootingTask::ShootPhotonsAtObject(LightTargetCombo& combo)
{
RGBColour colour; /* light color */
Colour PhotonColour; /* photon color */
int i; /* counter */
DBL theta, phi; /* rotation angles */
DBL dphi; /* deltas for theta and phi */
DBL jittheta, jitphi; /* jittered versions of theta and phi */
DBL minphi,maxphi;
/* these are minimum and maximum for theta and
phi for the spiral shooting */
DBL Attenuation; /* light attenuation for spotlight */
TRANSFORM Trans; /* transformation for rotation */
int mergedFlags=0; /* merged flags to see if we should shoot photons */
int notComputed=true; /* have the ray containers been computed for this point yet?*/
int hitAtLeastOnce = false; /* have we hit the object at least once - for autostop stuff */
ViewThreadData *renderDataPtr = GetViewDataPtr();
/* get the light source colour */
colour = combo.light->colour;
/* set global variable stuff */
renderDataPtr->photonSourceLight = combo.light;
renderDataPtr->photonTargetObject = combo.target;
/* first, check on various flags... make sure all is a go for this ObjectPtr */
mergedFlags = combo.computeMergedFlags();
if (!( ((mergedFlags & PH_RFR_ON_FLAG) && !(mergedFlags & PH_RFR_OFF_FLAG)) ||
((mergedFlags & PH_RFL_ON_FLAG) && !(mergedFlags & PH_RFL_OFF_FLAG)) ))
/* it is a no-go for this object... bail out now */
return;
renderDataPtr->photonSpread = combo.photonSpread;
/* ---------------------------------------------
main ray-shooting loop
--------------------------------------------- */
i = 0;
notComputed = true;
for(theta=combo.mintheta; theta<combo.maxtheta; theta+=combo.dtheta)
{
Cooperate();
SendProgress();
renderDataPtr->hitObject = false;
if (theta<EPSILON)
{
dphi=2*M_PI;
}
else
{
/* remember that for area lights, "theta" really means "radius" */
if (combo.light->Parallel)
{
dphi = combo.dtheta / theta;
}
else
{
dphi=combo.dtheta/sin(theta);
}
}
// FIXME: should copy from previously computed shootingdirection
ShootingDirection shootingDirection(combo.light,combo.target);
shootingDirection.compute();
minphi = -M_PI + dphi*randgen()*0.5;
maxphi = M_PI - dphi/2 + (minphi+M_PI);
for(phi=minphi; phi<maxphi; phi+=dphi)
{
int x_samples,y_samples;
int area_x, area_y;
/* ------------------- shoot one photon ------------------ */
/* jitter theta & phi */
jitphi = phi + (dphi)*(randgen() - 0.5)*1.0*GetSceneData()->photonSettings.jitter;
jittheta = theta + (combo.dtheta)*(randgen() - 0.5)*1.0*GetSceneData()->photonSettings.jitter;
/* actually, shoot multiple samples for area light */
if(combo.light->Area_Light && combo.light->Photon_Area_Light && !combo.light->Parallel)
{
x_samples = combo.light->Area_Size1;
y_samples = combo.light->Area_Size2;
}
else
{
x_samples = 1;
y_samples = 1;
}
for(area_x=0; area_x<x_samples; area_x++)
{
for(area_y=0; area_y<y_samples; area_y++)
{
Ray ray; /* ray that we shoot */
Assign_Vector(ray.Origin,combo.light->Center);
if (combo.light->Area_Light && combo.light->Photon_Area_Light && !combo.light->Parallel)
{
shootingDirection.recomputeForAreaLight(ray,area_x,area_y);
/* we must recompute the media containers (new start point) */
notComputed = true;
}
DBL dist_of_initial_from_center;
if (combo.light->Parallel)
{
DBL a;
VECTOR v;
/* assign the direction */
Assign_Vector(ray.Direction,combo.light->Direction);
/* project ctr onto plane defined by Direction & light location */
VDot(a,ray.Direction, shootingDirection.toctr);
VScale(v,ray.Direction, -a*shootingDirection.dist); /* MAYBE NEEDS TO BE NEGATIVE! */
VAdd(ray.Origin, shootingDirection.ctr, v);
/* move point along "left" distance theta (remember theta means rad) */
VScale(v,shootingDirection.left,jittheta);
/* rotate pt around ray.Direction by phi */
/* use POV funcitons... slower but easy */
Compute_Axis_Rotation_Transform(&Trans,combo.light->Direction,jitphi);
MTransPoint(v, v, &Trans);
VAddEq(ray.Origin, v);
// compute the length of "v" if we're going to use it
if (combo.light->Light_Type == CYLINDER_SOURCE)
{
VECTOR initial_from_center;
VSub(initial_from_center, ray.Origin, combo.light->Center);
VLength(dist_of_initial_from_center, initial_from_center);
}
}
else
{
DBL st,ct; /* cos(theta) & sin(theta) for rotation */
/* rotate toctr by theta around up */
st = sin(jittheta);
ct = cos(jittheta);
/* use fast rotation */
shootingDirection.v[X] = -st*shootingDirection.left[X] + ct*shootingDirection.toctr[X];
shootingDirection.v[Y] = -st*shootingDirection.left[Y] + ct*shootingDirection.toctr[Y];
shootingDirection.v[Z] = -st*shootingDirection.left[Z] + ct*shootingDirection.toctr[Z];
/* then rotate by phi around toctr */
/* use POV funcitons... slower but easy */
Compute_Axis_Rotation_Transform(&Trans,shootingDirection.toctr,jitphi);
MTransPoint(ray.Direction, shootingDirection.v, &Trans);
}
/* ------ attenuation for spot/cylinder (copied from point.c) ---- */
Attenuation = computeAttenuation(combo.light, ray, dist_of_initial_from_center);
/* set up defaults for reflection, refraction */
renderDataPtr->passThruPrev = true;
renderDataPtr->passThruThis = false;
renderDataPtr->photonDepth = 0.0;
// GetViewDataPtr()->Trace_Level = 0;
// Total_Depth = 0.0;
renderDataPtr->Stats()[Number_Of_Photons_Shot]++;
/* attenuate for area light extra samples */
Attenuation/=(x_samples*y_samples);
/* compute photon color from light source & attenuation */
PhotonColour = Colour(colour * Attenuation);
if (Attenuation<0.00001) continue;
/* handle the projected_through object if it exists */
if (combo.light->Projected_Through_Object != NULL)
{
/* try to intersect ray with projected-through ObjectPtr */
Intersection Intersect;
Intersect.Object = NULL;
if ( trace.FindIntersection(combo.light->Projected_Through_Object, Intersect, ray) )
{
/* we must recompute the media containers (new start point) */
notComputed = true;
/* we did hit it, so find the 'real' starting point of the ray */
/* find the farthest intersection */
VAddScaledEq(ray.Origin,Intersect.Depth+EPSILON, ray.Direction);
renderDataPtr->photonDepth += Intersect.Depth+EPSILON;
while(trace.FindIntersection( combo.light->Projected_Through_Object, Intersect, ray) )
{
VAddScaledEq(ray.Origin, Intersect.Depth+EPSILON, ray.Direction);
renderDataPtr->photonDepth += Intersect.Depth+EPSILON;
}
}
else
{
/* we didn't hit it, so stop now */
continue;
}
}
/* As mike said, "fire photon torpedo!" */
//Initialize_Ray_Containers(&ray);
ray.ClearInteriors () ;
for(vector<ObjectPtr>::iterator object = GetSceneData()->objects.begin(); object != GetSceneData()->objects.end(); object++)
{
if((*object)->Inside(ray.Origin, renderDataPtr) && ((*object)->interior != NULL))
ray.AppendInterior((*object)->interior);
}
notComputed = false;
//disp_elem = 0; /* for dispersion */
//disp_nelems = 0; /* for dispersion */
Trace::TraceTicket ticket(maxTraceLevel, adcBailout);
ray.SetFlags(Ray::PrimaryRay, false, true);
trace.TraceRay(ray, PhotonColour, 1.0, ticket, false);
/* display here */
if ((i++%100) == 0)
{
Cooperate();
SendProgress();
}
} // for(area_y...)
} // for(area_x...)
}
/* if we didn't hit anything and we're past the autostop angle, then
we should stop
as per suggestion from Smellenberg, changed autostop to a percentage
of the object's bounding sphere. */
/* suggested by Pabs, we only use autostop if we have it it once */
if (renderDataPtr->hitObject) hitAtLeastOnce=true;
if (hitAtLeastOnce && !renderDataPtr->hitObject && renderDataPtr->photonTargetObject)
if (theta > GetSceneData()->photonSettings.autoStopPercent*combo.maxtheta)
break;
} /* end of rays loop */
}
DBL PhotonShootingTask::computeAttenuation(const LightSource* Light, const Ray& ray, DBL dist_of_initial_from_center)
{
DBL costheta_spot;
DBL Attenuation = 1.0;
/* ---------- spot light --------- */
if (Light->Light_Type == SPOT_SOURCE)
{
VDot(costheta_spot, ray.Direction, Light->Direction);
if (costheta_spot > 0.0)
{
Attenuation = pow(costheta_spot, Light->Coeff);
if (Light->Radius > 0.0)
Attenuation *= cubic_spline(Light->Falloff, Light->Radius, costheta_spot);
}
else
Attenuation = 0.0;
}
/* ---------- cylinder light ----------- */
else if (Light->Light_Type == CYLINDER_SOURCE)
{
DBL k, len;
VDot(k, ray.Direction, Light->Direction);
if (k > 0.0)
{
len = dist_of_initial_from_center;
if (len < Light->Falloff)
{
DBL dist = 1.0 - len / Light->Falloff;
Attenuation = pow(dist, Light->Coeff);
if (Light->Radius > 0.0 && len > Light->Radius)
Attenuation *= cubic_spline(0.0, 1.0 - Light->Radius / Light->Falloff, dist);
}
else
Attenuation = 0.0;
}
else
Attenuation = 0.0;
}
return Attenuation;
}
}
|