颜色数据未正确存储

时间:2015-12-05 23:22:21

标签: java rendering bufferedimage

我正在尝试制作一个渲染系统,其深度图涉及处理alpha的常用像素。我的问题是没有正确设置颜色!我曾尝试使用System.out.println进行调试并测试各种组件,但无济于事我没有找到解决方案。

变量 处理像素的绘制,设置和清除所涉及的变量包括:private int[][] nodeprivate int[] pixelsprivate ArrayList<Integer> changedPixels

private int[][] node处理存储像素并处理深度[深度] [x + y *宽度],然后转移到BufferedImage像素。数据设置为清晰的黑色,最低深度设置为完全可见的黑色。

private int[] pixels是来自BufferedImage的数据来更改它,它是每次使用的唯一图像!默认情况下,所有数据都是完全可见的黑色

private ArrayList<Integer> changedPixels处理最后一帧中的像素,以便在不需要时不清除整个屏幕来帮助提升FPS。默认情况下为空,因为没有像素从前一帧更改。

方法 我有几种渲染系统的方法:setNode(int x, int y, int z, int color, int alphadrawScreen()clearScreen()。我还有一个绘图矩形和精灵功能,通过调用setNode()方法添加颜色来处理添加像素。

private void setNode(int x, int y, int z, int color, float alpha)
{
    color = Pixel.getColor(alpha, color);

    if (translate) // Move the pixel to  the correct location
    {
        x -= transX;
        y -= transY;
    }

    if (x < 0 || x >= width || y < 0 || y >= height || alpha <= 0.0f || nodeMap[z][x + y * width] == color) // Check if we need to draw the pixel
        return;

    for (int zz = z + 1; zz < maxDepth; zz++)
        if (Pixel.getAlpha(nodeMap[zz][x + y * width]) >= 1)
            return;

    if (alpha < 1.0f) // If pixel isn't completely opaque, then set it's alpha to the given one
        if (nodeMap[z][x + y * width] != color) // If color isn't equal to the one we supply, change it up correctly
            color = Pixel.getColorBlend(color, nodeMap[z][x + y * width]);

    if (color == Pixel.WHITE) System.out.println("Pixel is white at x: " + x + ", y: " + y);
    nodeMap[z][x + y * width] = color;
}

public void drawScreen()
{
    int color = clearColor;

    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
        {
            for (int z = maxDepth - 1; z > 0; z--)
            {
                if (Pixel.getAlpha(nodeMap[z][x + y * width]) > 0f)
                    color = Pixel.getColorBlend(color, nodeMap[z][x + y * width]);

                if (Pixel.getAlpha(color) >= 1f)
                    break;
            }

            if (pixels[x + y * width] != color)
            {
                pixels[x + y * width] = color;
                changedPixels.add(x + y * width);
            }
        }
}


public void clearScreen()
{
    for (Integer pixel : changedPixels)
    {
        for (int z = 0; z < maxDepth; z++)
        {
            if (z > 0)
                nodeMap[z][pixel] = clearColor;
            else
                nodeMap[z][pixel] = bgColor;
        }
    }

    changedPixels.clear();
}

public void drawRect(int offX, int offY, int z, int width, int height, int color)
{
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            setNode(x + offX, y + offY, z, color);
}

public static int getColorBlend(int color1, int color2)
{
    float a1 = getAlpha(color1);
    float a2 = getAlpha(color2);

    float a = Math.max(a1, a2);
    float r = ((getRed(color1) * a1) + (getRed(color2) * a2 * (1 - a1))) / a;
    float g = ((getGreen(color1) * a1) + (getGreen(color2) * a2 * (1 - a1))) / a;
    float b = ((getBlue(color1) * a1) + (getBlue(color2) * a2 * (1 - a1))) / a;

    return Pixel.getColor(a, r, g, b);
}

测试 我目前所做的是初始化渲染系统并将nodeMap和像素映射设置为前面提到的设置。完成此操作后,游戏引擎开始,然后是gui按钮中的方法(您可能需要它),但它会调用drawRect(0(x), 0(y), 1(z), 100(width), 20(height), Pixel.WHITE(color)),因为我测试它是否正在运行该方法以及它正在绘制哪些像素到。

问题 整体问题是屏幕是完全白色的,我无法弄清楚原因!我知道它与alpha混合没有任何关系,因为我已经使用了以前版本的渲染系统,它可以正常工作。

任何帮助都表示赞赏和抱歉,这是一个很长的问题,我只是想确保你拥有所需的一切,帮助我解决这个问题。我确实意识到这不是很有效,但我仍然喜欢这个系统。再次感谢!

0 个答案:

没有答案