-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRender.java
More file actions
307 lines (272 loc) · 9.87 KB
/
Copy pathRender.java
File metadata and controls
307 lines (272 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package renderer;
import primitives.*;
import scene.Scene;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
//import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
import elements.LightSource;
import geometries.Geometry;
public class Render {
private static final int MAX_CALC_COLOR_LEVEL = 3;
private Scene _scene;
private ImageWriter _imageWriter;
private static class GeoPoint {
public Geometry geometry;
public Point_3D point;
}
// ***************** Constructors ********************** //
/**
* construct Render with Image Writer and Scene
*
* @param imageWriter
* @param scene
*/
public Render(ImageWriter imageWriter, Scene scene) {
_scene = scene;
_imageWriter = imageWriter;
}
// ***************** Operations ******************** //
/**
* painting a picture
*/
public void renderImage() {
int Nx = _imageWriter.getNx();
int Ny = _imageWriter.getNy();
double distance = _scene.distance();
int width = _imageWriter.getWidth();
int height = _imageWriter.getHeight();
for (int i = 0; i < Ny; i++)
for (int j = 0; j < Nx; j++) {
Ray ray = _scene.camera().constructRayThroughPixel(Nx, Ny, i, j, distance, width, height);
Map<Geometry, List<Point_3D>> intersectionsPoints = _scene.getGeomtries().findIntersections(ray);
if (intersectionsPoints.isEmpty())
_imageWriter.writePixel(i, j, _scene.background().getColor());
else {
GeoPoint closesPoint = getClosestPoint(intersectionsPoints);
_imageWriter.writePixel(i, j, calcColor(closesPoint, ray).getColor());
}
}
}
/**
* prints grid
*
* @param interval
* - integer
*/
public void printGrid(int interval) {
int Nx = _imageWriter.getNx();
int Ny = _imageWriter.getNy();
for (int i = 0; i < Ny - 1; i++)
for (int j = 0; j < Nx - 1; j++)
if ((i + 1) % interval == 0 || (j + 1) % interval == 0)
_imageWriter.writePixel(j, i, _scene.ambientLight().color().getColor());
}
/**
*
*/
public void writeToImage() {
_imageWriter.writeToimage();
}
/***
* finding reflected ray
* @param n
* @param p
* @param v
* @return Ray
*/
private Ray constructReflectedRay(Vector n, Point_3D p, Ray v) {
try {
return new Ray(v.subtract(n.scale(2 * v.dotProduct(n))), addEps(p, v, n));
} catch (IllegalArgumentException s) {
return new Ray(v, addEps(p, v, n));
}
}
/***
* finding refracted ray
* @param n
* @param p
* @param v
* @return Ray
*/
private Ray constructRefractedRay(Vector n, Point_3D p, Ray v) {
return new Ray(v, addEps(p, v, n));
}
/***
* finds the closest intersected point
* @param ray
* @return GeoPoint
*/
private GeoPoint findClosestIntersection(Ray ray) {
Map<Geometry, List<Point_3D>> intersections = _scene.getGeomtries().findIntersections(ray);
if (!intersections.isEmpty())
return getClosestPoint(intersections);
return null;
}
/**
* wrap function for calcColor
*
* @param geopoint
* @param inRay
* @return Color
*/
private Color calcColor(GeoPoint geopoint, Ray inRay) {
return calcColor(geopoint, inRay, MAX_CALC_COLOR_LEVEL, 1.0);
}
/**
* calculate the color to paint the pixel
*
* @param p
* - Point_3D
* @param shape
* - Geometry
* @return color
*/
/*
* @SuppressWarnings("unlikely-arg-type") private Color calcColor(GeoPoint
* geopoint, Ray inRay, int level, double k) { if (level == 0 ||
* Coordinate.ZERO.equals(k)) return new Color(); Material mat =
* geopoint.geometry.material(); double kd = mat.kd(); double ks = mat.ks(); int
* nShininess = mat.nShininess(); double kr = mat.kr(); double kt = mat.kt();
* Color color = new Color(_scene.ambientLight().color()).scale(Math.max(0, 1 -
* kr - kt)); color = color.add(geopoint.geometry.emmission());
*
* Vector v = inRay.direction(); Vector n =
* geopoint.geometry.getNormal(geopoint.point);
*
* for (LightSource lightSource : _scene.lights()) { Vector l =
* lightSource.getL(geopoint.point); if (n.dotProduct(l) * n.dotProduct(v) > 0)
* { double o = occluded(l, geopoint); if (!Coordinate.ZERO.equals(o * k)) {
* Color lightIntensity = lightSource.getIntensity(geopoint.point).scale(o);
* color.add(calcDiffusive(kd, l, n, lightIntensity), calcSpecular(ks, l, n, v,
* nShininess, lightIntensity)); } } }
*
* Ray reflectedRay = constructReflectedRay(n, geopoint.point, inRay); GeoPoint
* reflectedPoint = findClosestIntersection(reflectedRay); if (reflectedPoint !=
* null) { Color reflectedLight = calcColor(reflectedPoint, reflectedRay, level
* - 1, k * kr).scale(kr); color.add(reflectedLight); }
*
* Ray refractedRay = constructRefractedRay(n, geopoint.point, inRay); GeoPoint
* refractedPoint = findClosestIntersection(refractedRay); if (refractedPoint !=
* null) { Color refractedLight = calcColor(refractedPoint, refractedRay, level
* - 1, k * kt).scale(kt); color.add(refractedLight); }
*
* return color; }
*/
/***
* create a cone shape of rays
* @param baseRay the ray in the center
* @param radius the radius of the ray
* @return a list with all the rays
*/
private ArrayList<Ray> coneRays(Ray baseRay, double radius) {
ArrayList<Ray> list = new ArrayList<>();
list.add(baseRay);
Random random = new Random();
Vector normal = baseRay.findNormal();
Vector cross = baseRay.crossProduct(normal);
double a,b;
for (int i = 0; i < 16; ++i) {
a = -radius + (2*radius)*random.nextDouble();
if (Coordinate.isZero(a))
a += 0.001;
b = -radius + (2*radius)*random.nextDouble();
if(Coordinate.isZero(b))
b += 0.001;
list.add(baseRay.nearbyRay(normal, cross, a, b));
}
return list;
}
/***
* recursive method to calculate the color to paint a pixel
* @param geopoint intersection point with the ray and a geometry
* @param inRay the ray that intersect the point
* @param level integer that defines the recursion level
* @param k double that defines the reflection/refraction quality
* @return the color to paint the pixel
*/
@SuppressWarnings("unlikely-arg-type")
private Color calcColor(GeoPoint geopoint, Ray inRay, int level, double k) {
if (level == 0 || Coordinate.ZERO.equals(k))
return new Color(0, 0, 0);
Material mat = geopoint.geometry.material();
double kd = mat.kd();
double ks = mat.ks();
int nShininess = mat.nShininess();
double kr = mat.kr();
double kt = mat.kt();
Color color = new Color(_scene.ambientLight().color()).scale(Math.max(0, 1 - kr - kt));
color = color.add(geopoint.geometry.emmission());
Vector v = inRay.direction();
Vector n = geopoint.geometry.getNormal(geopoint.point);
for (LightSource lightSource : _scene.lights()) {
Vector l = lightSource.getL(geopoint.point);
if (n.dotProduct(l) * n.dotProduct(v) > 0) {
double o = occluded(l, geopoint);
if (!Coordinate.ZERO.equals(o)) {
Color lightIntensity = lightSource.getIntensity(geopoint.point).scale(o);
color.add(calcDiffusive(kd, l, n, lightIntensity));
color.add(calcSpecular(ks, l, n, v, nShininess, lightIntensity));
}
}
}
Color reflectedLight = new Color();
Ray reflectedRay = constructReflectedRay(n, geopoint.point, inRay);
GeoPoint reflectedPoint = findClosestIntersection(reflectedRay);
if (reflectedPoint != null) {
ArrayList<Ray> reflectedList = coneRays(reflectedRay, 1);
int size = reflectedList.size();
for (int i = 0; i < size; ++i)
reflectedLight = calcColor(reflectedPoint, reflectedList.get(i), level - 1, k * kr);
reflectedLight.scale(kr / size);
color.add(reflectedLight);
}
Ray refractedRay = constructRefractedRay(n, geopoint.point, inRay);
GeoPoint refractedPoint = findClosestIntersection(refractedRay);
if (refractedPoint != null) {
Color refractedLight = new Color();
ArrayList<Ray> refractedList = coneRays(refractedRay, 1);
int size = refractedList.size();
for (int i = 0; i < size; ++i)
refractedLight = calcColor(refractedPoint, refractedList.get(i), level - 1, k * kt);
refractedLight.scale(kt / size);
color.add(refractedLight);
}
return color;
}
private Color calcDiffusive(double kd, Vector l, Vector n, Color lightIntensity) {
return new Color(lightIntensity).scale(kd * Math.abs(l.dotProduct(n)));
}
private Color calcSpecular(double ks, Vector l, Vector n, Vector v, int nShininess, Color lightIntensity) {
double vr = v.dotProduct(l.subtract(n.scale(2 * l.dotProduct(n))));
return vr > 0 ? new Color() : new Color(lightIntensity).scale(Math.pow(-vr, nShininess) * ks);
}
Point_3D addEps(Point_3D p, Vector v, Vector n) {
Vector epsVector = n.scale(n.dotProduct(v) > 0 ? 0.1 : -0.1);
return p.add(epsVector);
}
private double occluded(Vector l, GeoPoint geopoint) {
Vector tolightDirection = l.scale(-1);
Ray lightRay = new Ray(tolightDirection,
addEps(geopoint.point, tolightDirection, geopoint.geometry.getNormal(geopoint.point)));
Map<Geometry, List<Point_3D>> intersectionPoints = _scene.getGeomtries().findIntersections(lightRay);
double shadowK = 1;
for (Map.Entry<Geometry, List<Point_3D>> entry : intersectionPoints.entrySet())
shadowK *= entry.getKey().material().kt();
return shadowK;
}
private GeoPoint getClosestPoint(Map<Geometry, List<Point_3D>> intersectionsPoints) {
GeoPoint geopoint = new GeoPoint();
double minDistance = Double.MAX_VALUE;
Point_3D p0 = _scene.camera().p0();
for (Map.Entry<Geometry, List<Point_3D>> entry : intersectionsPoints.entrySet())
for (Point_3D point : entry.getValue())
if (p0.distance(point) < minDistance) {
geopoint.geometry = entry.getKey();
geopoint.point = point;
minDistance = p0.distance(point);
}
return geopoint;
}
}