projet-rendu/Lighting.java

125 lines
4.1 KiB
Java
Raw Normal View History

2022-04-14 09:33:39 +00:00
import java.util.*;
2022-04-19 19:59:52 +00:00
import java.util.List;
2022-04-14 09:33:39 +00:00
import algebra.*;
2022-04-19 19:59:52 +00:00
import java.awt.Color;
2022-04-14 09:33:39 +00:00
2022-04-12 10:08:58 +00:00
/* * The Lighting class describes a scene lighting environment
* @author: gmorin, smondet */
public class Lighting {
2022-04-14 09:33:39 +00:00
static final int NONE = 0;
static final int AMBIENT = 1;
static final int POINT = 2;
2022-04-14 09:39:54 +00:00
List<Light> lights;
2022-04-14 09:33:39 +00:00
/* Internal Class describing a light source */
2022-04-12 10:08:58 +00:00
private class Light {
public int type = NONE;
public double[] params;
2022-04-14 09:33:39 +00:00
public Light(int type, double[] params) {
this.type = type;
this.params = params;
2022-04-12 10:08:58 +00:00
}
}
2022-04-14 09:33:39 +00:00
2022-04-12 10:08:58 +00:00
public Lighting() {
2022-04-14 09:39:54 +00:00
lights = new LinkedList<Light>();
2022-04-14 09:33:39 +00:00
}
/* Adds a new ambient light source of intensity @ia to the environment. */
public void addAmbientLight(double ia) {
double[] v = new double[1];
v[0] = ia;
lights.add(new Light(AMBIENT, v));
}
2022-04-12 10:08:58 +00:00
/** Addsa */
2022-04-14 09:33:39 +00:00
public void addPointLight(double x, double y, double z, double id) {
2022-04-12 10:08:58 +00:00
double[] v = new double[5];
2022-04-14 09:33:39 +00:00
v[0] = x;
v[1] = y;
v[2] = z;
v[3] = id;
lights.add(new Light(POINT, v));
}
/*
* Computes the illuminated color of a 3D points of given position, normal and
2022-04-19 19:59:52 +00:00
* color, and given the camera position and material parameters.
2022-04-14 09:33:39 +00:00
* Returns an array of size 3.
*/
public double[] applyLights(Vector3 position, Vector3 normal, double[] color,
Vector3 cameraPosition,
double ka, double kd, double ks, double s) {
2022-04-19 19:59:52 +00:00
2022-04-12 10:08:58 +00:00
double[] litColor = new double[3];
/* total light intensity */
double I = 0.0;
2022-04-14 09:39:54 +00:00
Iterator<Light> it = lights.iterator();
2022-04-14 09:33:39 +00:00
while (it.hasNext()) {
Light light = (Light) it.next();
2022-04-12 10:08:58 +00:00
switch (light.type) {
case AMBIENT:
2022-04-14 09:39:54 +00:00
/* Ambient light */
double Ia = light.params[0];
I += Ia * ka;
2022-04-12 10:08:58 +00:00
break;
case POINT:
try {
2022-04-14 09:33:39 +00:00
/* vector from point to camera center */
Vector3 e = new Vector3(cameraPosition);
e.subtract(position);
e.normalize();
2022-04-14 09:39:54 +00:00
2022-04-14 09:33:39 +00:00
/* vector from point to light */
Vector3 l = new Vector3(light.params[0], light.params[1], light.params[2]);
l.subtract(position);
l.normalize();
2022-04-14 09:39:54 +00:00
2022-04-14 09:33:39 +00:00
/* half-vector between e and l */
Vector3 h = new Vector3(e);
h.add(l);
h.normalize();
2022-04-14 09:39:54 +00:00
2022-04-14 10:03:19 +00:00
double Id = light.params[3];
2022-04-14 20:35:42 +00:00
/* diffuse contribution */
2022-04-15 07:14:55 +00:00
double I_diff = Id * kd * normal.dot(l) / normal.norm() / l.norm();
2022-04-14 09:39:54 +00:00
2022-04-14 11:32:08 +00:00
/* specular contribution */
2022-04-15 07:14:55 +00:00
double I_spec = Id * ks * Math.pow(h.dot(normal) / h.norm() / normal.norm(), s);
2022-04-14 09:39:54 +00:00
2022-04-14 11:32:08 +00:00
I += I_diff + I_spec;
2022-04-14 09:33:39 +00:00
} catch (InstantiationException ex) {
/* should not reach */ } catch (SizeMismatchException ex) {
/* should not reach */ }
2022-04-12 10:08:58 +00:00
break;
default:
/* ignore unknow lights */
break;
}
2022-04-14 09:33:39 +00:00
}
litColor[0] = I * color[0];
litColor[1] = I * color[1];
litColor[2] = I * color[2];
2022-04-12 10:08:58 +00:00
return litColor;
}
2022-04-19 19:59:52 +00:00
/*
* Computes the illuminated color of a 3D points of given position, normal and
* color, and given the camera position and material parameters.
* Returns an array of size 3.
*/
public double[] applyLights(Vector3 position, Vector3 normal, Color color,
Vector3 cameraPosition, double ka, double kd, double ks, double s) {
double[] couleur = new double[3];
couleur[0] = color.getRed();
couleur[1] = color.getGreen();
couleur[2] = color.getBlue();
return applyLights(position, normal, couleur, cameraPosition, ka, kd, ks, s);
}
2022-04-12 10:08:58 +00:00
}