JOGL 的纹理加载
我一直在尝试加载 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
这是什么意思,我该如何解决它?
我尝试:
- 使用
\
和\\
和/
和//
- 更改照片的路径并拍摄从
c:\
到photoname.bmp
的所有路径都 - 使用
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:
- use
\
and\\
and/
and//
- change the path of the photo and take all the path from
c:\
to thephotoname.bmp
- rename the photo using numbers like
1.bmp
None worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您最新的评论来看,您不再收到 IOException,但在获取纹理实际渲染时仍然遇到问题(只是得到一个白色方块)。
我注意到以下内容不在您在此处发布的代码中(但可能在其他地方):
您需要 在绑定纹理之前为纹理生成 位置。另外,请确保您已启用纹理:
有关 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):
You need to generate places for your textures before binding them. Also, make sure you have enabled texturing:
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.
可能不再需要这方面的帮助,但我注意到 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.
这是在 JOGL 中加载纹理的简单方法。它也适用于 BMP。
也不要忘记启用和绑定,并设置纹理坐标。
Here is a simple way of loading a texture in JOGL. It works with BMP as well.
also dont forget to enable and bind, and set texture-coordinates.