package com.paul.persp; import com.paul.persp.geom.Point4f; import com.paul.persp.gfx.Screen; import com.paul.persp.maths.Matrix4f; public class Camera3D { private Matrix4f projMat; private Matrix4f viewMat; public Camera3D() { projMat = new Matrix4f(); projMat.setIdentity(); viewMat = new Matrix4f(); viewMat.setIdentity(); } public void setPerspectiveView(float fov, float aspectRatio, float near, float far) { float coTan2 = (float) (1.0f / Math.tan(Math.toRadians(fov * 0.5f))); float yScale = coTan2; float xScale = yScale / aspectRatio; projMat.set(0, xScale); projMat.set(1, 0.0f); projMat.set(2, 0.0f); projMat.set(3, 0.0f); projMat.set(4, 0.0f); projMat.set(5, yScale); projMat.set(6, 0.0f); projMat.set(7, 0.0f); projMat.set(8, 0.0f); projMat.set(9, 0.0f); projMat.set(10, -far / (near - far)); projMat.set(11, -1.0f); projMat.set(12, 0.0f); projMat.set(13, 0.0f); projMat.set(14, -near * far / (near - far)); projMat.set(15, 0.0f); } public void renderVertices(Point4f[] vertices, Screen screen) { for(int i = 0; i < vertices.length; i++) { Point4f transPos = Point4f.multiply(Point4f.multiply(vertices[i], viewMat), projMat); if(transPos.getX() < -1 || transPos.getX() > 1 || transPos.getY() < -1 || transPos.getY() > 1) continue; int x = (int) (Main.WIDTH * 0.5 + transPos.getX() * Main.WIDTH * 0.5 / transPos.getZ()); int y = (int) (Main.HEIGHT * 0.5 + transPos.getY() * Main.HEIGHT * 0.5 / transPos.getZ()); screen.drawPoint(x, y, 0xffffffff); } } }