我正在尝试使用 JOGL 淡入/淡出图像。我可以很好地渲染图像,但它总是以全强度渲染。下面是代码:
private void render(GLAutoDrawable glad) {
GL2 gl = glad.getGL().getGL2();
// Prepares the rendering, things such as disabling depth, clearing the buffer bit etc
setUpGL(gl);
BufferedImage background = main.getCurrentState().getImage();
// Render the background, adapted from here: http://bit.ly/QjeusP
renderImage(gl, background, background.getWidth(), background.getHeight());
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glColor4f(0, 0, 0, alpha);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex2f(0, background.getHeight());
gl.glVertex2f(background.getWidth(), background.getHeight());
gl.glVertex2f(background.getWidth(), 0);
gl.glVertex2f(0, 0);
gl.glEnd();
gl.glDisable(GL2.GL_BLEND);
}
变量 alpha 是一个浮点数,每次渲染递增 0.1,如果超过 1.0,则递减 0.1,直到小于 0,然后递增,依此类推。
编辑 1:这是更新 alpha 值的代码:
private void update() {
if (isTransparent) {
alpha += 0.1f;
if (alpha >= 1.0f) {
alpha = 1.0f;
isTransparent = false;
}
}
else {
alpha -= 0.1f;
if (alpha <= 0.0f) {
alpha = 0.0f;
isTransparent = true;
}
}
}