What I’m Trying To Do:
Loop through all triangles {
if there is an intensity value assigned to the vertex in the HashMap already {
set the vertex’s intensity to the already calculated intensity
} else {
calculate the intensity and set it to the vertex
pair up the vertex and the intensity and add it to the HashMap
}
}
Therefore using this logic, I should have to only calculate the intensity of the vertex only once.
However, the body of the “if there is an intensity value assigned to the vertex in the HashMap already” statement never gets called because it never is null.
When I print out all the keys and values in the HashMap, I get repeated keys.
Anyone know the issue?
private strictfp void calculateLight(int ambientIntensity, int intensity, Vector source) {
Map<Vector, Integer> calculated = new HashMap<Vector, Integer>();
for (Triangle tri : triangles) {
Vector n = tri.normal();
if (calculated.get(tri.v1) != null) {
tri.i1 = calculated.get(tri.v1);
} else {
tri.i1 = calcIntensity(n, tri.v1, source, intensity, ambientIntensity);
calculated.put(tri.v1, tri.i1);
}
if (calculated.get(tri.v2) != null) {
tri.i1 = calculated.get(tri.v2);
} else {
tri.i2 = calcIntensity(n, tri.v2, source, intensity, ambientIntensity);
calculated.put(tri.v2, tri.i2);
}
if (calculated.get(tri.v3) != null) {
tri.i1 = calculated.get(tri.v3);
} else {
tri.i3 = calcIntensity(n, tri.v3, source, intensity, ambientIntensity);
calculated.put(tri.v3, tri.i3);
}
}
Iterator<Vector> keySetIterator = calculated.keySet().iterator();
while (keySetIterator.hasNext()) {
Vector key = keySetIterator.next();
System.out.println("key: " + key + " value: " + calculated.get(key));
}
}