JOGL 的纹理加载

发布于 2024-08-18 20:09:00 字数 2285 浏览 5 评论 0原文

我一直在尝试加载 bmp 图片以将其用作程序中的纹理我使用了 IOStream 类来扩展 DataInputStream 来读取照片中的像素这段代码基于 C++ 的纹理加载器代码:

//class Data members
public static int BMPtextures[];
public static int BMPtexCount = 30;
public static int currentTextureID = 0;
//loading methode
static int loadBMPTexture(int index, String fileName, GL gl)
    {
        try
        {
            IOStream wdis = new IOStream(fileName);
            wdis.skipBytes(18);
            int width = wdis.readIntW(); 
            int height = wdis.readIntW();
            wdis.skipBytes(28);
            byte buf[] = new byte[wdis.available()];
            wdis.read(buf);
            wdis.close();
            gl.glBindTexture(GL.GL_TEXTURE_2D, BMPtextures[index]);
            gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, width, height, 0, GL.GL_BGR, GL.GL_UNSIGNED_BYTE, buf);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
            currentTextureID = index; 
            return currentTextureID;
    }
        catch (IOException ex)
        {
            // Utils.msgBox("File Error\n" + fileName, "Error", Utils.MSG_WARN);
            return -1;
        }
    }

和 IOStream 代码:

public class IOStream extends DataInputStream {

    public IOStream(String file) throws FileNotFoundException {
        super(new FileInputStream(file));
    }

    public short readShortW() throws IOException {
        return (short)(readUnsignedByte() + readUnsignedByte() * 256);
    }

    public int readIntW() throws IOException {
        return readShortW() + readShortW() * 256 * 256;
    }

    void read(Buffer[] buf) {

    }
}

​​以及调用:

GTexture.loadBMPTexture(1,"/BasicJOGL/src/basicjogl/data/Font.bmp",gl);

调试后我发现,当涉及到这一行时:

IOStream wdis = new IOStream(fileName);

发生了 IOExeption ,它是一个 DispatchException 这是什么意思,我该如何解决它?

我尝试:

  1. 使用 \\\///
  2. 更改照片的路径并拍摄从 c:\photoname.bmp 的所有路径都
  3. 使用 1.bmp 等数字重命名照片,

但没有成功。

I've been trying to load a bmp picture to use it as a texture at my program I've used a IOStream class to extend DataInputStream to read the pixels at the photo with this code based on a texture loader code for C++:

//class Data members
public static int BMPtextures[];
public static int BMPtexCount = 30;
public static int currentTextureID = 0;
//loading methode
static int loadBMPTexture(int index, String fileName, GL gl)
    {
        try
        {
            IOStream wdis = new IOStream(fileName);
            wdis.skipBytes(18);
            int width = wdis.readIntW(); 
            int height = wdis.readIntW();
            wdis.skipBytes(28);
            byte buf[] = new byte[wdis.available()];
            wdis.read(buf);
            wdis.close();
            gl.glBindTexture(GL.GL_TEXTURE_2D, BMPtextures[index]);
            gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, width, height, 0, GL.GL_BGR, GL.GL_UNSIGNED_BYTE, buf);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
            currentTextureID = index; 
            return currentTextureID;
    }
        catch (IOException ex)
        {
            // Utils.msgBox("File Error\n" + fileName, "Error", Utils.MSG_WARN);
            return -1;
        }
    }

and IOStream code :

public class IOStream extends DataInputStream {

    public IOStream(String file) throws FileNotFoundException {
        super(new FileInputStream(file));
    }

    public short readShortW() throws IOException {
        return (short)(readUnsignedByte() + readUnsignedByte() * 256);
    }

    public int readIntW() throws IOException {
        return readShortW() + readShortW() * 256 * 256;
    }

    void read(Buffer[] buf) {

    }
}

and the calling:

GTexture.loadBMPTexture(1,"/BasicJOGL/src/basicjogl/data/Font.bmp",gl);

after debugging I figured out that when it come to this line:

IOStream wdis = new IOStream(fileName);

an IOExeption occurred and it's a DispatchException What's this supposed to mean, and how can I solve it?

I tried to:

  1. use \ and \\ and / and //
  2. change the path of the photo and take all the path from c:\ to the photoname.bmp
  3. rename the photo using numbers like 1.bmp

None worked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女中豪杰 2024-08-25 20:09:00

从您最新的评论来看,您不再收到 IOException,但在获取纹理实际渲染时仍然遇到问题(只是得到一个白色方块)。

我注意到以下内容不在您在此处发布的代码中(但可能在其他地方):

gl.glGenTextures 

您需要 在绑定纹理之前为纹理生成 位置。另外,请确保您已启用纹理:

gl.glEnable(GL.GL_TEXTURE2D);

有关 OpenGL 纹理入门的其他信息/教程,我建议您阅读 NeHe Productions:OpenGL 课程 #06。此外,在页面底部,您将找到 JOGL 示例代码,帮助您将概念从 C 转换为 Java。

不管怎样,希望这能提供一些新的尝试想法。

Judging by your latest comment, you are no longer getting the IOException but are still having troubles getting the texture to actually render (just getting a white square).

I noticed the following are not in the code you posted here (but could be elsewhere):

gl.glGenTextures 

You need to generate places for your textures before binding them. Also, make sure you have enabled texturing:

gl.glEnable(GL.GL_TEXTURE2D);

For additional information / tutorials on getting started with OpenGL texturing, I recommend taking a read of NeHe Productions: OpenGL Lesson #06. Also, down the bottom of the page you will find JOGL sample code to help you convert the concepts from C to Java.

Anyway, hope this gives a few new ideas to try.

你是我的挚爱i 2024-08-25 20:09:00

可能不再需要这方面的帮助,但我注意到 IOStream 扩展了 DataInputStream 但在实际实现 read() 时它被留空。因此,无论您实际上从未将任何内容读入 buf ,这可能可以解释为什么您的纹理是空白的,但您不会遇到任何其他问题。

Probably don't need help on this anymore, but I noticed that IOStream extends DataInputStream but when it comes to actually implementing read() it's been left blank. so regardless you're never actually reading anything into buf which might explain why your texture is blank but you don't get any other problems.

残龙傲雪 2024-08-25 20:09:00

这是在 JOGL 中加载纹理的简单方法。它也适用于 BMP。

public static Texture loadTexture(String file) throws GLException, IOException
{
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(ImageIO.read(new File(file)), "png", os);
    InputStream fis = new ByteArrayInputStream(os.toByteArray());
    return TextureIO.newTexture(fis, true, TextureIO.PNG);
}

也不要忘记启用和绑定,并设置纹理坐标。

...
  gl.glEnableClientState(GL2ES1.GL_TEXTURE_COORD_ARRAY);
  if(myTexture == null)  
    myTexture = loadTexture("filename.png");
  myTexture.enable(gl);
  myTexture.bind(gl);
  gl.glTexCoordPointer(2, GL2ES1.GL_FLOAT, 0, textureCoords);
...

Here is a simple way of loading a texture in JOGL. It works with BMP as well.

public static Texture loadTexture(String file) throws GLException, IOException
{
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(ImageIO.read(new File(file)), "png", os);
    InputStream fis = new ByteArrayInputStream(os.toByteArray());
    return TextureIO.newTexture(fis, true, TextureIO.PNG);
}

also dont forget to enable and bind, and set texture-coordinates.

...
  gl.glEnableClientState(GL2ES1.GL_TEXTURE_COORD_ARRAY);
  if(myTexture == null)  
    myTexture = loadTexture("filename.png");
  myTexture.enable(gl);
  myTexture.bind(gl);
  gl.glTexCoordPointer(2, GL2ES1.GL_FLOAT, 0, textureCoords);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文