我需要讓我的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();
}
}
此帖被自動標記爲低質量,因爲它是如此短/唯一的代碼。你介意加入一些文字來解釋它是如何解決問題的嗎? – gung
其實,幾乎沒有新東西,你只是按照我的建議:http://stackoverflow.com/a/22280648/458157 – gouessej