本文整理汇总了C#中Ray.Trace方法的典型用法代码示例。如果您正苦于以下问题:C# Ray.Trace方法的具体用法?C# Ray.Trace怎么用?C# Ray.Trace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ray
的用法示例。
在下文中一共展示了Ray.Trace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Shade
public virtual Color Shade(Vector3D p, Vector3D n, Vector3D v, ArrayList lights,
ArrayList objects, Color bgnd)
{
IEnumerator lightSources = lights.GetEnumerator();
float r = 0;
float g = 0;
float b = 0;
while (lightSources.MoveNext())
{
Light light = (Light)lightSources.Current;
if (light.lightType == Light.AMBIENT)
{
r += ka * ir * light.ir;
g += ka * ig * light.ig;
b += ka * ib * light.ib;
}
else
{
Vector3D l;
if (light.lightType == Light.POINT)
{
l = new Vector3D(light.lvec.x - p.x, light.lvec.y - p.y, light.lvec.z - p.z);
l.Normalize();
}
else
{
l = new Vector3D(-light.lvec.x, -light.lvec.y, -light.lvec.z);
}
// Check if the surface point is in shadow
Vector3D poffset = new Vector3D(p.x + TINY * l.x, p.y + TINY * l.y, p.z + TINY *
l.z);
Ray shadowRay = new Ray(poffset, l);
if (shadowRay.Trace(objects))
{
break;
}
float lambert = Vector3D.Dot(n, l);
if (lambert > 0)
{
if (kd > 0)
{
float diffuse = kd * lambert;
r += diffuse * ir * light.ir;
g += diffuse * ig * light.ig;
b += diffuse * ib * light.ib;
}
if (ks > 0)
{
lambert *= 2;
float spec = v.Dot(lambert * n.x - l.x, lambert * n.y - l.y, lambert * n.z - l.z);
if (spec > 0)
{
spec = ks * ((float)Math.Pow((double)spec, (double)ns));
r += spec * light.ir;
g += spec * light.ig;
b += spec * light.ib;
}
}
}
}
}
// Compute illumination due to reflection
if (kr > 0)
{
float t = v.Dot(n);
if (t > 0)
{
t *= 2;
Vector3D reflect = new Vector3D(t * n.x - v.x, t * n.y - v.y, t * n.z - v.z);
Vector3D poffset = new Vector3D(p.x + TINY * reflect.x, p.y + TINY * reflect.y, p
.z + TINY * reflect.z);
Ray reflectedRay = new Ray(poffset, reflect);
if (reflectedRay.Trace(objects))
{
Color rcolor = reflectedRay.Shade(lights, objects, bgnd);
r += kr * rcolor.GetRed();
g += kr * rcolor.GetGreen();
b += kr * rcolor.GetBlue();
}
else
{
r += kr * bgnd.GetRed();
g += kr * bgnd.GetGreen();
b += kr * bgnd.GetBlue();
}
}
}
// Add code for refraction here
r = (r > 1f) ? 1f : r;
g = (g > 1f) ? 1f : g;
b = (b > 1f) ? 1f : b;
return new Color(r, g, b);
}