0

在过去的一个小时左右,我一直在尝试修复一个错误,使我尝试渲染的纹理只是单色。奇怪的是,这种加载纹理的方法以前对我来说非常好用,没有这样的问题。这是我加载纹理的方式(来自 BufferedImage):

public static int loadTexture(BufferedImage image)
{

    glEnable(GL_TEXTURE_2D);

    TextureImpl.unbind();

    try
    {

        int[ ] pixels = new int[image.getWidth() * image.getHeight()];
        image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0,
                image.getWidth());

        ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth()
                * image.getHeight() * BYTES_PER_PIXEL);
        for (int y = 0; y < image.getHeight(); y++)
        {
            for (int x = 0; x < image.getWidth(); x++)
            {
                int pixel = pixels[y * image.getWidth() + x];
                buffer.put((byte) ((pixel >> 16) & 0xFF));
                buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
                buffer.put((byte) (pixel & 0xFF));
                buffer.put((byte) ((pixel >> 24) & 0xFF)); 
            }
        }

        buffer.flip();

        int textureID = glGenTextures(); 
        glBindTexture(GL_TEXTURE_2D, textureID); 

        //Setup wrap mode
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                GL12.GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                GL12.GL_CLAMP_TO_EDGE);

        //Setup texture scaling filtering
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        //Send texel data to OpenGL
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(),
                image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        return textureID;

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    glDisable(GL_TEXTURE_2D);

    //Return the texture ID so we can bind it later again
    return 0;

}

这就是将纹理绘制到屏幕上的方式:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, blockTexture.getID());

glVertex2i(offsetX * cubeSize + x * cubeSize, y * cubeSize);
glVertex2i((offsetX * cubeSize + x * cubeSize) + cubeSize,
                        y * cubeSize);
glVertex2i((offsetX * cubeSize + x * cubeSize) + cubeSize,
                        (y * cubeSize) + cubeSize);
glVertex2i(offsetX * cubeSize + x * cubeSize,
                        (y * cubeSize) + cubeSize);

glTexCoord2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);

glDisable(GL_TEXTURE_2D);

上面的代码在显示列表中。知道什么可能导致四边形呈现为单一颜色吗?

4

1 回答 1

1

你有2个错误。

首先,您不能混合使用 glEnable 和 glVertex2i 等函数。glVertex2i 和 glTexCoord2f 必须在 glBegin 和 gEnd 之间。

其次,一旦调用 glVertex2i,顶点将具有先前指定的纹理状态。

所以我相信你的代码应该看起来像

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, blockTexture.getID());

glBegin(...); // you have to specify a primitive type

glTexCoord2f(0.0f, 0.0f);
glVertex2i(offsetX * cubeSize + x * cubeSize, y * cubeSize);

glTexCoord2f(1.0f, 0.0f);
glVertex2i((offsetX * cubeSize + x * cubeSize) + cubeSize,
                        y * cubeSize);

glTexCoord2f(1.0f, 1.0f);
glVertex2i((offsetX * cubeSize + x * cubeSize) + cubeSize,
                        (y * cubeSize) + cubeSize);

glTexCoord2f(0.0f, 1.0f);    
glVertex2i(offsetX * cubeSize + x * cubeSize,
                        (y * cubeSize) + cubeSize);

glEnd();

glDisable(GL_TEXTURE_2D);
于 2013-06-26T01:27:48.130 回答