Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  Realtime Plasma using per pixel blits  (Read 3139 times)
0 Members and 1 Guest are viewing this topic.
Offline relminator
« Posted 2013-08-17 09:38:02 »

Thanks BurnPizza and Riven!

Updated with a polar plasma.




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  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
314  
315  
316  
317  
318  
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339  
340  
341  
342  
343  
344  
345  
//****************************************************************************
//
//   Simple Per Pixel Plasma Generator
//   Richard Eric Lope (relminator)
//   http://rel.phatcode.net
//
//   Thanks to BurntPizza and Riven for teaching me how to
//   do a fast pixel write directly to a buffered image
//
//****************************************************************************


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;
import javax.swing.JPanel;


//****************************************************************************
//
//   Main Entry Point
//
//****************************************************************************

public class SimplePlasma extends JFrame
{

   private static final long serialVersionUID = 1L;

   final static int SCREEN_WIDTH = 640;
   final static int SCREEN_HEIGHT = 480;
   
   public SimplePlasma()
    {

        add( new Screen() );

        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
        setLocationRelativeTo( null );
        setTitle( "Plazma by Relminator" );
        setResizable( false );
        setVisible( true );
       
    }

    public static void main(String[] args)
    {
        new SimplePlasma();
    }
}


//****************************************************************************
//
//  
//
//****************************************************************************

class Screen extends JPanel implements Runnable  
{

   private static final long serialVersionUID = 1L;
   
   private Thread animator;
   
    private static final double FIXED_TIME_STEP = 1/60.0;
    private double accumulator = 0;
   
    private int fps = 0;
    private int framesPerSecond = 0;
    private double previousTime = 0;
    private double oldTime = 0;
   
    Plasma plasma = new Plasma();
   
    public Screen()
    {

        addKeyListener(new TAdapter());    
        setFocusable(true);
        setBackground(Color.BLACK);
        setDoubleBuffered(true);        
       
    }

    public void addNotify()            
    {
        super.addNotify();
        animator = new Thread( this );
        animator.start();
    }
   
    public void paint( Graphics g )    
    {
        super.paint(g);
       
        Graphics2D g2D = (Graphics2D)g;

        render( g2D );
       
        Toolkit.getDefaultToolkit().sync();      
        g.dispose();                    
       
    }

    private class TAdapter extends KeyAdapter    
    {

        public void keyPressed( KeyEvent e )
        {
            int key = e.getKeyCode();        
            if( key == KeyEvent.VK_ESCAPE )
            {
               System.exit(0);
            }
               
        }
       
    }
   
   
    public void update()       // update ng gameloop here
   {
       
       plasma.update();
        repaint();

    }

    public void render( Graphics2D g2D )      // drawing dito
   {
       
       
       
      g2D.drawImage( plasma.getImage(),
                   0,
                   0,
                   null );

        g2D.setColor( Color.BLUE );
        g2D.drawString( "FPS :" + fps, 0, 10 );
       
               
    }

   public void run()      // runnable interface needs run() implemented
  {
     
      double dt = getDeltaTime( getSystemTime() );
     
      // gameloop
       while( true )
        {

           dt = getDeltaTime( getSystemTime() );
         if( dt > FIXED_TIME_STEP ) dt = FIXED_TIME_STEP;
         accumulator += dt;
         
         while( accumulator >= FIXED_TIME_STEP )
         {
            update();
            accumulator -= FIXED_TIME_STEP;
           
         }
         
         try
            {
                Thread.sleep(15);
            }
            catch (InterruptedException e)
            {
                System.out.println("interrupted");
            }

        }
       
   }
   
   
   public double getSystemTime()
   {
      return System.nanoTime() / 1000000000.0;
   }

   public double getDeltaTime( double timerInSeconds )
   {
     
      double currentTime = timerInSeconds;
      double elapsedTime = currentTime - oldTime;
      oldTime = currentTime;
     
      framesPerSecond++;
     
      if( (currentTime - previousTime) > 1.0 )
      {
         previousTime = currentTime;
         fps = framesPerSecond;
         framesPerSecond = 0;
      }
     
      return elapsedTime;
   }
   
   
}




//****************************************************************************
//
//  Screen and logic constants
//
//****************************************************************************

class Constants
{

   public static final double FIXED_TIME_STEP = 1.0 / 60.0;

   public final static int SCREEN_WIDTH = 640;
   public final static int SCREEN_HEIGHT = 480;

}


//****************************************************************************
//
//
//
//****************************************************************************

class Plasma
{
   private static final double K1 = 35;
   private static final double K2 = 18;
   private static final double K3 = 50;
   
   private int kSin1 = (int)(K1 *(360/57.3));
   private int kSin2 = (int)(K2 *(360/57.3));
   private int kSin3 = (int)(K3 *(360/57.3));
   
   private int[] sinLut1 = new int[2048 * 4];
   private int[] sinLut2 = new int[2048 * 4];
   private int[] sinLut3 = new int[2048 * 4];
   
   private int a, b, c;
   
   BufferedImage image = new BufferedImage( Constants.SCREEN_WIDTH,
                                        Constants.SCREEN_HEIGHT,
                                        BufferedImage.TYPE_INT_ARGB);

   int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();  

   BufferedImage texture = new BufferedImage( 256,
                                      256,
                                      BufferedImage.TYPE_INT_ARGB);
   int[] texPixels = ((DataBufferInt) texture.getRaster().getDataBuffer()).getData();  

   public Plasma()
   {
      int size = sinLut1.length;
      for( int i = 0; i < size; i++ )
      {
         sinLut1[i] = (int)(Math.sin(i/K1) * 127 + Math.cos(i/K1) * 67);
         sinLut2[i] = (int)(Math.sin(i/K2) * 164 + Math.cos(i/K2) * 26);
         sinLut3[i] = (int)(Math.sin(i/K3) * 32 + Math.cos(i/K3) * 280);
         
      }
     
     
      double j = 255 / 360.0f * 3;
      double k = 255 / 360.0f * 2;
      double l = 255 / 360.0f * 1;
      double pa = 0;
      double pb = 0;
      double pc = 0;
     
     
      for( int y = 0; y < 256; y++ )
      {
         
         int r = (int)( 255 * Math.abs(Math.sin(pa * Math.PI / 180.0)) );
         int g = (int)( 255 * Math.abs(Math.sin(pb * Math.PI / 180.0)) );
         int b = (int)( 255 * Math.abs(Math.sin(pc * Math.PI / 180.0)) );
         c = (255 << 24) | (r << 16) | (g << 8) | (b);
         
         for( int x = 0; x < 256; x++ )
         {
            texPixels[y * 256 + x] = c;
         }
         pa += j;
         pb += k;
         pc += l;
         
      }
           
   }
   
   public void update()
   {
   
      a = (++a) % (kSin1 + 1);
      b = (++b) % (kSin2 + 1);
      c = (++c) % (kSin3 + 1);
     
      int offset = 0;
       for( int y = 0; y < Constants.SCREEN_HEIGHT; y++ )
      {
         for( int x = 0; x < Constants.SCREEN_WIDTH; x++ )
         {
            int tx = ( sinLut1[x + a + 2048] +
                     sinLut2[y + b + 2048] +
                     sinLut3[x + y + c + 2048] );
              int ty = ( sinLut3[x + y + c + 2048] +
                       sinLut1[(tx) & 2047] +
                       sinLut1[x + a + 2048] +
                       sinLut1[y + a + 2048] +
                       sinLut2[x + b + 2048]);
            tx = tx & 255;
            ty = ty & 255;
              int c = texPixels[ ty * 256 + tx ];
            pixels[offset++] = c;
         }
         
      }
       
   }
i
   public BufferedImage getImage()
   {
      return image;
   }

   
   
}


Jar and code:
www.rel.phatcode.net/junk.php?id=146

Enjoy!<
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-08-17 10:43:07 »

Wow that hurts my eyes, so good job! Looks pretty realistic to me.

Online BurntPizza
« Reply #2 - Posted 2013-08-17 15:16:03 »

Hmm, that is quite neat looking... I bet I could be modified into a nice water "shader," it looks pretty ripply.
I may investigate this later.

Good job!  Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #3 - Posted 2013-08-17 16:38:36 »

I bet I could be modified into a nice water "shader," it looks pretty ripply.

If you're looking for a water ripple effect using direct pixel manipulation you might also want to check out -

http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
http://www.neilwallis.com/projects/java/water/index.php

There's loads of variations of this algorithm around.  I used it in this - http://youtu.be/YNe03b5BxLM

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online BurntPizza
« Reply #4 - Posted 2013-08-17 16:46:00 »

I bet I could be modified into a nice water "shader," it looks pretty ripply.

If you're looking for a water ripple effect using direct pixel manipulation you might also want to check out -

http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
http://www.neilwallis.com/projects/java/water/index.php

There's loads of variations of this algorithm around.  I used it in this - http://youtu.be/YNe03b5BxLM

Ah, good stuff, I wasn't really looking for a water shader, but I'll definitely save these for later  Cheesy Thanks!
Offline relminator
« Reply #5 - Posted 2013-08-18 05:43:10 »

Thanks guys.  It's a little too slow for my tastes though.  appx 40 fps on my netbook. Sad.

Online BurntPizza
« Reply #6 - Posted 2013-08-18 05:45:40 »

Thanks guys.  It's a little too slow for my tastes though.  appx 40 fps on my netbook. Sad.



Netbook? What's the specs? 40 FPS doesn't sound too shabby for this kind of thing on a netbook. (If you are rendering at the resolution of that picture)

EDIT: Tested my benchmark from the other thread on the crappy laptop I'm on right now, got 42 FPS when rendering 512x512, every pixel. Normally it's about 130 FPS on my desktop, so for a system of any quality, I think you can expect a decent framerate.
You could also try the byte[] method in my benchmark code, similar to the int[], just interleaved rgb channels, it's a little bit faster.
Offline nsigma
« Reply #7 - Posted 2013-08-18 10:17:32 »

I'd take the FPS value with a big pinch of salt!  Your game loop looks completely screwy.  I get 50 fps as is, but if I change the sleep to Thread.sleep(1) I get 600 fps, though it visibly looks the same.

Changing your loop to just

1  
2  
dt = getDeltaTime(getSystemTime());
update();

which is a quick guess at maxing it out (may be wrong) gives ~300 fps and is now visibly faster.

The problem is also that you're using passive rendering, so although you might be able to measure how fast this can update the plasma, you're not measuring actual rendering time.  It's also theoretically not thread safe as you're updating the image in one thread and painting it in another (EDT).

You want to look into an active rendering loop using BufferStrategy and disable the standard repaint() method - something like ra4king's code here - http://www.java-gaming.org/topics/about-on-my-main-loop/26222/msg/229028/view.html#msg229028

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline relminator
« Reply #8 - Posted 2013-08-18 10:57:42 »

I'd take the FPS value with a big pinch of salt!  Your game loop looks completely screwy.  I get 50 fps as is, but if I change the sleep to Thread.sleep(1) I get 600 fps, though it visibly looks the same.

That's the idea(bolded). The simulation should run at 60 logical FPS even if the actual FPS could be 60 to 1000+.

I also like to slowdown instead of skipping frames when the actual FPS goes below 60.  AFAIK, there's also just a single thread that updates and draws. Care to explain where am I sharing data between threads?

BurntPizza:  An intel atom (acer aspire one) which is actually my daughter's. My laptop runs this at about 70 FPS.  I'll try to read up on the byte[] thingy(ARGB is the order right?).  Thanks!
Offline nsigma
« Reply #9 - Posted 2013-08-18 11:05:10 »

That's the idea(bolded). The simulation should run at 60 logical FPS even if the actual FPS could be 60 to 1000+.

I understood that was the idea.  However, you're not measuring anything useful otherwise (see next point).

AFAIK, there's also just a single thread that updates and draws. Care to explain where am I sharing data between threads?

repaint() does not call paint().  It queues up a repaint request on the Swing event dispatch thread - it's asynchronous and returns immediately.  Therefore you're updating the image in update() in the thread you create, and accessing it from the EDT in paint().

Also, as repaint() returns immediately, you're not actually measuring FPS because you're at no point measuring the actual cost of rendering to the screen.

You need to look at that active rendering loop I linked to.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline relminator
« Reply #10 - Posted 2013-08-18 12:45:34 »

Okay, I do have a bufferstrategy gameloop.  Thanks for the heads up.
Online BurntPizza
« Reply #11 - Posted 2013-08-18 16:40:42 »

I'll try to read up on the byte[] thingy(ARGB is the order right?).  Thanks!

The order is actually ABGR, or just BGR, weird, I know.
Offline relminator
« Reply #12 - Posted 2013-08-20 14:08:10 »

Updated with a newer plasma.   Code is in the zip.
Offline GNecro1
« Reply #13 - Posted 2013-08-31 17:31:34 »

do
texPixels[y * 256 + x] = c << 16;

much better result Cheesy

Java freak! Cheesy
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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
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!