Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (710)
Games in Android Showcase (212)
games submitted by our members
Games in WIP (784)
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 4049 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: 471
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  
You cannot reply to this message, because it is very, very old.

numerical (56 views)
2017-02-21 07:32:16

numerical (55 views)
2017-02-21 07:31:46

theagentd (161 views)
2017-02-18 13:42:33

theagentd (164 views)
2017-02-18 13:35:16

h.pernpeintner (1327 views)
2017-01-24 22:39:11

h.pernpeintner (1315 views)
2017-01-24 22:38:32

Galdo (1875 views)
2017-01-12 13:44:09

Archive (1967 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2506 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2307 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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!