Newer
Older
EPQ-3D-renderer / src / main / java / uk / org / floop / epq3d / Object3d.java
@cory cory on 2 Feb 2023 3 KB Fix #17
package uk.org.floop.epq3d;

public class Object3d {
    public PointComp[] points;
    public int[][] faceList;
    public Point2D[][] uvPoints;

    public Face[] faces;
    public Point3D boundingSphereC;
    public double boundingSphereR;
    public boolean hasEdges;
    public Texture[] textures;

    public Object3d(PointComp[] _points, int[][] _faceList, Point2D[][] _uvPoints, boolean _hasEdges, Texture[] _textures, boolean initialise){
        points = _points;
        faceList = _faceList;
        uvPoints = _uvPoints;
        hasEdges = _hasEdges;
        textures = _textures;

        if(initialise){initialise();}
    }


    public void invalidate(){
        for (Face face:
             faces) {
            face.invalidate();
        }
    }
    public void draw(drawData drawData){
        int iterator = 0;
        for (Face face:
             faces) {
            // check whether faces are pointing towards or away from the camera
            Vector3D camVec = new Vector3D(0,0,0);
            camVec.createFrom2Points(drawData.playerPos, face.points[0].point);
            int numberOfPixels = 0;
            if(drawData.backfaceCullingDisabled || face.normal.angleTo(camVec) >= Math.PI/2-0.01){
                numberOfPixels = face.draw(drawData);
            }
            if(drawData.debugHud){
            drawData.debugImg.getGraphics().drawString(iterator + ": " +numberOfPixels , 10, 20 + 10*iterator);
            iterator += 1;}
        }
    }
    public void initialise(){
        // init faces
        faces = new Face[faceList.length];
        for (int face = 0; face < faceList.length; face+= 1) {
            faces[face] = new Face();
            faces[face].points = new PointComp[faceList[face].length];
            for (int point = 0; point < faceList[face].length; point += 1){
                faces[face].points[point] = points[faceList[face][point]];
            }
            faces[face].hasEdges = hasEdges;
            faces[face].texture = textures[face];
            faces[face].UVPoints = uvPoints[face];
            faces[face].initialise();
        }
        // init bounding sphere
        double distance = 0;
        double newDis;
        Point3D pointA = points[0].point;
        Point3D pointB = points[0].point;
        // todo - maybe use some vector classes?7
        // i can't be bothered
        for (int i = 1; i < points.length; i+=1) {
            newDis = Math.pow(points[0].point.x - points[i].point.x, 2) +
                    Math.pow(points[0].point.y - points[i].point.y, 2) +
                    Math.pow(points[0].point.z - points[i].point.z, 2);
            if (newDis >= distance){
                pointA = points[i].point;
                distance = newDis;}}
        for (PointComp point : points) {
            newDis = Math.pow(pointA.x - point.point.x, 2) +
                    Math.pow(pointA.y - point.point.y, 2) +
                    Math.pow(pointA.z - point.point.z, 2);
            if (newDis >= distance){
                pointB = point.point;
                distance = newDis;}}
        boundingSphereC = new Point3D(
                (pointA.x + pointB.x) / 2,
                (pointA.y + pointB.y)/2,
                (pointA.z + pointB.z)/2);
        boundingSphereR = Math.sqrt(distance)/2;
        for (PointComp point:
                points) {
            distance = Math.sqrt(
                    Math.pow(boundingSphereC.x - point.point.x, 2)+
                    Math.pow(boundingSphereC.y - point.point.y, 2)+
                    Math.pow(boundingSphereC.z - point.point.z, 2));
            if(distance > boundingSphereR){
                boundingSphereR = distance;
            }
        }
    }
}