生成图块的更好方法

发布于 2024-08-24 17:53:43 字数 4991 浏览 10 评论 0原文

首先我要说的是,我对 OpenGL ES 真的很陌生(我昨天才开始=),但我确实有一些 Java 和其他语言的经验。

我看了很多教程,当然是Nehe的教程,我的工作主要是基于它。

作为测试,我开始创建一个“图块生成器”,以便创建一个类似塞尔达传说的小型游戏(只需在带纹理的正方形中移动一个家伙就很棒了:p)。

到目前为止,我已经实现了一个工作图块生成器,我定义了一个 char map[][] 数组来存储图块处于打开状态:

private char[][] map = {
            {0, 0, 20, 11, 11, 11, 11, 4, 0, 0},
            {0, 20, 16, 12, 12, 12, 12, 7, 4, 0},
            {20, 16, 17, 13, 13, 13, 13, 9, 7, 4},
            {21, 24, 18, 14, 14, 14, 14, 8, 5, 1},
            {21, 22, 25, 15, 15, 15, 15, 6, 2, 1},
            {21, 22, 23, 0, 0, 0, 0, 3, 2, 1},
            {21, 22, 23, 0, 0, 0, 0, 3, 2, 1},
            {26, 0, 0, 0, 0, 0, 0, 3, 2, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
    };

它正在工作,但我对此不满意,我确信有更好的方法可以做到这些东西:

1)加载纹理:

我创建了一个丑陋的数组,其中包含我想在该地图上使用的图块:(

private int[] textures = {
            R.drawable.herbe, //0
            R.drawable.murdroite_haut, //1
            R.drawable.murdroite_milieu, //2
            R.drawable.murdroite_bas, //3
            R.drawable.angledroitehaut_haut, //4
            R.drawable.angledroitehaut_milieu,  //5
    };

我故意剪切了它,我当前加载了 27 个图块)

所有这些都已存储在drawable文件夹中,每一个都是16*16的图块。

然后我使用这个数组来生成纹理并将它们存储在 HashMap 中以供以后使用:

int[] tmp_tex = new int[textures.length]; 
gl.glGenTextures(textures.length, tmp_tex, 0); 
texturesgen = tmp_tex; //Store the generated names in texturesgen 
for(int i=0; i < textures.length; i++)
{ 
    //Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), textures[i]);
    InputStream is = context.getResources().openRawResource(textures[i]);
    Bitmap bitmap = null;
    try {
        //BitmapFactory is an Android graphics utility for images
        bitmap = BitmapFactory.decodeStream(is);

    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    } 
    // Get a new texture name 
    // Load it up 
    this.textureMap.put(new Integer(textures[i]),new Integer(i)); 
    int tex = tmp_tex[i]; 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, tex); 
    //Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    bitmap.recycle();
} 

我很确定有更好的方法来处理这个问题......我只是无法弄清楚。如果有人有想法,我会洗耳恭听。

2) 绘制图块

我所做的是创建一个正方形和一个纹理贴图:

/** The initial vertex definition */
    private float vertices[] = { 
                                -1.0f, -1.0f, 0.0f,     //Bottom Left
                                1.0f, -1.0f, 0.0f,      //Bottom Right
                                -1.0f, 1.0f, 0.0f,      //Top Left
                                1.0f, 1.0f, 0.0f        //Top Right
                                                };

    private float texture[] = {         
            //Mapping coordinates for the vertices
              0.0f, 1.0f,
              1.0f, 1.0f,
              0.0f, 0.0f,
              1.0f, 0.0f

                                };

然后,在我的绘制函数中,我循环遍历贴图以定义要使用的纹理(在指向并启用缓冲区):

for(int y = 0; y < Y; y++){
    for(int x = 0; x < X; x++){
        tile = map[y][x];
        try 
            { 
                //Get the texture from the HashMap
                int textureid = ((Integer) this.textureMap.get(new Integer(textures[tile]))).intValue(); 
                gl.glBindTexture(GL10.GL_TEXTURE_2D, this.texturesgen[textureid]); 
            }    
            catch(Exception e) 
            { 
                return; 
            }

        //Draw the vertices as triangle strip
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);            
        gl.glTranslatef(2.0f, 0.0f, 0.0f); //A square takes 2x so I move +2x before drawing the next tile
    }
    gl.glTranslatef(-(float)(2*X), -2.0f, 0.0f); //Go back to the begining of the map X-wise and move 2y down before drawing the next line
}

这非常有效,因为我真的认为在 1000*1000 或更大的地图上,它会非常滞后(提醒一下,这是典型的塞尔达世界地图:http://vgmaps.com/Atlas/SuperNES/LegendOfZelda-ALinkToThePast-LightWorld.png )。

我读过有关 Vertex Buffer Object 和 DisplayList 的内容,但我找不到好的教程,nodoby 似乎还可以,哪个是最好的/有更好的支持(T1 和 Nexus One 还很遥远)。

我想就是这样,我已经放置了很多代码,但我认为它有帮助。

提前致谢 !

I'll start by saying that i'm REALLY new to OpenGL ES (I started yesterday =), but I do have some Java and other languages experience.

I've looked a lot of tutorials, of course Nehe's ones and my work is mainly based on that.

As a test, I started creating a "tile generator" in order to create a small Zelda-like game (just moving a dude in a textured square would be awsome :p).

So far, I have achieved a working tile generator, I define a char map[][] array to store wich tile is on :

private char[][] map = {
            {0, 0, 20, 11, 11, 11, 11, 4, 0, 0},
            {0, 20, 16, 12, 12, 12, 12, 7, 4, 0},
            {20, 16, 17, 13, 13, 13, 13, 9, 7, 4},
            {21, 24, 18, 14, 14, 14, 14, 8, 5, 1},
            {21, 22, 25, 15, 15, 15, 15, 6, 2, 1},
            {21, 22, 23, 0, 0, 0, 0, 3, 2, 1},
            {21, 22, 23, 0, 0, 0, 0, 3, 2, 1},
            {26, 0, 0, 0, 0, 0, 0, 3, 2, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
    };

It's working but I'm no happy with it, I'm sure there is a beter way to do those things :

1) Loading Textures :

I create an ugly looking array containing the tiles I want to use on that map :

private int[] textures = {
            R.drawable.herbe, //0
            R.drawable.murdroite_haut, //1
            R.drawable.murdroite_milieu, //2
            R.drawable.murdroite_bas, //3
            R.drawable.angledroitehaut_haut, //4
            R.drawable.angledroitehaut_milieu,  //5
    };

(I cutted this on purpose, I currently load 27 tiles)

All of theses are stored in the drawable folder, each one is a 16*16 tile.

I then use this array to generate the textures and store them in a HashMap for a later use :

int[] tmp_tex = new int[textures.length]; 
gl.glGenTextures(textures.length, tmp_tex, 0); 
texturesgen = tmp_tex; //Store the generated names in texturesgen 
for(int i=0; i < textures.length; i++)
{ 
    //Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), textures[i]);
    InputStream is = context.getResources().openRawResource(textures[i]);
    Bitmap bitmap = null;
    try {
        //BitmapFactory is an Android graphics utility for images
        bitmap = BitmapFactory.decodeStream(is);

    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    } 
    // Get a new texture name 
    // Load it up 
    this.textureMap.put(new Integer(textures[i]),new Integer(i)); 
    int tex = tmp_tex[i]; 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, tex); 
    //Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    bitmap.recycle();
} 

I'm quite sure there is a better way to handle that... I just was unable to figure it. If someone has an idea, i'm all ears.

2) Drawing the tiles

What I did was create a single square and a single texture map :

/** The initial vertex definition */
    private float vertices[] = { 
                                -1.0f, -1.0f, 0.0f,     //Bottom Left
                                1.0f, -1.0f, 0.0f,      //Bottom Right
                                -1.0f, 1.0f, 0.0f,      //Top Left
                                1.0f, 1.0f, 0.0f        //Top Right
                                                };

    private float texture[] = {         
            //Mapping coordinates for the vertices
              0.0f, 1.0f,
              1.0f, 1.0f,
              0.0f, 0.0f,
              1.0f, 0.0f

                                };

Then, in my draw function, I loop through the map to define the texture to use (after pointing to and enabling the buffers) :

for(int y = 0; y < Y; y++){
    for(int x = 0; x < X; x++){
        tile = map[y][x];
        try 
            { 
                //Get the texture from the HashMap
                int textureid = ((Integer) this.textureMap.get(new Integer(textures[tile]))).intValue(); 
                gl.glBindTexture(GL10.GL_TEXTURE_2D, this.texturesgen[textureid]); 
            }    
            catch(Exception e) 
            { 
                return; 
            }

        //Draw the vertices as triangle strip
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);            
        gl.glTranslatef(2.0f, 0.0f, 0.0f); //A square takes 2x so I move +2x before drawing the next tile
    }
    gl.glTranslatef(-(float)(2*X), -2.0f, 0.0f); //Go back to the begining of the map X-wise and move 2y down before drawing the next line
}

This works great by I really think that on a 1000*1000 or more map, it will be lagging as hell (as a reminder, this is a typical Zelda world map : http://vgmaps.com/Atlas/SuperNES/LegendOfZelda-ALinkToThePast-LightWorld.png ).

I've read things about Vertex Buffer Object and DisplayList but I couldn't find a good tutorial and nodoby seems to be OK on wich one is the best / has the better support (T1 and Nexus One are ages away).

I think that's it, I've putted a lot of code but I think it helps.

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日记撕了你也走了 2024-08-31 17:53:43

有几件事:

  1. 不需要使用哈希图,只需使用向量/列表。
  2. 拥有一个包含所有图块的大型纹理可能会更快/更容易。使用适当的纹理坐标来选择适当的图块。在这里,您可能需要对纹理过滤稍微小心一些。听起来您正在做一个 2D 游戏,在这种情况下您可能想要对图块使用最近邻过滤并将相机固定到整数像素位置。
  3. 使用GL_QUADS而不是GL_TRIANGLE_STRIP不是更容易吗?不确定你的代码 - 你似乎没有使用“纹理”数组。
  4. 只要您不绘制不在屏幕上的图块,地图大小就不会产生任何影响。你的代码应该是这样的

int minX = screenLeft / tileSize;
int minY = screenBottom / tileSize;
int maxX = screenRight / tileSize;
int maxY = screenTop / tilesSize;
for (int x = minX; x <= maxX; ++x)
{
   for (int y = minY; y < maxY; ++y)
   {
      ...

A couple of things:

  1. There's no need to use a hashmap, just use a vector/list.
  2. It may be faster/easier to have one large texture that contains all your tiles. Use appropriate texture coordinates to select the appropriate tile. You might have to be a little bit careful about texture filtering here. It sounds like you are doing a 2D game in which case you probably want to use nearest-neighbour filtering for the tiles and clamp the camera to integer pixel locations.
  3. Wouldn't it be easier to use GL_QUADS rather than GL_TRIANGLE_STRIP. Not sure about your code there - you don't seem to use the 'texture' array.
  4. The map size shouldn't make any difference, as long as you don't draw tiles that aren't on the screen. Your code should be something like:

.

int minX = screenLeft / tileSize;
int minY = screenBottom / tileSize;
int maxX = screenRight / tileSize;
int maxY = screenTop / tilesSize;
for (int x = minX; x <= maxX; ++x)
{
   for (int y = minY; y < maxY; ++y)
   {
      ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文