libGDX奇怪的渲染错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了libGDX奇怪的渲染错误相关的知识,希望对你有一定的参考价值。

我最近开始了我的第一个libGDX游戏,它一切顺利,一切都很好但是大约一分钟后没有任何渲染,渲染调用仍然进行,spritebatch工作正常,我只剩下黑屏,我甚至改变了'glClearColor()',但我还是留下了黑屏。我不知道这可能是什么。

我的主要课程:

    @Override
    public void create() {
        Gdx.graphics.setDisplayMode(Settings.screenWidth, Settings.screenHeight, Settings.fullscreen);
        Gdx.graphics.setVSync(Settings.VSync);
        Gdx.graphics.setTitle("Trench Warfare");

        batch = new SpriteBatch(1000);

        previous = System.currentTimeMillis();

        currentMap = new Map(this, 0);

        currentMap.addObject(new ColourMapObject(this, 0));
    }

    private void update() {Settings.screenHeight, Settings.fullscreen);
        Gdx.graphics.setVSync(Settings.VSync);
        batch.setColor(new Color(Settings.brightness, Settings.brightness, Settings.brightness, 1.0f));

        float delta = ((float)(System.currentTimeMillis() - previous)) / 1000.0f;
        previous = System.currentTimeMillis();

        currentMap.update(delta);
    }

    @Override
    public void render() { //Always called
        update();

        Gdx.gl.glClearColor(1, 0, 0, 1); //Red colour still black screen.
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        currentMap.render(batch); //Basicly list of textures to be rendered, they never stop rendering (Being called) despite black screen.
        batch.end();

        batch.flush();
    }

编辑:我们已经确定SpriteBatch在红色清晰颜色上呈现黑色屏幕后,它也会停止渲染纹理。我还确定即使在黑屏中,SpriteBatch的色调或颜色也会保持白色。

编辑,此代码采用纹理,然后变成不同颜色的不同纹理:

public class ColourMapObject extends MapObject {

    public enum Type {
        Dirt,
        Water,
        Trench,
    }

    private Texture terrainMap;
    private Texture trenchMap;
    private Texture soldierMap;
    private Texture buildingMap;
    private Texture shipMap;
    private int levelId;

    private Texture finalTexture;
    private Type[][] types;

    public ColourMapObject(TrenchMain main, int levelId) {
        super(main);
        this.levelId = levelId;

        //finalTexture = new Texture("/map" + String.valueOf(levelId) + "/terrainMap.png");
        finalTexture = new Texture("black.png");
        finalTexture.getTextureData().prepare();
        loadMap(levelId);
    }

    private void loadMap(int levelId) {
        //terrainMap = new Texture("/map" + String.valueOf(levelId) + "/terrainMap.png");
        terrainMap = new Texture("terrainMap.png");
        types = new Type[terrainMap.getWidth()][terrainMap.getHeight()];

        terrainMap.getTextureData().prepare();
        Pixmap pixmap = terrainMap.getTextureData().consumePixmap();

        for(int x = 0; x < terrainMap.getWidth(); x++) {
            for(int y = 0; y < terrainMap.getHeight(); y++) {
                types[x][y] = RandomMapColour.getType(new Color(pixmap.getPixel(x, y)));
                if(types[x][y] == null) types[x][y] = Type.Dirt;
            }
        }

//      trenchMap = new Texture("/map" + String.valueOf(levelId) + "/trenchMap.png");
//      
//      
//      soldierMap = new Texture("/map" + String.valueOf(levelId) + "/soldierMap.png");
//      
//      
//      buildingMap = new Texture("/map" + String.valueOf(levelId) + "/buildingMap.png");
//      
//      
//      shipMap = new Texture("/map" + String.valueOf(levelId) + "/shipMap.png");
    }

    @Override
    public void update(float delta) {
        super.update(delta);

        Pixmap draw = new Pixmap(Settings.screenWidth, Settings.screenHeight, Format.RGB888);

        float pX = (float)terrainMap.getWidth() / (float)draw.getWidth();
        float pY = (float)terrainMap.getHeight() / (float)draw.getHeight();

        for(int x = 0; x < draw.getWidth(); x++) {
            for(int y = 0; y < draw.getHeight(); y++) {
                switch(types[(int)((float)x * pX)][(int)((float)y * pY)]) {
                case Dirt:
                    draw.drawPixel(x, y, RandomMapColour.getDirtColour());
                    break;
                case Trench:
                    draw.drawPixel(x, y, RandomMapColour.getTrenchColour());
                    break;
                case Water:
                    draw.drawPixel(x, y, RandomMapColour.getWaterColour());
                    break;
                }
            }
        }

        finalTexture = new Texture(draw);
    }

    @Override
    public void render(SpriteBatch batch) {
        super.render(batch);
        float sx = ((float)TrenchMain.getScreenWidth()) / ((float)finalTexture.getWidth());
        float sy = ((float)TrenchMain.getScreenHeight()) / ((float)finalTexture.getHeight());

        batch.draw(finalTexture, 0, 0, 0, 0, finalTexture.getWidth(), finalTexture.getHeight(), sx, sy, 0, 0, 0, finalTexture.getWidth(), finalTexture.getHeight(), false, false);
    }
答案

我终于明白了,对于那些奇怪的人,我正在做的是在处理旧的更新时创建一个新的finalTexture

只需处理纹理。 finalTexture.dispose();

以上是关于libGDX奇怪的渲染错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 LibGdx 在 Java 中奇怪的 CPU 使用率

渲染 3d .g3db 模型时出现空白屏幕 [libgdx]

为啥片段着色器比渲染纹理更快?

从 Blender 2.8 导出到 LibGDX

Libgdx 渲染方法

LibGDX 3d 模型纹理未正确渲染