style: autoformat
This commit is contained in:
parent
08c323f8b3
commit
8fee10f2a5
|
@ -1,43 +1,62 @@
|
||||||
import java.lang.*; import java.util.*; import algebra.*;
|
import java.lang.*;
|
||||||
|
import java.util.*;
|
||||||
|
import algebra.*;
|
||||||
|
|
||||||
/* * The Lighting class describes a scene lighting environment
|
/* * The Lighting class describes a scene lighting environment
|
||||||
* @author: gmorin, smondet */
|
* @author: gmorin, smondet */
|
||||||
public class Lighting {
|
public class Lighting {
|
||||||
static final int NONE = 0 ; static final int AMBIENT = 1 ;
|
static final int NONE = 0;
|
||||||
static final int POINT = 2 ;
|
static final int AMBIENT = 1;
|
||||||
List lights ;
|
static final int POINT = 2;
|
||||||
|
List lights;
|
||||||
|
|
||||||
/* Internal Class describing a light source */
|
/* Internal Class describing a light source */
|
||||||
private class Light {
|
private class Light {
|
||||||
public int type = NONE;
|
public int type = NONE;
|
||||||
public double[] params;
|
public double[] params;
|
||||||
public Light (int type, double []params) {
|
|
||||||
this.type = type; this.params = params;
|
public Light(int type, double[] params) {
|
||||||
|
this.type = type;
|
||||||
|
this.params = params;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Lighting() {
|
public Lighting() {
|
||||||
lights = new LinkedList(); }
|
lights = new LinkedList();
|
||||||
|
}
|
||||||
|
|
||||||
/* Adds a new ambient light source of intensity @ia to the environment. */
|
/* Adds a new ambient light source of intensity @ia to the environment. */
|
||||||
public void addAmbientLight (double ia) {
|
public void addAmbientLight(double ia) {
|
||||||
double [] v = new double[1];
|
double[] v = new double[1];
|
||||||
v[0] = ia; lights.add (new Light (AMBIENT, v)); }
|
v[0] = ia;
|
||||||
|
lights.add(new Light(AMBIENT, v));
|
||||||
|
}
|
||||||
|
|
||||||
/** Addsa */
|
/** Addsa */
|
||||||
public void addPointLight (double x, double y, double z, double id)
|
public void addPointLight(double x, double y, double z, double id) {
|
||||||
{
|
|
||||||
double[] v = new double[5];
|
double[] v = new double[5];
|
||||||
v[0] = x; v[1] = y; v[2] = z; v[3] = id;
|
v[0] = x;
|
||||||
lights.add (new Light (POINT, v)); }
|
v[1] = y;
|
||||||
/* Computes the illuminated color of a 3D points of given position, normal and color,
|
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
|
||||||
|
* color,
|
||||||
* and given the camera position and material parameters.
|
* and given the camera position and material parameters.
|
||||||
* Returns an array of size 3. */
|
* Returns an array of size 3.
|
||||||
public double[] applyLights (Vector3 position, Vector3 normal, double[] color,
|
*/
|
||||||
|
public double[] applyLights(Vector3 position, Vector3 normal, double[] color,
|
||||||
Vector3 cameraPosition,
|
Vector3 cameraPosition,
|
||||||
double ka, double kd, double ks, double s)
|
double ka, double kd, double ks, double s) {
|
||||||
{
|
|
||||||
double[] litColor = new double[3];
|
double[] litColor = new double[3];
|
||||||
/* total light intensity */
|
/* total light intensity */
|
||||||
double I = 0.0;
|
double I = 0.0;
|
||||||
Iterator it = lights.iterator ();
|
Iterator it = lights.iterator();
|
||||||
while (it.hasNext ()){
|
while (it.hasNext()) {
|
||||||
Light light = (Light) it.next ();
|
Light light = (Light) it.next();
|
||||||
switch (light.type) {
|
switch (light.type) {
|
||||||
case AMBIENT:
|
case AMBIENT:
|
||||||
/* Ambient light : A COMPLETER */
|
/* Ambient light : A COMPLETER */
|
||||||
|
@ -46,31 +65,34 @@ public class Lighting {
|
||||||
case POINT:
|
case POINT:
|
||||||
try {
|
try {
|
||||||
/* vector from point to camera center */
|
/* vector from point to camera center */
|
||||||
Vector3 e = new Vector3 (cameraPosition);
|
Vector3 e = new Vector3(cameraPosition);
|
||||||
e.subtract (position);
|
e.subtract(position);
|
||||||
e.normalize ();
|
e.normalize();
|
||||||
/* vector from point to light*/
|
/* vector from point to light */
|
||||||
Vector3 l = new Vector3 (light.params[0], light.params[1], light.params[2]);
|
Vector3 l = new Vector3(light.params[0], light.params[1], light.params[2]);
|
||||||
l.subtract (position);
|
l.subtract(position);
|
||||||
l.normalize ();
|
l.normalize();
|
||||||
/* half-vector between e and l*/
|
/* half-vector between e and l */
|
||||||
Vector3 h = new Vector3 (e);
|
Vector3 h = new Vector3(e);
|
||||||
h.add (l);
|
h.add(l);
|
||||||
h.normalize ();
|
h.normalize();
|
||||||
/* diffuse contribution : A COMPLETER */
|
/* diffuse contribution : A COMPLETER */
|
||||||
/* double Id = ... */
|
/* double Id = ... */
|
||||||
/* specular contribution : A COMPLETER */
|
/* specular contribution : A COMPLETER */
|
||||||
/* double Is = ... */
|
/* double Is = ... */
|
||||||
/* I += Id + Is;*/
|
/* I += Id + Is; */
|
||||||
} catch (InstantiationException ex) { /* should not reach*/ }
|
} catch (InstantiationException ex) {
|
||||||
catch (SizeMismatchException ex) { /* should not reach*/ }
|
/* should not reach */ } catch (SizeMismatchException ex) {
|
||||||
|
/* should not reach */ }
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
/* ignore unknow lights */
|
/* ignore unknow lights */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
litColor[0] = I * color[0]; litColor[1] = I * color[1]; litColor[2] = I * color[2];
|
litColor[0] = I * color[0];
|
||||||
|
litColor[1] = I * color[1];
|
||||||
|
litColor[2] = I * color[2];
|
||||||
return litColor;
|
return litColor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue