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.*; 010import renderer.framebuffer.*; 011 012import java.util.List; 013import java.util.ArrayList; 014 015/** 016 Transform each (projected) {@link Vertex} of a {@link Model} 017 from the camera's image-plane to the logical pixel-plane. 018<p> 019 Return a new {@code Model} object, which contains all the 020 transformed vertices from the original model, to the renderer. 021*/ 022public class Viewport 023{ 024 /** 025 Use the dimensions of a {@link FrameBuffer.Viewport} to transform 026 each {@link Vertex} from the camera's image-plane to the logical 027 pixel-plane. 028 029 @param model {@link Model} whose {@link Vertex} objects are in the camera's image-plane 030 @param viewport a reference to a {@link FrameBuffer.Viewport} in a {@link FrameBuffer} 031 @return a new {@link Model} with {@link Vertex} objects in the logical pixel-plane 032 */ 033 public static Model imagePlane2pixelPlane(final Model model, 034 final FrameBuffer.Viewport viewport) 035 { 036 final int w = viewport.getWidthVP(); 037 final int h = viewport.getHeightVP(); 038 039 // A new vertex list to hold the transformed vertices. 040 final List<Vertex> newVertexList = 041 new ArrayList<>(model.vertexList.size()); 042 043 // Replace each Vertex object with one that 044 // lies in the logical pixel-plane. 045 for (final Vertex v : model.vertexList) 046 { 047 // Transform the vertex to the pixel-plane coordinate system. 048 final double x = 0.5 + w/2.001 * (v.x + 1); // x_pp = 0.5 + w/2 * (x_p+1) 049 final double y = 0.5 + h/2.001 * (v.y + 1); // y_pp = 0.5 + h/2 * (y_p+1) 050 // NOTE: Notice the 2.001 fudge factor in the last two equations. 051 // This is explained on page 142 of 052 // "Jim Blinn's Corner: A Trip Down The Graphics Pipeline" 053 // by Jim Blinn, 1996, Morgan Kaufmann Publishers. 054 055 newVertexList.add( new Vertex(x, y, 0.0) ); 056 } 057 058 // Return to the renderer a modified model. 059 return new Model(newVertexList, 060 model.primitiveList, 061 model.colorList, 062 model.name, 063 model.visible); 064 } 065 066 067 068 // Private default constructor to enforce noninstantiable class. 069 // See Item 4 in "Effective Java", 3rd Ed, Joshua Bloch. 070 private Viewport() { 071 throw new AssertionError(); 072 } 073}