我的替身到底怎么了
What the heck is happening to my doubles?
所以,我正在编写一个光线追踪器,我有一个基本的类来表示视口。它看起来像这样:
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.5fn"
" 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,但输出不对应于此。我的其他双算术也出了问题。
Michael Dorgan指出,相机可能是一个堆栈变量超出了范围,这正是它!
Camera(可能)是一个堆栈变量,您正在保存它。是不是超出了范围?:)
相关文章:
- 不断收到相同的错误...怎么了?C++
- 编译器没有显示错误,但我没有得到任何输出.怎么了?
- qsort 不适用于我的 impl 中的浮点类型。怎么了?
- 在C++中表现不佳.怎么了?
- 我的程序怎么了?(C )
- 这里怎么了?单位转换器
- 错误:未定义对 .. 的引用.怎么了
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 我在C++文件中的简单代码块上有 2 个错误。错误 C2059:语法错误:"?"。第二智能感知:预期表达式。怎么了?
- 我的代码怎么了?带有QT的原始输入API
- 使用 cin.get() 似乎没有读取我期望的字符。怎么了?
- 当其中一个字符串来自cin时,无法比较两个字符串.这里怎么了
- "5"怎么了?纸上一切都很好
- 我的cout怎么了
- C++:腹肌怎么了
- Qt OpenGL with Widgets:怎么了?
- 程序为某些条目提供了错误的输出!怎么了?
- 自动和复制构造函数:怎么了?
- 怎么了???只有七行
- 我的替身到底怎么了