/* * getPixelColor adds all lights and "shadows" at each point P of the image */ public static Couleur getPixelColor(V3 P, Couleur c, V3 N, int object_index) { Couleur pixel_color = c; float decay = 1; pixel_color = Color_Ambient(c); foreach (Light_Source light in lights) { if (light.point_light) { Elements.objects_on_screen[object_index].intersection_RayObject(light.position, P - light.position, out V3 P1, out V3 N1, out float t); decay = (float)Math.PI / (float)(Math.Pow(t, 2)); if (decay > 1) { decay = 1; } } pixel_color += decay * Color_Diffused(light, c, P, N); decay = 1; if (Elements.objects_on_screen[object_index].is_spec) { // if the light creates a specular effect pixel_color += Color_Specular(light, P, N, 200); } } pixel_color.check(); return(pixel_color); }
static void DrawFastPixel(int x, int y, Couleur c) { unsafe { byte RR, VV, BB; c.check(); c.To255(out RR, out VV, out BB); byte *ptr = (byte *)data.Scan0; ptr[(x * 3) + y * stride] = BB; ptr[(x * 3) + y * stride + 1] = VV; ptr[(x * 3) + y * stride + 2] = RR; } }
static void DrawFastPixel(int x, int y, Couleur c) { unsafe { byte RR, VV, BB; c.check(); c.To255(out RR, out VV, out BB); byte* ptr = (byte*)data.Scan0; ptr[(x * 3) + y * stride ] = BB; ptr[(x * 3) + y * stride + 1] = VV; ptr[(x * 3) + y * stride + 2] = RR; } }
public static Couleur RayTracer(V3 ray_origin, V3 ray_direction, int depth) { Object obj = null; float t = -1, t_min = float.MaxValue; V3 P_test, N_test, intersection_P, intersection_N; intersection_P = intersection_N = new V3(0, 0, 0); for (int i = 0; i < objects_on_screen.Count; i++) { if (Elements.objects_on_screen[i].intersection_RayObject(ray_origin, ray_direction, out P_test, out N_test, out t)) { if (t < t_min && t > 0) { obj = Elements.objects_on_screen[i]; intersection_P = P_test; intersection_N = N_test; t_min = t; } } } if (obj == null) { return(Black); } // if the object material is specular, split the ray into a reflection // and a refraction ray if (obj.is_spec && depth < MAX_RAY_DEPTH) { Couleur reflectionColor = Black; // compute reflection V3 reflectionRay; Couleur color = obj.getPixelColor(intersection_P); // recurse if (obj.reflective_index > 0) { reflectionRay = computeReflectionRay(ray_direction, intersection_N); // recurse reflectionColor = obj.reflective_index * RayTracer(intersection_P, reflectionRay, depth + 1); } V3 refractionRay; // recurse if (obj.refractive_index != 0) { refractionRay = computeRefractionRay(obj.refractive_index, ray_direction, intersection_N); Couleur refractionColor = RayTracer(intersection_P, refractionRay, depth + 1); float Kr = 1, Kt = 1; fresnel(obj.reflective_index, obj.refractive_index, intersection_N, ray_direction, ref Kr, ref Kt); color += reflectionColor * Kr + refractionColor * Kt; //color += refractionColor; color.check(); return(color); } color += reflectionColor; color.check(); return(color); } // object is a diffuse opaque object // compute color return(obj.getPixelColor(intersection_P)); }