为什么纹理显示错误的颜色

时间:2015-03-24 07:43:15

标签: java opengl jogl

我正在渲染一个带有立方体纹理的天空盒。但是纹理的颜色被证明是错误的。下面的代码用于加载图像。那是因为GL_RGB格式吗?有关于此的任何想法吗?

protected void loadImageData()
{
    String fileDir;

    for (int i = 0; i < 6; i++)
    {
        fileDir = "images/skybox/sky" + (i + 1) + ".jpg";

        try
        {
            BufferedImage image = ImageIO.read(new File(fileDir));
            byte[] data = ((DataBufferByte) image.getData().getDataBuffer()).getData();
            imageBuff[i] = GLBuffers.newDirectByteBuffer(data);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

设置纹理:

    loadImageData();

    gl.glGenTextures(1, cubeTexBuff);
    gl.glBindTexture(GL4.GL_TEXTURE_CUBE_MAP, cubeTexBuff.get(0));

    gl.glTexStorage2D(GL4.GL_TEXTURE_CUBE_MAP, 0, GL4.GL_RGB8, imageSize, imageSize);

    for (int i = 0; i < 6; i++)
    {
        gl.glTexImage2D(GL4.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL4.GL_RGB8, 
                imageSize, imageSize, 0, GL4.GL_RGB, GL4.GL_UNSIGNED_BYTE, imageBuff[i]);
    }

    gl.glTexParameteri(GL4.GL_TEXTURE_CUBE_MAP, GL4.GL_TEXTURE_MAG_FILTER, GL4.GL_LINEAR);
    gl.glTexParameteri(GL4.GL_TEXTURE_CUBE_MAP, GL4.GL_TEXTURE_MIN_FILTER, GL4.GL_LINEAR);

原始图片: original image

映射结果: result with wrong color

更新

提供了这个替代方案:enter link description here最终颜色变得正确,但仍然是一个小问题(方向相反),请看下面的结果:

    String fileDir;
    TextureData texData = null;

    for (int i = 0; i < 6; i++)
    {
        fileDir = "images/skybox/sky" + (i + 1) + ".jpg";

        try
        {
            texData = TextureIO.newTextureData(gl.getGLProfile(), 
                    new File(fileDir), false, TextureIO.JPG);

            texDataBuff[i] = texData.getBuffer();
            texDataBuff[i].rewind();

            //notice that the image used for skybox must be square
            imageSize = texData.getHeight();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    gl.glGenTextures(1, cubeTexBuff);
    gl.glBindTexture(GL4.GL_TEXTURE_CUBE_MAP, cubeTexBuff.get(0));

    gl.glTexStorage2D(GL4.GL_TEXTURE_CUBE_MAP, 0, GL4.GL_RGB8, imageSize, imageSize);

    for (int i = 0; i < 6; i++)
    {
        gl.glTexImage2D(GL4.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL4.GL_RGB8, 
                imageSize, imageSize, 0, texData.getPixelFormat(), texData.getPixelType(), texDataBuff[i]);
    }

    gl.glTexParameteri(GL4.GL_TEXTURE_CUBE_MAP, GL4.GL_TEXTURE_MAG_FILTER, GL4.GL_LINEAR);
    gl.glTexParameteri(GL4.GL_TEXTURE_CUBE_MAP, GL4.GL_TEXTURE_MIN_FILTER, GL4.GL_LINEAR);

原始图片:

enter image description here

映射结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

你可以这样做,翻转包含

            ImageUtil.flipImageVertically(imageBuff[i]);
            {
                TextureData textureData = AWTTextureIO.newTextureData(gl3.getGLProfile(), textureBuffIm,
                        mipmap);

                gl3.glBindTexture(GL3.GL_TEXTURE_2D, objects[Objects.texture.ordinal()]);
                {
                    int[] alignment = new int[1];
                    gl3.glGetIntegerv(GL3.GL_UNPACK_ALIGNMENT, alignment, 0);
//                System.out.println("alignment[0] "+alignment[0]);
//                System.out.println("textureData.getAlignment() "+textureData.getAlignment());
                    if (alignment[0] != textureData.getAlignment()) {

                        gl3.glPixelStorei(GL3.GL_UNPACK_ALIGNMENT, textureData.getAlignment());
                    }
                    {
                        gl3.glTexImage2D(GL3.GL_TEXTURE_2D, 0, textureData.getInternalFormat(), textureData.getWidth(),
                                textureData.getHeight(), textureData.getBorder(), textureData.getPixelFormat(),
                                textureData.getPixelType(), textureData.getBuffer());
                    }
                    if (alignment[0] != textureData.getAlignment()) {

                        gl3.glPixelStorei(GL3.GL_UNPACK_ALIGNMENT, alignment[0]);
                    }

请记住,AWTTextureIO总是翻转纹理,所以如果你想要正确使用它,你可以先将它翻转,这样AWTTextureIO会将它翻转回来,你将获得原始图像。 如果您仍需要使用imageBuffered

,则可以将其翻转