r/java • • Jul 14 '21

I Created a java 3D rendering engine from scratch (no natives just java) for no reason

Post image
772 Upvotes

94 comments sorted by

117

u/NitronHX Jul 14 '21 edited Jul 21 '21

Because you all asked for it : https://github.com/nbrugger-tgm/JRender

  1. The shadow on the sphere is not as intended as normals currently malfunction on spheres

  2. Why have I done this: no clue myself

  3. Should you do it: HEK NO

Data:

  • Render time : horrible, dependent on (hardcoded) settings & resolution between 0.01-25 seconds

  • Technique used : Raymarching & selfmade stuff

  • libs : "libgdx-math" (just used the Vec3,Vec2 and Quanterion class)

  • Runs on GPU : hell no, this would add performance, no way i am doing that

7

u/dpash Jul 15 '21

"libgdx-math" (just used the Vec3,Vec2 and Quanterion class)

Could you replace this with the recent vector API in modern JDKs? But there is a danger that it might improve performance :)

1

u/NitronHX Jul 15 '21 edited Jul 15 '21

Yeah big danger not doing that.

No the thing is, for the reason I did it performance doesn't matters. And the incubating vector api seems not very intuitive to work with and just harder to use, as it is just about SIMD but when it is released I will check it out and try stuff with it

2

u/dpash Jul 15 '21

Hopefully it should just involve changing the package names in the imports, assuming it doesn't have any API changes over the next few releases.

2

u/NitronHX Jul 15 '21

You can make a pr if you want 😉

2

u/Kantaja_ Jul 15 '21

they're probably optimized to simd by the jvm anyway

4

u/dpash Jul 15 '21

https://cr.openjdk.java.net/~vlivanov/talks/2019_CodeOne_MTE_Vectors.pdf

My understanding is that currently it's very very dependent on how the code is written and will only optimise unrolled loops, so it's very unlikely.

Using the Vector API will use SIMD.

3

u/Kango_V Jul 15 '21

You could try this "for fun": https://openjdk.java.net/jeps/338

2

u/NitronHX Jul 15 '21

I am looking forward a lot to this project, obviously. But I am scared that this involves a Lot of low level handwork which I have no idea about

2

u/Qildain Jul 15 '21

Are you open to pull requests? I love the idea of what you've done! I may even be able to point out some potential for cleanup - maybe, lol. No need to even merge them. I suppose if you have allowed forking, that's awesome too. I'll be checking out the repo shortly. Is it open source, and if so, what's the license? My intent is only to contribute if I can.

2

u/NitronHX Jul 15 '21 edited Jul 15 '21

Hi, thanks for your interest in this project.

You can use my project however you want, just credit me :)

For contribution, yes i am totally fine with that, i even enjoy it. Your PRs will most likely be merged and be appreciated by me.

edit: I would prefer if you use conventional commits

License is MIT

2

u/Qildain Jul 15 '21

Whatever you would like - Thanks for the info - I'll dig right in!

14

u/Ramexo Jul 15 '21

Yo know making something just becoes you can is not a bad thing. And more difficult prkkects are ones you will learn moust from.

And its probebly good for portofolio

3

u/[deleted] Jul 15 '21

Can you pin this?

41

u/Elyahu41 Jul 14 '21

Source code?

63

u/NitronHX Jul 14 '21

I am ashamed of the code but i can send it to you if you want (its not on github as its a trashpile)

138

u/gregorydgraham Jul 14 '21

Put it on GitHub anyway, it’s better than 99% of projects.

Besides, nothing encourages you to clean up your mess like visitors

8

u/NitronHX Jul 15 '21

Yeah willdo cleanup and upload it

0

u/[deleted] Jul 15 '21

[deleted]

48

u/NitronHX Jul 15 '21

Ok fine you convinced me, see it up tomorrow

18

u/actuallyitsnathaniel Jul 15 '21

hey dude, for real you should post it. this will throw you ahead of the rest if you're looking for work. it may not mean much coming from someone also looking for work, but seriously I'm impressed. from what I've researched, minecraft's first release was TERRIBLY written. so bad that in more recent years it was rebuilt completely. so I wouldn't feel too bad about revealing what you have. i'd cite my sources but I'm lazy. anyways, incredible work!!! excited to see what you've done!

0

u/[deleted] Jul 15 '21

[deleted]

15

u/NitronHX Jul 15 '21

I started as I was watching a yt video about raymarching. I wanted to use it as I don't need geometry data placed in my word and The base of raymarching is easy. Further I just wanted to test how far I can make it and how good the result will be. I didn't watched any guides during the development as it was a challenge to myself and there is not much to explore and learn if you be thought by a video. I still used stack overflow for question how to achieve certain things with the math lib (like calculating the angle between vectors and stuff)

17

u/Elyahu41 Jul 14 '21

Yes, I would like to see how you did it. As I'm still learning Java in college. It seems really interesting that you were able to do it with just math libraries.

29

u/NitronHX Jul 14 '21 edited Jul 15 '21

URL: https://github.com/nbrugger-tgm/JRender

It contains the whole project (including ~100mb textures/heigh/normal maps. to build it you need gradle.

for the UI (swing) i use a lib (reactJ)

The main class you want to take a look at is "RaymarchingShader" and also "SwingRenderer"

If you need help i sent you my discord in the PM

3

u/[deleted] Jul 15 '21

How did you create the textures/maps?

4

u/NitronHX Jul 15 '21

The textures and maps are downloaded from FREEpbr (Except the "test" textures I created them in paint dot net)

6

u/Elyahu41 Jul 14 '21

Awesome! Thanks! I'll take a look at it!

2

u/F14D Jul 17 '21

Nice. You should add an image (or two ) to the readme so people passing-by get an idea as to what the poject is/does...

1

u/ekbravo Jul 15 '21

Thank you

3

u/PotaytoPrograms Jul 15 '21

graphics is just maths lol

9

u/ekbravo Jul 15 '21

My vote to put on GitHub. Just do it.

4

u/NitronHX Jul 15 '21

Your vote has been heard, look it up tomorrow

5

u/ekbravo Jul 15 '21

I sympathize with you as I have many private repos on GitHub exactly because I felt that my code is not polished enough. But putting myself up for public review made me a better coder. Most of my private became public over time.

7

u/FrezoreR Jul 14 '21

Wait a minute.. you use notepad++ to write java code? 😱

13

u/NitronHX Jul 15 '21

No luckily not, just tweaked the quality variables. Developed it with intelliJ

3

u/FrezoreR Jul 15 '21

puh that makes sense!

3

u/PotaytoPrograms Jul 15 '21

ya missed a big chance to look even more amazing

1

u/ItsDokk Jul 15 '21

You’re not a developer if you don’t code exclusively in notepad! 😂

3

u/FrezoreR Jul 15 '21

Poor guys developing on Linux and MacOS 😂

6

u/PotaytoPrograms Jul 15 '21

I too make overly complex things and have no idea why

3

u/Persism Jul 15 '21

You should check out David Brakeen's Developing Games in Java - http://www.brackeen.com/javagamebook/

4

u/NitronHX Jul 15 '21

A game using this engine would run on like 0.05 fps 😂😂 But thanks for the link, love to educate myself

0

u/[deleted] Jul 15 '21

[deleted]

2

u/RandomName8 Jul 15 '21

I believe he referred to his own thing, not your link.

3

u/StealthyPanda69 Jul 15 '21

Holy shit dude are you me? I literally started making a graphics engine (and a physics engine) too like 4 days ago too for no reason. Mine is far from being done but if you wanna check it out its on github. https://github.com/StealthyPanda/buff Most of my time went in making the vector and quaternion classes but it barely functions right now.

Man this is an uncanny coincidence.

1

u/NitronHX Jul 15 '21

Illumiati incomming :)

As long as yours isn't done in Python or Java it is more usefull than mine

1

u/StealthyPanda69 Jul 15 '21

If you don't mine me asking, how long did this take you to complete? I have no idea what I've gotten myself into so it would be nice to know a ballpark.

1

u/NitronHX Jul 15 '21

Haha I think the coding itself took me like 8 to 10 hours. But in the past I watched 1 hour of video content about raymarching theory.

But there are some things I just know other persons have to learn. If you need any assistance text me so I can help you (discord or whatever)

2

u/jevring Jul 16 '21

Looks nice. Welcome to the (probably very small) group of people who write our own 3d renderers for fun =)

Here's mine: https://bitbucket.org/jevring/threedee/src/master/

2

u/NitronHX Jul 16 '21

Looks way more sophisticated than what i have done, my one is just a quick and dirty ~600 loc

5

u/PlasmaFarmer Jul 14 '21

I like you.

1

u/tcp-ip1541 Jul 15 '21

Absolutely sick! Can you share the code?

2

u/NitronHX Jul 15 '21

If you need it now there is a comment I shared a link to the src code. If you can wait I will clean the code and upload it to Github by tomorrow

1

u/Zeroneca Jul 15 '21

I am curious why Swing and not JavaFX?

2

u/NitronHX Jul 15 '21

Well JavaFX has no advantages for this use case I think. Also Jfx comes with a little pre-assembled 3D stuff (I think) but I wanted to start from scratch

Tldr: I don't saw any need for javafx

1

u/anuruddhak Jul 15 '21

You just did this for no reason, imagine if incentives and motivation were to be applied!

Well done!

I haven’t looked at the code but it sounds like an awesome project!

Do you have a means to integrate to GPU rendering?

1

u/NitronHX Jul 15 '21

Do you have a means to integrate to GPU rendering?

No, this project was not meant to be used for anything usefull its just an experiment.

I wanted to do it in java because of the things i like about java (autocomplete typesavety etc)

Maybe I will somewhen externalize parts of the heavy lifting to the GPU with TorndaoVM or something along this lines but thats it. (also as experiment)

1

u/AppleTrees2 Jul 16 '21

Very nice!

How would one acquire the knowledge to do this?

Can you point which books or resources you used to do it?

Thank you

1

u/NitronHX Jul 16 '21

Well the knowledge about java i aquired over a long time, since java is my hobby since a few years.

The rendering stuff I figured out myself. For raymarching which is the technique used to define objects and "find them" i watched a video once: https://youtu.be/PGtv-dBi2wE

Resources i used was just googeling stuff like "how to rotate a vector by a quaterion" and looking thru the results

Most of what you see is just stuff i came up with myself or leveraged from my physic knowledge about light-physics

I am sorry i cant be more helpful, but if you PM me i can maybe help more

1

u/AppleTrees2 Jul 16 '21

I see, cool.

I forgot most of the math stuff I know, and I doubt I could figure out the 3d stuff by myself, maybe 2d stuff but not 3d.

I will give that video a watch and see what I can figure out

Thank you

1

u/NitronHX Jul 16 '21

I forgot most of the math stuff I know, and I doubt I could figure out the 3d stuff by myself, maybe 2d stuff but not 3d.

The math isnt that hard you just have to know what the dot product is and the cross product works. And imagine what adding, multiplying vectors does.

Also vectors work pretty much the same in 2D and 3D only very minor differences.

And the mathematical operations behind are done by a lib anyways (except you want to torture yourself any further)

1

u/AppleTrees2 Jul 17 '21

I see, I don't have an intuitive grasp of the dot product/cross product because I haven't had a use for them in many years, decades even.

And the camera/view itself? can it also be derived from the dot product only?

1

u/NitronHX Jul 17 '21

The dot product describes how parallel two vector are at a scale feo. - 1 to 1 where - 1 means they are pointing exactly in opposite directions.

When using raymarching defining the cam and view is easy(no translation matrices). I uvmap the screen so the pxl coordinate is given as a number between 0 and 1 (so for example middle of the screen is 0.5,0.5. And then added a third component (1) to make it an 3D position. If I then define the camera position as 0.5,0.5, 0. It is one unit behind (z is deph) and in the middle of the viewport

Then raymarching is done and a ray is casted for every uv from the camera (eg 0.5,0.5,0,0) so FOV and all this stuff you don't have to worry about but you can change it by moving the camera back and forth

Hope I could help you

1

u/tiago4171 Aug 08 '21

I used to associate java with boredom, but this project changed my mind for good. For Sure I'm going to study java soon! Probably for a different purpose but I'll

1

u/NitronHX Aug 10 '21

Happy, to hear that😊 Nothing makes learning as easy as having a fun goal or project.

1

u/Bleidorb Aug 27 '21

What is a native?

2

u/NitronHX Sep 02 '21

Code written in c or c++ that is used by Java. It is called natives because it is the language Java is written in.

Means I didnt used JNI nor JNA

1

u/[deleted] Dec 07 '21

I’m developing a raytracer in Java as well. Have you tried implementing threads in the part where you render? I did that in mine and it increased the speed around 10x.

1

u/NitronHX Dec 25 '21

I implemented mulithreading. So every line is rendered in a seperate thread

for(var y = 0;....){
    lineRenderJobs.add(...);
}

where each thread loops over the line and renders each pixel

2

u/rjgo27 Jul 15 '21

Why java? - just because i can 🤣🤣🤣

2

u/_-pK Jul 15 '21

During my time as a trainee we had an apprentice for a while who (apperently) wrote a 3D animation render in Javascript. Didn't look as cool as yours though.

2

u/morphotomy Jul 15 '21

For no reason

Bullshit. I bet you learned a lot while you did this. Thats a great reason to do anything difficult.

Stop minimizing your ambition.

2

u/karianna Jul 15 '21

Forked and sent in first Pull Request - I love seeing this sort of thing - rock on for learning and having fun with Java!

2

u/wubwub Jul 29 '21

I don't know how many times I set out to do just that.

Kudos to you for doing it!!!!

1

u/Linux_user592 Jul 15 '21

Awsome Now make minecraft 2.0

1

u/alehel Jul 15 '21

No reason is the best reason!

1

u/[deleted] Jul 15 '21

How do you even start to create something like this. I'd love to see the source code. Even if it's bad code or whatever, it would give us some ideas anyways.

1

u/jacob_scooter Aug 02 '21

ok then linus torvalds 🖕

0

u/[deleted] Jul 14 '21

[deleted]

14

u/NitronHX Jul 14 '21

The bigges problem would be the recursion as GPUs don't support this because they don't have a call stack.

I use recursion to calculate the reflections by rendering a pixel from the POV of the reflection surface

10

u/NitronHX Jul 14 '21

It wouldn't even be that hard, I could figure it out in a day and port it to GPU code. The reason i made this is that i wanted to write a shader in java. If you programm a GPU you need to use another language and "inject" it into the GPU with java. So you need to write the shaders in HLSL or GLSL (c like languages)

Rarely seeing someone from my country here :)

0

u/[deleted] Jul 18 '21

[deleted]

1

u/NitronHX Jul 18 '21

No i really mean Austria, just keep it englisch for the community. Hallo Kamerad xD

2

u/[deleted] Jul 15 '21

[removed] — view removed comment

2

u/NitronHX Jul 15 '21

Yep researched about that too but tbh I didn't rly cared about performance because, well I created a 3d rendering engine in Java so it was clear from the beginning.

But I will have a look to experimwnt with tornado

2

u/NitronHX Jul 15 '21

If you want to play and Programm your gpu to do exactly such things you can use shader toy it's an online ide that let's you write code (HLSL) and directly executes it on your GPU

0

u/[deleted] Jul 14 '21

[deleted]

12

u/NitronHX Jul 14 '21

I use IntelliJ, just used it to show off some code and fill the space

Also you are wrong windows texteditor is superior

2

u/rafaellago Jul 15 '21

Edit on the command prompt is superior

1

u/ingframin Jul 15 '21

Well I kinda did that in the past. I started my programming journey with QBasic on ms-dos. I don’t remember the command though, but I still remember F6 was used to mark the end of file. (And it still works on Windows 10).

2

u/dpash Jul 15 '21

It is now that it supports new lines correctly.