2010-01-21 131 views
2

我一直在試圖加載一個bmp圖片作爲我的程序紋理使用IOStream類擴展DataInputStream讀取基於紋理的代碼照片上的像素用於C++加載程序代碼:JOGL紋理加載

//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

無效。

+0

從閱讀這個我不能告訴如果紋理位於jar或文件系統。你能詳細解釋一下嗎? – 2010-01-21 18:16:30

+0

在一個文件系統我創建了一個包「數據」,並添加到它的照片 但改變加載程序代碼後,它的工作原理和加載完美的紋理「我試圖打印圖片的信息,它的工作原理」.. ..但是當它來綁定紋理沒有發生任何事情時,它繪製了一個白色的方塊! – Nour 2010-01-22 09:01:40

+0

嗨,你解決了嗎? – elect 2015-08-07 13:31:20

回答

1

根據您的最新評論判斷,您不再獲得IOException,但仍然遇到讓紋理實際渲染(僅獲得白色正方形)的麻煩。

我注意到以下不在你這裏張貼的代碼(但可能是其他地方):

gl.glGenTextures 

你結合他們之前需要generate的地方爲你的紋理。此外,請確保您已啓用紋理:

gl.glEnable(GL.GL_TEXTURE2D); 

的其他信息/如何開始使用的OpenGL紋理教程,我建議採取的NeHe Productions: OpenGL Lesson #06讀。另外,在頁面的底部,您會發現JOGL示例代碼,以幫助您將概念從C轉換爲Java。

無論如何,希望這給了一些新的想法嘗試。

+0

不幸的是..這是寫在其他地方也是實現行 我問了一些朋友,他們告訴我,這可能是由我的電腦顯卡的硬件問題可能導致? 如果這是問題有無論如何解決呢? – Nour 2010-01-24 11:27:19

+0

@Nour - 假設您運行的是Windows或OSX,您可以嘗試使用OpenGL Extensions查看器來查看您的視頻卡支持的內容:http://www.realtech-vr.com/glview/ 如果您有舊視頻卡我建議嘗試加載尺寸完全爲256 * 256像素的紋理。 – Clinton 2010-01-24 23:41:05

0

可能不需要這方面的幫助了,但我注意到IOStream擴展了DataInputStream,但是在實際實現read()時它一直保留爲空。所以無論你從來沒有真正閱讀buf的東西,這可能解釋爲什麼你的紋理是空白的,但你沒有任何其他問題。

0

以下是在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); 
...