java - 在 LibGdx 中绘制网格线的健康方法

标签 java opengl libgdx rendering game-engine

所以,我想在我的游戏中绘制一个网格,当我把它写出来时,它在效率方面似乎非常不寻常(至少对我来说)。我在渲染方法中有两个 for 循环,用于渲染游戏,因此它被频繁且快速地调用。

这个 ShapeRenderer 部分是危险信号吗?

public void render(float deltaTime) {
    Gdx.gl.glClearColor(Color.GRAY.r, Color.GRAY.g, Color.GRAY.b, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);       

    batch.begin();
    this.world.getPlayer().render(deltaTime, batch);
    batch.end();

    // Gridlines
    shape.begin(ShapeType.Line);
    shape.setColor(Color.BLACK);
    for (int i = 0; i < Gdx.graphics.getHeight() / 32; i++) {
        shape.line(0, i * 32, Gdx.graphics.getWidth(), i * 32);
    }
    for (int i = 0; i < Gdx.graphics.getWidth() / 32; i++) {
        shape.line(i * 32, 0, i * 32, Gdx.graphics.getHeight());
    }
    shape.end();
}

最佳答案

您可以使用快速、简单且功能强大的 ImmediateModeRenderer20 。 请使用 PerspectiveCamera 查看以下代码:

// init primitive renderer
ImmediateModeRenderer20 lineRenderer = new ImmediateModeRenderer20(false, true, 0);

// create atomic method for line
public static void line(float x1, float y1, float z1,
                        float x2, float y2, float z2,
                        float r, float g, float b, float a) {
    lineRenderer.color(r, g, b, a);
    lineRenderer.vertex(x1, y1, z1);
    lineRenderer.color(r, g, b, a);
    lineRenderer.vertex(x2, y2, z2);
}

// method for whole grid
public static void grid(int width, int height) {
    for (int x = 0; x <= width; x++) {
        // draw vertical
        line(x, 0, 0,
                x, 0, -height,
                0, 1, 0, 0);
    }

    for (int y = 0; y <= height; y++) {
        // draw horizontal
        line(0, 0, -y,
                width, 0, -y,
                0, 1, 0, 0);
    }
}

// init 3d camera
PerspectiveCamera perspectiveCamera = new PerspectiveCamera(55, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

// your render loop {
perspectiveCamera.update();
lineRenderer.begin(perspectiveCamera.combined, GL30.GL_LINES);
//... lineRenderer works a lot
grid(10, 10);
//... lineRenderer works a lot
lineRenderer.end();
//}

希望对你有帮助!

关于java - 在 LibGdx 中绘制网格线的健康方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215500/

相关文章:

java - 用Java制作tar文件

c++ - 从未被着色器处理的位置照亮图元

c++ - opengl 库在哪里存储在 ubuntu : i need this to mention in my make file 上

java - 如何在Libgdx游戏上添加插页式广告?

java - 在 gradle 刷新时运行 Flyway 迁移

java - 用于度量的 Kubernetes Java API

java - 我如何让 spring-ws + tomcat 记录错误

java - 如何将值从处理程序传递到方法

c++ - GLFW - 用鼠标旋转相机的 View 矩阵使其旋转

background - libGDX 渲染大背景图像的最佳方法是什么?