Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package code.renderer;
- import java.awt.Color;
- import java.util.ArrayList;
- import java.util.List;
- import code.renderer.Scene.Polygon;
- /**
- * The Pipeline class has method stubs for all the major components of the
- * rendering pipeline, for you to fill in.
- *
- * Some of these methods can get quite long, in which case you should strongly
- * consider moving them out into their own file. You'll need to update the
- * imports in the test suite if you do.
- */
- public class Pipeline {
- /**
- * Returns true if the given polygon is facing away from the camera (and so
- * should be hidden), and false otherwise.
- */
- public static boolean isHidden(Polygon poly) {
- Vector3D vert[] = poly.getVertices();
- // float topmostPoint = Math.max(Math.max(vert[0].y,
- // vert[1].y),vert[2].y);
- Vector3D v1;
- Vector3D v2;
- Vector3D v3;
- // Vector3D vert[] = poly.orderAntiClockWise();
- v1 = vert[0];
- v2 = vert[1];
- v3 = vert[2];
- Vector3D a = new Vector3D(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
- Vector3D b = new Vector3D(v3.x - v2.x, v3.y - v2.y, v3.z - v2.z);
- Vector3D zH = a.crossProduct(b);
- if (zH.z >= 0) {//not sure if >= or just >
- return true;
- } else {
- return false;
- }
- // TODO fill this in.
- }
- /**
- * Computes the color of a polygon on the screen, once the lights, their
- * angles relative to the polygon's face, and the reflectance of the polygon
- * have been accounted for.
- *
- * @param lightDirection
- * The Vector3D pointing to the directional light read in from
- * the file.
- * @param lightColor
- * The color of that directional light.
- * @param ambientLight
- * The ambient light in the scene, i.e. light that doesn't depend
- * on the direction.
- */
- public static Color getShading(Polygon poly, Vector3D lightDirection, Color lightColor, Color ambientLight) {
- //System.out.println("LightDirection: "+lightDirection +" lightColor: "+lightColor+" ambientLight: "+ambientLight);
- Vector3D vert[] = poly.getVertices();
- Vector3D v1 = vert[0];
- Vector3D v2 = vert[1];
- Vector3D v3 = vert[2];
- Vector3D a = new Vector3D(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
- Vector3D b = new Vector3D(v3.x - v2.x, v3.y - v2.y, v3.z - v2.z);
- 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));
- // float normal = (float) Math.abs(Math.sqrt(n.x * n.x + n.y * n.y + n.z
- // * n.z));
- Vector3D unitNormal = n.unitVector();// new Vector3D(n.x / normal, n.y /
- // normal, n.z / normal);
- Vector3D lightDirectionUnitVector = lightDirection.unitVector();
- // float ambientLightIntensity[]=new float[3];
- // ambientLight.getColorComponents(ambientLightIntensity);
- // float incidentLightIntensity[] = new float[3];
- // lightColor.getColorComponents(incidentLightIntensity);
- // the above float's could be useless
- float cosTheta = unitNormal.dotProduct(lightDirectionUnitVector);//unitNormal.dotProduct(lightDirectionUnitVector);
- //float cosTheta = unitNormal.x*lightDirectionUnitVector.x+unitNormal.y*lightDirectionUnitVector.y+unitNormal.z*lightDirectionUnitVector.z;
- //if(cosTheta<0){
- // cosTheta=0;
- //}
- int finalR;
- int finalG;
- int finalB;
- if (cosTheta<0) {//if >= makes a difference if (lightDirection.z >= 0) {
- cosTheta=0;
- }//Not sure if > or < also if on the else statement i should use 1 for lightcolor.
- //System.out.println("This is where lightcolor is used");
- finalR = (int)(((ambientLight.getRed() / 255f) + (lightColor.getRed() / 255f) * cosTheta)
- * poly.getReflectance().getRed());
- finalG = (int) (((ambientLight.getGreen() / 255f) + (lightColor.getGreen() / 255f) * cosTheta)
- * poly.getReflectance().getGreen());
- finalB = (int) (((ambientLight.getBlue() / 255f) + (lightColor.getBlue() / 255f) * cosTheta)
- * poly.getReflectance().getBlue());
- //System.out.println("Red: " + finalR + "Green: " + finalG + "Blue: " + finalB);
- if(finalR>255){
- finalR=255;
- }
- if(finalG>255){
- finalG=255;
- }
- if(finalB>255){
- finalB=255;
- }
- if(finalR<0){
- finalR=0;
- }
- if(finalG<0){
- finalG=0;
- }
- if(finalB<0){
- finalB=0;
- }
- return new Color(finalR, finalG, finalB);
- // TODO fill this in.
- // return null;
- }
- /**
- * This method should rotate the polygons and light such that the viewer is
- * looking down the Z-axis. The idea is that it returns an entirely new
- * Scene object, filled with new Polygons, that have been rotated.
- *
- * @param scene
- * The original Scene.
- * @param xRot
- * An angle describing the viewer's rotation in the YZ-plane (i.e
- * around the X-axis).
- * @param yRot
- * An angle describing the viewer's rotation in the XZ-plane (i.e
- * around the Y-axis).
- * @return A new Scene where all the polygons and the light source have been
- * rotated accordingly.
- */
- public static Scene rotateScene(Scene scene, float xRot, float yRot) {
- // TODO fill this in.
- if (xRot == 0 && yRot == 0) {
- return scene;
- }
- ArrayList<Polygon> polys = new ArrayList<Polygon>();
- // Transform zRotation = Transform.newZRotation(scene.lightPos);
- // Vector3D lightPos = scene.getLight();
- Transform xRotationMatrix = Transform.newXRotation(xRot);
- Transform yRotationMatrix = Transform.newYRotation(yRot);
- //Transform zRotationMatrix = Transform.newZRotation(xRot+yRot);
- Transform intoOne = xRotationMatrix.compose(yRotationMatrix);
- Vector3D oldLight=scene.getLight();
- Vector3D newLightPos = intoOne.multiply(oldLight);
- // intoOne.multiply(lightPos);
- for (Polygon poly : scene.getPolygons()) {
- Vector3D newPoly[] = new Vector3D[3];//New 3 vertices
- // Vector3D newVec[] = new Vector3D[3];
- //System.out.println("Vert 1 before: "+poly.getVertices()[0]);
- newPoly[0] = intoOne.multiply(poly.getVertices()[0]);//First new vert
- newPoly[1] = intoOne.multiply(poly.getVertices()[1]);//Second new vert
- newPoly[2] = intoOne.multiply(poly.getVertices()[2]);//third new vert
- Polygon newPolygon= new Polygon(newPoly[0], newPoly[1], newPoly[2], poly.getReflectance());
- //System.out.println("Vert 1 after: "+newPoly[0]);
- polys.add(newPolygon);
- //poly=newPolygon;
- }
- //System.out.println("The new light pos is: " + newLightPos);
- return new Scene(polys, newLightPos);
- }
- /**
- * This should translate the scene by the appropriate amount.
- *
- * @param scene
- * @return
- */
- public static Scene translateScene(Scene scene) {
- Polygon maxX = scene.getPolygons().get(0);
- Polygon minX = scene.getPolygons().get(0);
- Polygon maxY = scene.getPolygons().get(0);
- Polygon minY = scene.getPolygons().get(0);
- for(Polygon poly:scene.getPolygons()){
- if(poly.getMaxX().x>maxX.getMaxX().x){
- maxX=poly;
- }
- if(poly.getMinX().x<minX.getMinX().x){
- minX=poly;
- }
- if(poly.getMaxY().y>maxY.getMaxY().y){
- maxY=poly;
- }
- if(poly.getMinY().y<minY.getMinY().y){
- minY=poly;
- }
- }
- float middleX=(maxX.getMaxX().x-minX.getMinX().x)/2;
- float middleY=(maxY.getMaxY().y-minY.getMinY().y)/2;
- float canvasXMiddle = GUI.CANVAS_WIDTH/2;
- float canvasYMiddle = GUI.CANVAS_HEIGHT/2;
- float moveX = canvasXMiddle-middleX;
- float moveY = canvasYMiddle-middleY;
- Transform xTrans = Transform.newTranslation(moveX,moveY,0);
- //Transform lightTrans = Transform.newTranslation(moveX,moveY,0);
- ArrayList<Polygon>polys=new ArrayList<Polygon>();
- //Vector3D newLightPos=lightTrans.multiply(scene.getLight());
- for (Polygon poly : scene.getPolygons()) {
- Vector3D newPoly[] = new Vector3D[3];//New 3 vertices
- // Vector3D newVec[] = new Vector3D[3];
- //System.out.println("Vert 1 before: "+poly.getVertices()[0]);
- newPoly[0] = xTrans.multiply(poly.getVertices()[0]);//First new vert
- newPoly[1] = xTrans.multiply(poly.getVertices()[1]);//Second new vert
- newPoly[2] = xTrans.multiply(poly.getVertices()[2]);//third new vert
- Polygon newPolygon= new Polygon(newPoly[0], newPoly[1], newPoly[2], poly.getReflectance());
- //System.out.println("Vert 1 after: "+newPoly[0]);
- polys.add(newPolygon);
- }
- //System.out.println("The new light pos is: " + newLightPos);
- return new Scene(polys, scene.getLight());
- // TODO fill this in.
- //return null;
- }
- /**
- * This should scale the scene.
- *
- * @param scene
- * @return
- */
- public static Scene scaleScene(Scene scene) {
- Polygon maxX = scene.getPolygons().get(0);
- Polygon minX = scene.getPolygons().get(0);
- Polygon maxY = scene.getPolygons().get(0);
- Polygon minY = scene.getPolygons().get(0);
- for(Polygon poly:scene.getPolygons()){
- if(poly.getMaxX().x>maxX.getMaxX().x){
- maxX=poly;
- }
- if(poly.getMinX().x<minX.getMinX().x){
- minX=poly;
- }
- if(poly.getMaxY().y>maxY.getMaxY().y){
- maxY=poly;
- }
- if(poly.getMinY().y<minY.getMinY().y){
- minY=poly;
- }
- }
- float diffX=1;
- if(maxX.getMaxX().x-GUI.CANVAS_WIDTH>0 || 0-minX.getMinX().x>0 ){
- if(maxX.getMaxX().x-GUI.CANVAS_WIDTH>0-minX.getMinX().x){
- //diffX=maxX.getMaxX().x-GUI.CANVAS_WIDTH;
- diffX=GUI.CANVAS_WIDTH/maxX.getMaxX().x;
- }
- else{
- //diffX=0-minX.getMinX().x;
- diffX=1/-minX.getMinX().x;
- }
- }
- float diffY=1;
- if(maxY.getMaxY().y-GUI.CANVAS_HEIGHT>0 || 0-minY.getMinY().y>0 ){
- if(maxY.getMaxY().y-GUI.CANVAS_HEIGHT>0-minY.getMinY().y){
- //diffY=maxY.getMaxY().y-GUI.CANVAS_HEIGHT;
- //diffY=diffY/maxY.getMaxY().y;
- diffY=GUI.CANVAS_HEIGHT/maxY.getMaxY().y;
- }
- else{
- //diffY=0-minY.getMinY().y;
- diffY=1/-minY.getMinY().y;
- }
- }
- //System.out.println("The x scale: "+diffX+" The y scale: "+diffY);
- Transform xTrans = Transform.newScale(diffX,diffY,0);
- ArrayList<Polygon>polys=new ArrayList<Polygon>();
- Vector3D newLightPos=xTrans.multiply(scene.getLight());
- for (Polygon poly : scene.getPolygons()) {
- Vector3D newPoly[] = new Vector3D[3];//New 3 vertices
- // Vector3D newVec[] = new Vector3D[3];
- //System.out.println("Vert 1 before: "+poly.getVertices()[0]);
- newPoly[0] = xTrans.multiply(poly.getVertices()[0]);//First new vert
- newPoly[1] = xTrans.multiply(poly.getVertices()[1]);//Second new vert
- newPoly[2] = xTrans.multiply(poly.getVertices()[2]);//third new vert
- Polygon newPolygon= new Polygon(newPoly[0], newPoly[1], newPoly[2], poly.getReflectance());
- //System.out.println("Vert 1 after: "+newPoly[0]);
- polys.add(newPolygon);
- }
- //System.out.println("The new light pos is: " + newLightPos);
- return new Scene(polys, newLightPos);
- // TODO fill this in.
- //return null;
- }
- /**
- * Computes the edgelist of a single provided polygon, as per the lecture
- * slides.
- */
- public static EdgeList computeEdgeList(Polygon poly) {
- // Vector3D v1;
- // Vector3D v2;
- // Vector3D v3;
- // Vector3D vert[] = poly.getVertices();
- Vector3D vert[] = poly.getVertices();
- // v1=vert[0];
- // v2=vert[1];
- // v3=vert[2];
- //System.out.println("Xvalue: " + vert[0].x + "yValue:" + vert[0].y);
- //System.out.println("Xvalue: " + vert[1].x + "yValue:" + vert[1].y);
- // int maxY = (int) Math.max(Math.max(vert[0].y, vert[1].y), vert[2].y);
- // int minY = (int) Math.min(Math.min(vert[0].y, vert[1].y), vert[2].y);
- //System.out.println("Unrounded max: "+Math.max(Math.max(vert[0].y, vert[1].y), vert[2].y));
- int maxY = Math.round( Math.max(Math.max(vert[0].y, vert[1].y), vert[2].y));
- //System.out.println("Rounded max: "+maxY);
- int minY = Math.round(Math.min(Math.min(vert[0].y, vert[1].y), vert[2].y));///////rounding so check ur ass
- //if(maxY>GUI.CANVAS_HEIGHT){//watch out here this is dodgy
- // maxY=GUI.CANVAS_HEIGHT;
- //}
- //if(minY<0){
- // minY=0;
- //}
- EdgeList listEdge = new EdgeList(minY, maxY);
- Vector3D a, b;
- for (int i = 0; i < 3; i++) {// from (0,1),(1,2),(2,0)
- // Vector3D a, b;
- int iPlusOne = i + 1;
- if (iPlusOne == 3) {
- iPlusOne = 0;
- }
- // if (vert[i].y > vert[iPlusOne].y) {// This may be redundant
- // a = vert[i];
- // b = vert[iPlusOne];
- // } else {
- // a = vert[iPlusOne];
- // b = vert[i];
- // }
- a = vert[i];
- b = vert[iPlusOne];
- float xSlope = (b.x - a.x) / (b.y - a.y);
- float zSlope = (b.z - a.z) / (b.y - a.y);
- float x = a.x;
- float z = a.z;
- int y = Math.round(a.y);
- if (a.y < b.y) {
- while (y <= Math.round(b.y)) {
- // xLeft(Y)=x;
- // listEdge.edgeL.put(y, new Float[4]);
- // listEdge.init(y);
- //listEdge.init(y);
- listEdge.addXLeft(y, x);
- // zLeft(Y)=z;
- //listEdge.init(y);
- listEdge.addZLeft(y, z);
- x = x + xSlope;
- z = z + zSlope;
- y++;
- }
- } else {
- while (y >= Math.round(b.y)) {
- // xRight(Y)=x;
- // listEdge.edgeL.put(y, new Float[4]);
- // listEdge.init(y);
- //System.out.println("When Y is: " + y + " Should not be 1500: " + x);
- //listEdge.init(y);
- listEdge.addXRight(y, x);
- // zRight(Y)=z;
- //listEdge.init(y);
- listEdge.addZRight(y, z);
- x = x - xSlope;
- z = z - zSlope;
- y--;
- }
- }
- }
- // TODO fill this in.
- //listEdge.printEdgeList();
- return listEdge;
- }
- /**
- * Fills a zbuffer with the contents of a single edge list according to the
- * lecture slides.
- *
- * The idea here is to make zbuffer and zdepth arrays in your main loop, and
- * pass them into the method to be modified.
- *
- * @param zbuffer
- * A double array of colours representing the Color at each pixel
- * so far.
- * @param zdepth
- * A double array of floats storing the z-value of each pixel
- * that has been coloured in so far.
- * @param polyEdgeList
- * The edgelist of the polygon to add into the zbuffer.
- * @param polyColor
- * The colour of the polygon to add into the zbuffer.
- */
- public static void computeZBuffer(Color[][] zbuffer, float[][] zdepth, EdgeList polyEdgeList, Color polyColor) {
- // TODO fill this in.
- for (int y = polyEdgeList.getStartY(); y < polyEdgeList.endY; y++) {
- // float zSlope=(polyEdgeList.getRightZ(y)-polyEdgeList.getLeftZ(y))/(polyEdgeList.getRightX(y)-polyEdgeList.getLeftX(y));
- float z = polyEdgeList.getLeftZ(y);
- int x = (int)polyEdgeList.getLeftX(y);///////rounding so check asshole
- while (x < Math.round(polyEdgeList.getRightX(y))) {
- if (x >= 0 && y >= 0 && x < GUI.CANVAS_WIDTH && y < GUI.CANVAS_HEIGHT && zdepth[x][y] > z) {
- //if (z < zdepth[x][y]) {
- zbuffer[x][y] = polyColor;
- zdepth[x][y] = z;
- //}
- }
- x++;
- //z=z+zSlope;
- }
- }
- }
- }
- // code for comp261 assignments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement