Advertisement
SforzandoCF

Untitled

Sep 25th, 2024 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package com.sforzando.graphics3d;
  2.  
  3. public class GraphicsRenderer {
  4.     public static Image render (List<DynamicModel> objects, List<StaticModel> models, GraphicsRenderingContext ctx) {
  5.         List<StaticModel.Face> faces = List.<StaticModel.Face>of();
  6.         for (StaticModel m : models)
  7.             for (StaticModel.Face f : m.getFaces())
  8.                 faces.add(f);
  9.         for (DynamicModel m : objects)
  10.             for (StaticModel.Face f : m.getCurrentPose().getFaces())
  11.                 faces.add(f);
  12.         Map<StaticModel.Face, PointMatrix> visibleMatrices = Map.<StaticModel.Face, PointMatrix>of();
  13.         Point3D cameraPos = ctx.getCamera().getPosition();
  14.         Plane cameraPlane = ctx.getCamera().getVector().calculatePerpendicularPlane();
  15.         for (StaticModel.Face f : faces) {
  16.             VertexMatrix vm = f.getVertices();
  17.             PointMatrix pm = new PointMatrix(f.generatePointMatrix().getFirstVertex());
  18.             for (Vertex v : vm.getVertices()) {
  19.                 Point3D vPos = v.getPosition();
  20.                 double dist = vPos.calculateDistanceTo(cameraPlane);
  21.                 BoundedPlane fovAtDist = BoundedPlane.centered(cameraPos, angleTransform(ctx.getFOV()) * dist);
  22.                 Point2D newVPos = fovAtDist.coerce(vPos);
  23.                 pm.addPoint(newVPos);
  24.             }
  25.             boolean isOutOfBounds = true;
  26.             for (Point2D p : pm.getPoints()) {
  27.                 if (!p.isBeyondBounds()) {
  28.                     isOutOfBounds = false;
  29.                     break;
  30.                 }
  31.             }
  32.             if (!isOutOfBounds) visibleMatrices.put(f, pm);
  33.         }
  34.         DirectColorModel out = DirectColorModel(32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
  35.         WritableRaster image = new WritableRaster(out.createCompatibleSampleModel(ctx.getWidth(), ctx.getHeight()), new Point(0, 0));
  36.         for (StaticModel.Face f : visibleMatrices.keySet()) {
  37.             Texture texture = f.getTexture();
  38.             Raster r = transformTexture(texture, visibleMatrices.get(f));
  39.             AlphaComposite.SrcAtop.createContext(texture.asImage().getColorModel(), out, null).compose(image, r, image);
  40.         }
  41.         return new BufferedImage(out, image, true, new Hashtable<String, Object>());
  42.     }
  43.    
  44.     public static double angleTransform (int fov) {
  45.         double radians = (fov / 180) * Math.PI;
  46.         return Math.pow(Math.pow(1 - Math.abs(Math.cos(radians)), 2), Math.pow(Math.sin(radians), 2), 1 / 2);
  47.     }
  48.    
  49.     public static Raster transformTexture (Texture t, PointMatrix m) {
  50.         PointMatrix tM = t.generatePointMatrix();
  51.         t.translate(m.getPointPos(m.getPoint()).getX() - tM.getPointPos(tM.getPoint()).getX(), m.getPointPos(m.getPoint()).getY() - tM.getPointPos(tM.getPoint()).getY());
  52.         if (m.getPoint(m.getFirstPoint() + 1) != tM.getPoint(tM.getFirstPoint() + 1) {
  53.             t.redefineOrigin(tM.getFirstPoint());
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement