OpenGL ES的纹理映射溢出 [英] OpenGL ES Texture Mapping Overflow

查看:127
本文介绍了OpenGL ES的纹理映射溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动画二维雪碧表。我哈瓦有很多角色动画的不同帧大小一个精灵表。对于一个动画,我缩放顶点,以适应一帧,然后更改纹理位置动画。作品pretty以及一个动画,但再次切换到不同的帧大小和规模顶点和装配质感另一个动画的时候,我得到的副作用,其中质地stretcht不恰当,它只是在一个动画帧,但要两个动画之间的改变看起来很糟糕。

I want to animate a 2d Sprite sheet. I hava a sprite sheet with a lot of character animation with different frame size. For a single animation, I scale a vertex to fit one frame and then change Texture position for animation. Works pretty well for one animation, but when switching to another animation with different frame size and scale vertex and fitting texture again, I get side effect where texture is stretcht and not fitting, it is just on one animation frame, but make the change between two animations look very bad.

我想,那是因为顶点尺寸的变化。所以我的想法IST,有一个固定的顶点尺寸,适合质地没有它strechting充分顶点(高度为每一个动画是固定的)。

I think, that is because of the vertex-size change. So my idea ist, to have a fixed vertex size and fit the texture without strechting it to the full vertex (height for every animation is fixed).

也许有形象,有利于,所以我创建了一个:

Maybe a image will help, so I created one:

下面是我的code,希望这就够了:

Here is my code, hope it is enough:

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()。

so for every new animation, I call newVertex().

推荐答案

的出。
我想你应该使用相同的总体思路。如果需要,我可以详细说明如何正确放置纹理你的情况描述。

Check this out. I suppose you should use the same general idea. If needed I can describe in detail how to place texture correctly in your case.

这篇关于OpenGL ES的纹理映射溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆