使用 Slick/OpenGL 渲染和 Cropping/Stretching 图像(使用 getSubImage)

Rendering and Cropping/Stretching an Image Using Slick/OpenGL (using getSubImage)

我正在尝试使用 Slick 在项目中的某些 2D 精灵上重新创建阴影效果。为此,我正在重新着色精灵的副本并使用 Slick OpenGL 使用以下方法拉伸它:

public static void getStretched(Shape shape, Image image) {

    TextureImpl.bindNone();
    image.getTexture().bind();

    SGL GL = Renderer.get();

    GL.glEnable(SGL.GL_TEXTURE_2D);
    GL.glBegin(SGL.GL_QUADS);

    //topleft
    GL.glTexCoord2f(0f, 0f);
    GL.glVertex2f(shape.getPoints()[0], shape.getPoints()[1]);

    //topright
    GL.glTexCoord2f(0.5f, 0f);
    GL.glVertex2f(shape.getPoints()[2], shape.getPoints()[3]);

    //bottom right
    GL.glTexCoord2f(1f, 1f);
    GL.glVertex2f(shape.getPoints()[4], shape.getPoints()[5]);

    //btoom left
    GL.glTexCoord2f(0.5f, 1f);
    GL.glVertex2f(shape.getPoints()[6], shape.getPoints()[7]);

    GL.glEnd();
    GL.glDisable(SGL.GL_TEXTURE_2D);
    TextureImpl.bindNone();
}

This gives the almost the desired effect, aside from the fact that the image is cropped a bit.

This becomes more extreme for higher distortions

我是 OpenGL 的新手,所以如果能提供一些有关如何解决此问题的帮助会很棒。 此外,如果我将图像输入使用 getSubImageOpenGL renders the original image, rather than the sub image.

获得的方法

我不确定为什么会发生这种情况,因为精灵本身是使用 getSubImage 从精灵表中获取的,并且没有渲染问题。 将不胜感激!

I'm recolouring a copy of the sprite and stretching it

问题是您拉伸了纹理坐标,但被精灵覆盖的区域保持不变。如果阴影超出四边形图元覆盖的区域,则会被裁剪。
您必须 "stretch" 顶点坐标而不是纹理坐标。为阴影定义一个菱形几何体并将纹理包裹在其上:

float distortionX = ...;

GL.glEnable(SGL.GL_TEXTURE_2D);
GL.glBegin(SGL.GL_QUADS);

//topleft
GL.glTexCoord2f(0f, 0f);
GL.glVertex2f(shape.getPoints()[0] + distortionX, shape.getPoints()[1]);

//topright
GL.glTexCoord2f(1f, 0f);
GL.glVertex2f(shape.getPoints()[2] + distortionX, shape.getPoints()[3]);

//bottom right
GL.glTexCoord2f(1f, 1f);
GL.glVertex2f(shape.getPoints()[4], shape.getPoints()[5]);

//btoom left
GL.glTexCoord2f(0f, 1f);
GL.glVertex2f(shape.getPoints()[6], shape.getPoints()[7]);

GL.glEnd();

[...] if I feed an image into the method that was obtained using getSubImage, [...] the sprite itself is taken from a spritesheet [...]

精灵仅覆盖整个纹理的一小块矩形区域。该区域由纹理坐标定义。您必须使用与绘制精灵本身时相同的纹理坐标。