简体   繁体   English

Java图像作为ByteBuffer用于OpenGL纹理映射

[英]Java image as ByteBuffer for OpenGL texture mapping

I'm using openGL and trying to get my head around texture mapping. 我正在使用openGL,并试图绕过纹理映射。

At the moment, I have the following to add an image that I will use: 目前,我有以下几点要添加的图像:

GL11.glBindTexture(GL11.GL_TEXTURE_2D, 1);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GL11.glTexParameteri (GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
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.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);

GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, 32, 32, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, "imageData");

Where I have the string "imageData" I need a ByteBuffer for my image. 在有字符串"imageData" ,我的图像需要一个ByteBuffer。 How do I get this? 我怎么得到这个? The image is in my src folder in Eclipse. 该图像位于Eclipse中的src文件夹中。

This is untested but looks like it would work 这未经测试,但看起来可以正常工作

ByteBuffer buffer = ByteBuffer.wrap(getBytesFromFile(filename));

There's an implementation of getBytesFromFile at http://www.exampledepot.com/egs/java.io/File2ByteArray.html http://www.exampledepot.com/egs/java.io/File2ByteArray.html上有getBytesFromFile的实现

EDIT: Okay that example is really gone now, so here's an implementation of reading to a ByteBuffer 编辑:好的,这个例子现在真的不见了,所以这是读取ByteBuffer的实现

private static ByteBuffer readToBuffer(String filename) throws IOException
{
    File file = new File(filename);
    ByteBuffer bb = ByteBuffer.allocate((int) file.length());
    FileInputStream fis = new FileInputStream(filename);

    int bytesRead = 0;
    byte[] buf = new byte[BYTES_PER_READ];

    while (bytesRead != -1)
    {
        bb.put(buf, 0, bytesRead);
        bytesRead = fis.read(buf);
    }

    fis.close();

    return bb;
}

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

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