Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 1658 times)
0 Members and 1 Guest are viewing this topic.
Offline JuMisMa

Senior Newbie


Medals: 1



« Posted 2013-07-12 18: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 :

KDTREE:
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.

ANTI-ALIASING:
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.

SMOOTH SHADOW:
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 RT_SCENE.java and RT_KDTREE.java

Java 7 is required (i think)

Thank'you.
Fred

The executable jar
The eclipse jar (kepler)


Offline matheus23

JGO Kernel


Medals: 98
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2013-07-12 21: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: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


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

Shocked

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

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


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

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

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

Might not be useful in games but it looks darn beautiful!
Offline kpars

JGO Ninja


Medals: 57
Projects: 4
Exp: 2 years


Kemoy Labs/Radirius Game/Engine Developer


« Reply #5 - Posted 2013-07-13 17:07:46 »

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

"Living is easy with eyes closed, misunderstanding all you see. It's getting hard to be someone, but it all works out." ¤¤ Kemoy Labs: http://www.kemoy.net/
Offline moogie

JGO Knight


Medals: 11
Projects: 5
Exp: 10 years


Java games rock!


« Reply #6 - Posted 2013-07-14 10: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 )
Warning! Untrusted content: moogie submitted an applet to JGO.


 
If the applet asks for permissions, it will have full access to your system. (read more)
JGO cannot be held responsible for the contents and/or behavior of the hosted applets.

Up/down arrow = thrust
left / rigth = roll
Mouse drag = move mouse view.

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

Senior Newbie


Medals: 1



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

Thank's.

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

http://www.flipcode.com/archives/Raytracing_Topics_Techniques-Part_7_Kd-Trees_and_More_Speed.shtml
Offline moogie

JGO Knight


Medals: 11
Projects: 5
Exp: 10 years


Java games rock!


« Reply #8 - Posted 2013-07-15 01: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.
Offline moogie

JGO Knight


Medals: 11
Projects: 5
Exp: 10 years


Java games rock!


« Reply #9 - Posted 2013-07-15 01: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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline moogie

JGO Knight


Medals: 11
Projects: 5
Exp: 10 years


Java games rock!


« Reply #10 - Posted 2013-07-15 23: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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (64 views)
2014-04-15 18:08:23

BurntPizza (62 views)
2014-04-15 03:46:01

UprightPath (75 views)
2014-04-14 17:39:50

UprightPath (58 views)
2014-04-14 17:35:47

Porlus (76 views)
2014-04-14 15:48:38

tom_mai78101 (101 views)
2014-04-10 04:04:31

BurntPizza (161 views)
2014-04-08 23:06:04

tom_mai78101 (256 views)
2014-04-05 13:34:39

trollwarrior1 (209 views)
2014-04-04 12:06:45

CJLetsGame (216 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!