0

我试图在我的 lwjgl 窗口中显示一个 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);

片段着色器:

#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)

我猜我做错了很多事情。如果您觉得我可以提供更多信息,请在评论部分发表。谢谢!

PS我说纹理是手动加载的原因是因为我习惯于使用 Slick-Util 来加载纹理,但是我无法将它用于这种特定的纹理,因为我听说 Slick-Util 不支持 npot 纹理。

4

1 回答 1

1

您以错误的顺序将纹素推送到缓冲区。

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循环。

于 2013-10-18T08:16:04.650 回答