本文整理汇总了C#中PixelBuffer.SetColor方法的典型用法代码示例。如果您正苦于以下问题:C# PixelBuffer.SetColor方法的具体用法?C# PixelBuffer.SetColor怎么用?C# PixelBuffer.SetColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PixelBuffer
的用法示例。
在下文中一共展示了PixelBuffer.SetColor方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Render
/// <summary>
/// Renders the scene into a pixel buffer.
/// </summary>
public void Render(PixelBuffer pixbuf)
{
float dx = 1.0f / pixbuf.Width, dy = 1.0f / pixbuf.Height;
camera.AspectRatio = (float)pixbuf.Width / pixbuf.Height;
// Free parallelism, why not! Note a Parallel.For loop
// over each row is slightly faster but less readable.
Parallel.ForEach(pixbuf, (pixel) =>
{
var color = Vector.Zero;
float u = pixel.X * dx;
float v = pixel.Y * dy;
var rays = new[]
{
camera.Trace(2 * (u - 0.25f * dx) - 1, 2 * (v - 0.25f * dy) - 1),
camera.Trace(2 * (u + 0.25f * dx) - 1, 2 * (v - 0.25f * dy) - 1),
camera.Trace(2 * (u - 0.25f * dx) - 1, 2 * (v + 0.25f * dy) - 1),
camera.Trace(2 * (u + 0.25f * dx) - 1, 2 * (v + 0.25f * dy) - 1),
};
// Trace a packet of 4 coherent AA rays
var packet = scene.Intersects4(rays);
// Convert the packet to a set of usable ray-geometry intersections
Intersection<Model>[] hits = packet.ToIntersection<Model>(scene);
for (int t = 0; t < 4; ++t)
{
if (hits[t].HasHit)
{
color += new Vector(0.1f, 0.1f, 0.1f);
var ray = rays[t];
var model = hits[t].Instance;
// Parse the surface normal returned and then process it manually
var rawNormal = new Vector(hits[t].NX, hits[t].NY, hits[t].NZ);
var normal = model.CorrectNormal(rawNormal); // Important!
// Calculate the new ray towards the light source
var hitPoint = ray.PointAt(hits[t].Distance);
var toLight = lightPosition - hitPoint; // from A to B = B - A
var lightRay = new Ray(hitPoint + normal * Constants.Epsilon, toLight);
// Is the light source occluded? If so, no point calculating any lighting
if (!scene.Occludes(lightRay, 0, toLight.Length()))
{
// Compute the Lambertian cosine term (rendering equation)
float cosLight = Vector.Dot(normal, toLight.Normalize());
// Calculate the total light attenuation (inverse square law + cosine law)
var attenuation = lightIntensity * cosLight / Vector.Dot(toLight, toLight);
color += model.Material(hits[t].Mesh).BRDF(toLight.Normalize(), ray.Direction, normal) * attenuation;
}
}
}
// Average the 4 per-pixel samples
pixbuf.SetColor(pixel, color / 4);
});
}