Commits

zercha committed bfde595

Trying to fix specular... buggy although no nullpointers, everything runs fine

Comments (0)

Files changed (6)

src/frontend/Startup.java

 
 		Settings settings = new Settings();
 
-		Scene scene = getScene(settings);
+		Scene scene = getScene();
 		RaySettings raySettings = new RaySettings(settings);
 		Raytracer raytracer = new Raytracer(scene, raySettings, new RaytracerCallback() {
 			@Override
 		SwapPanel swapPanel = new SwapPanel();
 		frame.add(swapPanel,BorderLayout.CENTER);
 		
-
+		
 		JPanel buttonBar = createMenuBar(openGLPanel, rayPanel, swapPanel, raySettings);
 		frame.add(buttonBar, BorderLayout.NORTH);
 		/*
 		m3.setColor(new Color(150,150,150));
 		plane.setMaterial(m3);
 		
-		//front wall
-		Plane plane2 = new Plane(new Vector3d(28, 0, 0), new Vector3d(-1, 0, 0));
-		plane2.setMaterial(Material.getMirror());
-		
 		//left wall
 		Plane plane3 = new Plane(new Vector3d(0, 0, 28), new Vector3d(0, 0, -1));
 		Material m5 = new Material();
 		m5.setColor(new Color(150,150,150));
 		plane3.setMaterial(Material.getMirror());
+		
+		//front wall
+		Plane plane2 = new Plane(new Vector3d(28, 0, 0), new Vector3d(-1, 0, 0));
+		plane2.setMaterial(m5);
 	
 		//back wall
 		Plane plane4 = new Plane(new Vector3d(-28, 0, 0), new Vector3d(1, 0, 0));
 		Plane plane5 = new Plane(new Vector3d(0, 0, -28), new Vector3d(0, 0, 1));
 		Material m7 = new Material();
 		m7.setColor(new Color(150,150,150));
-		plane5.setMaterial(m7);
+		plane5.setMaterial(Material.getMirror());
 		
 		//roof
 		Plane plane6 = new Plane(new Vector3d(0, 28, 0), new Vector3d(0, -1, 0));
 		Quad cuboid = new Quad(new Vector3d(8,-2,0), new Vector3d(-1,0,0),4,4);
 		Material red = new Material();
 		red.setColor(Color.red);
-		red.setRefractiveIndex(1);
 		red.setRefractionRatio(0.7);
-		red.setReflectionRatio(0.3);
+//		red.setReflectionRatio(0.3);
 		cuboid.setMaterial(red);
 		
 		Quad cuboid2 = new Quad(new Vector3d(4,-2,0), new Vector3d(-1,0,0),3,3);
 		Material green = new Material();
 		green.setColor(Color.green);
-		green.setRefractiveIndex(1);
 		green.setRefractionRatio(0.7);
 		cuboid2.setMaterial(green);
 		
+		Quad cuboid3 = new Quad(new Vector3d(16,-2,0), new Vector3d(-1,0,0),2,7);
+		Material blue = new Material();
+		blue.setColor(Color.blue);
+		blue.setRefractionRatio(0.3);
+		cuboid3.setMaterial(blue);
+		
 		//front light
 		Sphere light = new Sphere(new Vector3d(0,-1,0),0.1);//(new Vector3d(-5,3,3),0.2);
 		light.setMaterial(Material.getStandardLight());
 		Cuboid light2 = new Cuboid(new Vector3d(-2,-1,0), new Vector3d(0.1,1,1));
 		light2.setMaterial(Material.getStandardLight());
 		
+		Sphere sRed = new Sphere(new Vector3d(0,7,3),1);
+		Sphere sGreen = new Sphere(new Vector3d(0,7,0),1);
+		Sphere sBlue = new Sphere(new Vector3d(0,7,-3),1);
+		
+		Material m97 = new Material();
+		Material m98 = new Material();
+		Material m99 = new Material();
+		
+		m97.setRefractionRatio(0.7);
+		m97.setReflectionRatio(0.4);
+		m98.setRefractionRatio(0.7);
+		m98.setReflectionRatio(0.4);
+		m99.setRefractionRatio(0.7);
+		m99.setReflectionRatio(0.4);
+		m97.setColor(Color.red);
+		m98.setColor(Color.green);
+		m99.setColor(Color.blue);
+		m97.setRefractiveIndex(1.33);
+		m98.setRefractiveIndex(1.33);
+		m99.setRefractiveIndex(1.33);
+		
+		sRed.setMaterial(m97);
+		sGreen.setMaterial(m98);
+		sBlue.setMaterial(m99);
+		
+		
 		scene.setCamera(cam);
 		
 		scene.addShape(plane);
 		scene.addShape(plane4);
 		scene.addShape(plane5);
 		scene.addShape(plane6);
-//		scene.addShape(cuboid);
-//		scene.addShape(cuboid2);
-//		scene.addShape(sphere2);
+		scene.addShape(cuboid);
+		scene.addShape(cuboid2);
+		scene.addShape(cuboid3);
+		scene.addShape(sphere2);
 		scene.addShape(sphere3);
+		scene.addShape(sRed);
+		scene.addShape(sBlue);
+		scene.addShape(sGreen);
 //		scene.addShape(light2);
 		scene.addShape(light);
 

src/raytracer/PhongModel.java

 
 	private VectorColor getSpecular(IntersectionData data, double specularity,/* MaterialData materialData, */Shape lightSource) {
 		
+		Vector3d V;
+		if(data.getIncomingRay().refCamPos == null)
+			V = data.getIncomingRay().getDirection().negate();
+		else{
+			V = data.getPosition().sub(data.getIncomingRay().refCamPos).normalize();
+		}
 		
 		Vector3d normal = data.getNormal();
 		Vector3d direction = lightSource.getPosition().sub(data.getPosition()).normalize().negate();
 		Vector3d Rm = Ray.getReflectedRay(normal, data.getPosition(), direction).direction;// direction.sub((normal).mul(2.0 * direction.dot(normal))).normalize();
 		
-		Vector3d V;
-		if(data.getIncomingRay().dirToCam == null)
-			V = data.getIncomingRay().getDirection().negate();
-		else
-			V = data.getIncomingRay().dirToCam;
+		
 		VectorColor Is = lightSource.getMaterial().getColor(data);
 		
 		double dot = Rm.dot(V);

src/raytracer/Ray.java

 	
 	protected Vector3d origin;
 	protected Vector3d direction;
-	public Vector3d dirToCam = null;
+	public Vector3d refCamPos = null;
 	
 	public Ray(Vector3d origin, Vector3d direction){
 		this.origin = origin;

src/raytracer/RaySettings.java

 public class RaySettings extends SpecificSettings {
 	public int maxBounces = 10;
 	public int antiAliasingQuality = 1;
-	public int shadowQuality = 8;
+	public int shadowQuality = 1;
 
 	public RaySettings(Settings settings) {
 		super(settings);

src/raytracer/Raytracer.java

 		List<Shape> shapes = scene.getShapes();
 		
 		IntersectionData data;
+		double alpha = 1;
+		int amount = 1;
 		for (int i = 0; i < shapes.size(); i++) {
 			if(shapes.get(i) == light)
 				continue;
 						return VectorColor.BLACK;
 					}
 					else{
-						color = color.mul(data.getShape().getMaterial().getColor(data).mul(refractiveRatio));
-						color = color.mul(refractiveRatio);
+						color = color.add(data.getShape().getMaterial().getColor(data)).mul(refractiveRatio);
+						alpha *= 1-refractiveRatio;
+						amount++;
+//						color = color.mul(refractiveRatio);
 					}
 				}
 			}
 		}
+		alpha = 1 - alpha;
+		color.div(amount);
+		color.mul(alpha);
 		return color;
 	}
 	

src/scene/shapes/Material.java

 	private Map<String, VectorColor> colors = new HashMap<String, VectorColor>(2);
 	
 	private double reflectionRatio = 0, refractionRatio = 0;
-	private double diffuseRatio = 1, specularRatio = 0.4;
+	private double diffuseRatio = 0.7, specularRatio = 0.3;
 	private double specularity = 20, refractiveIndex = 1;
 	
 	private Texture texture = null;
 			diffuseRatio = this.diffuseRatio;
 		}
 		
-		if (reflectionRatio > 0 /*&& refractiveIndex == 1*/) {
+		if (reflectionRatio > 0 && refractiveIndex == 1) {
 			Ray r = Ray.getReflectedRay(data);
-			fixRayDirToCam(r);
+			fixRayCamPoint(r,data);
 			rayData.add(new Tuple3<Ray, Double, Double>(r,
 					getReflectionRatio(), refractiveIndex));
 		}
 			Ray reflectedRay = refData.getReflectedRay();
 			Ray refractedRay = refData.getRefractedRay();
 			
-			fixRayDirToCam(reflectedRay);
-			fixRayDirToCam(refractedRay);
+			fixRayCamPoint(reflectedRay,data);
+			fixRayCamPoint(refractedRay,data);
 			
 			if (reflectedRay != null && refData.getReflection() > 0) {
 				rayData.add(new Tuple3<Ray, Double, Double>(reflectedRay,
 				rayData.add(new Tuple3<Ray, Double, Double>(refractedRay,
 						refractionRatio*refData.getTransmission(), n2));
 			}
+			
+			
 		}
 		
 		MaterialData md = new ConstantMaterialData(diffuseRatio, specularRatio, specularity);
 		return new MaterialRayData(md, rayData.toArray(new Tuple3[]{}));
 	}
 	
-	private void fixRayDirToCam(Ray r){
-		if(r.dirToCam == null)
-			r.dirToCam = r.getDirection().negate();
-		else
-			r.dirToCam = r.dirToCam;
+	private void fixRayCamPoint(Ray r, IntersectionData data){
+		
+		if(r != null)
+			r.refCamPos = data.getPosition().sub(r.getDirection().mul(data.getDistance()));
 	}
 	
 	/**