【发布时间】:2012-02-22 14:56:43
【问题描述】:
我想为 2d Sprite 表设置动画。我有一个精灵表,里面有很多不同帧大小的角色动画。对于单个动画,我缩放一个顶点以适合一帧,然后更改动画的纹理位置。对于一个动画效果很好,但是当切换到另一个具有不同帧大小和缩放顶点并再次拟合纹理的动画时,我得到了纹理被拉伸而不适合的副作用,它只是在一个动画帧上,但是在两个动画看起来很糟糕。
我认为,这是因为顶点大小的变化。所以我的想法是,有一个固定的顶点大小并适合纹理而不会将其拉伸到完整顶点(每个动画的高度都是固定的)。
这是我的代码,希望够用了:
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 的代码?
-
只是词汇表上的一点。您所说的顶点实际上是一个顶点缓冲区对象,即几何。真正的顶点是一个空间点,根据定义没有大小。
-
@mkbeckish 对,我想要情况 2,但不知道。因为我对opengl很陌生,所以上面是我为显示我的代码情况而编写的第一个代码,因为也许整个概念是错误的,我不知道。
-
@rockeye 和以前一样,我对它很陌生,所以感谢您提供的信息,对,我的意思是顶点缓冲区对象,尤其是正方形。
标签: android opengl-es mapping overflow textures