我試圖用JOGL淡入/淡出圖像。我可以渲染圖像,但它總是呈現完整的強度。下面是代碼:OpenGL淡出/不工作
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;
}
}
}
也許你應該顯示'renderImage'的代碼和更新'alpha'值的代碼?而什麼意思*「它不會淡入」*?圖像是否總是呈現完整的強度? – 2012-07-25 15:34:58
@ChristianRau道歉,請參閱編輯 – mikeythemissile 2012-07-25 15:41:26
'alpha'在哪裏定義和使用? – 2012-07-25 15:42:29