我应该如何在3d引擎中实现视图剪裁平面?

ogsagwnx  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(319)

这个项目完全是用java从头开始编写的。从科维德开始我就觉得无聊,所以我想要一些能占用我时间的东西,教我一些酷的东西。不过,我已经在这个问题上纠结了大约一个星期了。当我尝试使用我的近平面剪裁方法时,它会将新的顶点倾斜到屏幕的另一侧,但有时效果很好。
故障截图
成功截图
所以我的想法可能是,因为它有时工作,我只是没有做剪辑在正确的时间在管道中?
我从面部剔除和照明开始,
然后对顶点应用摄影机视图变换,
然后我夹在附近的飞机上
最后,我应用投影矩阵和剪辑任何剩余的屏幕外三角形
代码:
这将计算交点。抱歉,如果它是混乱或太长,我不是很有经验的编码,我的专业是物理,而不是计算机科学。

public Vertex vectorIntersectPlane(Vector3d planePos, Vector3d planeNorm, Vector3d lineStart, Vector3d lineEnd){

    float planeDot = planeNorm.dotProduct(planePos);
    float startDot = lineStart.dotProduct(planeNorm);
    float endDot = lineEnd.dotProduct(planeNorm);
    float midPoint = (planeDot - startDot) / (endDot - startDot);

    Vector3d lineStartEnd = lineEnd.sub(lineStart);
    Vector3d lineToIntersect = lineStartEnd.scale(midPoint);

    return new Vertex(lineStart.add(lineToIntersect));
}

public float distanceFromPlane(Vector3d planePos, Vector3d planeNorm, Vector3d vert){

    float x = planeNorm.getX() * vert.getX();
    float y = planeNorm.getY() * vert.getY();
    float z = planeNorm.getZ() * vert.getZ();

    return (x + y + z - (planeNorm.dotProduct(planePos)));
}

//When a triangle gets clipped it has 4 possible outcomes
// 1 it doesn't actually need clipping and gets returned
// 2 it gets clipped into 1 new triangle, for testing these are red
// 3 it gets clipped into 2 new triangles, for testing 1 is green, and 1 is blue
// 4 it is outside the view planes and shouldn't be rendered
public void clipTriangles(){

    Vector3d planePos = new Vector3d(0, 0, ProjectionMatrix.fNear, 1f);
    Vector3d planeNorm = Z_AXIS.clone();

    final int length = triangles.size();

    for(int i = 0; i < length; i++) {

        Triangle t = triangles.get(i);

        if(!t.isDraw())
            continue;

        Vector3d[] insidePoint = new Vector3d[3];
        int insidePointCount = 0;

        Vector3d[] outsidePoint = new Vector3d[3];
        int outsidePointCount = 0;

        float d0 = distanceFromPlane(planePos, planeNorm, t.getVerticesVectors()[0]);
        float d1 = distanceFromPlane(planePos, planeNorm, t.getVerticesVectors()[1]);
        float d2 = distanceFromPlane(planePos, planeNorm, t.getVerticesVectors()[2]);

        //Storing distances from plane and counting inside outside points
        {
            if (d0 >= 0){

                insidePoint[insidePointCount] = t.getVerticesVectors()[0];
                insidePointCount++;
            }else{

                outsidePoint[outsidePointCount] = t.getVerticesVectors()[0];
                outsidePointCount++;
            }
            if (d1 >= 0){

                insidePoint[insidePointCount] = t.getVerticesVectors()[1];
                insidePointCount++;
            }else{

                outsidePoint[outsidePointCount] = t.getVerticesVectors()[1];
                outsidePointCount++;
            }
            if (d2 >= 0){

                insidePoint[insidePointCount] = t.getVerticesVectors()[2];
                insidePointCount++;
            }else{

                outsidePoint[outsidePointCount] = t.getVerticesVectors()[2];
            }
        }

        //Triangle has 1 point still inside view, remove original triangle add new clipped triangle
        if (insidePointCount == 1) {

            t.dontDraw();

            Vertex newVert1 = vectorIntersectPlane(planePos, planeNorm, insidePoint[0], outsidePoint[0]);
            Vertex newVert2 = vectorIntersectPlane(planePos, planeNorm, insidePoint[0], outsidePoint[1]);
            vertices.add(newVert1);
            vertices.add(newVert2);

            //Triangles are stored with vertex references instead of the actual vertex object. 
            Triangle temp = new Triangle(t.getVertKeys()[0], vertices.size() - 2, vertices.size() - 1, vertices);
            temp.setColor(1,0,0, t.getBrightness(), t.getAlpha());
            triangles.add(temp);

            continue;
        }

        //Triangle has two points inside remove original add two new clipped triangles
        if (insidePointCount == 2) {

            t.dontDraw();

            Vertex newVert1 = vectorIntersectPlane(planePos, planeNorm, insidePoint[0], outsidePoint[0]);
            Vertex newVert2 = vectorIntersectPlane(planePos, planeNorm, insidePoint[1], outsidePoint[0]);
            vertices.add(newVert1);
            vertices.add(newVert2);

            Triangle temp = new Triangle(t.getVertKeys()[0], t.getVertKeys()[1], vertices.size() - 1, vertices);
            temp.setColor(0, 1, 0, t.getBrightness(), t.getAlpha());
            triangles.add(temp);

            temp = new Triangle(t.getVertKeys()[0], t.getVertKeys()[1], vertices.size() - 2, vertices);
            temp.setColor(0, 0, 1, t.getBrightness(), t.getAlpha());
            triangles.add(temp);

            continue;
        }
    }
}
3hvapo4f

3hvapo4f1#

我发现了问题,新裁剪的三角形没有得到正确的顶点引用。他们只是被赋予三角形的第一个顶点,不管它是否在视图中。

相关问题