PBRT
|
00001 00002 /* 00003 pbrt source code Copyright(c) 1998-2012 Matt Pharr and Greg Humphreys. 00004 00005 This file is part of pbrt. 00006 00007 Redistribution and use in source and binary forms, with or without 00008 modification, are permitted provided that the following conditions are 00009 met: 00010 00011 - Redistributions of source code must retain the above copyright 00012 notice, this list of conditions and the following disclaimer. 00013 00014 - Redistributions in binary form must reproduce the above copyright 00015 notice, this list of conditions and the following disclaimer in the 00016 documentation and/or other materials provided with the distribution. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00019 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00020 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00021 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 */ 00031 00032 #if defined(_MSC_VER) 00033 #pragma once 00034 #endif 00035 00036 #ifndef PBRT_TEXTURES_CHECKERBOARD_H 00037 #define PBRT_TEXTURES_CHECKERBOARD_H 00038 00039 // textures/checkerboard.h* 00040 #include "pbrt.h" 00041 #include "texture.h" 00042 #include "paramset.h" 00043 #include "montecarlo.h" 00044 #include "shape.h" 00045 #include "parallel.h" 00046 #include "progressreporter.h" 00047 00048 // CheckerboardTexture Declarations 00049 template <typename T> class Checkerboard2DTexture : public Texture<T> { 00050 public: 00051 // Checkerboard2DTexture Public Methods 00052 Checkerboard2DTexture(TextureMapping2D *m, Reference<Texture<T> > c1, 00053 Reference<Texture<T> > c2, const string &aa) 00054 : mapping(m), tex1(c1), tex2(c2) { 00055 // Select antialiasing method for _Checkerboard2DTexture_ 00056 if (aa == "none") aaMethod = NONE; 00057 else if (aa == "closedform") aaMethod = CLOSEDFORM; 00058 else { 00059 Warning("Antialiasing mode \"%s\" not understood by " 00060 "Checkerboard2DTexture; using \"closedform\"", aa.c_str()); 00061 aaMethod = CLOSEDFORM; 00062 } 00063 } 00064 ~Checkerboard2DTexture() { 00065 delete mapping; 00066 } 00067 T Evaluate(const DifferentialGeometry &dg) const { 00068 float s, t, dsdx, dtdx, dsdy, dtdy; 00069 mapping->Map(dg, &s, &t, &dsdx, &dtdx, &dsdy, &dtdy); 00070 if (aaMethod == NONE) { 00071 // Point sample _Checkerboard2DTexture_ 00072 if ((Floor2Int(s) + Floor2Int(t)) % 2 == 0) 00073 return tex1->Evaluate(dg); 00074 return tex2->Evaluate(dg); 00075 } 00076 else { 00077 // Compute closed-form box-filtered _Checkerboard2DTexture_ value 00078 00079 // Evaluate single check if filter is entirely inside one of them 00080 float ds = max(fabsf(dsdx), fabsf(dsdy)); 00081 float dt = max(fabsf(dtdx), fabsf(dtdy)); 00082 float s0 = s - ds, s1 = s + ds; 00083 float t0 = t - dt, t1 = t + dt; 00084 if (Floor2Int(s0) == Floor2Int(s1) && Floor2Int(t0) == Floor2Int(t1)) { 00085 // Point sample _Checkerboard2DTexture_ 00086 if ((Floor2Int(s) + Floor2Int(t)) % 2 == 0) 00087 return tex1->Evaluate(dg); 00088 return tex2->Evaluate(dg); 00089 } 00090 00091 // Apply box filter to checkerboard region 00092 #define BUMPINT(x) \ 00093 (Floor2Int((x)/2) + \ 00094 2.f * max((x/2)-Floor2Int(x/2) - .5f, 0.f)) 00095 float sint = (BUMPINT(s1) - BUMPINT(s0)) / (2.f * ds); 00096 float tint = (BUMPINT(t1) - BUMPINT(t0)) / (2.f * dt); 00097 float area2 = sint + tint - 2.f * sint * tint; 00098 if (ds > 1.f || dt > 1.f) 00099 area2 = .5f; 00100 return (1.f - area2) * tex1->Evaluate(dg) + 00101 area2 * tex2->Evaluate(dg); 00102 } 00103 } 00104 private: 00105 // Checkerboard2DTexture Private Data 00106 TextureMapping2D *mapping; 00107 Reference<Texture<T> > tex1, tex2; 00108 enum { NONE, CLOSEDFORM } aaMethod; 00109 }; 00110 00111 00112 template <typename T> class Checkerboard3DTexture : public Texture<T> { 00113 public: 00114 // Checkerboard3DTexture Public Methods 00115 Checkerboard3DTexture(TextureMapping3D *m, Reference<Texture<T> > c1, 00116 Reference<Texture<T> > c2) 00117 : mapping(m), tex1(c1), tex2(c2) { 00118 } 00119 ~Checkerboard3DTexture() { 00120 delete mapping; 00121 } 00122 T Evaluate(const DifferentialGeometry &dg) const { 00123 Vector dpdx, dpdy; 00124 Point p = mapping->Map(dg, &dpdx, &dpdy); 00125 if ((Floor2Int(p.x) + Floor2Int(p.y) + Floor2Int(p.z)) % 2 == 0) 00126 return tex1->Evaluate(dg); 00127 else 00128 return tex2->Evaluate(dg); 00129 } 00130 private: 00131 // Checkerboard3DTexture Private Data 00132 TextureMapping3D *mapping; 00133 Reference<Texture<T> > tex1, tex2; 00134 }; 00135 00136 00137 Texture<float> *CreateCheckerboardFloatTexture(const Transform &tex2world, 00138 const TextureParams &tp); 00139 Texture<Spectrum> *CreateCheckerboardSpectrumTexture(const Transform &tex2world, 00140 const TextureParams &tp); 00141 00142 #endif // PBRT_TEXTURES_CHECKERBOARD_H