JOGL透明背景

时间:2014-03-07 09:34:16

标签: java background transparency jogl

我需要让我的GLCanvas具有透明背景。我想要收到的效果是,JFrame背景中不会出现黑色背景而是灰色。我用 capabilities.setBackgroundOpaque(假); 和 gl.glClearColor(0.0f,0.0f,0.0f,0.0f); 但它不起作用。我知道有同样的问题(Jogl: How to make a transparent background on com.jogamp.opengl.swt.GLCanvas?),但我不明白HUD是如何工作的。在旁边,我只是简单地理解为什么metod capabilities.setBackgroundOpaque(false);不起作用。

这是我的代码

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();

}

}

2 个答案:

答案 0 :(得分:0)

而是使用GLJPanel,将其放入JInternalFrame,在两者上调用setOpaque(false)。我是在基于JOGL的Eclipse RCP应用程序中完成的,以便创建一个透明的“窗口”,我可以将其移到GUI的其余部分之上。

P.S:JOGL本身已有2个WaveFront OBJ加载器,而我的(最强大的)加载器位于JogAmp's Ardor3D Continuation

答案 1 :(得分:0)

我是jogl的新手,当我使用jogl时遇到了类似的问题,但我终于找到了一个解决方案。 使用以下代码创建具有透明背景的GLJPanel对象。

//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);