1
我想動畫2d精靈表單。我有一個精靈表,其中包含大量具有不同幀大小的角色動畫。對於單個動畫,我縮放頂點以適應一個幀,然後更改動畫的紋理位置。對於一個動畫效果相當不錯,但是當切換到另一個具有不同幀大小和縮放頂點並再次貼合紋理的動畫時,我會在紋理拉伸且不適合的情況下產生副作用,它只是在一個動畫幀上,但在兩者之間進行更改兩個動畫看起來非常糟糕。OpenGL ES紋理映射溢出
我想,這是因爲頂點大小的變化。所以我的想法是,有一個固定的頂點大小,適合紋理而不會將其拖到完整頂點(每個動畫的高度都固定)。
也許有形象,有利於,所以我創建一個:img http://oi44.tinypic.com/14l09hk.jpg
這裏是我的代碼,希望是不夠的:
public boolean nextFrame() {
float textureWidth = textureMap()[currentAnimation][0];
float frameCount = textureMap()[currentAnimation][1];
float frameWidth = textureWidth/frameCount;
if (loop) {
if (currentFrame == frameCount)
currentFrame = 0;
} else {
if (currentFrame == frameCount) {
setAnimation(AnimationConstants.IDLE);
loop = true;
return false;
}
}
float x_left = (float) currentFrame * frameWidth/textureWidth;
float x_right = (float) (currentFrame * frameWidth + frameWidth)
/textureWidth;
texture[0] = x_left; // top left x
texture[1] = 1.0f; // top left y
texture[2] = x_left; // bottom left x
texture[3] = 0.0f; // bottom left y
texture[4] = x_right; // top right x
texture[5] = 1.0f; // top right y
texture[6] = x_right; // bottom right x
texture[7] = 0.0f; // bottom right y
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
currentFrame++;
return true;
}
private void newVertex() {
float textureWidth = textureMap()[currentAnimation][0];
float frameCount = textureMap()[currentAnimation][1];
float frameWidth = textureWidth/frameCount;
float width = (float) frameWidth/(float) frameHeight;
vertices[0] = pos_x; // bottom left x
vertices[1] = pos_y; // bottom left y
vertices[3] = pos_x; // top left x
vertices[4] = pos_y + (1.0f * scale); // top left y
vertices[6] = pos_x + (width * scale); // bottom right x
vertices[7] = pos_y; // bottom right y
vertices[9] = pos_x + (width * scale); // top right x
vertices[10] = pos_y + (1.0f * scale); // top right y
// z values
vertices[2] = -0.2f; // bottom left z
vertices[5] = -0.2f; // top left z
vertices[8] = -0.2f; // bottom right z
vertices[11] = -0.2f; // top right z
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
}
所以每一個新的動畫,我叫newVertex()。
所以,你想要情況2,但向我們展示了情況1的代碼? – mbeckish 2012-02-22 16:25:23
只是詞彙上的一點。你稱之爲頂點的實際上是一個頂點緩衝對象,即一個幾何。一個真正的頂點是一個空間點,根據定義沒有大小。 – rockeye 2012-02-22 16:27:40
@mkbeckish的權利,我想情況2,不知道。因爲我對opengl非常陌生,以上是我的第一個代碼,用於顯示我的代碼情況,因爲也許整個概念是錯誤的,我不知道。 – 2012-02-22 17:49:34