android - 使用六边形网格提高绘图性能

标签 android opengl-es hexagonal-tiles

我正在开发我的第一个 openGL 游戏,灵感来自 playstation 网络上的游戏“Greed Corp”。这是一款基于六角网格的回合制策略游戏。每个六边形瓷砖都有自己的高度和纹理。

我目前正在根据我阅读的一些示例和教程绘制六边形。这是我的 hextile 类:

public class HexTile
{
    private float height;

    private int[] textures = new int[1];

    private float vertices[] = {     0.0f,   0.0f, 0.0f,    //center
                                     0.0f,   1.0f, 0.0f,    // top
                                    -1.0f,   0.5f, 0.0f,    // left top
                                    -1.0f,  -0.5f, 0.0f,    // left bottom
                                     0.0f,  -1.0f, 0.0f,    // bottom
                                     1.0f,  -0.5f, 0.0f,    // right bottom
                                     1.0f,  0.5f, 0.0f,     // right top
    };

    private short[] indices = {      0, 1, 2, 3, 4, 5, 6, 1};

    //private float texture[] = { };

    private FloatBuffer vertexBuffer;
    private ShortBuffer indexBuffer;
    //private FloatBuffer textureBuffer;    

    public HexTile()
    {
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        vertexBuffer = vbb.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

        ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
        ibb.order(ByteOrder.nativeOrder());
        indexBuffer = ibb.asShortBuffer();
        indexBuffer.put(indices);
        indexBuffer.position(0);

        /*ByteBuffer tbb = ByteBuffer.allocateDirect(texture.length * 4);
        tbb.order(ByteOrder.nativeOrder());
        textureBuffer = tbb.asFloatBuffer();
        textureBuffer.put(texture);
        textureBuffer.position(0);*/
    }

    public void setHeight(float h)
    {
        height = h;
    }
    public float getHeight()
    {
        return height;
    }

    public void draw(GL10 gl)
    {
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        //gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        gl.glDrawElements(GL10.GL_TRIANGLE_FAN, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
    }

    public void loadGLTexture(GL10 gl, Context context)
    {
        textures[0] = -1;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.hex);

        while(textures[0] <= 0)
            gl.glGenTextures(1, textures, 0); 

        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        bitmap.recycle();
    }
}

我循环遍历所有可见图 block 以绘制它们的每一帧,最大为 11 * 9 个图 block 。然而,这将我的帧速率降低到 38,而且甚至没有在它们上绘制纹理,只是扁平的六边形。

现在我正试图弄清楚如何提高性能。我认为一次绘制整个网格可能会更快,但我不知道该怎么做,因为每个图 block 可以有不同的高度,并且很可能与相邻的图 block 具有不同的纹理。

我非常感谢这方面的一些帮助,因为我想开始真正的游戏 ^.^

最佳答案

假设您的六边形网格是静态的,您只需旋转所有六边形一次,生成它们的几何图形,然后将所有内容附加到一个(或多个,如果您有超过 2^16 个顶点)大型 VBO,您可以将其绘制在一个去吧。

就纹理而言,您可以使用 texture atlas .

关于android - 使用六边形网格提高绘图性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6249615/

相关文章:

javascript - 选择六 Angular 形周围的附近六 Angular 形

android - AppCompatActivity 使用样式删除操作栏高程/阴影

android - 摆脱 Activity 顶部的渐变(Android)

java - 安卓:OpenGL ES2 纹理不工作

opengl - 使用顶点/像素着色器(Open GL/DirectX)进行通用计算

android - 使用六边形图像随机生成图案

android - 从后台返回应用程序时,保存在单例中的静态数据有时为空

android - 谷歌播放服务在 url 中没有文件夹

iphone - 如何知道 GLCameraRipple 示例中的涟漪何时消失?

c# - 垂直六角网格 : Get x rings of tiles surrounding a specific coordinate