2013-10-18 41 views
0

我試圖在lwjgl窗口中顯示npot紋理。結果是這樣的: enter image description hereLWJGL - 「手動加載」NPOT紋理着色器

紋理重複4次,顛倒以及水平線扭曲。顯然這不是預期的結果。這裏是什麼,我覺得是相關的源代碼:

// load the image 
BufferedImage image = null; 
try { 
    image = ImageIO.read(new File(path)); 
} 
// exit on error 
catch (IOException exception) { 
    Utility.errorExit(exception); 
} 
// add the image's data to a bytebuffer 
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); 
for(int x = 0; x < image.getWidth(); x++) { 
    for(int y = 0; y < image.getHeight(); y++) { 
     int pixel = image.getRGB(x, y); 
     buffer.put((byte) ((pixel >> 16) & 0xFF)); // red 
     buffer.put((byte) ((pixel >> 8) & 0xFF)); // green 
     buffer.put((byte) (pixel & 0xFF));   // blue 
     buffer.put((byte) 0xFF);     // alpha 
    } 
} 
// flip the buffer 
buffer.flip(); 
// generate and bind the texture 
int handle = GL11.glGenTextures(); 
GL11.glBindTexture(GL31.GL_TEXTURE_RECTANGLE, handle); 
//Setup wrap mode 
GL11.glTexParameteri(GL31.GL_TEXTURE_RECTANGLE, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); 
GL11.glTexParameteri(GL31.GL_TEXTURE_RECTANGLE, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); 

//Setup texture scaling filtering 
GL11.glTexParameteri(GL31.GL_TEXTURE_RECTANGLE, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); 
GL11.glTexParameteri(GL31.GL_TEXTURE_RECTANGLE, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); 
// set the texture data 
GL11.glTexImage2D(GL31.GL_TEXTURE_RECTANGLE, 0, GL11.GL_RGBA8, image.getWidth(), image.getHeight(), 0, 
     GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); 
// return the handle 
return handle; 

實用的方法來紋理綁定到採樣器:

加載紋理

實用方法

// set the sampler's texture unit GL20.glUniform1i(samplerLocation, GL13.GL_TEXTURE0 + textureUnit); // bind the texture to the texture unit GL13.glActiveTexture(GL13.GL_TEXTURE0 + textureUnit); GL11.glBindTexture(GL31.GL_TEXTURE_RECTANGLE, textureID); 

Fragment着色器:

#version 150 
#extension GL_ARB_texture_rectangle : enable 

uniform sampler2DRect sampler; 

in vec2 vTexture; 
out vec4 color; 

void main() 
{ 
    color = texture2DRect(sampler, vTexture); 
} 

的最後一條信息,我覺得會是有關什麼是我的紋理座標是:

  • 左下點:(0,0)
  • 左上點:(0,600)
  • 右上點:(800,600)
  • 右下點(800,0)

我在猜測我做了多件事情錯誤。如果您覺得我可以提供更多信息,請在評論部分發帖。謝謝!

附:我說紋理是手動加載的原因是因爲我習慣於使用Slick-Util來加載紋理,但是我無法將它用於此特定紋理,因爲我聽到Slick-Util不支持npot紋理。

回答

1

您正在按照錯誤的順序將texels推送到緩衝區。

ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); 
for(int x = 0; x < image.getWidth(); x++) { 
    for(int y = 0; y < image.getHeight(); y++) { 
     int pixel = image.getRGB(x, y); 
     buffer.put((byte) ((pixel >> 16) & 0xFF)); // red 
     buffer.put((byte) ((pixel >> 8) & 0xFF)); // green 
     buffer.put((byte) (pixel & 0xFF));   // blue 
     buffer.put((byte) 0xFF);     // alpha 
    } 
} 

您正在迭代內部循環的高度。 glTexImage2D預計數據是基於掃描線的,而不是基於列的。因此,請嘗試更換您的xy循環。

+0

是的,這樣做!非常感謝。 – Aaron