camera.h 8.62 KB
Newer Older
Matt Pharr's avatar
Matt Pharr committed
1 2

/*
3
    pbrt source code is Copyright(c) 1998-2016
Matt Pharr's avatar
Matt Pharr committed
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
                        Matt Pharr, Greg Humphreys, and Wenzel Jakob.

    This file is part of pbrt.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are
    met:

    - Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    - Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 */

#if defined(_MSC_VER)
#define NOMINMAX
#pragma once
#endif

#ifndef PBRT_CORE_CAMERA_H
#define PBRT_CORE_CAMERA_H

// core/camera.h*
#include "pbrt.h"
#include "geometry.h"
#include "transform.h"
Matt Pharr's avatar
Matt Pharr committed
45
#include "film.h"
46
#include "sampling.h"
Matt Pharr's avatar
Matt Pharr committed
47

Matt Pharr's avatar
Matt Pharr committed
48 49
namespace pbrt {

50 51 52 53 54 55 56 57
    struct CameraSample {
        Point2f pFilm;
        Point2f pLens;
        Float time;
        Float random;
    };

    struct Bokeh {
58
        /// also used as type
59
        int blades = 0;
60
        /// strength and distribution of optical vignetting
61 62
        float radius = 0.5;
        float rateOfChange = 2;
63
        /// caches to avoid recalc
64 65
        float step;
        std::unique_ptr<Point2f[]> corners;
66
        /// Decribes the ratio of the two circle which determine the shape of the bokeh
67
        float innerRadius;
68
        /// 0 even destribution, <0 center is emphasized, >0 edges are emphasized
69
        int weightDistr;
70
        /// How strong the distribution is emphazised
71 72
        float weightStrength;
        float integral;
73 74
    };

75 76
    void initBokehVerts(Bokeh &bokehOptions);

Matt Pharr's avatar
Matt Pharr committed
77
// Camera Declarations
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
    class Camera {
    public:
        // Camera Interface
        Camera(const AnimatedTransform &CameraToWorld, Float shutterOpen,
               Float shutterClose, Film *film, const Medium *medium);

        virtual ~Camera();

        virtual Float GenerateRay(const CameraSample &sample, Ray *ray) const = 0;

        virtual Float GenerateRayDifferential(const CameraSample &sample,
                                              RayDifferential *rd) const;

        virtual Spectrum We(const Ray &ray, Point2f *pRaster2 = nullptr) const;

        virtual void Pdf_We(const Ray &ray, Float *pdfPos, Float *pdfDir) const;

        virtual Spectrum Sample_Wi(const Interaction &ref, const Point2f &u,
                                   Vector3f *wi, Float *pdf, Point2f *pRaster,
                                   VisibilityTester *vis) const;

        // Camera Public Data
        AnimatedTransform CameraToWorld;
        const Float shutterOpen, shutterClose;
        Film *film;
        const Medium *medium;


    protected:


        Bokeh bokehOptions;

111
        inline Point2f biasSample(const CameraSample &sample, float &weight) const {
112 113


114
            Point2f ret = sample.pLens;
115
            float edge = 0.0f;
116

117
            if (bokehOptions.blades >= 3) {
118

119
                Float a = sample.pLens.x;
120

121
                float xSample = sample.pLens.x;
122 123 124
                int index = static_cast<int>(xSample * bokehOptions.blades);
                index = std::min(index, bokehOptions.blades - 1);
                xSample = (xSample - (index * (1.0f / bokehOptions.blades))) * bokehOptions.blades;
125

126
                int previous = index + 1 == bokehOptions.blades ? 0 : index + 1;
127
                Point2f A = Point2f(0, 0);
128
                Point2f B = bokehOptions.corners[index];
129 130
                Point2f C = bokehOptions.corners[previous];

131

132 133
                Float r1 = xSample;
                Float r2 = sample.pLens.y;
134

135
                // edge and bias?
136

137
                ret = (1 - sqrt(r1)) * A + (sqrt(r1) * (1 - r2)) * B + (sqrt(r1) * r2) * C;
138 139 140
                edge = (r1);

            }// optical vignetting enabled
141
            else if (abs(bokehOptions.rateOfChange) > 0.002f) {
142 143 144

                float rateOfChange = bokehOptions.rateOfChange, radius = bokehOptions.innerRadius;

145
                Point2f s = ConcentricSampleDisk(sample.pLens);
146

147
                // convert film samples to [0-1] scale
148
                Point2f ndc = sample.pFilm;
149 150 151 152 153 154 155
                float res = std::max(film->fullResolution.x, film->fullResolution.y);
                ndc.y = film->fullResolution.y - ndc.y;
                ndc.y = ndc.y - film->fullResolution.y/2;
                ndc.x = ndc.x - film->fullResolution.x/2;
                ndc *=2;
                ndc.x /= res;
                ndc.y /= res;
156

157 158
                // convert to [-1,1]
                float c1 = s.x * s.x +  s.y *  s.y;
159

160 161
                s.x -= rateOfChange * ndc.x;
                s.y -= rateOfChange * ndc.y;
162

163 164
                float c2 = s.x * s.x +  s.y *  s.y;
                c2 *= radius; // shape
165 166


167
                // rejectionSampling
168 169
                if ( c2 > 1)
                    weight = -1.0e20f;
170

171
                ret = s;
172
                edge = std::max(c1, c2);
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187

            } else { //default circle
                Point2f uOffset = 2.f * sample.pLens - Vector2f(1, 1);
                // Handle degeneracy at the origin
                if (uOffset.x == 0 && uOffset.y == 0) return Point2f(0, 0);

                // Apply concentric mapping to point
                Float theta, r;
                if (std::abs(uOffset.x) > std::abs(uOffset.y)) {
                    r = uOffset.x;
                    theta = PiOver4 * (uOffset.y / uOffset.x);
                } else {
                    r = uOffset.y;
                    theta = PiOver2 - PiOver4 * (uOffset.x / uOffset.y);
188 189
                }

190
                ret = r * Point2f(std::cos(theta), std::sin(theta));
191
                edge = std::abs(r);
192 193
            }

194 195 196 197 198 199 200 201 202

            float power = bokehOptions.weightStrength;
            float distr = bokehOptions.weightDistr;
            float intg = bokehOptions.integral;

            if (distr < 0) {
                edge= 1-edge;
                distr= -distr;
                power= -power;
203
            }
204

205 206 207 208 209
            float w = power * pow(edge, distr) + 1 - intg;

            if (weight > 0) {
                weight = w;
            }
210

211 212
            // default
            return ret;
213
        }
214

215 216 217 218 219 220 221
    };


    inline std::ostream &operator<<(std::ostream &os, const CameraSample &cs) {
        os << "[ pFilm: " << cs.pFilm << " , pLens: " << cs.pLens <<
           StringPrintf(", time %f ]", cs.time);
        return os;
Matt Pharr's avatar
Matt Pharr committed
222
    }
Matt Pharr's avatar
Matt Pharr committed
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
    class ProjectiveCamera : public Camera {
    public:
        // ProjectiveCamera Public Methods
        ProjectiveCamera(const AnimatedTransform &CameraToWorld,
                         const Transform &CameraToScreen,
                         const Bounds2f &screenWindow, Float shutterOpen,
                         Float shutterClose, Float lensr, Float focald, Film *film,
                         const Medium *medium)
                : Camera(CameraToWorld, shutterOpen, shutterClose, film, medium),
                  CameraToScreen(CameraToScreen) {
            // Initialize depth of field parameters
            lensRadius = lensr;
            focalDistance = focald;

            // Compute projective camera transformations

            // Compute projective camera screen transformations
            ScreenToRaster =
                    Scale(film->fullResolution.x, film->fullResolution.y, 1) *
                    Scale(1 / (screenWindow.pMax.x - screenWindow.pMin.x),
                          1 / (screenWindow.pMin.y - screenWindow.pMax.y), 1) *
                    Translate(Vector3f(-screenWindow.pMin.x, -screenWindow.pMax.y, 0));
            RasterToScreen = Inverse(ScreenToRaster);
            RasterToCamera = Inverse(CameraToScreen) * RasterToScreen;
        }

    protected:
        // ProjectiveCamera Protected Data
        Transform CameraToScreen, RasterToCamera;
        Transform ScreenToRaster, RasterToScreen;
        Float lensRadius, focalDistance;


    };
258 259
}
// namespace pbrt
Matt Pharr's avatar
Matt Pharr committed
260

Matt Pharr's avatar
Matt Pharr committed
261
#endif  // PBRT_CORE_CAMERA_H