Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Realtime raytracing experiments in pure Java  (Read 6098 times)
0 Members and 1 Guest are viewing this topic.
Offline Varkas
« Reply #30 - Posted 2013-01-10 16:23:19 »

Good work!

Thanks Smiley Good to know that it runs with OpenJDK too.

I meanwhile found out why the volatile integer worked. The workerDone callback is synchronized altogether.

1  
2  
3  
4  
5  
6  
7  
8  
9  
    public synchronized void workerDone()
    {
        int count = doneCount.incrementAndGet();
       
        if(count == workers.size())
        {
            notify();
        }
    }


Instead of doneCount.incrementAndGet() the r4 had a doneCount++ there. But since the whole method is synchronized, there was no race condition there, and it was sufficient to have the variable volatile, to make sure all cores have the same value.

Learned something again. I must say in all the years of using Java I didn't learn as much about concurrency than in this small project. It's been good to try Smiley

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline gouessej
« Reply #31 - Posted 2013-01-10 20:33:22 »

OpenJDK 1.7 update 6

I guess my graphics tricks don't like that. Can you run it from a console window, and see if there is a ClassCastException or ArrayIndexOutOfBounds exception thrown? I had to reach deep into the raster classes to get Java2D working as a framebuffer for me.
The latest version (-r4) works, I get 11 FPS on my Intel Inside Pentium D. When I maximize the window, it becomes really slow.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #32 - Posted 2013-01-11 02:48:38 »

@ra4king: Wow, that is some gaming equipment you have there! Raytracing is a simple concept, but vector math ... well you know how to use OpenGL, so I assume have already learned most it Smiley
I am an expert in vector math and linear algebra in general. My only problem is laziness haha Cheesy

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Varkas
« Reply #33 - Posted 2013-01-11 11:23:14 »

I've added shadows. The impact on performance seems to be about 30%, that is harsh, but it still works quite fine and the shadows add a lot to the impression.



The latest demo with shadows added:

http://www.java-gaming.org/user-generated-content/members/132627/simpleray-r6.jar

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Danny02
« Reply #34 - Posted 2013-01-11 13:50:08 »

would you mind puttin up your  code?
want to try if i would run well on the GPU(your code)
Offline Varkas
« Reply #35 - Posted 2013-01-12 12:33:50 »

I haven't made up my mind yet if I want to publish this as open source.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Danny02
« Reply #36 - Posted 2013-01-12 14:44:24 »

why not?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #37 - Posted 2013-01-12 21:07:14 »

Doooo ittttt......I want to learn how in the world you do stuff like this :S

Offline Geemili

Senior Member


Medals: 9
Projects: 1
Exp: 2 years


No Games Finished


« Reply #38 - Posted 2013-01-13 01:00:01 »

I get 5 FPS... My computer certainly isn't the most powerful thing out there, that's for sure.
Offline deepthought
« Reply #39 - Posted 2013-01-13 03:18:52 »

this is encouraging. i'm gonna try my own idea for realtime raytracing.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longor1996
« Reply #40 - Posted 2013-01-13 18:13:04 »

Just a Question: How does your topmost code look like?
I mean the Code that is shooting the rays and puts the rgb-values into the picture.
And what do you use to display the image?

I'm just curious.

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline Varkas
« Reply #41 - Posted 2013-01-13 23:05:54 »

I'm writing the data to a BufferedImage and draw that through a g.drawImage() call. It's surprisingly fast, because I can use images without alpha channel.

The code looks like this - the real tracing code is in the object.hit() and object.trace() methods. I've tried to keep the framework expandable to have more object types but planes and spheres. TracerDataSet is kept in one instance per thread. linepix is an int [] which will take the pixels of one line. Ray, p, lineV and the like are instances of a 3D vector type. Look is the looking vector of the viewer into the scene.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
    void calculateScene(int yStart, int yEnd, TracerDataSet data)
    {
        int width = data.linepix.length;
        int height = displayPanel.getHeight();
       
        final int hw = width >> 1;
        final int hh = height >> 1;
       
        for(int y=yEnd; y>yStart; y--)
        {
            data.lineV.set(look);
            data.lineV.add(vert, y);
           
            for(int x=-hw; x<hw; x++)
            {
                data.ray.set(data.lineV);
                data.ray.add(horz, x);
               
                data.p.set(camera);
               
                final int rgb = traceObjects(data);

                data.linepix[hw+x] = rgb;
            }          
            displayPanel.setline(hh-y, data.linepix);
        }                
    }
   

    private int traceObjects(TracerDataSet data)
    {
        boolean go;
        int brightness = 255;
       
        long objectRgb = -1;
       
        do
        {
            go = false;
           
            final double t = findIntersection(data);

            if(data.bestObject != null)
            {
                final long color = data.bestObject.hit(data.p, data.ray, light, t);
               
                if(color == -1L)
                {
                    // mirror, p and v are set up by hit().
                   go = true;
                    brightness = brightness * 230 >> 8;
                }
                else
                {
                    objectRgb = color;
                }
            }
        }
        while(go && brightness > 16);

        if(objectRgb == -1)
        {
            // Nothing hit

            data.ray.norm();
            final int tx = (int)(Textures.clouds.getWidth() * (data.ray.x+1.0) * 0.5);
            final int ty = (int)(Textures.clouds.getHeight() * (data.ray.y+1.0) * 0.5);

            objectRgb = RGB.spread(Textures.clouds.getRGB(tx, ty));
        }
        else
        {
            // shadows
           // need to calculate ray from data.p to light source
           
            data.ray.set(light);
            data.ray.sub(data.p);
           
            findIntersection(data);
            if(data.bestObject != null)
            {
                // shadow
               // brightness = brightness * 10 >> 8;
               brightness = 32;
            }
        }
       
        return RGB.shadeAndCompact(objectRgb, brightness);
    }

    private double findIntersection(TracerDataSet data)
    {
        final double raylen2 = data.ray.length2();

        double bestT = Double.MAX_VALUE;
        data.bestObject = null;
       
        for(int i=0; i<objects.size(); i++)
        {
            final SceneObject object = objects.get(i);
            final double t = object.trace(data.p, data.ray, raylen2);

            if(t >= 0 && t < bestT)
            {
                data.bestObject = object;
                bestT = t;
            }
        }
       
        return bestT;
    }

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Varkas
« Reply #42 - Posted 2013-01-14 09:33:29 »

would you mind puttin up your  code?
want to try if i would run well on the GPU(your code)

I'll clean up the code a bit and publish it under the GPL as part of this project:

http://sourceforge.net/p/freemapper/code/26/tree/gardenlife/trunk/

Give me some hours ...

Edit:

The code is there now. It's a bit farther evolved than the one used for the demo in this thread, but it didn't cut into performance too badly. The SimpleRay class has a main method which you can run and which will show the demo scene:

http://sourceforge.net/p/freemapper/code/27/tree/gardenlife/trunk/src/tracer/

I want to use the tracer for this project eventually:

http://www.java-gaming.org/topics/greenspace-a-park-or-landscape-design-toy/27520/msg/258381/view.html#msg258381

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline krasse
« Reply #43 - Posted 2013-01-14 14:26:54 »

Nice!
I get a quite steady 40fps with shadows, which is really impressive Smiley

Offline Varkas
« Reply #44 - Posted 2013-01-14 15:27:53 »

People can now begin to stop saying "Java is slow" Grin

I'm also positively impressed. I didn't expect this when I started, although I knew that the Java runtime got much better during the past years.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Sammyster

Senior Newbie


Medals: 1



« Reply #45 - Posted 2013-01-14 21:11:59 »

That was really cool.

40-45fps with the shadows, not bad  Smiley
Pages: 1 [2]
  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.

BurntPizza (24 views)
2014-09-19 03:14:18

Dwinin (39 views)
2014-09-12 09:08:26

Norakomi (68 views)
2014-09-10 13:57:51

TehJavaDev (93 views)
2014-09-10 06:39:09

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!