2.5D引擎,z缓冲无法正常工作

时间:2019-07-16 13:15:47

标签: java 2.5d

我目前正在用Java制作2.5D FPS游戏。而且因为我想制作更多不同的地图,所以我制作了更高的墙,但是我不知道如何更改z缓冲区代码来处理不同高度的墙。较高的块使用与较小的块相同的renderwall方法。

https://imgur.com/a/d75fyJY

您可以看到我的问题是较小的墙壁覆盖了较高的墙壁。

public void renderWall1(double x0,double y0,double x1,double y1,int tex,double xt0,double xt1,             双高1){

    double xc0 = ((x0 - 0.5) - xCam) * 2;
    double yc0 = ((y0 - 0.5) - yCam) * 2;
    double xx0 = xc0 * rCos - yc0 * rSin;
    double u0 = ((-height1 - 0.5) - zCam1) * 2;
    double l0 = ((+0.5 - height1) - zCam1) * 2;
    double zz0 = yc0 * rCos + xc0 * rSin;
    double xc1 = ((x1 - 0.5) - xCam) * 2;
    double yc1 = ((y1 - 0.5) - yCam) * 2;
    double xx1 = xc1 * rCos - yc1 * rSin;
    double u1 = ((-height1 - 0.5) - zCam1) * 2;
    double l1 = ((+0.5 - height1) - zCam1) * 2;
    double zz1 = yc1 * rCos + xc1 * rSin;

    xt0 *= size * 2;
    xt1 *= size * 2;

    double zClip = 0.2;

    if (zz0 < zClip && zz1 < zClip)
        return;

    if (zz0 < zClip) {
        double p = (zClip - zz0) / (zz1 - zz0);
        zz0 = zz0 + (zz1 - zz0) * p;
        xx0 = xx0 + (xx1 - xx0) * p;
        xt0 = xt0 + (xt1 - xt0) * p;
    }

    if (zz1 < zClip) {
        double p = (zClip - zz0) / (zz1 - zz0);
        zz1 = zz0 + (zz1 - zz0) * p;
        xx1 = xx0 + (xx1 - xx0) * p;
        xt1 = xt0 + (xt1 - xt0) * p;
    }

    double xPixel0 = xCenter - (xx0 / zz0 * fov);
    double xPixel1 = xCenter - (xx1 / zz1 * fov);

    if (xPixel0 >= xPixel1)
        return;
    int xp0 = (int) Math.ceil(xPixel0);
    int xp1 = (int) Math.ceil(xPixel1);
    if (xp0 < 0)
        xp0 = 0;
    if (xp1 > width)
        xp1 = width;

    double yPixel00 = (u0 / zz0 * fov + yCenter);
    double yPixel01 = (l0 / zz0 * fov + yCenter);
    double yPixel10 = (u1 / zz1 * fov + yCenter);
    double yPixel11 = (l1 / zz1 * fov + yCenter);

    double iz0 = 1 / zz0;
    double iz1 = 1 / zz1;

    double iza = iz1 - iz0;

    double ixt0 = xt0 * iz0;
    double ixta = xt1 * iz1 - ixt0;
    double iw = 1 / (xPixel1 - xPixel0);

    for (int x = xp0; x < xp1; x++) {
        double pr = (x - xPixel0) * iw;
        double iz = iz0 + iza * pr;

        int xTex = (int) ((ixt0 + ixta * pr) / iz);

        double yPixel0 = yPixel00 + (yPixel10 - yPixel00) * pr - 0.5;
        double yPixel1 = yPixel01 + (yPixel11 - yPixel01) * pr;

        int yp0 = (int) Math.ceil(yPixel0);
        int yp1 = (int) Math.ceil(yPixel1);
        if (yp0 < 0)
            yp0 = 0;
        if (yp1 > height)
            yp1 = height;

        double ih = 1 / (yPixel1 - yPixel0);
        for (int y = yp0; y < yp1; y++) {
            double pry = (y - yPixel0) * ih;
            int yTex = (int) (size * 2 * pry);

            if (zBufferWall[x] > iz)
                continue;

            zBufferWall[x] = iz;

            int color = Art.walls.pixels[((xTex) + (tex % size) * size * 2)
                    + (yTex + tex / size * size * 2) * sheetSize];

            if (color != 0xffff00ff) {
                pixels[x + y * width] = color;
                zBuffer[x + y * width] = 1 / iz * 4;
            }
        }
    }
}

0 个答案:

没有答案