【问题标题】:LWJGL - Shader for a 'manually loaded' NPOT textureLWJGL - “手动加载” NPOT 纹理的着色器
【发布时间】:2013-10-18 16:16:04
【问题描述】:

我试图在我的 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)

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

P.S. 我之所以说纹理是手动加载的,是因为我习惯于使用 Slick-Util 来加载纹理,但是当我听到 Slick 时,我无法将它用于这种特定的纹理-Util 不支持 npot 纹理。

【问题讨论】:

    标签: opengl glsl shader lwjgl texture-mapping


    【解决方案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 循环。

    【讨论】:

    • 是的,做到了!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多