Java、LWJGL、OpenGL 1.1、将BufferedImage解码为Bytebuffer并跨类绑定(bind)到OpenGL

标签 java opengl lwjgl bufferedimage bytebuffer

我一直在尝试解决这个问题,但没有成功。我有四个类,主类、对象类、 Sprite 类和图像加载器类。

我正在尝试使用图像加载器并使用此处找到的方法加载 png:http://www.java-gaming.org/topics/bufferedimage-to-lwjgl-texture/25516/msg/220280/view.html#msg220280转换为ByteBuffer,然后绑定(bind)到OpenGL。

图像位于其自己的单独资源文件夹中。应该绘制的是:( /image/NrKbd.png ) (32x32),但我看到的是一个白色的盒子,它具有图像的尺寸,但不具有实际的纹理。

如果有人知道我可以去哪里解决这个问题,我将不胜感激。我是 OpenGL 新手,希望避免使用外部库来了解实际代码的工作原理。谢谢。

已更新!

对于以下任何人,我实现了 Vallentin 提供的建议,但是四边形现在采用图像中第一个像素的颜色。我尝试实现 C.S here 提供的图像加载器。但是现在我收到此错误:

javax.imageio.IIOException: Error reading PNG image data
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1291)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1560)
at pImageLoader.loadBIn(pImageLoader.java:60)
at Monkeybars.main(Monkeybars.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Caused by: javax.imageio.IIOException: Destination type from ImageReadParam does not match!
at javax.imageio.ImageReader.getDestination(ImageReader.java:2862)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1263)
... 8 more

Exception in thread "main" java.lang.NullPointerException
at pImageLoader.loadpSprite(pImageLoader.java:75)
at Monkeybars.main(Monkeybars.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我在 Windows 7 上使用 IntelliJ 12,使用 Java 6。我尝试加载的图像链接在上面。如果有人有任何想法,我愿意接受建议。

主要 block :

   public static void main(String[] args){
    System.out.println("Sup!");
    int width = 800 ;
    int height = 600;
    try{
        Display.setDisplayMode(new DisplayMode(width,height));
        Display.setTitle("The Playground");
        Display.create();
    }catch (LWJGLException e){
        e.printStackTrace();
        Display.destroy();
        System.exit(1);
    }


    //Initialize OpenGL
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, width, height, 0f, 1f, -1f);

    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glEnable(GL11.GL_BLEND);



    pObject MrRedSquare = new pObject(300f,300f,pImageLoader.loadpSprite(pImageLoader.loadBIn("test.png")));

    while(!Display.isCloseRequested()){

        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        MrRedSquare.drawMe();
        Display.sync(60);
        Display.update();
    }
}
}

纹理加载器,新方法。

public static BufferedImage loadBIn(String filepath){

    BufferedImage image;
    // get the reader
    ImageReader ir = ImageIO.getImageReadersByFormatName("png").next();

    // get the default param
    ImageReadParam p = ir.getDefaultReadParam();
    p.setDestinationType(
            // define the image type to return if supported
            ImageTypeSpecifier.createInterleaved(
                    ColorSpace.getInstance(ColorSpace.CS_sRGB),
                    new int[] {0, 1, 2, 3},    // <-- the order of the color bands to return so the bytes are in the desired order
                    DataBuffer.TYPE_BYTE,
                    true, false)
    );

    try{
        InputStream stream = pImageLoader.class.getClassLoader().getResourceAsStream(filepath);
        ImageInputStream imageStream = ImageIO.createImageInputStream(stream);
        ir.setInput(imageStream);
        image = ir.read(0, p);
    }catch(Exception e){
        System.out.print("IMAGELOADER CANNOT OBTAIN ASSET");
        e.printStackTrace();
        return null;

    }
    return image;

}

较旧的代码,保留用于存档目的

主要:

public static void main(String[] args){
    System.out.println("Sup!");
    try{
        Display.setDisplayMode(new DisplayMode(800,600));
        Display.setTitle("The Playground");
        Display.create();
    }catch (LWJGLException e){
        e.printStackTrace();
        Display.destroy();
        System.exit(1);
    }


    //Initialize OpenGL
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 800, 0, 600, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);


    pObject MrRedSquare = new pObject(300,300,pImageLoader.loadpSprite(pImageLoader.loadBI("square.png")));

    while(!Display.isCloseRequested()){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        MrRedSquare.drawMe();
        Display.sync(60);
        Display.update();
    }
}

对象(绘制代码):

    public void drawMe(){

    // store the current model matrix
    GL11.glPushMatrix();

    // bind to the appropriate texture for this sprite
    texture.bind();

    // translate to the right location and prepare to draw
    GL11.glTranslatef(x, y, 0);

    // draw a quad textured to match the sprite
    GL11.glBegin(GL11.GL_QUADS);
    {
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(0, 0);

        GL11.glTexCoord2f(0, texture.getHeight());
        GL11.glVertex2f(0, height);

        GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
        GL11.glVertex2f(width,height);

        GL11.glTexCoord2f(texture.getWidth(), 0);
        GL11.glVertex2f(width,0);
    }
    GL11.glEnd();

    // restore the model view matrix to prevent contamination
    GL11.glPopMatrix();

} 

纹理:

public class pSprite {

protected ByteBuffer spriteData;
protected BufferedImage spriteImage;
protected int id;

protected int width;
protected int height;

//---------------------------------------------------------------------------------------
//Constructors:

pSprite(BufferedImage sI, ByteBuffer s, int i){
    spriteImage = sI;
    spriteData = s;
    id = i;

    width = spriteImage.getWidth();
    height = spriteImage.getHeight();
}
//---------------------------------------------------------------------------------------
//Methods:
public void bind(){
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
} 

图像加载器:

public class pImageLoader {

public static BufferedImage loadBI(String filepath){
    BufferedImage image;
    try{
        InputStream input = pImageLoader.class.getResourceAsStream(filepath);
        image = ImageIO.read(input);
    }catch (Exception e){
        System.out.print("IMAGELOADER: Cannot obtain asset.");
        e.printStackTrace();
        return null;
    }
    return image;
}

public static pSprite loadpSprite(BufferedImage image){

    //http://www.java-gaming.org/topics/bufferedimage-to-lwjgl-texture/25516/msg/220280/view.html#msg220280

    int pixels[] = new int[image.getWidth() * image.getHeight()];
    image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
    ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); // <-- 4 for RGBA, 3 for RGB

    for(int y = 0; y < image.getHeight(); y++){
        for(int x = 0; x < image.getWidth(); x++){
            int pixel = pixels[y * image.getWidth() + x];
            buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
            buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
            buffer.put((byte) (pixel & 0xFF));               // Blue component
            buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
        }
    }

    buffer.flip();

    int textureID = glGenTextures(); //Generate texture ID
    glBindTexture(GL_TEXTURE_2D, textureID);

    // Setup wrap mode
    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);

    //Setup texture scaling filtering
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //Send texel data to OpenGL
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    return new pSprite(image, buffer, textureID);
    }
}

最佳答案

您的代码存在很多问题。我发现的最重要的问题是:

1) 你的问题,首先是你没有使用 32 位图像(即 RGBA),而是使用 24 位图像(即 RGB)。

所以您用于指定图像类型的代码是错误的。您应该使用以下内容:

p.setDestinationType(
    // define the image type to return if supported
    ImageTypeSpecifier.createInterleaved(
                    ColorSpace.getInstance(ColorSpace.CS_sRGB),
                    new int[] {0, 1, 2}, // <-- you are expecting 3 color bands since your image is RGB 
                    DataBuffer.TYPE_BYTE,
                    false,   //<-- this is alpha, your image doesn't have any
                    false)
);

2) 根据图像转换过程的注释说明,您应该使用大小为 width * height * 3ByteBuffer >。一般来说,这是事实。然而,似乎 LWJGL 不会让你这样做(它强制矩形尺寸?)所以你应该将此值保留为 4 并修改你的 alpha,如下所示:

buffer.put((byte) 0xFF);    // alpha always 1.0 i.e. opaque

这应该正确加载您的图像。请注意,原始代码中的另一个问题(除了忘记使用 glEnable(GL_TEXTURE_2D))是您没有设置混合函数(顺便说一句,一旦不需要设置,您可以设置它)它在每一帧中)。因此,即使您使用了 32 位纹理,您的透明区域也会显示为黑色。

3) 你的第三个问题是,当你给出纹理坐标时,你使用 glTexCoord2f(texture.getWidth(),texture.getHeight()); 这是错误,因为纹理坐标的范围可以是 [0, 1]。唯一可行的方法是,如果您为 GL_TEXTURE_WRAP_SGL_TEXTURE_WRAP_T 设置了纹理参数,那么它们都使用 GL_REPEAT(默认值) 。但是,您将其设置为GL_CLAMP_TO_EDGE。因此,如果您指定的纹理值大于 1,最终图像将具有图像边框的颜色。

所以你应该像下面这样修改你的代码:

GL11.glBegin(GL11.GL_QUADS);
{
    GL11.glTexCoord2f(0, 0);   // <--
    GL11.glVertex2f(0, 0);

    GL11.glTexCoord2f(0, 1);   // <--
    GL11.glVertex2f(0, height);

    GL11.glTexCoord2f(1, 1);   // <--
    GL11.glVertex2f(width, height);

    GL11.glTexCoord2f(1, 0);   // <--
    GL11.glVertex2f(width,0);
}

我的建议是,当你尝试纹理时,使用一个大的简单图像,这样你就可以看到发生了什么(正确的 Alpha、颠倒等),例如一个带有内部蓝色矩形的红色矩形或类似的东西,这样您就可以确定纹理的问题是什么,而不是“它不起作用”。

希望对您有帮助

关于Java、LWJGL、OpenGL 1.1、将BufferedImage解码为Bytebuffer并跨类绑定(bind)到OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18263275/

相关文章:

c++ - 帧缓冲区在移动时切掉模型的一部分

java - 延迟访问服务器以尽量避开高峰时间

java - 如何在另一个 Activity 中发生的成功事件中使按钮从不可见变为可见

java http请求getRemoteAddr总是返回127.0.0.1

java - 可以使用反射访问方法变量吗?

opengl - GLSL 中的 HLSL cbuffer 等效项

python - glDrawElements 方法不使用索引和顶点进行绘制

java - 地形在水中闪烁|深度缓冲区精度问题?

java - 我在 lwjgl 中的多维数据集无法正确呈现

java - OpenGL - Java - 渲染问题,多边形闪烁和消失