我一直在尝试加载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.bmp1.bmp这样的数字重命名照片都没起作用。
发布于 2010-01-24 03:46:03
从您最近的评论来看,您不再获得IOException,但在实际渲染纹理方面仍有困难(只获得一个白色方格)。
我注意到以下内容不在您在这里发布的代码中(但可能在其他地方):
gl.glGenTextures 在绑定之前,您需要对纹理进行生成定位。此外,请确保启用了纹理处理:
gl.glEnable(GL.GL_TEXTURE2D);有关开始使用OpenGL纹理的其他信息/教程,我建议阅读NeHe制作: OpenGL第06课。此外,在页面底部,您将找到JOGL示例代码,以帮助您将概念从C转换为Java。
总之,希望这能给我们一些新的想法。
发布于 2010-03-27 01:19:07
在这方面可能不再需要帮助了,但我注意到IOStream扩展了DataInputStream,但是当涉及到实际实现read()时,它被保留为空白。因此,无论如何,您从来没有实际读取到buf的任何东西,这可能解释了为什么您的纹理是空白的,但您没有任何其他问题。
发布于 2012-05-06 22:16:12
以下是在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);
...https://stackoverflow.com/questions/2109046
复制相似问题