我試圖給我的應用程序的用戶加載他們選擇的圖像做出背景的能力。通過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);
}
也許你沒有注意到標題,我想用一個Texture對象來實現這一點,正是因爲經過我的研究時間,NEHE啓動了它,它似乎是最新的,最簡單的方法去紋理。而且由於我使用JOGL的經驗還不到一週,所以我很難理解NEHE中正在發生的一切,並且我拒絕複製和粘貼代碼,特別是如果我不理解它的話......但是感謝你的貢獻,真正有幫助 – Ethan
另外我需要接受圖像文件,這些圖像文件是.bmp,.png,.jpg和.gif,NeHe的例子並不包括這些圖像文件。 – Ethan
NeHe的教程*做*使用紋理對象。 –