Implement AWT in JOGL

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Implement AWT in JOGL

choracy69
But I have  question, because I have some code in AWT and I want implement in JOGL. Code in AWT in INIT():

mwfuel = new Color(190,204,91).getRGB();
    tranfuel = new Color(168,192,20).getRGB();
    conifuel = new Color(85,101,39).getRGB();
    decifuel = new Color(110,80,42).getRGB();
    burnt = new Color(48,48,48).getRGB();
    bfire = new Color(192,64,0).getRGB();
    fire = new Color(255,96,0).getRGB();

    grid = new int[d][h];
    futureGrid = new int[d][h];

    for (int i=0; i<d;i++){        // sets the whole terrain to 0 indicating that this pixel did not catch fire
      for (int j=0; j<h;j++){
        grid[i][j] = 0;
        terrain = rand.nextInt(5);
        switch(terrain) {
        case 1:  
          bi.setRGB(i,j,mwfuel);
          break;
        case 2:
          bi.setRGB(i,j,tranfuel);
          break;
        case 3:
          bi.setRGB(i,j,conifuel);
          break;
        case 4:
          bi.setRGB(i,j,decifuel);
          break;
        }
      }
    }

How can I use something like that in JOGL:

new Color(168,192,20).getRGB();
And something like:

bi.setRGB(i,j,conifuel);
Where bi is: BufferedImage bi;



Thanks for any reponse.
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

elect
Hi choracy, which graphic card do you have?
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

choracy69
Hi elect,

I have Intel(R) HD Graphics 4000.

Maybe JOGL has something like draw point?

Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

elect
choracy69 wrote
Hi elect,

I have Intel(R) HD Graphics 4000.

Maybe JOGL has something like draw point?
Sure it has.

Which operating system do you use?

What do you want to achieve exactly?
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

choracy69
Windows 7.

Hm, I visualize Forest Fire, I know that I can change the system of visulization for my app, but I prefer maybe change AWT function on some function from JOGl.
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

elect
Ok, but what it is meant to be exactly bi? How are you going to use it?

Anyway, supposing you need to load as a texture, you could create a byteBuffer as big as d*h and then in you switch you just set the given pixel, supposing h is the width and d the height, you set byteBuffer.put(i*d+j, color)
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

choracy69
Ok, I will try with byteBUffer.

This is my fragment of code:

public class Fire extends Applet
    implements MouseListener, MouseMotionListener, KeyListener, Runnable
{
    int d = 900;
    int h = 600;
   
    Random rand = new Random();
    BufferedImage bi;
    Graphics2D big;
    Thread artist = null;
   
    int [][] grid, futureGrid;  // asigns 2 arrays
    int a = 0;
    int b = 0;
    int tempx = 0;
    int tempy = 0;
    int mwfuel, tranfuel, conifuel, decifuel, burnt, bfire, fire;    //color of different fuel types
    int terrain = 0;
    int E =  rand.nextInt(100);
    int windd = 0;
    int wspeed = 0;
    int xt = 0;
    String direc;
    int spread = 0;
    int spoty = 0;
    int spotx = 0;
   
   
   
    public void init()
    {
        setSize(d, h);
        setBackground( new Color(48,48,48) );
        addMouseMotionListener(this);
        addMouseListener(this);
        addKeyListener(this);
        setFocusable(true);
        bi = (BufferedImage) createImage(d, h);
        big = bi.createGraphics();
       
        wspeed = rand.nextInt(25);
        mwfuel = new Color(190,204,91).getRGB();
        tranfuel = new Color(168,192,20).getRGB();;
        conifuel = new Color(85,101,39).getRGB();;
        decifuel = new Color(110,80,42).getRGB();;
        burnt = new Color(48,48,48).getRGB();;
        bfire = new Color(192,64,0).getRGB();;
        fire = new Color(255,96,0).getRGB();;
       
        grid = new int[d][h];
        futureGrid = new int[d][h];
       
        for (int i=0; i<d;i++){        // sets the whole terrain to 0 indicating that this pixel did not catch fire
          for (int j=0; j<h;j++){
            grid[i][j] = 0;
            terrain = rand.nextInt(5);
            switch(terrain) {
            case 1:  
              bi.setRGB(i,j,mwfuel);
              break;
            case 2:
              bi.setRGB(i,j,tranfuel);
              break;
            case 3:
              bi.setRGB(i,j,conifuel);
              break;
            case 4:
              bi.setRGB(i,j,decifuel);
              break;
            }
          }
        }

        windd = rand.nextInt(5);
        switch (windd) {
        case 1:
          direc = "North";
          break;
        case 2:
          direc = "South West";
          break;
        case 3:
          direc = "West";
          break;
        case 4:
          direc = "North West";
          break;
        }
    System.out.println("Init sie wykonał");
    }

...
}

And fragment of paint()

public void paint(Graphics g)
    {
    Graphics2D g2 = (Graphics2D) g;  

    //System.out.println("PAINT");
    if (xt<wspeed) {
    xt++;
    }
    if (xt>wspeed) {
      xt--;
    }
    if (xt==wspeed) {
      wspeed = rand.nextInt(25);
    }
    if ((xt>10)&&(xt<15)) {
      spread = 1;
    }
    if ((xt>=15)&&(xt<20)) {
      spread = 2;
    }
    if (xt>=20) {
      spread = 3;
    }    

    for (int x=1;x<d-1; x++){
        for (int y=1; y<h-1;y++){
       
         
           
        int nb = neighbors(x,y);
        if ((grid[x][y] == 0)&&(nb == 0)){           //current is green and neighbours are green
          futureGrid[x][y] = 0;    
        }
        else if ((grid[x][y] == 0) && (nb != 0) && (bi.getRGB(x,y) == mwfuel) && (E<59)) {
                    bi.setRGB(x,y,fire);
            futureGrid[x][y] = 1;
            if(windd == 1) {
              if((y-spread>0)&&(y-spread<h)){
                for(int w=0;w<=spread;w++) {
                  bi.setRGB(x,y-w,fire);
                  futureGrid[x][y-w] = 1;
                }
                int tempy = rand.nextInt(y);
                if (spread == 3) {
                  if (tempy<5) {
                    tempy = rand.nextInt(y);
                    bi.setRGB(x,tempy,fire);
                    grid[x][tempy]=1;
                  }
                }
              }
            }

            if(windd ==2) {
              if((x-spread>0)&&(x-spread<d-1)) {
                if((y+spread>0)&&(y+spread<h)){
                  for(int w=0;w<=spread;w++) {
                    bi.setRGB(x-w,y+w,fire);
                    futureGrid[x-w][y+w] = 1;
                  }
                }
              }
            }
...
    int[][] temp = grid;
    grid = futureGrid;
    futureGrid = temp;
    g2.drawImage(bi, null, 0, 0);
    }
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

Xerxes Rånby
GLG2D is an implementation of Graphics2D that uses JogAmp JOGL
https://github.com/brandonborkholder/glg2d
http://brandonborkholder.github.io/glg2d/
Since your example uses Graphics2D then this is a good way to accelerate
your application!

" In order to render your scene using OpenGL instead of Java2D, just wrap your JComponent inside a GLG2DCanvas.

JComponent renderPane = ...

JFrame frame = new JFrame("My Game");
frame.setContentPane(new GLG2DCanvas(renderPane));
...

And that's it! "

2016-02-17 14:41 GMT+01:00 choracy69 [via jogamp] <
ml-node+s762907n4036285h98@n3.nabble.com>:

> Ok, I will try with byteBUffer.
>
> This is my fragment of code:
>
> public class Fire extends Applet
>     implements MouseListener, MouseMotionListener, KeyListener, Runnable
> {
>     int d = 900;
>     int h = 600;
>
>     Random rand = new Random();
>     BufferedImage bi;
>     Graphics2D big;
>     Thread artist = null;
>
>     int [][] grid, futureGrid;  // asigns 2 arrays
>     int a = 0;
>     int b = 0;
>     int tempx = 0;
>     int tempy = 0;
>     int mwfuel, tranfuel, conifuel, decifuel, burnt, bfire, fire;
>  //color of different fuel types
>     int terrain = 0;
>     int E =  rand.nextInt(100);
>     int windd = 0;
>     int wspeed = 0;
>     int xt = 0;
>     String direc;
>     int spread = 0;
>     int spoty = 0;
>     int spotx = 0;
>
>
>
>     public void init()
>     {
>         setSize(d, h);
>         setBackground( new Color(48,48,48) );
>         addMouseMotionListener(this);
>         addMouseListener(this);
>         addKeyListener(this);
>         setFocusable(true);
>         bi = (BufferedImage) createImage(d, h);
>         big = bi.createGraphics();
>
>         wspeed = rand.nextInt(25);
>         mwfuel = new Color(190,204,91).getRGB();
>         tranfuel = new Color(168,192,20).getRGB();;
>         conifuel = new Color(85,101,39).getRGB();;
>         decifuel = new Color(110,80,42).getRGB();;
>         burnt = new Color(48,48,48).getRGB();;
>         bfire = new Color(192,64,0).getRGB();;
>         fire = new Color(255,96,0).getRGB();;
>
>         grid = new int[d][h];
>         futureGrid = new int[d][h];
>
>         for (int i=0; i<d;i++){        // sets the whole terrain to 0
> indicating that this pixel did not catch fire
>           for (int j=0; j<h;j++){
>             grid[i][j] = 0;
>             terrain = rand.nextInt(5);
>             switch(terrain) {
>             case 1:
>               bi.setRGB(i,j,mwfuel);
>               break;
>             case 2:
>               bi.setRGB(i,j,tranfuel);
>               break;
>             case 3:
>               bi.setRGB(i,j,conifuel);
>               break;
>             case 4:
>               bi.setRGB(i,j,decifuel);
>               break;
>             }
>           }
>         }
>
>         windd = rand.nextInt(5);
>         switch (windd) {
>         case 1:
>           direc = "North";
>           break;
>         case 2:
>           direc = "South West";
>           break;
>         case 3:
>           direc = "West";
>           break;
>         case 4:
>           direc = "North West";
>           break;
>         }
>     System.out.println("Init sie wykonał");
>     }
>
> ...
> }
>
> And fragment of paint()
>
> public void paint(Graphics g)
>     {
>     Graphics2D g2 = (Graphics2D) g;
>
>     //System.out.println("PAINT");
>     if (xt<wspeed) {
>     xt++;
>     }
>     if (xt>wspeed) {
>       xt--;
>     }
>     if (xt==wspeed) {
>       wspeed = rand.nextInt(25);
>     }
>     if ((xt>10)&&(xt<15)) {
>       spread = 1;
>     }
>     if ((xt>=15)&&(xt<20)) {
>       spread = 2;
>     }
>     if (xt>=20) {
>       spread = 3;
>     }
>
>     for (int x=1;x<d-1; x++){
>         for (int y=1; y<h-1;y++){
>
>
>
>         int nb = neighbors(x,y);
>         if ((grid[x][y] == 0)&&(nb == 0)){           //current is green
> and neighbours are green
>           futureGrid[x][y] = 0;
>         }
>         else if ((grid[x][y] == 0) && (nb != 0) && (bi.getRGB(x,y) ==
> mwfuel) && (E<59)) {
>                     bi.setRGB(x,y,fire);
>             futureGrid[x][y] = 1;
>             if(windd == 1) {
>               if((y-spread>0)&&(y-spread<h)){
>                 for(int w=0;w<=spread;w++) {
>                   bi.setRGB(x,y-w,fire);
>                   futureGrid[x][y-w] = 1;
>                 }
>                 int tempy = rand.nextInt(y);
>                 if (spread == 3) {
>                   if (tempy<5) {
>                     tempy = rand.nextInt(y);
>                     bi.setRGB(x,tempy,fire);
>                     grid[x][tempy]=1;
>                   }
>                 }
>               }
>             }
>
>             if(windd ==2) {
>               if((x-spread>0)&&(x-spread<d-1)) {
>                 if((y+spread>0)&&(y+spread<h)){
>                   for(int w=0;w<=spread;w++) {
>                     bi.setRGB(x-w,y+w,fire);
>                     futureGrid[x-w][y+w] = 1;
>                   }
>                 }
>               }
>             }
> ...
>     int[][] temp = grid;
>     grid = futureGrid;
>     futureGrid = temp;
>     g2.drawImage(bi, null, 0, 0);
>     }
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://forum.jogamp.org/Implement-AWT-in-JOGL-tp4036268p4036285.html
> To start a new topic under jogl, email
> ml-node+s762907n782158h61@n3.nabble.com
> To unsubscribe from jogamp, click here
> <http://forum.jogamp.org/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=762907&code=eGVyeGVzQGd1ZGlubmEuY29tfDc2MjkwN3wtNTE5NjUwMzEw>
> .
> NAML
> <http://forum.jogamp.org/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

elect
In reply to this post by choracy69
Kind of hard for me to get it from a code snippet, could you try to describe by words to give me an idea what you want/need to do? Also a screenshot if possible
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

gouessej
Administrator
In reply to this post by choracy69
Hi

I had to port something a bit similar from AWT to JOGL. There are several ways to do it. You can simply create an RGB texture data object and modify its data at runtime or you can create a single VBO containing your vertices and your colors. You'll be able to export it as an image and to create a texture data object from an image by using AWTTextureIO and TextureIO.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

choracy69
Hi gouessej,

Thanks for reposnse.
Maybe, Do you have any example with using texture RGB to draw some basic primitives in JOGL?
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

gouessej
Administrator
Actually, I did that with JogAmp's Ardor3D Continuation, it uses JOGL under the hood. I can explain to you step by step how to do it in pure JOGL if you want.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

choracy69
If you found a moment I would be very grateful for an explanation of a simple example.
Reply | Threaded
Open this post in threaded view
|

Re: Implement AWT in JOGL

gouessej
Administrator
Hi

At first, create a direct NIO buffer (3 bytes per pixel : red, green and blue):
ByteBuffer buffer = Buffers.newDirectByteBuffer(drawable.getSurfaceWidth() * drawable.getSurfaceHeight() * 3);

Then, create the TextureData object:
TextureData texData = new TextureData(drawable.getGLProfile(), GL.GL_RGB, drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), 0, GLPixelBuffer.GLPixelAttributes.convert(drawable.getGL(), 3, false), false, false, false, buffer, null);

After that, use those lines of code to set a RGB value:
https://github.com/gouessej/Ardor3D/blob/9b8b30718a8b82221d1a75e4dce23dea8a19836c/ardor3d-core/src/main/java/com/ardor3d/image/util/ImageUtils.java#L346

Create the texture from the texture data:
http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/opengl/util/texture/TextureIO.html#newTexture(com.jogamp.opengl.util.texture.TextureData)

Call texture.bind() so that OpenGL knows which texture is in use and use glTexCoord to pass the texture coordinates with the vertex coordinates, look at the glVertex calls and add your glTexCoord calls:
https://gist.github.com/gouessej/3420e2b6f632efdddf98

Finally, update the texture at runtime, use glTexSubImage2D:
https://github.com/gouessej/Ardor3D/blob/9b8b30718a8b82221d1a75e4dce23dea8a19836c/ardor3d-jogl/src/main/java/com/ardor3d/renderer/jogl/JoglRenderer.java#L628

N.B: minimize the surface of update, don't always update the whole texture. If the performance isn't satisfying, you'll need something much more complicated...

Sorry, it's not trivial but it gives you everything required to port your code from AWT to JOGL.
Julien Gouesse | Personal blog | Website