AggregateBSDF.cpp

Go to the documentation of this file.
00001 /**<!-------------------------------------------------------------------->
00002    @file   AggregateBSDF.cpp
00003    @author Travis Fischer (fisch0920@gmail.com)
00004    @author Matthew Jacobs (jacobs.mh@gmail.com)
00005    @date   Fall 2008
00006    
00007    @brief
00008       Linear combination of different BSDF implementations, where the 
00009    coefficients are determined either a priori (inherent property described 
00010    in scenefile) or via a texture map lookup
00011    <!-------------------------------------------------------------------->**/
00012 
00013 #include "AggregateBSDF.h"
00014 #include "Material.h"
00015 #include <Random.h>
00016 
00017 AggregateBSDF::~AggregateBSDF() {
00018    FOREACH(BSDFListIter, m_bsdfs, iter) {
00019       safeDelete(iter->bsdf);
00020    }
00021 }
00022 
00023 void AggregateBSDF::init() {
00024    BSDF::init();
00025    
00026    if (m_bsdfs.size() <= 0) {
00027       ASSERT(m_parent);
00028       ASSERT(m_parent->contains("bsdfs"));
00029       
00030       m_bsdfs = m_parent->getValue<BSDFList>("bsdfs");
00031    }
00032    
00033    // select active bsdf according to aggregate pdfs (cdf)
00034    const real_t x = Random::sample() - EPSILON;
00035    unsigned index = 0;
00036    real_t sum = 0;
00037    
00038    for(unsigned i = m_bsdfs.size(); i--;) {
00039       // sanity checks
00040       ASSERT(m_bsdfs[i].pdf >= 0 && m_bsdfs[i].pdf <= 1);
00041       ASSERT(m_bsdfs[i].bsdf && m_bsdfs[i].bsdf != this);
00042       sum += m_bsdfs[i].pdf;
00043       
00044       if (x <= sum) {
00045          index = i;
00046          break;
00047       }
00048    }
00049    
00050    ASSERT(sum >= 0 && sum <= 1 + EPSILON);
00051    ASSERT(m_bsdf < m_bsdfs.size());
00052 }
00053 
00054 void AggregateBSDF::setWi(const Vector3 &wi) {
00055    FOREACH(BSDFListIter, m_bsdfs, iter) {
00056       ASSERT(iter->bsdf);
00057       iter->bsdf->setWi(wi);
00058    }
00059 }
00060 
00061 Event AggregateBSDF::sample() {
00062    m_bsdfs[m_bsdf].bsdf->setWi(m_wi);
00063    return m_bsdfs[m_bsdf].bsdf->sample();
00064 }
00065 
00066 real_t AggregateBSDF::getPdf(const Event &event) {
00067    //const unsigned index = event.getMetadata<unsigned>();
00068    // TODO: correct?
00069    m_bsdfs[m_bsdf].bsdf->setWi(m_wi);
00070    return m_bsdfs[m_bsdf].bsdf->getPdf(event) / m_bsdfs[m_bsdf].pdf;
00071 }
00072 
00073 SpectralSampleSet AggregateBSDF::getBSDF(const Vector3 &wi, const Vector3 &wo) {
00074    return m_bsdfs[m_bsdf].bsdf->getBSDF(wi, wo);
00075 }
00076 
00077 bool AggregateBSDF::isSpecular(Event &event) const {
00078    return m_bsdfs[m_bsdf].bsdf->isSpecular(event);
00079 }
00080 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6