2014-03-07 210 views
2

我需要让我的GLCanvas具有透明背景。我想要收到的效果是,不会出现黑色背景,而是来自JFrame背景的灰色。我使用 capabilities.setBackgroundOpaque(false); gl.glClearColor(0.0f,0.0f,0.0f,0.0f);以及glglClearColor(0.0f,0.0f,0.0f,0.0f);以及glglClearColor(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);不起作用。JOGL透明背景

这里是我的代码

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

} 

}

回答

0

而是使用GLJPanel,把它做成的JInternalFrame,在两个调用setOpaque(假)。我在一个基于JOGL的Eclipse RCP应用程序中做了这个工作,以创建一个透明的“窗口”,我可以移动到GUI的其余部分之上。

P.S:在JOGL本身已经有2个WaveFront OBJ装载机,我的(最强大的)在JogAmp's Ardor3D Continuation

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); 
+0

此帖被自动标记为低质量,因为它是如此短/唯一的代码。你介意加入一些文字来解释它是如何解决问题的吗? – gung

+0

其实,几乎没有新东西,你只是按照我的建议:http://stackoverflow.com/a/22280648/458157 – gouessej