使用 LWJGL 渲染二维图块的最快方法?

Fastest way to render 2D tiles using LWJGL?

我开始观看 these 使用 LWJGL 创建 2d top-down 游戏的教程,我读到 VBO 应该很快,但是对于每帧渲染 48*48 个图块,我只能得到大约 100FPS,这非常慢因为我会在游戏中添加更多的东西,而不仅仅是一些静态的、不移动或改变的瓷砖。

我该怎么做才能让它更快?请记住,我刚刚开始学习 lwjgl 和 opengl,所以我可能不会知道很多东西。

无论如何,这是我的部分代码(我从代码中删除了一些毫无意义的部分并用一些描述替换它们):

主循环

double targetFPS = 240.0;
        double targetUPS = 60.0;

        long initialTime = System.nanoTime();
        final double timeU = 1000000000 / targetUPS;
        final double timeF = 1000000000 / targetFPS;
        double deltaU = 0, deltaF = 0;
        int frames = 0, updates = 0;
        long timer = System.currentTimeMillis();

        while (!window.shouldClose()) {
            long currentTime = System.nanoTime();
            deltaU += (currentTime - initialTime) / timeU;
            deltaF += (currentTime - initialTime) / timeF;
            initialTime = currentTime;

            if (deltaU >= 1) {
                // --- [ update ] ---
                --INPUT HANDLING FOR BASIC MOVEMENT, CLOSING THE GAME AND TURNING VSYNC ON AND OFF USING A METHOD FROM THE INPUT HANDLER CLASS--

                world.correctCamera(camera, window);

                window.update();

                updates++;
                deltaU--;
            }

            if (deltaF >= 1) {
                // --- [ render ] ---
                glClear(GL_COLOR_BUFFER_BIT);
                world.render(tileRenderer, shader, camera, window);
                window.swapBuffers();

                frames++;
                deltaF--;
            }
            --PRINTING THE FPS AND UPS EVERY SECOND--
        }

使用的输入处理方法:

I have this in my constructor:
this.keys = new boolean[GLFW_KEY_LAST];
for(int i = 0; i < GLFW_KEY_LAST; i++)
    keys[i] = false;

And here are the methods: 
public boolean isKeyDown(int key) {
    return glfwGetKey(window, key) == 1;
}
public boolean isKeyPressed(int key) {
    return (isKeyDown(key) && !keys[key]);
}
public void update() {
    for(int i = 32; i < GLFW_KEY_LAST; i++)
        keys[i] = isKeyDown(i);
}

这是来自世界的渲染方法class:

public void render(TileRenderer renderer, Shader shader, Camera camera, Window window) {
    int posX = ((int) camera.getPosition().x + (window.getWidth() / 2)) / (scale * 2);
    int posY = ((int) camera.getPosition().y - (window.getHeight() / 2)) / (scale * 2);
    for (int i = 0; i < view; i++) {
        for (int j = 0; j < view; j++) {
            Tile t = getTile(i - posX, j + posY);
            if (t != null)
                renderer.renderTile(t, i - posX, -j - posY, shader, world, camera);
        }
    }
}

这是来自 TileRenderer 的 renderTile() 方法:

public void renderTile(Tile tile, int x, int y, Shader shader, Matrix4f world, Camera camera) {
    shader.bind();
    if (tileTextures.containsKey(tile.getTexture()))
        tileTextures.get(tile.getTexture()).bind(0);

    Matrix4f tilePosition = new Matrix4f().translate(new Vector3f(x * 2, y * 2, 0));
    Matrix4f target = new Matrix4f();

    camera.getProjection().mul(world, target);
    target.mul(tilePosition);

    shader.setUniform("sampler", 0);
    shader.setUniform("projection", target);

    model.render();
}

这是模型class的构造函数和渲染方法:

public Model(float[] vertices, float[] texture_coords, int[] indices) {
    draw_count = indices.length;

    v_id = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, v_id);
    glBufferData(GL_ARRAY_BUFFER, createBuffer(vertices), GL_STATIC_DRAW);

    t_id = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, t_id);
    glBufferData(GL_ARRAY_BUFFER, createBuffer(texture_coords), GL_STATIC_DRAW);

    i_id = glGenBuffers();
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);

    IntBuffer buffer = BufferUtils.createIntBuffer(indices.length);
    buffer.put(indices);
    buffer.flip();

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

public void render() {
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glBindBuffer(GL_ARRAY_BUFFER, v_id);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, t_id);
    glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);
    glDrawElements(GL_TRIANGLES, draw_count, GL_UNSIGNED_INT, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
}

我将顶点、纹理坐标和索引存储在图块渲染器中:

float[] vertices = new float[]{
            -1f, 1f, 0, //top left     0
            1f, 1f, 0, //top right     1
            1f, -1f, 0, //bottom right 2
            -1f, -1f, 0, //bottom left 3
    };

    float[] texture = new float[]{
            0, 0,
            1, 0,
            1, 1,
            0, 1,
    };

    int[] indices = new int[]{
            0, 1, 2,
            2, 3, 0
    };

我不知道还能在这里放什么,但是完整的源代码和资源 + 着色器文件可以在 github here.

上找到

对于您当前的系统,我建议您根据纹理对图块进行分组。创建这样的东西:

Map<Texture, List<Tile>> tiles = new HashMap<Texture, List<Tile>>()

然后当你去渲染你的瓦片地图时,你只需要为每组瓦片设置一次纹理,而不是每个瓦片一次。这节省了将 textures/texture id 推送到 GPU 的 PCI-E 带宽。你会像这样实现(伪代码):

for (Texture tex : tile.keySet())
{
    BIND TEXTURE
    for (Tile tile : tiles.get(tex))
    {
        SET UNIFORMS
        RENDER
    }
}

我在这些方面看到的其他内容是您正在将投影矩阵分别推到每个图块。当您是 运行 着色器程序时,给定制服的值将保持不变,直到您更改它或直到程序结束。统一设置一次投影矩阵。

您似乎每隔 renderTile(...) 调用一次。给定值不变,在render pass前计算一次,然后在renderTile(...)方法中作为变量传入,而不是传入cameraworld.