我的影子实施无效

时间:2015-06-02 10:26:00

标签: c raytracing

我正在尝试在我的光线跟踪器中实现阴影,但它不起作用。 目前这就是我所拥有的:

enter image description here

这是我的影子功能:

int             shadow_sphere(t_lvector *vec, t_obj *obj)
{
  float         a;
  float         b;
  float         c;
  float         delta;
  float         k;
  float         k2;
  t_sphere      *sphere;

  sphere = (t_sphere *)obj->elem;
  a = pow(vec->lx, 2) +
    pow(vec->ly, 2) + pow(vec->lz, 2);
  b = 2 * ((vec->px * vec->lx) +
           (vec->py * vec->ly) +
           (vec->pz * vec->lz));
  c = pow(vec->px, 2) +
    pow(vec->py, 2) + pow(vec->pz, 2) - pow(sphere->radius, 2);
  delta = pow(b, 2) - 4 * a * c;
  if (delta >= 0)
    {
      k = (-b + sqrt(delta)) / (2.0f * a);
      k2 = (-b - sqrt(delta)) / (2.0f * a);
      if (k < 0)
        k = k2;
      if (k > 0 && k2 > 0)
        k = (k > k2) ? k2 : k;
      if (k > 0.000001 && k < 1.000001)
        {
          return (1);
        }
    }
  return (0);
}

我称这个函数是这样的:

 float         cosa;

  if (obj->type_obj == SPHERE)
    cosa = light_up_sphere(rt, obj, vector, light);
  if (obj->type_obj == PLAN)
    cosa = light_up_plan(rt, obj, vector, light);
  if (obj->type_obj == CONE)
    cosa = light_up_cone(rt, obj, vector, light);
  if (obj->type_obj == CYLINDRE)
    cosa = light_up_cylindre(rt, obj, vector, light);
  if (cosa < 0)
    cosa = 0;
  if (apply_shadow(vector, obj) == 1)
    cosa = 0;
  return (cosa);

lx,ly和lz是光的矢量。它从我的光线照射物体并在光线位置结束的那一刻开始。

vec->px = (rt->eye->x) + obj->k * rt->vec->x;

vec->py = (rt->eye->y) + obj->k * rt->vec->y;

vec->pz = (rt->eye->z) + obj->k * rt->vec->z;

vec->lx = light->pos.x - vec->px;

vec->ly = light->pos.y - vec->py;

vec->lz = light->pos.z - vec->pz;

你知道我做错了什么吗? 感谢。

编辑:我做了一些修改后得到了这个: enter image description here

我有一个影子,但不是在正确的地方。 你知道为什么吗 ?

0 个答案:

没有答案