2012-06-19 73 views
2

我試圖給我的應用程序的用戶加載他們選擇的圖像做出背景的能力。通過Java加載圖像沒有問題,但我無法將圖像轉換爲紋理....我只是在GLCanvas上出現了一個大灰色框。這是代碼我迄今爲止:使用紋理對象繪製JOGL的基本紋理

//if there's an image to overlay, render it 
    if (renderImage) { 

     gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); 

     if (texture == null && img != null) { 

      texture = TextureIO.newTexture(img, true); 
      texture.enable(); 
      texture.bind(); 
     } 

     gl.glBegin(GL.GL_POLYGON); 
     gl.glNormal3f(0,0,1); 
      gl.glTexCoord2d(-texture.getWidth(), -texture.getHeight()); 
      gl.glVertex2d(-25, -25); 
      gl.glTexCoord2d(-texture.getWidth(), texture.getHeight()); 
      gl.glVertex2d(canvas.getWidth(),0); 
      gl.glTexCoord2d(texture.getWidth(), texture.getHeight()); 
      gl.glVertex2d(canvas.getWidth(), canvas.getHeight()); 
      gl.glTexCoord2d(texture.getWidth(), -texture.getHeight()); 
      gl.glVertex2d(0, canvas.getHeight()); 
     gl.glEnd(); 
     gl.glFlush(); 
    } 
    //otherwise, render "grass" 
    else { 

     gl.glClearColor(0.0f, 0.65f, 0.0f, 0.0f); 

     //Clear buffer and set background color to green (the "grass" on the sides of the intersection) 
     gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); 
    } 
+0

也許你沒有注意到標題,我想用一個Texture對象來實現這一點,正是因爲經過我的研究時間,NEHE啓動了它,它似乎是最新的,最簡單的方法去紋理。而且由於我使用JOGL的經驗還不到一週,所以我很難理解NEHE中正在發生的一切,並且我拒絕複製和粘貼代碼,特別是如果我不理解它的話......但是感謝你的貢獻,真正有幫助 – Ethan

+0

另外我需要接受圖像文件,這些圖像文件是.bmp,.png,.jpg和.gif,NeHe的例子並不包括這些圖像文件。 – Ethan

+0

NeHe的教程*做*使用紋理對象。 –

回答

3

嘗試這種情況:

gl.glBegin(GL.GL_QUADS); 
gl.glNormal3f(0,0,1); 
gl.glTexCoord2d(0.0, 0.0); 
gl.glVertex2d(0.0, 0.0); 
gl.glTexCoord2d(1.0, 0.0); 
gl.glVertex2d(canvas.getWidth(), 0.0); 
gl.glTexCoord2d(1.0, 1.0); 
gl.glVertex2d(canvas.getWidth(), canvas.getHeight()); 
gl.glTexCoord2d(0.0, 1.0); 
gl.glVertex2d(0.0, canvas.getHeight()); 
gl.glEnd(); 

非重複的紋理座標在0.0到1.0範圍內的默認。

+0

它做到了,geeze它只是我的座標.....如果我插在我原來的glBegin(GL.GL_POLYGONS)之間,它也可以。謝謝! – Ethan

+0

沒問題。使用'GL_POLYGON'時要小心,它只支持凸多邊形。 – genpfault

0

我把

if (texture == null && img != null) { 

     texture = TextureIO.newTexture(img, true); 
     texture.enable(); 
     texture.bind(); 
    } 

在嘗試,catch語句。