public P2 toP2(P3 p) { P3 Vp = this.normal(p); float b = -Vn.dotProduct(Vp); float h = Vp.dotProduct(Ve); float u; float dotProduct = (float)Math.Min(1, Math.Max(-1, b)); float phi = (float)Math.Acos(dotProduct); float v = (float)(phi / Math.PI); float dotCross = Vp.dotProduct(P3.crossProduct(Vn,Ve)); float dotProduct2 = (float)Math.Min(1, Math.Max(-1, h / Math.Sin(phi))); float theta = (float)((Math.Acos(dotProduct2)) / (2 * Math.PI)); if(dotCross > 0) { u = theta; } else { u = 1 - theta; } return new P2(u, v); }
public RColor illumination(P3 p, SceneObject so) { P3 surfaceNormal = so.shape.normal(p); float shade = surfaceNormal.dotProduct(this.direction); if (shade < 0) { return new RColor(0, 0, 0); } else { return this.color.scalarMultiply(shade); } }
public RColor Coloring(P3 p, Ray r, SceneObject so) { P2 textureLoc = so.shape.toP2(p); RColor textureColor = so.texture.texture(textureLoc); RColor sumOfLights = new RColor(0, 0, 0); RColor lightColor; foreach (Light lightSource in Form1.lights) { lightColor = lightSource.illumination(p,so); sumOfLights = sumOfLights.add(lightColor); } return RColor.multiplyColor(textureColor, sumOfLights); }
public P3 sub(P3 v) { return new P3(v.x - this.x, v.y - this.y, v.z - this.z); }
public float normLength(P3 v) { return (float)Math.Sqrt(x * x + y * y + z * z); }
public float dotProduct(P3 v) { return x * v.x + y * v.y + z * v.z; }
public P3 add(P3 v) { return new P3(v.x + this.x, v.y + this.y, v.z + this.z); }
public static P3 mul(P3 p, P3 p2) { return new P3(p.x * p2.x, p.y * p2.y, p.z * p2.z); }
//public static SceneObject shootRay(Ray r, out double d) //{ // float dist; // SceneObject obj = closest(r); // if (obj == null) // { // return null; // } // dist = obj.distance(r); // if (dist == infinity) // { // return null; // } // else // { // return obj; // } //} //public static SceneObject closest(Ray ray) //{ // SceneObject sceneObj = null; // float dist, smallestDist = infinity; // foreach (SceneObject obj in objects) // { // if ((dist = obj.distance(ray)) <= smallestDist) // { // smallestDist = dist; // sceneObj = obj; // } // } // return sceneObj; //} public void noName() { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { float size1 = size; P3 p = new P3(i / size1 * 2.0f - 1, 0, j / size1 * 2.0f - 1); Ray r = new Ray(camera, p.sub(camera).normalize()); float distance = sphere.intersect(r); if (distance != -1) { P3 sp = r.travel(distance); P3 n = sphere.normal(sp); float intensity = n.dotProduct(direction); if (intensity < 0) { intensity = 0; } pixels[i, j] = directional.scalarMultiply(intensity); } else { pixels[i, j] = new RColor(0, 0, 0); } } } panel.Refresh(); }
public P3 normal(P3 p) { P3 v2 = p.sub(this.center); return v2.scale(1 / radius); }
public Directional(RColor color, P3 direction) { this.color = color; this.direction = direction.normalize(); }
public RColor colorAt(P3 p, Ray r) { return skin.Coloring(p, r, this); }
public RColor illumination(P3 p, SceneObject so) { return this.color; }
public P2 toP2(P3 p) { return new P2(0, 0); }
public P3 normal(P3 p) { return n; }
public Plane(P3 p, P3 n) { this.p = p; this.n = n; this.d = -(n.dotProduct(p)); }
public Ray(P3 origin, P3 direction) { this.direction = (direction).normalize(); this.origin = origin; }
private void pitchBar_Scroll(object sender, ScrollEventArgs e) { direction = P3.fromSpherical(3.5f, pitchBar.Value); noName(); }
public Sphere(P3 center, float radius) { this.center = center; this.radius = radius; }
public static P3 crossProduct(P3 a, P3 b) { return new P3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); }
private void headingBar_Scroll(object sender, ScrollEventArgs e) { direction = P3.fromSpherical(headingBar.Value, 3.1f); noName(); }
private void zoomBar_Scroll(object sender, ScrollEventArgs e) { camera = new P3(0, zoomBar.Value, 0); noName(); }