2012-02-22 129 views
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()。

+0

所以,你想要情况2,但向我们展示了情况1的代码? – mbeckish 2012-02-22 16:25:23

+0

只是词汇上的一点。你称之为顶点的实际上是一个顶点缓冲对象,即一个几何。一个真正的顶点是一个空间点,根据定义没有大小。 – rockeye 2012-02-22 16:27:40

+0

@mkbeckish的权利,我想情况2,不知道。因为我对opengl非常陌生,以上是我的第一个代码,用于显示我的代码情况,因为也许整个概念是错误的,我不知道。 – 2012-02-22 17:49:34

回答

0

检查this出。 我想你应该使用相同的一般想法。如果需要,我可以详细描述如何在你的案例中正确地放置纹理。