Join us on Discord!
You can help CodeWalrus stay online by donating here.

[Java] OpenGL ray tracer

Started by Snektron, April 13, 2016, 08:49:18 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Snektron

This weekend i decided to try to make a ray tracer. It can render triangle meshes, loaded from wavefront obj files.
It works by having all triangles stored in a bounded volume hierarchy, so only a part of the mesh needs to be tested against
rays, instead of every triangle.



You can download it here:
http://quantuminfinity.space/files/RayTracer.jar

I highly recommend only running this if you have a modern GPU. It requires OpenGL 4.3 to run, and will eat a load of GPU processing power.
To run it, just double-click and everything should work fine (on most platforms). If you want to load your own wavefront object model, run
java -jar RayTracer.jar model.obj
replace model.obj with the file name to load.
Make sure the object model has normals and is triangulated! (if you are unsure, open it in blender and export it with everything but triangulate and write normals disabled). Colors / textures etc are not supported.
It depends on the model and location, but most of the time i was able to achieve the 60 fps cap, though it gets a bit slower when the viewer is inside the object.
The default dragon has about 2000 faces and 1000 vertices, but i tried with a model with up to 24000 vertices too.

Im not really planning to release the source, but if you want you can decompile it :P
You can find the shader source in shaders/trace.csh when you open the jar as a zip file though.
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Dream of Omnimaga

That looks great and I am surprised it ran somewhat fast despite being a ray tracer. I don't get 60 FPS but rather something around 15, but it was still cool.

What are the controls besides the mouse, though?
  • Calculators owned: TI-82 Advanced Edition Python TI-84+ TI-84+CSE TI-84+CE TI-84+CEP TI-86 TI-89T cfx-9940GT fx-7400G+ fx 1.0+ fx-9750G+ fx-9860G fx-CG10 HP 49g+ HP 39g+ HP 39gs (bricked) HP 39gII HP Prime G1 HP Prime G2 Sharp EL-9600C
  • Consoles, mobile devices and vintage computers owned: Huawei P30 Lite, Moto G 5G, Nintendo 64 (broken), Playstation, Wii U

Snektron

WASD, space and shift to fly around. YTou can use control to get a speed boost.
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Dream of Omnimaga

Thanks. At first I was gonna reply that the keys do nothing, until I realized we have to hold the mouse button down while pressing them. :P
  • Calculators owned: TI-82 Advanced Edition Python TI-84+ TI-84+CSE TI-84+CE TI-84+CEP TI-86 TI-89T cfx-9940GT fx-7400G+ fx 1.0+ fx-9750G+ fx-9860G fx-CG10 HP 49g+ HP 39g+ HP 39gs (bricked) HP 39gII HP Prime G1 HP Prime G2 Sharp EL-9600C
  • Consoles, mobile devices and vintage computers owned: Huawei P30 Lite, Moto G 5G, Nintendo 64 (broken), Playstation, Wii U

Dudeman313

That's cool!
That dragon's really neat. :)
  • Calculators owned: TI-84 PCE
  • Consoles, mobile devices and vintage computers owned: Android O Phone
Does this qualify as a signature? 
The answer is "Sure."


ben_g

Quote from: Cumred_Snektron on April 13, 2016, 08:49:18 AM
It works by having all triangles stored in a bounded volume hierarchy, so only a part of the mesh needs to be tested against
rays, instead of every triangle.
So is it doing the kind of ray tracing that most engines use for collision and hit detection for every pixel to render the model?
Since it probably does almost all calculations per-pixel, does that mean that it can render insanely high-poly models with barely any slowdown?

It's a cool project, though it unfortunately doesn't seem to run on my laptop (since the 'run with powerfull graphics processor' option doesn't appear on .jar files, it's probably running on the on-board GPU, which is probably the cause).

Dream of Omnimaga

I am curious personally about if this could be used to draw maps? Also raytracing is starting being used in game experiments such as Quake Wars: Raytraced. I am curious about when computers will be powerful enough to run high-quality raytracing at 60 FPS...
  • Calculators owned: TI-82 Advanced Edition Python TI-84+ TI-84+CSE TI-84+CE TI-84+CEP TI-86 TI-89T cfx-9940GT fx-7400G+ fx 1.0+ fx-9750G+ fx-9860G fx-CG10 HP 49g+ HP 39g+ HP 39gs (bricked) HP 39gII HP Prime G1 HP Prime G2 Sharp EL-9600C
  • Consoles, mobile devices and vintage computers owned: Huawei P30 Lite, Moto G 5G, Nintendo 64 (broken), Playstation, Wii U

Snektron

#7
Quote from: ben_g on April 15, 2016, 04:07:32 AM
Quote from: Cumred_Snektron on April 13, 2016, 08:49:18 AM
It works by having all triangles stored in a bounded volume hierarchy, so only a part of the mesh needs to be tested against
rays, instead of every triangle.
So is it doing the kind of ray tracing that most engines use for collision and hit detection for every pixel to render the model?
Since it probably does almost all calculations per-pixel, does that mean that it can render insanely high-poly models with barely any slowdown?

It's a cool project, though it unfortunately doesn't seem to run on my laptop (since the 'run with powerfull graphics processor' option doesn't appear on .jar files, it's probably running on the on-board GPU, which is probably the cause).

I don't know what most engines use, but my tracer uses a BVH tree for storage. And sadly its not entirely true that it takes almost no power to render. It takes very little time to calculate find a node that intersects with the ray, but you ofcourse have to traverse all children of the tree until you are sure it doesn't hit anything, which makes it take a little more time.

It should use your primary GPU, i have an internal one too and it doesn't use that... If you get an error message you can post it in pastebin and i can see what you can do.
I should actually implements some capabilities checks since it needs OpenGL 4.3 ( if you get an exception with the message "shader = 0" then your opengl version is not high enough since it can't create the compute shader)

Quote from: DJ Omnimaga on April 15, 2016, 04:17:03 AM
I am curious personally about if this could be used to draw maps? Also raytracing is starting being used in game experiments such as Quake Wars: Raytraced. I am curious about when computers will be powerful enough to run high-quality raytracing at 60 FPS...

That's what i'm trying to find out too. There was Brigade engine too which could be used to make entirely raytraced games, but idk if it still exists. I do hear everywhere that raytracing is the future of game engines, since the traditional rasterizing is a cheat method...
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


ben_g

It just opens a window and closes it again, without any error message.

Also this is a laptop, so the on-board and dedicated GPU are in an 'optimus' configuration, and the on-board GPU always renders the desktop and non graphically-heavy programs, while programs that it recougnizes as needing extra GPU power should use the dedicated GPU instead, though this detection doesn't work well most of the time.

Snektron

Have you tried to run it from the commandline?

java -jar RayTracer.jar
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Dream of Omnimaga

  • Calculators owned: TI-82 Advanced Edition Python TI-84+ TI-84+CSE TI-84+CE TI-84+CEP TI-86 TI-89T cfx-9940GT fx-7400G+ fx 1.0+ fx-9750G+ fx-9860G fx-CG10 HP 49g+ HP 39g+ HP 39gs (bricked) HP 39gII HP Prime G1 HP Prime G2 Sharp EL-9600C
  • Consoles, mobile devices and vintage computers owned: Huawei P30 Lite, Moto G 5G, Nintendo 64 (broken), Playstation, Wii U

ben_g

Quote from: Cumred_Snektron on April 15, 2016, 10:19:23 AM
Have you tried to run it from the commandline?

java -jar RayTracer.jar

When just running it from the command prompt:
[spoiler=error log]
Unimplemented element: s
Size:
        Shapes (4179): 200592.0 bytes
        Triangles (2090): 66880.0 bytes
        Vertices (1017): 16272.0 bytes
        Total: 283744.0 bytes
Max depth: 13
java.lang.IllegalStateException: This functionality is not available.
        at org.lwjgl.system.Checks.checkFunctionality(Checks.java:57)
        at org.lwjgl.opengl.GL44.getInstance(GL44.java:137)
        at org.lwjgl.opengl.GL44.getInstance(GL44.java:132)
        at org.lwjgl.opengl.GL44.nglBindBuffersBase(GL44.java:363)
        at org.lwjgl.opengl.GL44.glBindBuffersBase(GL44.java:397)
        at net.quantuminfinity.engine.gl.shader.compute.ShaderStorageBuffer.bind(ShaderStorageBuffer.java:38)
        at net.quantuminfinity.bvh.CSTracer.render(CSTracer.java:54)
        at net.quantuminfinity.bvh.BVH.onRender(BVH.java:71)
        at net.quantuminfinity.engine.Engine.render(Engine.java:127)
        at net.quantuminfinity.engine.Engine.loop(Engine.java:90)
        at net.quantuminfinity.engine.Engine.start(Engine.java:65)
        at net.quantuminfinity.bvh.BVH.main(BVH.java:87)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)[/spoiler]

But it works when I run the command prompt with the dedicated GPU and then start it from there.

Snektron

It looks like the integrated gpu does not support OpenGL 4.4, and i don't think Java programs get recognized as gpu intensive games by your system...
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Dream of Omnimaga

WOuld there be a way to make this with an older OpenGL version that can run on a larger amount of computers?
  • Calculators owned: TI-82 Advanced Edition Python TI-84+ TI-84+CSE TI-84+CE TI-84+CEP TI-86 TI-89T cfx-9940GT fx-7400G+ fx 1.0+ fx-9750G+ fx-9860G fx-CG10 HP 49g+ HP 39g+ HP 39gs (bricked) HP 39gII HP Prime G1 HP Prime G2 Sharp EL-9600C
  • Consoles, mobile devices and vintage computers owned: Huawei P30 Lite, Moto G 5G, Nintendo 64 (broken), Playstation, Wii U

Snektron

I can replace some of the functionality, like the shader storage buffer objects (which were introduced in GL 4.4) with ordinary textures and the compute shader with a fragment shader (which appearantly would be a little slower),  allowing it to run on basically any hardware that supports OpenGL 2.0, but this is more a proof of concept program. I am actually plkanning on trying out if i can achieve this effect in Unreal Engine, which would allow it to run on anything that supports OpenGL ES 2 or DirectX 9 (i think).
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Powered by EzPortal