Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. package code.renderer;
  2.  
  3. import java.awt.Color;
  4. import java.util.List;
  5.  
  6. /**
  7.  * The Scene class is where we store data about a 3D model and light source
  8.  * inside our renderer. It also contains a static inner class that represents
  9.  * one single polygon.
  10.  *
  11.  * Method stubs have been provided, but you'll need to fill them in.
  12.  *
  13.  * If you were to implement more fancy rendering, e.g. Phong shading, you'd want
  14.  * to store more information in this class.
  15.  */
  16. public class Scene {
  17.     List<Polygon> polygons;
  18.     Vector3D lightPos;
  19.  
  20.     public Scene(List<Polygon> polygons, Vector3D lightPos) {
  21.         this.polygons = polygons;
  22.         this.lightPos = lightPos;
  23.     }
  24.  
  25.     public Vector3D getLight() {
  26.         // TODO fill this in.
  27.         return this.lightPos;
  28.     }
  29.  
  30.     public List<Polygon> getPolygons() {
  31.         // TODO fill this in.
  32.         return this.polygons;
  33.     }
  34.  
  35.     /**
  36.      * Polygon stores data about a single polygon in a scene, keeping track of
  37.      * (at least!) its three vertices and its reflectance.
  38.      *
  39.      * This class has been done for you.
  40.      */
  41.     public static class Polygon {
  42.         Vector3D[] vertices;
  43.         Color reflectance;
  44.  
  45.         /**
  46.          * @param points
  47.          *            An array of floats with 9 elements, corresponding to the
  48.          *            (x,y,z) coordinates of the three vertices that make up
  49.          *            this polygon. If the three vertices are A, B, C then the
  50.          *            array should be [A_x, A_y, A_z, B_x, B_y, B_z, C_x, C_y,
  51.          *            C_z].
  52.          * @param color
  53.          *            An array of three ints corresponding to the RGB values of
  54.          *            the polygon, i.e. [r, g, b] where all values are between 0
  55.          *            and 255.
  56.          */
  57.         public Polygon(float[] points, int[] color) {
  58.             this.vertices = new Vector3D[3];
  59.  
  60.             float x, y, z;
  61.             for (int i = 0; i < 3; i++) {
  62.                 x = points[i * 3];
  63.                 y = points[i * 3 + 1];
  64.                 z = points[i * 3 + 2];
  65.                 this.vertices[i] = new Vector3D(x, y, z);
  66.             }
  67.  
  68.             int r = color[0];
  69.             int g = color[1];
  70.             int b = color[2];
  71.             this.reflectance = new Color(r, g, b);
  72.         }
  73.  
  74.         /**
  75.          * An alternative constructor that directly takes three Vector3D objects
  76.          * and a Color object.
  77.          */
  78.         public Polygon(Vector3D a, Vector3D b, Vector3D c, Color color) {
  79.             this.vertices = new Vector3D[] { a, b, c };
  80.             this.reflectance = color;
  81.         }
  82.  
  83.         public Vector3D[] getVertices() {
  84.             return vertices;
  85.         }
  86.  
  87.         public Vector3D[] orderAntiClockWise() {
  88.             Vector3D vert[] = this.vertices;
  89.             float topmostPoint = Math.max(Math.max(vert[0].y, vert[1].y), vert[2].y);
  90.  
  91.             Vector3D v1;
  92.             Vector3D v2;
  93.             Vector3D v3;
  94.             if (vert[0].y == topmostPoint) {
  95.                 v1 = vert[0];
  96.  
  97.                 v2 = vert[1];
  98.                 v3 = vert[2];
  99.             } else if (vert[1].y == topmostPoint) {
  100.                 v1 = vert[1];
  101.  
  102.                 v2 = vert[0];
  103.                 v3 = vert[2];
  104.             } else {
  105.                 v1 = vert[2];
  106.  
  107.                 v2 = vert[1];
  108.                 v3 = vert[0];
  109.             }
  110.  
  111.             if (v2.x >= v3.x) {
  112.                 // v2 = vert[1];
  113.                 // v3 = vert[2];  
  114.             } else {
  115.                 Vector3D temp = v2;
  116.                 v2 = v3;
  117.                 v3 = temp;
  118.             }
  119.             Vector3D finalList[] = new Vector3D[3];
  120.             finalList[0] = v1;
  121.             finalList[1] = v2;
  122.             finalList[2] = v3;
  123.             return finalList;
  124.  
  125.         }
  126.  
  127.         public Color getReflectance() {
  128.             return reflectance;
  129.         }
  130.  
  131.         public Vector3D getMinX() {
  132.             Vector3D minX = vertices[0];
  133.  
  134.             for (Vector3D current : vertices) {
  135.                 if (current.x < minX.x) {
  136.                     minX = current;
  137.                 }
  138.  
  139.             }
  140.             return minX;
  141.         }
  142.  
  143.         public Vector3D getMaxX() {
  144.             Vector3D maxX = vertices[0];
  145.  
  146.             for (Vector3D current : vertices) {
  147.                 if (current.x > maxX.x) {
  148.                     maxX = current;
  149.                 }
  150.  
  151.             }
  152.             return maxX;
  153.         }
  154.  
  155.         public Vector3D getMinY() {
  156.             Vector3D minY = vertices[0];
  157.  
  158.             for (Vector3D current : vertices) {
  159.                 if (current.y < minY.y) {
  160.                     minY = current;
  161.                 }
  162.  
  163.             }
  164.             return minY;
  165.         }
  166.  
  167.         public Vector3D getMaxY() {
  168.             Vector3D maxY = vertices[0];
  169.  
  170.             for (Vector3D current : vertices) {
  171.                 if (current.y > maxY.y) {
  172.                     maxY = current;
  173.                 }
  174.  
  175.             }
  176.             return maxY;
  177.         }
  178.  
  179.         @Override
  180.         public String toString() {
  181.             String str = "polygon:";
  182.  
  183.             for (Vector3D p : vertices)
  184.                 str += "\n  " + p.toString();
  185.  
  186.             str += "\n  " + reflectance.toString();
  187.  
  188.             return str;
  189.         }
  190.     }
  191. }
  192.  
  193. // code for COMP261 assignments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement