生成一个向量
generate a vector
我有一条射线从(x0,y0,z0)开始,到屏幕上的一个像素结束。此外,我有一个带有A x B像素的屏幕。
如何在 (i,j) 像素处生成从起点到终点的射线?
我知道公式,但我无法在 c++ 中实现它。感谢您的帮助
您没有足够的信息。
您需要知道:
- 视点(即摄像机在看哪个点)
- 视野
- 定义相机相对于世界坐标的方向的"向上"和"向右"矢量。
以下是我自己的光线追踪器中的一些相关代码:
camera::camera(const point3& _eye, const point3& _center) :
eye(_eye), center(_center)
{
up.set(0, 1, 0);
recalc();
fov(30);
m_aspect = 4.0 / 3;
}
camera::camera(const point3& _eye, const point3& _center, const vector3& _up) :
eye(_eye), center(_center), up(_up)
{
recalc();
fov(30);
m_aspect = 4.0 / 3;
}
void camera::recalc()
{
// renormalise the up vector
up.normalise();
// calculate unit view direction vector
view = vector3(eye, center);
view.normalise();
// and the right hand view vector
right.cross(view, up);
right.normalise();
// and re-base the up vector (may not be normalised)
up.cross(right, view);
}
void camera::fov(double fovy)
{
m_fovy = math::deg2rad(fovy) / 2.0;
m_tanf = tan(m_fovy);
}
void camera::aspect(double aspect)
{
m_aspect = aspect;
}
void camera::aspect(int x, int y)
{
m_aspect = (double)x / y;
}
ray camera::cast_ray(double x, double y) const
{
vector3 dir(view);
dir.add_scaled(right, m_tanf * m_aspect * x);
dir.add_scaled(up, m_tanf * y);
dir.normalise();
return ray(eye, dir, 0, 1.0);
}
相关文章:
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 为什么一个向量上的多线程操作很慢
- 将一个向量插入另一个向量的某个位置
- 将指向给定子类的指针从一个向量复制到另一个向量
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 使用 std::transform 将向量向量 (a) 添加到另一个向量向量 (b)
- 如何按另一个向量的方向调整一个向量?
- 将字符向量复制到另一个向量
- 制作一对共享指针并推送一个向量
- 你能把一个向量<int64>投射到一个向量<uint8>吗
- 大家好,当一个类有一个向量作为它的数据成员时,为什么它的大小总是24?
- 为什么我们需要在优先级队列声明中添加一个向量作为参数?
- 如何将所有指针从一个向量移动到另一个向量?
- 将一个向量映射到不同的对,即使向量映射到每对时是不同的?
- 生成一个类Name_class并将两种数据类型存储在一个向量中
- 使用步骤c++构建一个向量
- 如何从另一个向量中的另一个第一元素减去向量中的第一元素
- 将一个向量对放在一个无序映射与一个映射中