이미지를 그리는 방법이 있습니다. 그것은 Vertex와 UVBuffers를 사용합니다.Android Open GL ES 텍스처가 엉망입니다.
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
을 그리고 그것은 마법처럼 작동합니다
나는 다음과 같이 그립니다.
그러나 같은 Draw 메서드를 사용하여 내 DrawString 메서드를 시도 할 때. 텍스처 UV 맵 또는 일부 버퍼가 엉망이됩니다 (모든 것은 누락 된 픽셀이있는 단색 직사각형과 같습니다).
나는 Angelcode 비트 맵 글꼴 생성기를 사용합니다.
유일한 차이점은 현재 캐릭터의 데이터에 따라 텍스처와 UV 맵을 설정한다는 것입니다.
나는 다른 기능에서 뭔가를 엉망으로 만든 경우를 대비하여 모든 나의 그리기 기능을 게시 할 예정입니다.
public void Begin(int Source, int Destination)
{
GL.glEnable(GL10.GL_BLEND);
GL.glBlendFunc(Source, Destination);
}
내 모든 버퍼와 변수 (I 함수에 선언하고 싶지 않았다)
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>();
방법을 그립니다
public void Draw(Texture2D texture, Rectangle destination, Rectangle source, GLColor color)
{
Draw(texture, destination, source, color, new Vector2(0, 0), 0);
}
졸라 매는 끈에서 main 메소드에 액세스
그리기 방법
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;
}
}
}
그리고 마지막으로 종료 방법 W :
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();
}
난 여전히 해결책을 찾기 위해 시도. 또한이 문제를 이해하는 데 도움이되는 이미지를 게시하려고합니다. 미리 감사드립니다.
어떻게 "엉망이 되니?" 아무것도 보이지 않나요? 텍스처를 보여 주지만 올바르게 맵핑되지 않았습니까? 캐릭터가 동일한 기준선에 있지 않습니까? –
지정하십시오. 그리기 문자열 이후의 모든 것은 누락 된 픽셀이있는 사각형과 같습니다. –
DrawString과의 차이점은 비트 맵 글꼴 아틀라스에 대해 다른 텍스처 좌표를 계산하지만 위의 코드에서는 아무 것도 잘못 보이지 않는다는 것입니다. SpriteBatch 클래스의 일부 상태가 손상되었을 수 있습니다 (원본 및 대상 rectange가 사용되는 방법이 명확하지 않음). 전체 코드가 없으면이를 구별하기가 어려울 수 있습니다. –