Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/labs/introtoprogramming/lab5/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import labs.introtoprogramming.lab5.gui.SceneRendererWindow;
import labs.introtoprogramming.lab5.scene.Scene;
import labs.introtoprogramming.lab5.scene.Camera;
import labs.introtoprogramming.lab5.scene.SceneObject;
import labs.introtoprogramming.lab5.scene.Transform;
import labs.introtoprogramming.lab5.scenes.DemoScene;

Expand All @@ -22,9 +23,11 @@ public static void main(String[] args) {

private void run() {
Scene demoScene = new DemoScene();
SceneObject empty = new SceneObject();
Camera camera = createCamera();
camera.getTransform().setParent(empty.getTransform());
demoScene.addSceneObjects(camera);
demoScene.addController(new KeyAndMouseMovementController(camera));
demoScene.addController(new KeyAndMouseMovementController(empty));

SceneRendererWindow window = new SceneRendererWindow(demoScene);
window.setTitle(APP_TILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ public BasicRaytracingRender(Raster raster) {
@Override
public void render(Scene scene) {
Camera camera = scene.getCamera().orElseThrow(NoCameraException::new);
Transform transform = camera.getTransform();
Vector3 pos = camera.getTransform().position();
int width = raster.getWidth();
int height = raster.getHeight();
double aspectRatio = camera.aspectRatio();
double fieldOfView = Math.tan(camera.fieldOfView() / 2);
Ray primaryRay = new Ray(pos, Vector3.ZERO);
Transform transform = camera.getTransform();
primaryRay.setOrigin(transform.applyPoint(transform.position()));
for (int y = 0; y < height; y++) {
double normalizedY = 1 - 2 * (y + 0.5) / height;
double cameraY = normalizedY * fieldOfView;
Expand All @@ -40,7 +41,6 @@ public void render(Scene scene) {
double cameraX = normalizedX * aspectRatio * fieldOfView;
Vector3 direction = new Vector3(cameraX, cameraY, -1).normalize();
primaryRay.setDirection(transform.applyVector(direction).normalize());
primaryRay.setOrigin(transform.applyPoint(transform.position()));
Color color = getColor(primaryRay, scene);
raster.setPixel(x, y,
(byte) color.getRed(),
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/labs/introtoprogramming/lab5/scene/Transform.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Transform {
private Vector3 rotation;
private Vector3 scale;
private Matrix44 matrix;
private Transform parent;

/**
* Container of object transformations.
Expand Down Expand Up @@ -65,6 +66,9 @@ public void setScale(Vector3 scale) {
* @return transformed vector to local space
*/
public Vector3 applyVector(Vector3 v) {
if (parent != null) {
v = parent.applyVector(v);
}
return matrix.applyVector(v);
}

Expand All @@ -75,6 +79,9 @@ public Vector3 applyVector(Vector3 v) {
* @return transformed point to local space
*/
public Vector3 applyPoint(Vector3 v) {
if (parent != null) {
v = parent.applyPoint(v);
}
return matrix.applyPoint(v);
}

Expand All @@ -89,4 +96,8 @@ public Vector3 applyPoint(Vector3 v) {
public void setMatrix(Vector3 right, Vector3 up, Vector3 forward, Vector3 translate) {
matrix.setMatrix(right, up, forward, translate);
}

public void setParent(Transform parent) {
this.parent = parent;
}
}