Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sforzando.graphics3d;
- public class GraphicsRenderer {
- public static Image render (List<DynamicModel> objects, List<StaticModel> models, GraphicsRenderingContext ctx) {
- List<StaticModel.Face> faces = List.<StaticModel.Face>of();
- for (StaticModel m : models)
- for (StaticModel.Face f : m.getFaces())
- faces.add(f);
- for (DynamicModel m : objects)
- for (StaticModel.Face f : m.getCurrentPose().getFaces())
- faces.add(f);
- Map<StaticModel.Face, PointMatrix> visibleMatrices = Map.<StaticModel.Face, PointMatrix>of();
- Point3D cameraPos = ctx.getCamera().getPosition();
- Plane cameraPlane = ctx.getCamera().getVector().calculatePerpendicularPlane();
- for (StaticModel.Face f : faces) {
- VertexMatrix vm = f.getVertices();
- PointMatrix pm = new PointMatrix(f.generatePointMatrix().getFirstVertex());
- for (Vertex v : vm.getVertices()) {
- Point3D vPos = v.getPosition();
- double dist = vPos.calculateDistanceTo(cameraPlane);
- BoundedPlane fovAtDist = BoundedPlane.centered(cameraPos, angleTransform(ctx.getFOV()) * dist);
- Point2D newVPos = fovAtDist.coerce(vPos);
- pm.addPoint(newVPos);
- }
- boolean isOutOfBounds = true;
- for (Point2D p : pm.getPoints()) {
- if (!p.isBeyondBounds()) {
- isOutOfBounds = false;
- break;
- }
- }
- if (!isOutOfBounds) visibleMatrices.put(f, pm);
- }
- DirectColorModel out = DirectColorModel(32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
- WritableRaster image = new WritableRaster(out.createCompatibleSampleModel(ctx.getWidth(), ctx.getHeight()), new Point(0, 0));
- for (StaticModel.Face f : visibleMatrices.keySet()) {
- Texture texture = f.getTexture();
- Raster r = transformTexture(texture, visibleMatrices.get(f));
- AlphaComposite.SrcAtop.createContext(texture.asImage().getColorModel(), out, null).compose(image, r, image);
- }
- return new BufferedImage(out, image, true, new Hashtable<String, Object>());
- }
- public static double angleTransform (int fov) {
- double radians = (fov / 180) * Math.PI;
- return Math.pow(Math.pow(1 - Math.abs(Math.cos(radians)), 2), Math.pow(Math.sin(radians), 2), 1 / 2);
- }
- public static Raster transformTexture (Texture t, PointMatrix m) {
- PointMatrix tM = t.generatePointMatrix();
- t.translate(m.getPointPos(m.getPoint()).getX() - tM.getPointPos(tM.getPoint()).getX(), m.getPointPos(m.getPoint()).getY() - tM.getPointPos(tM.getPoint()).getY());
- if (m.getPoint(m.getFirstPoint() + 1) != tM.getPoint(tM.getFirstPoint() + 1) {
- t.redefineOrigin(tM.getFirstPoint());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement