LWJGL textures rendering as single color

for the past hour or so I've been trying to fix a bug that makes it so that the textures which I attempt to render are just single colors. The odd thing is that this method of loading textures has worked perfectly fine for me before with no problems like this. Here's how I load the texture (From a 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;

}

And this is how the texture is drawn to the screen:

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

The above code is within a display list. Any idea what may be causing the quads to render as a single color?


You have 2 mistakes.

First, you cannot mix functions like glEnable and glVertex2i. glVertex2i and glTexCoord2f must be between glBegin and gEnd.

Second, once you call glVertex2i the vertex will have the texture states that have been previously specified.

So I believe your code should look like

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);
链接地址: http://www.djcxy.com/p/34026.html

上一篇: LWJGL:纹理使用背景颜色进行渲染

下一篇: LWJGL纹理渲染为单色