我想加載一個圖像作爲使用LWJGL庫的openGL的紋理。從我發現到目前爲止,我需要將紋理作爲ByteBuffer傳遞給openGL。我現在所擁有的是一些正確加載圖像的代碼,並將其存儲在BufferedImage對象中。問題是,我不知道如何從一個BufferedImage一個字節緩衝區,其中包含了使用正確的格式用OpenGL數據獲取(作爲函數GL11.glTexImage2D輸入())。 非常感謝幫助!如何使用LWJGL加載用作openGL紋理的圖像?
5
A
回答
6
下面是Space Invaders示例中的一個方法,可以根據需要進行操作。 (我認爲)
/**
* Convert the buffered image to a texture
*/
private ByteBuffer convertImageData(BufferedImage bufferedImage) {
ByteBuffer imageBuffer;
WritableRaster raster;
BufferedImage texImage;
ColorModel glAlphaColorModel = new ComponentColorModel(ColorSpace
.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 },
true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
bufferedImage.getWidth(), bufferedImage.getHeight(), 4, null);
texImage = new BufferedImage(glAlphaColorModel, raster, true,
new Hashtable());
// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f, 0f, 0f, 0f));
g.fillRect(0, 0, 256, 256);
g.drawImage(bufferedImage, 0, 0, null);
// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer())
.getData();
imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}
1
當施加作爲紋理是不正確的,這意味着,接受的解決方案可能不會產生相同的結果對各種圖像I使用由Ron但是圖像的顏色上述方案。
試圖修復與色彩的問題,我試圖用原來的BufferedImage
的ColorModel
,它可以通過調用BufferedImage#getColorModel
訪問。但是,它給了我一個例外,原始圖像的ColorModel
與WritableRaster
對象不兼容。
我找了一個解決方案,我發現this之一。我不使用Raster.createInterleavedRaster
創建WritableRaster
,而是使用ColorModel#createCompatibleWritableRaster
。
希望這會有所幫助。這裏是代碼:
public static ByteBuffer load(BufferedImage bufferedImage) {
WritableRaster raster = bufferedImage.getColorModel().createCompatibleWritableRaster
(bufferedImage.getWidth(), bufferedImage.getHeight());
BufferedImage textureImage = new BufferedImage(bufferedImage.getColorModel(), raster,
true, new Hashtable<>());
Graphics graphics = textureImage.getGraphics();
graphics.setColor(new Color(0, 0, 0));
graphics.fillRect(0, 0, 256, 256);
graphics.drawImage(bufferedImage, 0, 0, null);
byte[] data = ((DataBufferByte) textureImage.getRaster().getDataBuffer()).getData();
ByteBuffer imageBuffer = ByteBuffer.allocate(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}
相關問題
- 1. 加載紋理LWJGL
- 2. 使用Cocoa將圖像加載到OpenGL紋理
- 3. 使用OpenCV加載OpenGL紋理
- 4. 使用Boost.GIL加載opengl紋理
- 5. 使用SDL加載OpenGL紋理
- 6. Java - LWJGL加載紋理,僅加載我加載的最新紋理
- 7. OpenGL ES:使用紋理:空白而不是紋理圖像
- 8. 如何使用Qt的OpenGL類加載和綁定紋理?
- 9. 如何在openGL中使用背景圖像的紋理
- 10. opengl es 2.0紋理加載
- 11. LWJGL螺紋紋理
- 12. 使用OpenGL和LWJGL無縫紋理圓柱體
- 13. 如何在THREE.js中將預加載的圖像作爲紋理使用
- 14. 一些圖像將不會加載 - android的openGL紋理
- 15. 繪製紋理不起作用LWJGL
- 16. 的OpenGL使用的紋理
- 17. 使用ByteBuffer作爲OpenGL中的紋理
- 18. OpenGL紋理格式,爲OpenGL創建圖像/紋理數據
- 19. 作爲ByteBuffer用於OpenGL紋理映射的Java圖像
- 20. 將PNG圖像加載到數組並將其用作紋理
- 21. OpenGL:使用FTGL和紋理
- 22. LWJGL/OpenGL禁用平滑紋理當「放大」
- 23. 如何在C中使用GLX_EXT_texture_from_pixmap將X11 Pixmap用作OpenGL紋理?
- 24. 如何將POT圖像轉換爲POT位圖以在紋理上使用OpenGL?
- 25. 你如何在LWJGL中使用非2次冪的紋理?
- 26. 使用C++加載紋理數據(OpenGL ES 1.x)
- 27. OpenGL繪圖紋理
- 28. OpenGL土壤 - 無法加載紋理
- 29. OpenGL ES 2.0加載動態紋理
- 30. Opengl obj物體加載器與紋理
我已經想通了,謝謝:) 我只是標記答案爲答覆的努力 – Bartvbl 2011-04-23 10:04:19