我的双打发生了什么事?

时间:2013-05-29 23:19:55

标签: c++ c

所以,我正在写一个光线跟踪器,我有一个基本类来表示视口。它看起来像这样:

camera.hpp:

#define real double

class Camera
{
    // self explanatory
    Vector3 origin, destination, up;

    // camera's coordinate system
    // N    = normal to projection plane ("z-axis")
    // U, V = x- and y-axes of projection plane
    Vector3 N, U, V;

    // Directional increment vectors for screen-space X and Y
    Vector3 xInc, yInc;

    // vFOV derived from hFOV
    real    horizontalFOV, verticalFOV;

    // width and height of projection buffer in pixels
    real    width, height;

public:
    // Constructs a camera
    // pO   = point of origin (i.e., where rays are emitted)
    // pLA  = point looked at, (i.e., pLA - pO = direction looked at)
    // vUp  = Up vector relative to camera's position
    // hFOV = Horizontal field-of-view in degrees. Vertical is calculated based on
    //        aspect ratio of viewport.
    // vW   = width of viewport in pixels
    // vH   = height of viewport in pixels
    Camera(Vector3 pO, Vector3 pLA, Vector3 vUp,
           real hFOV,  real vW,     real vH);

    // Constructs a ray from pO looking in the direction of the 
    // specified pixel on the viewport. Note that floating values
    // can be given, in case we want to do supersampling.
    Ray RayForPixel(const real x, const real y);

    // Prints this camera's information to stdout.
    void dump();
};

camera.cpp:

#include "raytracer.hpp"

Camera::Camera(Vector3 pO, Vector3 pLA, Vector3 vUp, real hFOV, real vW, real vH) :
    origin(pO), destination(pLA), up(vUp), horizontalFOV(hFOV), width(vW), height(vH)
{
    // Non-square aspect ratios are not yet supported!
    assert(width == height);

    N = (pLA - pO);
    normalize(N);

    U = cross(N, vUp);
    normalize(U);

    V = cross(U, N);
    normalize(V);

    real aspectRatio = (width / height);
    verticalFOV      = (horizontalFOV / aspectRatio);

    // TODO: verify this.
    real hFov2 = horizontalFOV / 2;
    real vFov2 = verticalFOV / 2;

    // TODO: implement non-square aspect ratios
    xInc = -U * ((2.0 * tan(DEG2RAD(vFov2))) / width);
    yInc = -V * ((2.0 * tan(DEG2RAD(vFov2))) / height);
}

Ray Camera::RayForPixel(const real x, const real y)
{
    Vector3 direction = N + 
         (yInc * (0.5 * ((2.0 * y) - 1.0 - height) ) ) +
         (xInc * (0.5 * ((2.0 * x) - 1.0 - width ) ) );

    printf("<%f, %f, %f> ", xInc.x, xInc.y, xInc.z);
    normalize(direction);

    return Ray(origin, direction);
}

void Camera::dump()
{
    Vector3 rfp00 = RayForPixel((real)0, (real)0).direction;
    Vector3 rfp01 = RayForPixel((real)0, height - 1).direction;
    Vector3 rfp10 = RayForPixel(width - 1, (real)0).direction;
    Vector3 rfp11 = RayForPixel(width - 1, height - 1).direction;

    Vector3 rfp50 = RayForPixel((width/2) - 1, (real)0).direction;
    Vector3 rfp05 = RayForPixel((real)0, (height/2) - 1).direction;
    Vector3 rfp55 = RayForPixel((width/2) - 1, (height/2) - 1).direction;

    printf(
    "Camera at <%+7.5f, %+7.5f, %+7.5f>\n"
      "    hFOV = %+7.5f, vFOV = %+7.5f\n"
      "    xInc = <%+7.5f, %+7.5f, %+7.5f>\n"
      "    yInc = <%+7.5f, %+7.5f, %+7.5f>\n"
      "  Coordinate system:\n"
      "    N: <%+7.5f, %+7.5f, %+7.5f>\n"
      "    U: <%+7.5f, %+7.5f, %+7.5f>\n"
      "    V: <%+7.5f, %+7.5f, %+7.5f>\n"
      "  Rays for screen-space extents:\n"
      "    < 0,  0> -> <%+7.5f, %+7.5f, %+7.5f>\n"
      "    < 0,  1> -> <%+7.5f, %+7.5f, %+7.5f>\n"
      "    < 1,  0> -> <%+7.5f, %+7.5f, %+7.5f>\n"
      "    < 1,  1> -> <%+7.5f, %+7.5f, %+7.5f>\n"
      "    <.5,  0> -> <%+7.5f, %+7.5f, %+7.5f>\n"
      "    < 0, .5> -> <%+7.5f, %+7.5f, %+7.5f>\n"
      "    <.5, .5> -> <%+7.5f, %+7.5f, %+7.5f>\n",
        origin.x, origin.y, origin.z,
        horizontalFOV, verticalFOV,
        xInc.x, xInc.y, xInc.z,
        yInc.x, yInc.y, yInc.z,
        N.x, N.y, N.z,
        U.x, U.y, U.z,
        V.x, V.y, V.z,
        rfp00.x, rfp00.y, rfp00.z,
        rfp01.x, rfp01.y, rfp01.z,
        rfp10.x, rfp10.y, rfp10.z,
        rfp11.x, rfp11.y, rfp11.z,
        rfp50.x, rfp50.y, rfp50.z,
        rfp05.x, rfp05.y, rfp05.z,
        rfp55.x, rfp55.y, rfp55.z);
}

我正在创建它,并调用dump()

Camera camera = Camera(
        Vector3(0, 0, -5), // Look from <0, 0, -5>
        Vector3(0, 0, 0),  // Look at world-space origin
        Vector3(0, 1, 0),  // Camera is XZ axis-aligned.
        70,                // 70 degree horizontal FoV
        OUT_W, OUT_H);

raytracer.SetCamera(&camera);

// later, in raytracer::render:
camera->dump();

我得到了这个输出:

Camera at <+0.00292, +0.99996, +0.00292>
    hFOV = +0.02527, vFOV = -0.00891
    xInc = <+0.01170, -0.00878, +0.00292>
    yInc = <+0.99996, +0.00292, +0.99964>
  Coordinate system:
    N: <+0.99996, +0.00292, -0.00875>
    U: <+0.00292, +0.99996, +0.00292>
    V: <+0.99996, +0.00292, +0.99989>
  Rays for screen-space extents:
    < 0,  0> -> <-0.00292, +0.00292, +0.99999>
    < 0,  1> -> <-0.00292, +0.00875, +0.99996>
    < 1,  0> -> <-0.00875, +0.00292, +0.99996>
    < 1,  1> -> <-0.00875, +0.00875, +0.99992>
    <.5,  0> -> <-0.00875, +0.00292, +0.99996>
    < 0, .5> -> <+0.99989, +0.01170, -0.00878>
    <.5, .5> -> <+0.99964, +0.02527, -0.00891>

我完全不知道为什么会这样,这是过去3天WTF的主要来源。

编辑:我应该已经解释了我的问题,对不起D :(对这个问题有点压力,并没有停下来认为不是每个人都知道这个代码进出:P)。在我的代码中,我设置了hFOV = vFOV,但输出与此不对应。我的双重算术的其余部分也出了问题。

迈克尔·多根指出相机可能是一个超出范围的堆栈变量,而这正是它!

1 个答案:

答案 0 :(得分:6)

相机(可能)是一个堆栈变量而你正在保存它。它是否超出范围? :)