生成向量 [英] generate a vector

查看:168
本文介绍了生成向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个光线从(x0,y0,z0)开始,并以屏幕上的一个像素结束。此外,我有一个屏幕与A x B像素。

I have a ray starting from (x0, y0, z0) and ending at a pixel on the screen. Moreover, I have a one screen with A x B pixels.

如何在(i,j)像素处从起点到终点产生射线?

How can I generate ray from starting point to ending points at (i,j) pixels ?

我知道公式,但我无法设法在c ++中实现它。感谢您的帮助

I know the formula but I could not manage to implement it in c++ . Thanks for help

推荐答案

您没有足够的信息。

知道:


  1. 视角(即相机正在观看的点)

  2. 视野

  3. 定义相机相对于世界坐标的方向的向上和向右向量。

以下是我自己的ray-tracer的一些相关代码:

Here's some related code from my own ray-tracer:

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);
}

这篇关于生成向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆