Skip to content

antekone/jraytracer

Repository files navigation

Java Raytracer demo

Everyone needs to write a raytracer, right?

So this is a simple real-time raytracer written in Java with a Swing-based interactive UI. It's CPU-only, doesn't use OpenGL, and doesn't use GPU acceleration. Move the camera with WASD, zoom with = and -, and quit with Q or ESC. The scene features 5 colored spheres arranged in a circle, a ground plane, and multiple light sources (point, directional, and ambient).

The project is just a prototype, and doesn't do proper double buffering. That's why there can be some artifacts during redrawing.

Features

  • Real-time multi-core rendering,
  • Interactive camera movement and zoom,
  • Scene with multiple spheres, ground, and lights.

Controls

  • W, A, S, D: Move camera
  • = / -: Zoom in/out
  • Q or ESC: Quit
  • [ and ]: Rotate camera

Screenshots

Screenshot 1

Screenshot 2

Screenshot 3

Rectangles

How it works

  • Spheres: Ray/sphere hits are solved from the quadratic equation of the sphere surface; the closest valid t is chosen and the surface normal is the vector from the center to the hit point.
  • Rectangles: A plane hit is found via the rectangle normal; the ray is discarded if parallel, outside the near/far t range, or if the projected hit falls outside the half-width/half-height extents along the rectangle’s basis axes.
  • Shadows: For each light a shadow ray is cast from the visible point toward the light (or along the light direction); if any object intersects before the light, that light contributes nothing.
  • Light & shading: Ambient adds constant intensity, while point and directional lights add diffuse (dot product) and Phong-style specular highlights; reflective materials blend the local color with a recursively traced reflection ray.
  • Reflection: The reflection ray is computed by mirroring the view vector around the surface normal; its color contribution is recursively traced and mixed with the local shading by the material’s reflectivity.

Build & Run

./gradlew shadowJar
java -jar build/libs/jtracer-1.0-SNAPSHOT-all.jar

Tested only on Linux X11 + KDE.

License

Public domain.

Credits

Gabriel Gambetta (https://gabrielgambetta.com)

About

Java Raytracing Demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages