Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.15 KB | None | 0 0
  1. package code.renderer;
  2.  
  3. import java.awt.Color;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import code.renderer.Scene.Polygon;
  8.  
  9. /**
  10.  * The Pipeline class has method stubs for all the major components of the
  11.  * rendering pipeline, for you to fill in.
  12.  *
  13.  * Some of these methods can get quite long, in which case you should strongly
  14.  * consider moving them out into their own file. You'll need to update the
  15.  * imports in the test suite if you do.
  16.  */
  17. public class Pipeline {
  18.  
  19.     /**
  20.      * Returns true if the given polygon is facing away from the camera (and so
  21.      * should be hidden), and false otherwise.
  22.      */
  23.     public static boolean isHidden(Polygon poly) {
  24.  
  25.         Vector3D vert[] = poly.getVertices();
  26.         // float topmostPoint = Math.max(Math.max(vert[0].y,
  27.         // vert[1].y),vert[2].y);
  28.  
  29.         Vector3D v1;
  30.         Vector3D v2;
  31.         Vector3D v3;
  32.         // Vector3D vert[] = poly.orderAntiClockWise();
  33.         v1 = vert[0];
  34.         v2 = vert[1];
  35.         v3 = vert[2];
  36.  
  37.         Vector3D a = new Vector3D(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
  38.         Vector3D b = new Vector3D(v3.x - v2.x, v3.y - v2.y, v3.z - v2.z);
  39.         Vector3D zH = a.crossProduct(b);
  40.  
  41.         if (zH.z >= 0) {//not sure if >= or just >
  42.             return true;
  43.         } else {
  44.             return false;
  45.         }
  46.  
  47.         // TODO fill this in.
  48.     }
  49.  
  50.     /**
  51.      * Computes the color of a polygon on the screen, once the lights, their
  52.      * angles relative to the polygon's face, and the reflectance of the polygon
  53.      * have been accounted for.
  54.      *
  55.      * @param lightDirection
  56.      *            The Vector3D pointing to the directional light read in from
  57.      *            the file.
  58.      * @param lightColor
  59.      *            The color of that directional light.
  60.      * @param ambientLight
  61.      *            The ambient light in the scene, i.e. light that doesn't depend
  62.      *            on the direction.
  63.      */
  64.     public static Color getShading(Polygon poly, Vector3D lightDirection, Color lightColor, Color ambientLight) {
  65.         //System.out.println("LightDirection: "+lightDirection +" lightColor: "+lightColor+" ambientLight: "+ambientLight);
  66.         Vector3D vert[] = poly.getVertices();
  67.         Vector3D v1 = vert[0];
  68.         Vector3D v2 = vert[1];
  69.         Vector3D v3 = vert[2];
  70.  
  71.         Vector3D a = new Vector3D(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
  72.         Vector3D b = new Vector3D(v3.x - v2.x, v3.y - v2.y, v3.z - v2.z);
  73.  
  74.         Vector3D n =a.crossProduct(b); //new Vector3D((a.x * b.z - a.z * b.y), (a.z * b.x - a.x * b.z), (a.x * b.y - a.y * b.x));
  75.         // float normal = (float) Math.abs(Math.sqrt(n.x * n.x + n.y * n.y + n.z
  76.         // * n.z));
  77.  
  78.         Vector3D unitNormal = n.unitVector();// new Vector3D(n.x / normal, n.y /
  79.                                                 // normal, n.z / normal);
  80.         Vector3D lightDirectionUnitVector = lightDirection.unitVector();
  81.  
  82.         // float ambientLightIntensity[]=new float[3];
  83.         // ambientLight.getColorComponents(ambientLightIntensity);
  84.  
  85.         // float incidentLightIntensity[] = new float[3];
  86.         // lightColor.getColorComponents(incidentLightIntensity);
  87.  
  88.         // the above float's could be useless
  89.        
  90.         float cosTheta = unitNormal.dotProduct(lightDirectionUnitVector);//unitNormal.dotProduct(lightDirectionUnitVector);
  91.         //float cosTheta = unitNormal.x*lightDirectionUnitVector.x+unitNormal.y*lightDirectionUnitVector.y+unitNormal.z*lightDirectionUnitVector.z;
  92.         //if(cosTheta<0){
  93.         //  cosTheta=0;
  94.         //}
  95.         int finalR;
  96.         int finalG;
  97.         int finalB;
  98.         if (cosTheta<0) {//if >= makes a difference if (lightDirection.z >= 0) {
  99.             cosTheta=0;
  100.         }//Not sure if > or < also if on the else statement i should use 1 for lightcolor.
  101.             //System.out.println("This is where lightcolor is used");
  102.             finalR = (int)(((ambientLight.getRed() / 255f) + (lightColor.getRed() / 255f) * cosTheta)
  103.                     * poly.getReflectance().getRed());
  104.             finalG = (int) (((ambientLight.getGreen() / 255f) + (lightColor.getGreen() / 255f) * cosTheta)
  105.                     * poly.getReflectance().getGreen());
  106.             finalB = (int) (((ambientLight.getBlue() / 255f) + (lightColor.getBlue() / 255f) * cosTheta)
  107.                     * poly.getReflectance().getBlue());
  108.            
  109.        
  110.         //System.out.println("Red: " + finalR + "Green: " + finalG + "Blue: " + finalB);
  111.        
  112.         if(finalR>255){
  113.             finalR=255;
  114.         }
  115.         if(finalG>255){
  116.             finalG=255;
  117.         }
  118.         if(finalB>255){
  119.             finalB=255;
  120.         }
  121.        
  122.         if(finalR<0){
  123.             finalR=0;
  124.         }
  125.         if(finalG<0){
  126.             finalG=0;
  127.         }
  128.         if(finalB<0){
  129.             finalB=0;
  130.         }
  131.         return new Color(finalR, finalG, finalB);
  132.  
  133.         // TODO fill this in.
  134.         // return null;
  135.     }
  136.  
  137.     /**
  138.      * This method should rotate the polygons and light such that the viewer is
  139.      * looking down the Z-axis. The idea is that it returns an entirely new
  140.      * Scene object, filled with new Polygons, that have been rotated.
  141.      *
  142.      * @param scene
  143.      *            The original Scene.
  144.      * @param xRot
  145.      *            An angle describing the viewer's rotation in the YZ-plane (i.e
  146.      *            around the X-axis).
  147.      * @param yRot
  148.      *            An angle describing the viewer's rotation in the XZ-plane (i.e
  149.      *            around the Y-axis).
  150.      * @return A new Scene where all the polygons and the light source have been
  151.      *         rotated accordingly.
  152.      */
  153.     public static Scene rotateScene(Scene scene, float xRot, float yRot) {
  154.         // TODO fill this in.
  155.         if (xRot == 0 && yRot == 0) {
  156.             return scene;
  157.         }
  158.         ArrayList<Polygon> polys = new ArrayList<Polygon>();
  159.         // Transform zRotation = Transform.newZRotation(scene.lightPos);
  160.         // Vector3D lightPos = scene.getLight();
  161.  
  162.         Transform xRotationMatrix = Transform.newXRotation(xRot);
  163.         Transform yRotationMatrix = Transform.newYRotation(yRot);
  164.        
  165.         //Transform zRotationMatrix = Transform.newZRotation(xRot+yRot);
  166.        
  167.         Transform intoOne = xRotationMatrix.compose(yRotationMatrix);
  168.        
  169.         Vector3D oldLight=scene.getLight();
  170.         Vector3D newLightPos = intoOne.multiply(oldLight);
  171.         // intoOne.multiply(lightPos);
  172.  
  173.         for (Polygon poly : scene.getPolygons()) {
  174.            
  175.            
  176.            
  177.             Vector3D newPoly[] = new Vector3D[3];//New 3 vertices
  178.  
  179.             // Vector3D newVec[] = new Vector3D[3];
  180.             //System.out.println("Vert 1 before: "+poly.getVertices()[0]);
  181.             newPoly[0] = intoOne.multiply(poly.getVertices()[0]);//First new vert
  182.             newPoly[1] = intoOne.multiply(poly.getVertices()[1]);//Second new vert
  183.             newPoly[2] = intoOne.multiply(poly.getVertices()[2]);//third new vert
  184.             Polygon newPolygon= new Polygon(newPoly[0], newPoly[1], newPoly[2], poly.getReflectance());
  185.             //System.out.println("Vert 1 after: "+newPoly[0]);
  186.             polys.add(newPolygon);
  187.             //poly=newPolygon;
  188.            
  189.         }
  190.         //System.out.println("The new light pos is: " + newLightPos);
  191.         return new Scene(polys, newLightPos);
  192.     }
  193.  
  194.     /**
  195.      * This should translate the scene by the appropriate amount.
  196.      *
  197.      * @param scene
  198.      * @return
  199.      */
  200.     public static Scene translateScene(Scene scene) {
  201.         Polygon maxX = scene.getPolygons().get(0);
  202.         Polygon minX = scene.getPolygons().get(0);
  203.         Polygon maxY = scene.getPolygons().get(0);
  204.         Polygon minY = scene.getPolygons().get(0);
  205.        
  206.         for(Polygon poly:scene.getPolygons()){
  207.             if(poly.getMaxX().x>maxX.getMaxX().x){
  208.                 maxX=poly;
  209.             }
  210.             if(poly.getMinX().x<minX.getMinX().x){
  211.                 minX=poly;
  212.             }
  213.            
  214.             if(poly.getMaxY().y>maxY.getMaxY().y){
  215.                 maxY=poly;
  216.             }
  217.             if(poly.getMinY().y<minY.getMinY().y){
  218.                 minY=poly;
  219.             }
  220.            
  221.            
  222.            
  223.         }
  224.        
  225.         float middleX=(maxX.getMaxX().x-minX.getMinX().x)/2;
  226.         float middleY=(maxY.getMaxY().y-minY.getMinY().y)/2;
  227.        
  228.         float canvasXMiddle = GUI.CANVAS_WIDTH/2;
  229.         float canvasYMiddle = GUI.CANVAS_HEIGHT/2;
  230.        
  231.         float moveX = canvasXMiddle-middleX;
  232.         float moveY = canvasYMiddle-middleY;
  233.        
  234.         Transform xTrans = Transform.newTranslation(moveX,moveY,0);
  235.         //Transform lightTrans = Transform.newTranslation(moveX,moveY,0);
  236.         ArrayList<Polygon>polys=new ArrayList<Polygon>();
  237.         //Vector3D newLightPos=lightTrans.multiply(scene.getLight());
  238.        
  239.        
  240.         for (Polygon poly : scene.getPolygons()) {
  241.  
  242.             Vector3D newPoly[] = new Vector3D[3];//New 3 vertices
  243.  
  244.             // Vector3D newVec[] = new Vector3D[3];
  245.             //System.out.println("Vert 1 before: "+poly.getVertices()[0]);
  246.             newPoly[0] = xTrans.multiply(poly.getVertices()[0]);//First new vert
  247.             newPoly[1] = xTrans.multiply(poly.getVertices()[1]);//Second new vert
  248.             newPoly[2] = xTrans.multiply(poly.getVertices()[2]);//third new vert
  249.             Polygon newPolygon= new Polygon(newPoly[0], newPoly[1], newPoly[2], poly.getReflectance());
  250.             //System.out.println("Vert 1 after: "+newPoly[0]);
  251.             polys.add(newPolygon);
  252.         }
  253.         //System.out.println("The new light pos is: " + newLightPos);
  254.         return new Scene(polys, scene.getLight());
  255.        
  256.        
  257.        
  258.            
  259.         // TODO fill this in.
  260.         //return null;
  261.     }
  262.  
  263.     /**
  264.      * This should scale the scene.
  265.      *
  266.      * @param scene
  267.      * @return
  268.      */
  269.     public static Scene scaleScene(Scene scene) {
  270.        
  271.         Polygon maxX = scene.getPolygons().get(0);
  272.         Polygon minX = scene.getPolygons().get(0);
  273.         Polygon maxY = scene.getPolygons().get(0);
  274.         Polygon minY = scene.getPolygons().get(0);
  275.        
  276.         for(Polygon poly:scene.getPolygons()){
  277.             if(poly.getMaxX().x>maxX.getMaxX().x){
  278.                 maxX=poly;
  279.             }
  280.             if(poly.getMinX().x<minX.getMinX().x){
  281.                 minX=poly;
  282.             }
  283.            
  284.             if(poly.getMaxY().y>maxY.getMaxY().y){
  285.                 maxY=poly;
  286.             }
  287.             if(poly.getMinY().y<minY.getMinY().y){
  288.                 minY=poly;
  289.             }
  290.            
  291.            
  292.            
  293.         }
  294.        
  295.         float diffX=1;
  296.         if(maxX.getMaxX().x-GUI.CANVAS_WIDTH>0 || 0-minX.getMinX().x>0 ){
  297.             if(maxX.getMaxX().x-GUI.CANVAS_WIDTH>0-minX.getMinX().x){
  298.                 //diffX=maxX.getMaxX().x-GUI.CANVAS_WIDTH;
  299.                 diffX=GUI.CANVAS_WIDTH/maxX.getMaxX().x;
  300.                
  301.             }
  302.             else{
  303.                 //diffX=0-minX.getMinX().x;
  304.                 diffX=1/-minX.getMinX().x;
  305.             }
  306.            
  307.            
  308.         }
  309.                
  310.         float diffY=1;
  311.        
  312.         if(maxY.getMaxY().y-GUI.CANVAS_HEIGHT>0 || 0-minY.getMinY().y>0 ){
  313.             if(maxY.getMaxY().y-GUI.CANVAS_HEIGHT>0-minY.getMinY().y){
  314.                 //diffY=maxY.getMaxY().y-GUI.CANVAS_HEIGHT;
  315.                 //diffY=diffY/maxY.getMaxY().y;
  316.                 diffY=GUI.CANVAS_HEIGHT/maxY.getMaxY().y;
  317.                
  318.             }
  319.             else{
  320.                 //diffY=0-minY.getMinY().y;
  321.                 diffY=1/-minY.getMinY().y;
  322.             }
  323.            
  324.            
  325.         }
  326.        
  327.              
  328.        
  329.        
  330.         //System.out.println("The x scale: "+diffX+" The y scale: "+diffY);
  331.         Transform xTrans = Transform.newScale(diffX,diffY,0);
  332.         ArrayList<Polygon>polys=new ArrayList<Polygon>();
  333.         Vector3D newLightPos=xTrans.multiply(scene.getLight());
  334.        
  335.        
  336.         for (Polygon poly : scene.getPolygons()) {
  337.  
  338.             Vector3D newPoly[] = new Vector3D[3];//New 3 vertices
  339.  
  340.             // Vector3D newVec[] = new Vector3D[3];
  341.             //System.out.println("Vert 1 before: "+poly.getVertices()[0]);
  342.             newPoly[0] = xTrans.multiply(poly.getVertices()[0]);//First new vert
  343.             newPoly[1] = xTrans.multiply(poly.getVertices()[1]);//Second new vert
  344.             newPoly[2] = xTrans.multiply(poly.getVertices()[2]);//third new vert
  345.             Polygon newPolygon= new Polygon(newPoly[0], newPoly[1], newPoly[2], poly.getReflectance());
  346.             //System.out.println("Vert 1 after: "+newPoly[0]);
  347.             polys.add(newPolygon);
  348.         }
  349.         //System.out.println("The new light pos is: " + newLightPos);
  350.         return new Scene(polys, newLightPos);
  351.        
  352.        
  353.         // TODO fill this in.
  354.         //return null;
  355.     }
  356.  
  357.     /**
  358.      * Computes the edgelist of a single provided polygon, as per the lecture
  359.      * slides.
  360.      */
  361.     public static EdgeList computeEdgeList(Polygon poly) {
  362.         // Vector3D v1;
  363.         // Vector3D v2;
  364.         // Vector3D v3;
  365.         // Vector3D vert[] = poly.getVertices();
  366.         Vector3D vert[] = poly.getVertices();
  367.         // v1=vert[0];
  368.         // v2=vert[1];
  369.         // v3=vert[2];
  370.         //System.out.println("Xvalue: " + vert[0].x + "yValue:" + vert[0].y);
  371.         //System.out.println("Xvalue: " + vert[1].x + "yValue:" + vert[1].y);
  372.  
  373.         // int maxY = (int) Math.max(Math.max(vert[0].y, vert[1].y), vert[2].y);
  374.         // int minY = (int) Math.min(Math.min(vert[0].y, vert[1].y), vert[2].y);
  375.         //System.out.println("Unrounded max: "+Math.max(Math.max(vert[0].y, vert[1].y), vert[2].y));
  376.         int maxY = Math.round( Math.max(Math.max(vert[0].y, vert[1].y), vert[2].y));
  377.         //System.out.println("Rounded max: "+maxY);
  378.         int minY = Math.round(Math.min(Math.min(vert[0].y, vert[1].y), vert[2].y));///////rounding so check ur ass
  379.        
  380.         //if(maxY>GUI.CANVAS_HEIGHT){//watch out here this is dodgy
  381.         //  maxY=GUI.CANVAS_HEIGHT;
  382.         //}
  383.         //if(minY<0){
  384.         //  minY=0;
  385.         //}
  386.         EdgeList listEdge = new EdgeList(minY, maxY);
  387.  
  388.         Vector3D a, b;
  389.         for (int i = 0; i < 3; i++) {// from (0,1),(1,2),(2,0)
  390.             // Vector3D a, b;
  391.             int iPlusOne = i + 1;
  392.             if (iPlusOne == 3) {
  393.                 iPlusOne = 0;
  394.             }
  395.  
  396.             // if (vert[i].y > vert[iPlusOne].y) {// This may be redundant
  397.             // a = vert[i];
  398.             // b = vert[iPlusOne];
  399.             // } else {
  400.             // a = vert[iPlusOne];
  401.             // b = vert[i];
  402.             // }
  403.             a = vert[i];
  404.             b = vert[iPlusOne];
  405.  
  406.             float xSlope = (b.x - a.x) / (b.y - a.y);
  407.             float zSlope = (b.z - a.z) / (b.y - a.y);
  408.             float x = a.x;
  409.             float z = a.z;
  410.             int y = Math.round(a.y);
  411.             if (a.y < b.y) {
  412.                 while (y <= Math.round(b.y)) {
  413.                     // xLeft(Y)=x;
  414.                     // listEdge.edgeL.put(y, new Float[4]);
  415.                     // listEdge.init(y);
  416.                     //listEdge.init(y);
  417.                     listEdge.addXLeft(y, x);
  418.                     // zLeft(Y)=z;
  419.                     //listEdge.init(y);
  420.                     listEdge.addZLeft(y, z);
  421.                     x = x + xSlope;
  422.                     z = z + zSlope;
  423.                     y++;
  424.                 }
  425.  
  426.             } else {
  427.                 while (y >= Math.round(b.y)) {
  428.                     // xRight(Y)=x;
  429.                     // listEdge.edgeL.put(y, new Float[4]);
  430.                     // listEdge.init(y);
  431.                     //System.out.println("When Y is: " + y + " Should not be 1500: " + x);
  432.                     //listEdge.init(y);
  433.                     listEdge.addXRight(y, x);
  434.                     // zRight(Y)=z;
  435.                     //listEdge.init(y);
  436.                     listEdge.addZRight(y, z);
  437.                     x = x - xSlope;
  438.                     z = z - zSlope;
  439.                     y--;
  440.                 }
  441.  
  442.             }
  443.  
  444.         }
  445.         // TODO fill this in.
  446.         //listEdge.printEdgeList();
  447.         return listEdge;
  448.     }
  449.  
  450.     /**
  451.      * Fills a zbuffer with the contents of a single edge list according to the
  452.      * lecture slides.
  453.      *
  454.      * The idea here is to make zbuffer and zdepth arrays in your main loop, and
  455.      * pass them into the method to be modified.
  456.      *
  457.      * @param zbuffer
  458.      *            A double array of colours representing the Color at each pixel
  459.      *            so far.
  460.      * @param zdepth
  461.      *            A double array of floats storing the z-value of each pixel
  462.      *            that has been coloured in so far.
  463.      * @param polyEdgeList
  464.      *            The edgelist of the polygon to add into the zbuffer.
  465.      * @param polyColor
  466.      *            The colour of the polygon to add into the zbuffer.
  467.      */
  468.     public static void computeZBuffer(Color[][] zbuffer, float[][] zdepth, EdgeList polyEdgeList, Color polyColor) {
  469.         // TODO fill this in.
  470.  
  471.         for (int y = polyEdgeList.getStartY(); y < polyEdgeList.endY; y++) {
  472.             // float zSlope=(polyEdgeList.getRightZ(y)-polyEdgeList.getLeftZ(y))/(polyEdgeList.getRightX(y)-polyEdgeList.getLeftX(y));
  473.             float z = polyEdgeList.getLeftZ(y);
  474.             int x = (int)polyEdgeList.getLeftX(y);///////rounding so check asshole
  475.             while (x < Math.round(polyEdgeList.getRightX(y))) {
  476.                
  477.                    
  478.                 if (x >= 0 && y >= 0 && x < GUI.CANVAS_WIDTH && y < GUI.CANVAS_HEIGHT && zdepth[x][y] > z) {
  479.                 //if (z < zdepth[x][y]) {
  480.                     zbuffer[x][y] = polyColor;
  481.                     zdepth[x][y] = z;
  482.                 //}
  483.                 }
  484.                
  485.  
  486.                 x++;
  487.                 //z=z+zSlope;
  488.  
  489.             }
  490.  
  491.         }
  492.  
  493.     }
  494. }
  495.  
  496. // code for comp261 assignments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement