Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (774)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ( almost :) ) Realtime Java Raytracing  (Read 5992 times)
0 Members and 1 Guest are viewing this topic.
Offline JuMisMa

Senior Newbie

Medals: 1

« Posted 2013-07-12 16:44:35 »

Hy everyone.

First, please sorry for my poor english but i’m french.

Here is my first raytracer in java. With low parameters, i’m close to realtime. On my computer - core
i5 2500K 4 cores at 4.2ghz - the demo’s scene - 17762 triangles - is about 3 fps with a screen at 600x400.
With higher parameters (anti-alliasing, smooth shadows, higher recursion), it can be very, very slow.

You can configure the raytracer with the « Param.txt » configuration file in "_Param" directory. All the
parameters are explained and easy to understand.

Some coding tips :

I’m using 2 kdtree for this demo.
- 1 for static objects that use SAH optimisation.
- 1 for dynamic objects, that must be rebuilt on each frame, with middle splitting axe. With « symetric »
objects, it’s not very much slower than SAH.

The KDTree build is multithreaded. At level 2, 4 threads are created. After, for the SAH version only,
if there are enough faces, 3 threads are created, one for each plane to check.

I use another optimization : as the triangles coordinate are sorted on the current axe, we don’t just use
left and right child, but i introduce a middle child, that was copied to the right child.

When turn on, the rendering is made twice :
- The standart one
- One the second pass, each pixel is compared to the 4 around. A delta is computed. If this delta
is higher the a value (AA_DELTA_COLOR in parameter’s file) then AA_LEVEL rays are fired.

For smooth shadow, i’m using a poisson distribution on light area (each light have is own rectangular
area) to avoid banding.

I’m a developper, not a graphist, so sorry for the demo’s scene that I know to be awful. All the
algorithms used come from the web. I’m just tried to use the best. I'm not a great programmer, as
i can see in this forum, so, please, be gentle with me and thank’s for reading my code and give
me you’re feedback and what can be improved. In particular, the normal mapping part, that i'm pretty
sure to be false. The most important classes are and

Java 7 is required (i think)


The executable jar
The eclipse jar (kepler)

Offline matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

« Reply #1 - Posted 2013-07-12 19:23:30 »

Well... yeah. Almost Cheesy

Get 1 FPS when minimized and with the lowest settings.

4 cores, 8 threads, 3.3 GHz.

Each frame takes about 10 seconds, when I turn on antialiasing and especially when I turn on smooth shadows. Looks really, really nice tho, and runs fast for what it shows!

Keep it up!

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

« Reply #2 - Posted 2013-07-12 20:52:24 »


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

« Reply #3 - Posted 2013-07-12 21:21:41 »

i7 2600K @ 4.5GHz: 3 FPS \o/

Offline relminator
« Reply #4 - Posted 2013-07-13 03:57:40 »

Might not be useful in games but it looks darn beautiful!
Offline kpars
« Reply #5 - Posted 2013-07-13 15:07:46 »

I just tried it.
I win with 4 FPS! Looks great  Grin
Offline moogie

JGO Ninja

Medals: 16
Projects: 6
Exp: 10 years

Java games rock!

« Reply #6 - Posted 2013-07-14 08:26:19 »

good work! back in the day I attempted to make a real time ray tracer for a java4k competition... I did get it to fit in 4k... but with no room for game play Tongue

I did have an interesting ( and to my knowledge novel ) idea for ray triangle intersection test which may be quite fast... i never did follow it up, but if you want i can outline the algorithm to see if it gives any speed improvements.

Oh I found a version of that realtime raytracer: (well before i started shrinking it Tongue )
[applet archive=jrtrtapplet.jar class=org.moogiesoft.JRTRT.TestApplet width=800 height=600]
Up/down arrow = thrust
left / rigth = roll
Mouse drag = move mouse view.

(Not sure why the Embeded applet tag is not working Sad )

Java4k RIP 2014
Offline JuMisMa

Senior Newbie

Medals: 1

« Reply #7 - Posted 2013-07-14 16:14:35 »


I'm using barycentric coordinates algo for ray-triangles intersections from :
Offline moogie

JGO Ninja

Medals: 16
Projects: 6
Exp: 10 years

Java games rock!

« Reply #8 - Posted 2013-07-14 23:40:09 »

Hehe, that site looks familiar Smiley I think i have used it in the past.

The ray/triangle intersection method that I had though up is to replace the barycentric coordinates conversion and subsequent tests with three simple distance tests.

The main thought is to approximate the triangle vertices as arcs on large radius circles: as the radius of a circle gets larger, an arc of of that circle of a fixed length starts to look like a straight line.

So to perform a ray-triangle intersection test:
1. calculate the intersection with the plane of the triangle
2. test to see whether the origin-intersection distance is less than the radius. If greater then stop as it does not intersect the triangle.
3. repeat 2 for remaining vertices.
4. ray does intersect the triangle.

Of course to speed up this algorithm i was planning to precompute and store the origins for each of the circles with the triangle and to only compare the squared distances (i.e. no square roots necessary)

To calculate the origins for each of the circles:
-take the 2D normal of a triangle vertex and project inward from the mid point of the vertex by a sufficiently large radius.
-for optimisation, the radius would be a constant and used for all triangles.

This algorithm is not limited to just triangles, any convex polygon can be represented by simply increasing the vertices checks.

There is a potential bonus property of this method, it should reduce the visibility of seams between triangles in a mesh as the triangles will be slightly bulging.

However this method may have some strange effects due to floating point precision between large and small numbers.

I never implemented it so if you do I would love to hear if it works and if it provides any speed boost.

Java4k RIP 2014
Offline moogie

JGO Ninja

Medals: 16
Projects: 6
Exp: 10 years

Java games rock!

« Reply #9 - Posted 2013-07-14 23:55:59 »

Infact I was going to use a modified version of this algorithm for the java 4k real time ray tracer. To help save code size I was was only going replace the ray-plane intersection test with a ray-sphere intersection test. In the same way as a arc of a large circle can approximate a vertex, a large radius sphere can approximate a polygon face.

Java4k RIP 2014
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline moogie

JGO Ninja

Medals: 16
Projects: 6
Exp: 10 years

Java games rock!

« Reply #10 - Posted 2013-07-15 21:45:48 »

I just had a thought: it is probable that you can skip calculating step the mid point of a vertex: given sufficiently large radius, and arc will approximate a straight line and thus the difference in distance between a triangle vertex of an edge and the origin as compared to the mid point of the edge to the origin will be minuscule.

Java4k RIP 2014
Pages: [1]
  ignore  |  Print  

EgonOlsen (1865 views)
2018-06-10 19:43:48

EgonOlsen (1887 views)
2018-06-10 19:43:44

EgonOlsen (1255 views)
2018-06-10 19:43:20

DesertCoockie (1686 views)
2018-05-13 18:23:11

nelsongames (1353 views)
2018-04-24 18:15:36

nelsongames (1970 views)
2018-04-24 18:14:32

ivj94 (2742 views)
2018-03-24 14:47:39

ivj94 (1944 views)
2018-03-24 14:46:31

ivj94 (3035 views)
2018-03-24 14:43:53

Solater (1086 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!