有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android Open GL ES纹理混乱

我有自己的画图方法。它使用顶点和UV缓冲区

我画如下:

SpriteBatch.Begin(blendMode) <- sets up blending
SpriteBatch.Draw(parameters) <- add vertices to the list of vertex arrays
SpriteBatch.End() <- Sets up buffers and draw out everything at once

它就像魅力一样

然而,当我尝试我的DrawString方法时,它使用了相同的Draw方法。纹理UV贴图或一些缓冲区出错(所有东西都像一个缺少像素的实心矩形绘制)

我使用Angelcode位图字体生成器

我使用相同的功能唯一的区别是根据当前角色的数据设置纹理和UV贴图

我将发布我所有的绘图函数,以防我在其他函数中搞砸了

public void Begin(int Source, int Destination)
{
    GL.glEnable(GL10.GL_BLEND);
    GL.glBlendFunc(Source, Destination);
}

我所有的缓冲区和变量(我不想在函数中声明)

ArrayList<float[]> vertices = new ArrayList<float[]>();
FloatBuffer vertexBuffer;
ArrayList<short[]> indices = new ArrayList<short[]>();
ShortBuffer indexBuffer;
float minU = 0;
float maxU = 1;
float minV = 0;
float maxV = 1;
ArrayList<GLColor> colors = new ArrayList<GLColor>();
GLColor c_color;
ArrayList<float[]> vertexUVs = new ArrayList<float[]>();
FloatBuffer uvBuffer;
ArrayList<TransformationMatrix> matrices = new ArrayList<TransformationMatrix>();
TransformationMatrix matrix = new TransformationMatrix();
ArrayList<Integer> textures = new ArrayList<Integer>();

Draw方法,从DrawString访问main方法

public void Draw(Texture2D texture, Rectangle destination, Rectangle source, GLColor color)
{
    Draw(texture, destination, source, color, new Vector2(0, 0), 0);
}

画法

public void Draw(Texture2D texture, Rectangle destination, Rectangle source, GLColor color, Vector2 origin, float Rotation)
{
    vertices.add(new float[]{-origin.X, -origin.Y,
            destination.Width - origin.X, -origin.Y,
            destination.Width - origin.X, destination.Height - origin.Y,
                    -origin.X, destination.Height - origin.Y});

    indices.add(new short[]{0, 1, 2, 2, 3, 0});

    //Generate UV of Vertices

    minU = 0;
    maxU = 1;
    minV = 0;
    maxV = 1;

    if (source != null)
    {
        minU = (float)source.X / (float)texture.getWidth();
        maxU = (float)(source.X + source.Width) / (float)texture.getWidth();
        minV = (float)source.Y / (float)texture.getHeight();
        maxV = (float)(source.Y + source.Height) / (float)texture.getHeight();
    }
    vertexUVs.add(new float[]{minU, minV,
                        maxU, minV,
                        maxU, maxV,
                        minU, maxV});

    //Calculate Matrix
    matrix = new TransformationMatrix();
    matrix.Translate(destination.X + origin.X, destination.Y + origin.Y, 0);
    matrix.Rotate(0, 0, Rotation);
    matrices.add(matrix);

    colors.add(color);
    textures.add(texture.ID);
    }

绘制字符串方法(导致错误)

public void DrawString(SpriteFont spriteFont, String Text, Vector2 vector, GLColor color)
{
    int cursorX = (int)vector.X;
    int cursorY = (int)vector.Y;

    for (int i = 0; i < Text.length(); i++)
    {
        char c = Text.charAt(i);
        if (c == '\n') cursorX += spriteFont.LineHeight;
        else
        {
            int index = (int)c;

            //Draw Character
            if (spriteFont.Characters.length <= index)
            {
                Log.d("SpriteFont error", "Character is not presented in SpriteFont!");
                continue;
            }

            CharacterDescriptor cd = spriteFont.Characters[index];

            Rectangle source = new Rectangle(cd.x, cd.y, cd.Width, cd.Height);
            //Draw Character
            Rectangle destination = new Rectangle(cursorX + cd.XOffset, cursorY + cd.YOffset, cd.Width, cd.Height);

            Draw(spriteFont.Pages[cd.Page], destination, source, color);

            cursorX += cd.XAdvance;
        }
    }
}

最后是结束方法:

public void End()
{
    vertexBuffer = ByteBuffer.allocateDirect(vertices.size() * 8 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    for (int i = 0; i < vertices.size(); i++)
    {
        vertexBuffer.put(vertices.get(i));
    }
    vertexBuffer.flip();


    //Generate Indices
    indexBuffer = ByteBuffer.allocateDirect(indices.size() * 6 * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
    for (int i = 0; i < vertices.size(); i++)
    {
        indexBuffer.put(indices.get(i));
    }
    indexBuffer.flip();

    //Generate UV of Vertices
    uvBuffer = ByteBuffer.allocateDirect(vertexUVs.size() * 8 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    for (int i = 0; i < vertexUVs.size(); i++)
    {
        uvBuffer.put(vertexUVs.get(i));
    }
    uvBuffer.flip();

    //Bind Vertices

    for (int i = 0; i < colors.size(); i++)
    {
        //Bind Pointers
        GL.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        GL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        GL.glEnable(GL10.GL_TEXTURE_2D);
        vertexBuffer.position(i * 8);
        GL.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
        uvBuffer.position(i * 8);
        GL.glTexCoordPointer(2, GL10.GL_FLOAT, 0, uvBuffer);
        matrix = matrices.get(i);
        c_color = colors.get(i);

        GL.glMatrixMode(GL10.GL_MODELVIEW);
        GL.glLoadIdentity();
        GL.glTranslatef(matrix.TranslationX, matrix.TranslationY, matrix.TranslationZ);
        GL.glRotatef((float)Math.sqrt(matrix.RotationX * matrix.RotationX + matrix.RotationY*matrix.RotationY + matrix.RotationZ*matrix.RotationZ), matrix.RotationX, matrix.RotationY, matrix.RotationZ);
        //Bind Texture
        GL.glBindTexture(GL10.GL_TEXTURE_2D, textures.get(i));
        GL.glColor4f(c_color.R, c_color.G, c_color.B, c_color.A);

        //Draw Elements
        GL.glDrawElements(GL10.GL_TRIANGLES, 8, GL10.GL_UNSIGNED_SHORT, indexBuffer);

        GL.glBindTexture(GL10.GL_TEXTURE_2D, 0);
        GL.glDisable(GL10.GL_TEXTURE_2D);
        GL.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        GL.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

    //Disable things
    GL.glDisable(GL10.GL_BLEND);
    colors.clear();
    matrices.clear();
    vertices.clear();
    vertexUVs.clear();
}

我仍在努力寻找解决办法。我还将尝试发布一张图片来帮助您理解这个问题。提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    我真的很惭愧,我每次都回答我的问题,但我发现了。我没有从绘图缓冲区中清除纹理缓冲区。我真的不喜欢我在这个网站上没有真正的问题。但至少现在已经解决了。在解决了我的一个问题后,我现在和往常一样觉得自己很愚蠢。谢谢你的评论