简体   繁体   English

没有光滑的OpenGL纹理

[英]OpenGL Textures without Slick

I searched for Texture Implementations without the Slick Utils library. 我搜索了没有Slick Utils库的Texture Implementations。 I found 2 ways, to do this: 我发现了两种方法可以做到这一点:

The first, saves the pixels with strange byteshifting in a byte buffer: 首先,将具有奇怪字节移位的像素保存在字节缓冲区中:

int loadTexture(){
        try{
            BufferedImage img = ImageIO.read(getClass().getClassLoader().getResourceAsStream("background.png"));
            int pixels[] = new int[img.getWidth() * img.getHeight()];
            img.getRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());
            ByteBuffer buffer = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 3);
            for(int x = 0; x < img.getWidth(); x++){
                for(int y = 0; y < img.getHeight(); y++){
                    int pixel = pixels[y * img.getWidth() + x];
                    buffer.put((byte) ((pixel >> 16) & 0xFF));
                    buffer.put((byte) ((pixel >> 8) & 0xFF));
                    buffer.put((byte) (pixel & 0xFF));
                }
            }
            buffer.flip();
            int textureId = glGenTextures();
            glBindTexture(GL_TEXTURE_2D, textureId);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, img.getWidth(), img.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
            return textureId;
        }
        catch(Exception e){
            e.printStackTrace();
            return 0;
        }
    }

This returns a texture id as well, and i haven't any idea how tu use this id. 这也返回一个纹理ID,我也不知道您如何使用此ID。

The second way doesnt do any byteshifting, and uses a IntBuffer: Also, it is a ready class to save different textures with names and so on. 第二种方法不做任何字节移位,而是使用IntBuffer:此外,它是一个准备就绪的类,用于保存带有名称等的不同纹理。 The Code of these: 这些代码:

ublic class TextureIO {

    private final IntBuffer texture;
    private final int       width;
    private final int       height;

    private int             id;


    public TextureIO(final InputStream inputStream) throws IOException {

        BufferedImage image = ImageIO.read(inputStream);

        width = image.getWidth();
        height = image.getHeight();

        final AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
        tx.translate(0, -height);

        final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        image = op.filter(image, null);

        final int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
        texture = BufferUtils.createIntBuffer(pixels.length);
        texture.put(pixels);
        texture.rewind();

    }


    public void init() {

        GL11.glEnable(GL11.GL_TEXTURE_2D);

        final IntBuffer buffer = BufferUtils.createIntBuffer(1);
        GL11.glGenTextures(buffer);
        id = buffer.get(0);

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, texture);

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    }


    public void bind() {

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);

    }


    public void unbind() {

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    }


}

Im really new to lwjgl development, and want to know which version is better. 我真的是lwjgl开发的新手,并且想知道哪个版本更好。 Cause im a friend of implementing such things by myself, i want the lwjgl.jar to be the own library im using. 使我成为自己实现此类事情的朋友,我希望lwjgl.jar成为我正在使用的自己的库。

I read on different sites, the buffer.flip() method would be necassary. 我在不同的站点上阅读,buffer.flip()方法是不必要的。 but why? 但为什么? And why the second version doesnt do this? 为什么第二个版本不这样做呢? Also, i want to understand the difference between this two implementations, what happens in the first and what in the second? 另外,我想了解这两种实现之间的区别,第一种和第二种会发生什么?

Thank you! 谢谢!

Both of those are pretty bad implementations IMO. 两者都是非常糟糕的IMO实现。 I would recommend watching this video for a more standard approach. 我建议您观看视频,以了解更标准的方法。 Although you would have to also use the PNGDecoder.jar library it is like an extension to the lwjgl library. 尽管您还必须使用PNGDecoder.jar库,但这就像是lwjgl库的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM