Newer
Older
EPQ-3D-renderer / src / Face.java
@cory cory on 9 Nov 2022 1 KB Implementation of:
import java.security.cert.TrustAnchor;
import java.util.ArrayList;

public class Face {
    PointComp[] points;
    Vector3D normal;

    public void rotate(Matrix camMatrix){
        for (PointComp point:
             points){
            camMatrix.multiplyPoint3to(point.point, point.rotatedPoint);
        }
    }
    public void project(int FPDis, int scrX, int scrY){
        for (PointComp point:
                points){
            point.rotatedPoint.projectOn(FPDis, scrX, scrY, point.projectedPoint);
        }
    }
    public void separateTris(){

    }
    public void calculateNormal(){
        // too many new variables
        Point3D point0 = points[0].point;
        Point3D point1 = points[1].point;
        Point3D point2;
        Vector3D vec1 = new Vector3D(point1.x - point0.x, point1.y - point0.y, point1.z - point0.z);
        Vector3D vec2 = new Vector3D(0,0,0); // initialisation otherwise intellij gets mad
        // find a vector which is not inline with other vectors
        boolean valid = false; int i = 2;
        while(!valid && i < points.length) {
            point2 = points[i].point;
            vec2 = new Vector3D(point2.x - point0.x, point2.y - point0.y, point2.z - point0.z);
            double angle = Math.abs(vec1.angleTo(vec2));
            if(angle > 0.1 && angle < 2*Math.PI - 0.1){
                //  if the angle between the vectors is between a threshold, the two vectors are valid.
                // else, calculate the second vector using a different set of points.
                valid = true;
            }}
        if(!valid){throw new RuntimeException("Could not calculate normal of face");}
        normal = new Vector3D(
                vec1.y*vec2.z - vec1.z*vec2.y,
                vec1.z*vec2.x - vec1.x*vec2.z,
                vec1.x*vec2.y - vec1.y*vec2.x);
    }
}