Skip to content

Commit

Permalink
flash light
Browse files Browse the repository at this point in the history
  • Loading branch information
nevcohen committed May 17, 2021
1 parent fc9673a commit ff6c268
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
33 changes: 33 additions & 0 deletions cameraproject/src/elements/FlashLight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package elements;

import primitives.Color;
import primitives.Point3D;
import primitives.Vector;

public class FlashLight extends PointLight implements LightSource {

private Vector direction;
private int exp;

public FlashLight(Color intensity, Point3D position, Vector direction, int exp) {
super(intensity, position);
if(exp<1)
throw new IllegalArgumentException("exp less then 1");
this.direction = direction.normalized();
this.exp = exp;

}

@Override
public Color getIntensity(Point3D p) {
double dirL = direction.dotProduct(super.getL(p));
if (dirL <= 0)
return Color.BLACK;
return super.getIntensity(p).scale(Math.pow(dirL, exp));
}

@Override
public Vector getL(Point3D p) {
return super.getL(p);
}
}
12 changes: 3 additions & 9 deletions cameraproject/src/elements/SpotLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@
/**
*
*/
public class SpotLight extends PointLight implements LightSource {

private Vector direction;
public class SpotLight extends FlashLight implements LightSource {

public SpotLight(Color intensity, Point3D position, Vector direction) {
super(intensity, position);
this.direction = direction.normalized();
super(intensity, position, direction, 1);
}

@Override
public Color getIntensity(Point3D p) {
double dirL = direction.dotProduct(getL(p));
if (dirL <= 0)
return Color.BLACK;
return super.getIntensity(p).scale(dirL);
return super.getIntensity(p);
}

@Override
Expand Down
39 changes: 39 additions & 0 deletions cameraproject/src/unittests/elements/LightTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ public void sphereSpot() {
render.renderImage();
render.writeToImage();
}

/**
* Produce a picture of a sphere lighted by a flash light
*/
@Test
public void sphereFlash() {
scene1.geometries.add(sphere);
scene1.lights.add(new FlashLight(new Color(500, 300, 0), new Point3D(-50, -50, 50), new Vector(1, 1, -2), 3) //
.setKl(0.00001).setKq(0.00000001));

ImageWriter imageWriter = new ImageWriter("lightSphereSpot", 500, 500);
Render render = new Render()//
.setImageWriter(imageWriter) //
.setCamera(camera1) //
.setRayTracer(new RayTracerBasic(scene1));
render.renderImage();
render.writeToImage();
}

/**
* Produce a picture of a two triangles lighted by a directional light
Expand Down Expand Up @@ -121,6 +139,8 @@ public void trianglesPoint() {
render.renderImage();
render.writeToImage();
}



/**
* Produce a picture of a two triangles lighted by a spot light
Expand All @@ -141,6 +161,25 @@ public void trianglesSpot() {
render.writeToImage();
}

/**
* Produce a picture of a two triangles lighted by a flash light
*/
@Test
public void trianglesFlash() {
scene2.geometries.add(triangle1.setMaterial(new Material().setKd(0.5).setKs(0.5).setShininess(300)),
triangle2.setMaterial(new Material().setKd(0.5).setKs(0.5).setShininess(300)));
scene2.lights.add(new FlashLight(new Color(500, 250, 250), new Point3D(10, -10, -130), new Vector(-2, -2, -1), 3) //
.setKl(0.0001).setKq(0.000005));

ImageWriter imageWriter = new ImageWriter("lightTrianglesFlash", 500, 500);
Render render = new Render()//
.setImageWriter(imageWriter) //
.setCamera(camera2) //
.setRayTracer(new RayTracerBasic(scene2));
render.renderImage();
render.writeToImage();
}

/**
*
*/
Expand Down

0 comments on commit ff6c268

Please sign in to comment.