Question

I need to make my GLCanvas to have a transparent background. The effect that I want to receive is that there will be not black background but grey from JFrame background. I use capabilities.setBackgroundOpaque(false); and gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); but it does not working. I know that there was the same question (Jogl: How to make a transparent background on com.jogamp.opengl.swt.GLCanvas?) but I do not understand how That HUD works. Beside, I just simply dont understand why the metod capabilities.setBackgroundOpaque(false); does not work.

here is my code

public class JOGL implements GLEventListener {

private static final int FPS = 30;

@Override
public void display(GLAutoDrawable gLDrawable) {

    GL2 gl = gLDrawable.getGL().getGL2();

    // Clear the drawing area
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    // Reset the current matrix to the "identity"
    gl.glLoadIdentity();

    // Move the "drawing cursor" around
    gl.glTranslatef(-1.5f, 0.0f, -6.0f);

    // Drawing Using Triangles
    gl.glBegin(GL.GL_TRIANGLES);
    gl.glColor3f(1.0f, 0.0f, 0.0f);    // Set the current drawing color to red
    gl.glVertex3f(0.0f, 1.0f, 0.0f);   // Top
    gl.glColor3f(0.0f, 1.0f, 0.0f);    // Set the current drawing color to green
    gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
    gl.glColor3f(0.0f, 0.0f, 1.0f);    // Set the current drawing color to blue
    gl.glVertex3f(1.0f, -1.0f, 0.0f);  // Bottom Right
    // Finished Drawing The Triangle
    gl.glEnd();

    gl.glFlush();

}

@Override
public void init(GLAutoDrawable glDrawable) {
    GL2 gl = glDrawable.getGL().getGL2();
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

@Override
public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {

}

@Override
public void dispose(GLAutoDrawable gLDrawable) {
}

public static void main(String[] args) {

    GLCapabilities capabilities = new GLCapabilities(null);
    capabilities.setBackgroundOpaque(false);
    final GLCanvas canvas = new GLCanvas(capabilities);
    final JFrame frame = new JFrame("JOGL - test obj loading");
    frame.setLayout(null);
    final FPSAnimator animator = new FPSAnimator(canvas, FPS);
    canvas.addGLEventListener(new JOGL());

    JPanel p = new JPanel();

    frame.add(canvas);
    frame.add(p);
    p.setBackground(Color.red);
    canvas.setBounds(0, 0, 1024, 768);
    p.setBounds(0, 0, 100, 500);
    frame.setSize(1040, 900);
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            animator.stop();
            frame.dispose();
            System.exit(0);
        }
    });
    frame.setVisible(true);
    animator.start();
    canvas.requestFocus();

}

}

Was it helpful?

Solution

Rather use a GLJPanel, put it into a JInternalFrame, call setOpaque(false) on both. I did it in an Eclipse RCP application based on JOGL in order to create a transparent "window" that I could move above the rest of the GUI.

P.S: There are already 2 WaveFront OBJ loaders in JOGL itself and mine (the most robust) is in JogAmp's Ardor3D Continuation.

OTHER TIPS

I'm new to jogl and I have the similar problem when I'm using jogl, but I finally find one solution. Use the following codes to create a GLJPanel object with transparent background.

//Indeed, use GLJPanel instead of GLCanvas
GLProfile glp = GLProfile.getDefault();
GLCapabilities glcap = new GLCapabilities(glp);
glcap.setAlphaBits(8);
GLJPanel pane = new GLJpanel(glcap);
pane.setOpaque(false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top