Переполнение в OpenGL ES Texture Mapping - PullRequest
1 голос
/ 22 февраля 2012

Я хочу анимировать лист 2d Sprite.У меня есть спрайт с большим количеством анимации персонажей с разным размером кадра.Для отдельной анимации я масштабирую вершину, чтобы она поместилась в один кадр, а затем меняю положение текстуры для анимации.Работает довольно хорошо для одной анимации, но при переключении на другую анимацию с другим размером кадра и вершиной масштаба и повторной подгонкой текстуры, я получаю побочный эффект, когда текстура растягивается и не умещается, она находится только на одном кадре анимации, но вносить изменения междудве анимации выглядят очень плохо.

Я думаю, это из-за изменения размера вершины.Поэтому моя идея состоит в том, чтобы иметь фиксированный размер вершины и разместить текстуру, не растягивая ее до полной вершины (высота для каждой анимации фиксирована).

Может быть, изображение поможет, поэтому я создал его: 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 ().

1 Ответ

0 голосов
/ 02 августа 2012

Проверьте это .Я полагаю, вы должны использовать ту же общую идею.При необходимости могу подробно описать, как правильно разместить текстуру в вашем случае.

...