我正在使用libgdx編寫一個帶OpenGL的JWindow類型UI。出於某種原因,即使在啓用GL20.GL_BLEND
後,我的應用仍然具有黑色背景。libgdx透明覆蓋
這裏是我的代碼:
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class MyGdxGame extends ApplicationAdapter {
Stage stage;
SpriteBatch batch;
ShapeRenderer shapeRenderer;
@Override
public void create() {
stage = new Stage(new FitViewport(2560, 1440));
batch = new SpriteBatch();
shapeRenderer = new ShapeRenderer();
shapeRenderer.setAutoShapeType(true);
}
@Override
public void render() {
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClearColor(0, 0, 0, .25f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.RED);
shapeRenderer.rect(100, 25, 200, 450);
shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.GREEN);
shapeRenderer.rect(303, 24, 10, 451);
shapeRenderer.end();
batch.end();
Gdx.gl.glDisable(GL20.GL_BLEND);
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void dispose() {
stage.dispose();
batch.dispose();
shapeRenderer.dispose();
}
}
我發現有人問同樣的事情的線程,但在C(How to make an OpenGL rendering context with transparent background?)。我甚至嘗試將LwglAWTCanvas添加到JWindow中,但我仍然無法使背景透明。
我想要一個清晰的背景,所以我可以在用戶屏幕上繪製矩形/形狀。
謝謝!
的可能的複製[?如何使透明背景的OpenGL渲染上下文(http://stackoverflow.com/questions/4052940/how-to-make-an-opengl-rendering-context-with-transparent-background) – pleluron
不是真的重複,因爲這不是一種本地語言。 –
這就是我想指出的。在java中沒有答案,請參閱[this](http://stackoverflow.com/questions/10276020/lwjgl-window-with-transparent-background)。 – pleluron