代码之家  ›  专栏  ›  技术社区  ›  Jeff Storey

Java OpenGL混合图像颜色

  •  0
  • Jeff Storey  · 技术社区  · 15 年前

    应绘制图像1作为基础图像。图2应该画在图1的上方。如果图像2是非透明的,它应该替换图像1的内容(不是混合,而是覆盖其中的内容)。只要图像2是透明的,图像1就会显示出来。我试着用下面的代码来实现这一点,但显然我在混合方面做得不正确。

                gl.glEnable(GL.GL_BLEND);
                if (iconTexture1 != null)
                {
                    gl.glEnable(GL.GL_TEXTURE_2D);
                    iconTexture1.bind();
                    double red = (double) fillColor.getRed() / 255.0;
                    double green = (double) fillColor.getGreen() / 255.0;
                    double blue = (double) fillColor.getBlue() / 255.0;
                    gl.glColor4d(red, green, blue, this.getOpacity());
                    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
                    TextureCoords texCoords = iconTexture1.getImageTexCoords();
                    gl.glScaled(width, height, 1d);
                    dc.drawUnitQuad(texCoords);
                }
                if (iconTexture2 != null)
                {
                    gl.glEnable(GL.GL_TEXTURE_2D);
                    iconTexture2.bind();
                    // image2 is all white, so color it here
                    gl.glColor4d(1d, 0d, 0d, 1d);
    
                    // TODO: What blend function should I be using here to allow image 2 to overwrite what is already there?
    
                    TextureCoords texCoords = iconTexture2.getImageTexCoords();
                    gl.glScaled(width, height, 1d);
                    dc.drawUnitQuad(texCoords);
                }
    

    杰夫

    1 回复  |  直到 15 年前
        1
  •  2
  •   DJClayworth    15 年前

    1. 在绘制完图像1之前,不应打开混合功能。这样做之前将混合图像1与任何已经存在。

    处理OpenGL似乎不起作用的东西的一个好方法是去除所有的复杂性,然后一点一点地添加回去。纹理是你最复杂的部分-留到最后。