Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Translucency 3d LWJGL/JOGL windows?  (Read 3043 times)
0 Members and 1 Guest are viewing this topic.
Offline whome

Junior Member




Carte Noir Java


« Posted 2010-10-15 10:52:21 »

JDK6 has an unofficial translucency window API and JDK7 supposed to give a public API.
http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html
http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
http://www.curious-creature.org/2007/09/28/translucent-shaped-windows-extreme-gui-makeover/

Demos works fine in WinXP/Win7 machine, applications use Java2D rendering. I have not tried animated j2d windows yet how its working.

But is there a way use LWJGL or JOGL app pixel-perfect translucent windows?

I have seen some DirectX 3d animated demos doing it so technically its possible in Windows.
Offline gouessej
« Reply #1 - Posted 2010-10-15 12:02:57 »

Hi!

JOGL 1.1.1a can work with this but I have never tested this feature with JOGL 2.0 beta.

Offline whome

Junior Member




Carte Noir Java


« Reply #2 - Posted 2010-10-15 19:17:12 »

Thx, do anyone has or know a simple demo JOGL application doing it?
Would love to see LWJGL demo as well if its capable of doing it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 339
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2010-10-15 20:48:49 »

All LWJGL is, is a GL binding. Use AWT to manage your windows.

Cas Smiley

Offline whome

Junior Member




Carte Noir Java


« Reply #4 - Posted 2010-10-16 19:17:29 »

I gave a test LWJGL-2.6 library, could not get it work. Trying to run a simple 3d animation with fully transparent background. Animation objects may have 0-100% translucency pixels.

AWTUtilities.setWindowOpaque(this, false);
If I give true its a normal undecored window and animation works properly. Giving false seems to render nothing and window is like a ghost. Its there but not really shown anything.

[CODE]
package fi.mbnet.akini.lwjgl;

import java.nio.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

import org.lwjgl.opengl.*;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.util.glu.*;

import com.sun.awt.AWTUtilities;

public class AWTTest extends Frame {
   private Thread renderThread;
   private GameCanvas canvas;

   public void initialize() throws LWJGLException {
      setUndecorated(true);
      setBackground(new Color(0,0,0,0));
      setTitle("AWTTest");
      setSize(640, 480);
      setLocation(40,40);

      // true  = opaque undecored window works
      // false = per-pixel transparency window does not work
      AWTUtilities.setWindowOpaque(this, false);

      setLayout(new GridLayout(1,1));

      canvas = new GameCanvas();
      add(canvas);

      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent evt) {
            try {
               renderThread.interrupt();
               renderThread.join(5*1000);
            } catch (Exception ex) { }
            renderThread = null;
            dispose();
         }
      });

      addComponentListener(new ComponentAdapter() {
         public void componentMoved(ComponentEvent evt) {
            canvas.isInitialized = false;
            System.out.println( String.format("moved to %d,%d", getX(), getY()) );
         }
         public void componentResized(ComponentEvent evt) {
            Shape shape = new Rectangle(0, 0, getWidth(), getHeight());
            //Shape shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());

            AWTUtilities.setWindowShape(AWTTest.this, shape);

            canvas.isInitialized = false;
            System.out.println( String.format("resized to %d,%d", getWidth(), getHeight()) );
         }
      });

      // add same listeners to frame and canvas objects
      addKeyListener(new KeyAdapter() {
         public void keyReleased(KeyEvent evt) {
            switch(evt.getKeyCode()) {
            case KeyEvent.VK_ESCAPE:
               Frame frm = AWTTest.this;
               frm.dispatchEvent(new WindowEvent(frm, WindowEvent.WINDOW_CLOSING));
               break;
            }
         }
      });
      for(KeyListener listener : getKeyListeners())
         canvas.addKeyListener(listener);
   
      renderThread = new Thread(new Runnable() {
         public void run() {
            while(true) {
               try {
                  Thread.sleep( (long)1000/60 );
                  canvas.update();
                  canvas.repaint();
               } catch (InterruptedException ex) {
                  break;
               } catch (Exception ex) {
                  ex.printStackTrace();
                  //break;
               }
            }
         }
      });
      renderThread.start();
   }

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

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            AWTTest test = new AWTTest();
            try {
               test.initialize();
               test.setResizable(true);
               test.setVisible(true);
            } catch (Exception ex) {
               ex.printStackTrace();
            }
         }
      });
   }

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

   private class GameCanvas extends AWTGLCanvas {
      private int curHeight = -1;
      private int curWidth = -1;
      private volatile float angle;
      private volatile boolean isInitialized;

      private GameCanvas() throws LWJGLException  {
         super();
      }

      private void initialize() {
         System.out.println("initialize");
         curWidth  = getWidth();
         curHeight = getHeight();
         GL11.glViewport(0, 0, curWidth, curHeight);
         GL11.glLoadIdentity();
         GLU.gluOrtho2D(0.0f, (float)curWidth, 0.0f, (float)curHeight);
         GL11.glMatrixMode(GL11.GL_MODELVIEW);
      }

      public void update() {
         angle += 2.0f;
      }

      public void paintGL() {
         try {
            //if (getWidth() != curWidth || getHeight() != curHeight) {
            if (!isInitialized) {
               initialize();
               isInitialized = true;
            }

            // clear background
            GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

            // rotate box at a center
            GL11.glPushMatrix();
            GL11.glColor3f(1f, 1f, 0f);
            GL11.glTranslatef((float)curWidth / 2.0f, (float)curHeight / 2.0f, 0.0f);
            GL11.glRotatef(angle, 0f, 0f, 1.0f);
            GL11.glRectf(-50.0f, -50.0f, 50.0f, 50.0f);
            GL11.glPopMatrix();

            swapBuffers();
         } catch (Exception ex) {
            throw new RuntimeException(ex);
         }
      }
   }

}
[/CODE]
Offline whome

Junior Member




Carte Noir Java


« Reply #5 - Posted 2010-10-17 14:00:36 »

Some progress, using ellipse window, AWTGLCanvas and JPanel panel components, rotating rectangles. Here is a screenshot.
http://koti.mbnet.fi/akini/lwjgl/transparentwindow1.jpeg


But it is unusable at the moment, runs very slow eating CPU 100%. Lower part jpanel is transparent but lwjgl upper part is always opaque. I guess it was not that simple to use java 3d libraries as was hoping for. Working DirectX 3d demos I've seen might do some heavy tricks, although they run super smooth and low cpu%.

[CODE]
/**
 * http://koti.mbnet.fi/akini/
 * @author Aki Nieminen
 * @version $Id$
 */
package fi.mbnet.akini.lwjgl;

import java.nio.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

import org.lwjgl.opengl.*;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.util.glu.*;

import com.sun.awt.AWTUtilities;

/**
 * Test translucent Java window
 * http://www.java-gaming.org/topics/translucency-3d-lwjgl-jogl-windows/23127/view.html
 */
public class AWTTest extends Frame {
   private Thread renderThread;
   private GameCanvas canvas;
   private FooPanel panel;

   public void initialize() throws LWJGLException {
      setUndecorated(true);
      setBackground(new Color(0,0,0,0));
      setTitle("AWTTest");
      setSize(640, 480);
      setLocation(40,40);

      setLayout(new GridLayout(2,1));

      canvas = new GameCanvas();
      add(canvas);

      panel = new FooPanel();
      add(panel);

      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent evt) {
            try {
               renderThread.interrupt();
               renderThread.join(5*1000);
               // set to opaque or window area may show garbage
               Shape shape = new Rectangle(0, 0, getWidth(), getHeight());
               AWTUtilities.setWindowShape(AWTTest.this, shape);
               AWTUtilities.setWindowOpaque(AWTTest.this, true);
            } catch (Exception ex) { }
            renderThread = null;
            dispose();
         }
      });

      addComponentListener(new ComponentAdapter() {
         public void componentShown(ComponentEvent evt) {
            if (!AWTUtilities.isWindowOpaque(AWTTest.this))
               return;
            AWTUtilities.setWindowOpaque(AWTTest.this, false);
            System.out.println("setWindowOpaque=false");

         }
         public void componentMoved(ComponentEvent evt) {
            canvas.isInitialized = false;
            System.out.println( String.format("moved to %d,%d"
               , getX(), getY()
            ));
         }
         public void componentResized(ComponentEvent evt) {
            //Shape shape = new Rectangle(0, 0, getWidth(), getHeight());
            Shape shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
            AWTUtilities.setWindowShape(AWTTest.this, shape);

            canvas.isInitialized = false;
            System.out.println( String.format("resized to %d,%d"
               , getWidth(), getHeight()
            ));
         }
      });

      // add same listeners to frame and canvas objects
      addKeyListener(new KeyAdapter() {
         public void keyReleased(KeyEvent evt) {
            switch(evt.getKeyCode()) {
            case KeyEvent.VK_ESCAPE:
               Frame frm = AWTTest.this;
               frm.dispatchEvent(new WindowEvent(frm, WindowEvent.WINDOW_CLOSING));
               break;
            }
         }
      });
      for(KeyListener listener : getKeyListeners()) {
         canvas.addKeyListener(listener);
      }
   
      renderThread = new Thread(new Runnable() {
         public void run() {
            while(true) {
               try {
                  Thread.sleep( (long)1000/15 );
                  panel.update();
                  canvas.update();
                  //panel.repaint();
                  //canvas.repaint();
                  AWTTest.this.repaint();
               } catch (InterruptedException ex) {
                  break;
               } catch (Exception ex) {
                  ex.printStackTrace();
                  //break;
               }
            }
         }
      });
      renderThread.start();
   }

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

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            AWTTest test = new AWTTest();
            try {
               test.initialize();
               test.setResizable(true);
               test.setVisible(true);
            } catch (Exception ex) {
               ex.printStackTrace();
            }
         }
      });
   }

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

   private class GameCanvas extends AWTGLCanvas {
      private int curHeight = -1;
      private int curWidth = -1;
      private volatile float angle;
      private volatile boolean isInitialized;

      private GameCanvas() throws LWJGLException  {
         super();
      }

      private void initialize() {
         System.out.println("initialize");
         curWidth  = getWidth();
         curHeight = getHeight();
         GL11.glViewport(0, 0, curWidth, curHeight);
         GL11.glLoadIdentity();
         GLU.gluOrtho2D(0.0f, (float)curWidth, 0.0f, (float)curHeight);
         GL11.glMatrixMode(GL11.GL_MODELVIEW);
      }

      public void update() {
         angle += 2.0f;
      }

      public void paintGL() {
         try {
            //if (getWidth() != curWidth || getHeight() != curHeight) {
            if (!isInitialized) {
               initialize();
               isInitialized = true;
            }

            // clear background
            GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

            // rotate box at a center
            GL11.glPushMatrix();
            GL11.glColor3f(1f, 1f, 0f);
            GL11.glTranslatef((float)curWidth / 2.0f, (float)curHeight / 2.0f, 0.0f);
            GL11.glRotatef(angle, 0f, 0f, 1.0f);
            GL11.glRectf(-50.0f, -50.0f, 50.0f, 50.0f);
            GL11.glPopMatrix();

            swapBuffers();
         } catch (Exception ex) {
            throw new RuntimeException(ex);
         }
      }
   }


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

   private class FooPanel extends JPanel {
      double angle = 0;
      private void update() {
         angle += 2;
         if (angle >= 360) angle = 0;
      }

      protected void paintComponent(Graphics g) {
         final int R = 040;
         final int G = 140;
         final int B = 240;
         Paint p = new GradientPaint(0.0f, 0.0f,
            new Color(R, G, B, 0),
            getWidth(), getHeight(),
            new Color(R, G, B, 255),
            false
         );
         Graphics2D g2d = (Graphics2D)g;
         g2d.setPaint(p);
         g2d.fillRect(0, 0, getWidth(), getHeight());

         g2d.setColor(new Color(150,60,170,127));
         g2d.rotate(Math.toRadians(angle), getWidth()/2, getHeight()/2);
         g2d.translate(getWidth()/2-100, getHeight()/4-40);
         g2d.fillRect(50, 50, 100, 100);
      }
   }

}
[/CODE]
Offline gouessej
« Reply #6 - Posted 2010-10-17 14:17:00 »

Thx, do anyone has or know a simple demo JOGL application doing it?
Our demos are there:
http://github.com/sgothel/jogl-demos/tree/master/src/demos/

If you want to use the source code you already wrote but with JOGL, replace the AWTGLCanvas by our GLCanvas. I remind you that JOGL is known for its quite good interoperability with Java2D/Swing:
http://weblogs.java.net/blog/2006/10/12/easy-2d3d-mixing-swing
This demo uses GLJPanel (Swing OpenGL panel).

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.

ctomni231 (33 views)
2014-07-18 06:55:21

Zero Volt (29 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (26 views)
2014-07-16 23:30:00

Cero (41 views)
2014-07-16 00:42:17

Riven (43 views)
2014-07-14 18:02:53

OpenGLShaders (31 views)
2014-07-14 16:23:47

Riven (30 views)
2014-07-14 11:51:35

quew8 (29 views)
2014-07-13 13:57:52

SHC (65 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!