Newer
Older
EPQ-3D-renderer / src / Object3d.java
import java.awt.*;
import java.awt.image.BufferedImage;

public class Object3d {
    public PointComp[] points;
    public int[][] faceList;
    public Face[] faces;
    public Point3D boundingSphereC;
    public double boundingSphereR;

    public Object3d(PointComp[] _points, int[][] _faceList, boolean initialise){
        points = _points;
        faceList = _faceList;
        if(initialise){initialise();}
    }
    public void invalidate(){
        for (Face face:
             faces) {
            face.invalidate();
        }
    }
    public void draw(BufferedImage img, BufferedImage debugimg, Matrix camMatrix, double FPDis, int scrX, int scrY, Point3D playerPos){
        int iterator = 0;
        for (Face face:
             faces) {
            // check whether faces are pointing towards or away from the camera
            // TODO figure out angle
            Vector3D camVec = new Vector3D(0,0,0);
            camVec.createFrom2Points(playerPos, face.points[0].point);
            int numberOfPixels = 0;
            if(face.normal.angleTo(camVec) <= Math.PI/2-0.01){
                numberOfPixels = face.draw(img, debugimg, camMatrix, FPDis, scrX, scrY);
            }
            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].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?
        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;
        boolean valid = false;
        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;
            }
        }
    }
}