001/* 002 * Renderer 9. 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 world 016 coordinate system to the {@link Camera}'s view coordinate system. 017<p> 018 For each {@code Vertex} object in a {@code Model} object, use the 019 {@link Camera}'s world-to-view translation {@link Vector} to transform 020 the object's {@code Vertex} coordinates from the world coordinate 021 system to the camera's view coordinate system. 022*/ 023public class World2View 024{ 025 /** 026 Use a {@link Camera}'s world-to-view translation {@link Vector} to 027 transform each {@link Vertex} from the world coordinate system to 028 the camera's view coordinate system. 029 030 @param model {@link Model} with {@link Vertex} objects in the world coordinate system 031 @param camera a {@link Camera} with a translation {@link Vector} 032 @return a new {@link Model} with {@link Vertex} objects in the {@link Camera}'s view coordinate system 033 */ 034 public static Model world2view(final Model model, 035 final Camera camera) 036 { 037 // We translate each vertex in the oppposite 038 // direction of what the camera was translated by. 039 final Vector negViewVector = camera.getViewVector().times(-1); 040 041 // A new vertex list to hold the transformed vertices. 042 final List<Vertex> newVertexList = 043 new ArrayList<>(model.vertexList.size()); 044 045 // Replace each Vertex object with one that 046 // contains view coordinates. 047 for (final Vertex v : model.vertexList) 048 { 049 // We translate the vertex in the oppposite 050 // direction of what the camera was translated by. 051 newVertexList.add( negViewVector.plus(v) ); 052 } 053 054 return new Model(newVertexList, 055 model.primitiveList, 056 model.colorList, 057 model.name, 058 model.visible); 059 } 060 061 062 063 // Private default constructor to enforce noninstantiable class. 064 // See Item 4 in "Effective Java", 3rd Ed, Joshua Bloch. 065 private World2View() { 066 throw new AssertionError(); 067 } 068}