2014-03-07 243 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