001/*
002 * Renderer 4. The MIT License.
003 * Copyright (c) 2022 rlkraft@pnw.edu
004 * See LICENSE for details.
005*/
006
007package renderer.pipeline;
008
009import renderer.scene.*;
010
011import java.util.List;
012import java.util.ArrayList;
013
014/**
015   Transform each {@link Vertex} of a {@link Model} from the model's
016   (private) local coordinate system to the {@link Camera}'s (shared)
017   coordinate system.
018<p>
019   For each {@code Vertex} object in a {@code Model} object, use a
020   {@link Position}'s translation {@link Vector} to translate the
021   object's {@code Vertex} coordinates from the model's coordinate
022   system to the camera's coordinate system.
023<p>
024   Return a new {@code Model} object, which contains all the translated
025   vertices from the original model, to the renderer. The original model
026   object, which belongs to the client program, remains unchanged. So the
027   renderer gets the mutated model and the client sees its model as being
028   preserved.
029*/
030public class Model2Camera
031{
032   /**
033      Use a {@link Position}'s translation {@link Vector} to transform
034      each {@link Vertex} from a {@link Model}'s coordinate system to
035      the {@link Camera}'s coordinate system.
036
037      @param position  {@link Position} with a {@link Model} and a translation {@link Vector}
038      @return a new {@link Model} with {@link Vertex} objects in the camera's coordinate system
039   */
040   public static Model model2camera(final Position position)
041   {
042      final Model model = position.getModel();
043      final Vector translation = position.getTranslation();
044
045      // A new vertex list to hold the translated vertices.
046      final List<Vertex> newVertexList =
047                            new ArrayList<>(model.vertexList.size());
048
049      // Replace each Vertex object with one that
050      // contains camera coordinates.
051      for (final Vertex v : model.vertexList)
052      {
053         newVertexList.add( translation.plus(v) );
054      }
055
056      // Return to the renderer a modified model.
057      return new Model(newVertexList,
058                       model.primitiveList,
059                       model.colorList,
060                       position.name + "::" + model.name,
061                       model.visible);
062   }
063
064
065
066   // Private default constructor to enforce noninstantiable class.
067   // See Item 4 in "Effective Java", 3rd Ed, Joshua Bloch.
068   private Model2Camera() {
069      throw new AssertionError();
070   }
071}