我正在使用libgdx编写一个使用OpenGL的JWindow类型UI。不知何故,即使启用了GL20.GL_BLEND
,我的应用程序仍然具有黑色背景。
以下是我的代码:
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语言中问了同样的问题(如何制作带透明背景的OpenGL渲染上下文?)。我甚至试过将LwglAWTCanvas添加到JWindow中,但仍无法使背景透明。
我想要一个清晰的背景,这样我就可以在用户屏幕上绘制矩形/形状。
谢谢!