-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.java
More file actions
214 lines (169 loc) · 6.82 KB
/
Renderer.java
File metadata and controls
214 lines (169 loc) · 6.82 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
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import libs.*;
import src.*;
public class Renderer{
private Display display;
private int[] resolution;
private Camera camera;
private RenderableObject[] objects;
private Light light;
private int[][] skybox;
private long time;
public Renderer() {
display = new Display(480, 300, "Raymarcher");
resolution = new int[]{480,300};
time = 0;
vec3 camPosition = new vec3(0,0,-150);
vec3 sphereCenter = new vec3(0,0,0);
int sphereSize = 50;
vec3 lightPosition = new vec3(0,-10,-90);
vec3 lightColor = new vec3(1,1,1);
camera = new Camera(camPosition);
Material sph_mat = new Material(new vec3(1,0,0));
Sphere sphere = new Sphere(sphereCenter, sphereSize, sph_mat);
Material pl_mat = new Material(new vec3(0,1,0));
Plane plane = new Plane(new vec3(0f,1f,0f).normalize(), 200,pl_mat);
objects = new RenderableObject[]{sphere,plane};
light = new Light(lightPosition,lightColor);
try {
BufferedImage image = utils.convertToARGB(ImageIO.read(new File("res/studio.jpg")));
skybox = utils.get2D(image);
BufferedImage texture = utils.convertToARGB(ImageIO.read(new File("res/checkers.png")));
sphere.setTexture(utils.get2D(texture));
} catch (IOException e) {
System.out.println("Exception!" + e);
}
}
// Generates a frame of the scene
private BufferedImage generateFrame() {
// Calculated by dividing screen width by screen height
float aspectRatio = (float)resolution[0] / resolution[1];
// The degrees of the field of view in the y direction
float fovY = 70;
float fovX = fovY * aspectRatio;
int iterMax = 5;
vec3[] alpha = new vec3[iterMax];
BufferedImage frame = new BufferedImage(resolution[0], resolution[1], BufferedImage.TYPE_INT_RGB);
int maxRayDist = 10000;
int maxSightDist = 5000;
for (int x = 0; x < resolution[0]; x++) {
for (int y = 0; y < resolution[1]; y++) {
vec3 px = camera.pos().copy();
float dirX = (float) Math.sin(Math.toRadians(fovX * ((float) x / resolution[0] - 0.5f)));
float dirY = (float) Math.sin(Math.toRadians(fovY * ((float) y / resolution[1] - 0.5f)));
float dirZ = (float) Math.sqrt(Math.max(0, 1 - (dirX * dirX) - (dirY * dirY)));
vec3 dir = new vec3(dirX, dirY, dirZ).normalize();
boolean finished = false;
int iter = 0;
while (!finished && iter < iterMax) {
rayMarchReturn rayMarchReturn = rayMarch(this.objects, px);
float closestSignedDist = rayMarchReturn.signedDist;
RenderableObject closestObject = this.objects[rayMarchReturn.object];
if (closestSignedDist < 1) {
dir = vec3.reflect(closestObject.getNormal(px), dir);
alpha[iter] = calcColor(closestObject, px);
px.add(vec3.scale(dir, 1f));
iter++;
} else if (closestSignedDist > maxRayDist || vec3.getDist(px, camera.pos()) > maxSightDist) {
// Point is too far from objects
float[] textCoord = utils.getUV(dir);
vec3 skyboxColor = utils.intToRGB(this.skybox[(int) (textCoord[0] * (skybox.length - 1))][(int) (textCoord[1] * (skybox[0].length - 1))]);
alpha[iter] = skyboxColor;
iter++;
finished = true;
} else {
// Ensure we don't get stuck in an infinite loop
float stepSize = Math.max(closestSignedDist, 0.01f); // Prevents too small steps
px.add(vec3.scale(dir, stepSize));
}
}
vec3 px_color = new vec3(0, 0, 0);
for (int a = 0; a < iter; a++) {
px_color.add(alpha[a].mult((iter - (float) a) / (float) utils.summation(iter)));
}
frame.setRGB(x, y, utils.rgbToInt(px_color));
}
}
return frame;
}
private rayMarchReturn rayMarch(RenderableObject[] objects, vec3 px) {
float closestSignedDist = objects[0].signedDist(px);
int object = 0;
for (int i = 1; i < objects.length; i++) {
if (objects[i].signedDist(px) < closestSignedDist) {
closestSignedDist = objects[i].signedDist(px);
object = i;
}
}
return new rayMarchReturn(object, closestSignedDist);
}
private vec3 calcColor(RenderableObject obj, vec3 pt) {
vec3 baseColor = obj.getColor(pt);
float shadowValue = isPointInShadow(pt);
float ambientIntensity = 0.12f;
float diffuseIntensity = 0.5f * shadowValue;
float specularIntensity = 0.4f * shadowValue;
vec3 ambient = vec3.scale(light.getColor(), ambientIntensity);
vec3 normalVector = obj.getNormal(pt);
vec3 pointToLightVector = vec3.getDir(pt, light.pos());
float diffuseDot = Math.max(vec3.dot(normalVector,pointToLightVector),0);
vec3 diffuse = new vec3(diffuseDot * diffuseIntensity);
vec3 viewVector = vec3.getDir(pt, camera.pos());
vec3 reflectVector = vec3.reflect(obj.getNormal(pt), pointToLightVector.reverse());
float specularDot = (float)Math.pow(Math.max(vec3.dot(viewVector, reflectVector),0),32);
vec3 specular = vec3.mult(new vec3(specularDot * specularIntensity), light.getColor());
vec3 calcColor = vec3.add(ambient, diffuse, specular).mult(baseColor);
return calcColor;
}
private float isPointInShadow(vec3 pt) {
vec3 pointToLightVector = vec3.getDir(pt, light.pos());
vec3 px = pt.copy();
px.add(vec3.scale(pointToLightVector,1));
boolean finished = false;
while (!finished) {
rayMarchReturn rayMarchReturn = rayMarch(objects, px);
float closestSignedDist = rayMarchReturn.signedDist;
if (closestSignedDist < .01) {
return 0;
}
else if (closestSignedDist > vec3.getDist(px, light.pos())) {
return 1;
}
else {
px.add(vec3.scale(pointToLightVector,closestSignedDist));
}
}
return 0;
}
public void render(float deltaTime) {
time += deltaTime;
updateObjects();
//camera.setPos(display.updateCam().mult(-150));
display.updateCam();
light.setPos(new vec3((float)Math.sin(time/5/100f)*100f, -90, -90));
display.render(generateFrame());
}
public void updateFPS(float fps) {
display.updateFPS(fps);
}
private void updateObjects() {
int speed = 1000;
float deltaTime = FrameLoop.getInstance().getDeltaTime()/1000;
for (RenderableObject object : objects) {
object.setTime(time);
}
camera.move(new vec3(0,0,1).mult(display.getVertical() * deltaTime * speed));
camera.move(new vec3(1,0,0).mult(display.getHorizontal() * deltaTime * speed));
}
public static class rayMarchReturn {
public final int object;
public final float signedDist;
public rayMarchReturn(int object, float signedDist) {
this.signedDist = signedDist;
this.object = object;
}
}
}