射线追踪器阴影问题

时间:2018-12-18 21:58:10

标签: c++ shader intersection trace

我正在使用光线追踪器,但是 我在阴影部分停留了几天。 我的影子真的很奇怪。这是光线跟踪器的图像:

enter image description here

Image of shadow with two spheres

黑色部分应该是阴影。

射线的原点始终为(0.f,-10.f,-500.f),因为这是透视投影,因此是相机的眼睛。当光线撞击平面时,命中点始终是光线的原点,但对于球体而言,则有所不同。它是不同的,因为它基于球体的位置。平面和球体之间永远不会有交集,因为原点存在巨大差异。 我也尝试在盒子上添加阴影,但这也不起作用。两个球之间的阴影确实起作用!

如果有人想查看交叉点代码,请告诉我。

感谢您抽出宝贵的时间来帮助我!

相机

Camera::Camera(float a_fFov, const Dimension& a_viewDimension, vec3 a_v3Eye, vec3 a_v3Center, vec3 a_v3Up) :
m_fFov(a_fFov),
m_viewDimension(a_viewDimension),
m_v3Eye(a_v3Eye),
m_v3Center(a_v3Center),
m_v3Up(a_v3Up)
{
  // Calculate the x, y and z axis
  vec3 v3ViewDirection = (m_v3Eye - m_v3Center).normalize();
  vec3 v3U = m_v3Up.cross(v3ViewDirection).normalize();
  vec3 v3V = v3ViewDirection.cross(v3U);

  // Calculate the aspect ratio of the screen
  float fAspectRatio = static_cast<float>(m_viewDimension.m_iHeight) / 
                       static_cast<float>(m_viewDimension.m_iWidth);
  float fViewPlaneHalfWidth = tanf(m_fFov / 2.f);
  float fViewPlaneHalfHeight = fAspectRatio * fViewPlaneHalfWidth;

  // The bottom left of the plane
  m_v3ViewPlaneBottomLeft = m_v3Center - v3V * fViewPlaneHalfHeight - v3U * fViewPlaneHalfWidth;

  // The amount we need to increment to get the direction. The width and height are based on the field of view.
  m_v3IncrementX = (v3U * 2.f * fViewPlaneHalfWidth);
  m_v3IncrementY = (v3V * 2.f * fViewPlaneHalfHeight);
}

Camera::~Camera()
{
}

const Ray Camera::GetCameraRay(float iPixelX, float iPixelY) const
{
  vec3 v3Target = m_v3ViewPlaneBottomLeft + m_v3IncrementX * iPixelX + m_v3IncrementY * iPixelY;
  vec3 v3Direction = (v3Target - m_v3Eye).normalize();

  return Ray(m_v3Eye, v3Direction);
}

相机设置

Scene::Scene(const Dimension& a_Dimension) :
m_Camera(1.22173f, a_Dimension, vec3(0.f, -10.f, -500.f), vec3(0.f, 0.f, 0.f), vec3(0.f, 1.f, 0.f))
{
  // Setup sky light
  Color ambientLightColor(0.2f, 0.1f, 0.1f);
  m_AmbientLight = new AmbientLight(0.1f, ambientLightColor);

  // Setup shapes
  CreateShapes();

  // Setup lights
  CreateLights();

  // Setup buas
  m_fBias = 1.f;
}

场景对象

Sphere* sphere2 = new Sphere();
sphere2->SetRadius(50.f);
sphere2->SetCenter(vec3(0.f, 0.f, 0.f));
sphere2->SetMaterial(matte3);

Plane* plane = new Plane(true);
plane->SetNormal(vec3(0.f, 1.f, 0.f));
plane->SetPoint(vec3(0.f, 0.f, 0.f));
plane->SetMaterial(matte1);

场景灯

PointLight* pointLight1 = new PointLight(1.f, Color(0.1f, 0.5f, 0.7f), vec3(0.f, -200.f, 0.f), 1.f, 0.09f, 0.032f);

阴影功能

for (const Light* light : a_Lights) {
    vec3 v3LightDirection = (light->m_v3Position - a_Contact.m_v3Hitpoint).normalized();

    light->CalcDiffuseLight(a_Contact.m_v3Point, a_Contact.m_v3Normal, m_fKd, lightColor);

    Ray lightRay(a_Contact.m_v3Point + a_Contact.m_v3Normal * a_fBias, v3LightDirection);

    bool test = a_RayTracer.ShadowTrace(lightRay, a_Shapes);

    vec3 normTest = a_Contact.m_v3Normal;
    float test2 = normTest.dot(v3LightDirection);

    // No shadow
    if (!test) {
        a_ResultColor += lightColor * !test * test2;
    }
    else {
        a_ResultColor = Color(); // Test code - change color to black.
    }
}

1 个答案:

答案 0 :(得分:5)

您有几个错误:

  • Sphere::Collides中,未设置m_fCollisionTime的{​​{1}}
  • t2>=t1中,如果Sphere::Collides为负,则射线实际上不与球体相交(这会在球的顶部产生奇怪的阴影)
  • 将飞机放下,您会看到球的阴影
  • 从眼睛拍摄光线时,您需要检查最近碰撞(尝试一下,交换对象的顺序,球体突然变成飞机的后方)

在修复了这些问题后,您会得到以下信息:enter image description here