Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. package code.renderer;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.image.BufferedImage;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11.  
  12. import code.renderer.Scene.Polygon;
  13.  
  14. public class Renderer extends GUI {
  15.     Scene s;
  16.  
  17.     Color ambientLightColor;
  18.     Color lightColor;
  19.  
  20.     @Override
  21.     protected void onLoad(File file) {
  22.         ArrayList<Polygon> poly = new ArrayList<Polygon>();
  23.         Vector3D lightPos;
  24.  
  25.         // TODO fill this in.
  26.         try {
  27.             // make a reader
  28.             BufferedReader br = new BufferedReader(new FileReader(file));
  29.             String line;
  30.  
  31.             String tokens1[] = br.readLine().split("\\s+");
  32.             float v_x = Float.parseFloat(tokens1[0]);
  33.             float v_y = Float.parseFloat(tokens1[1]);
  34.             float v_z = Float.parseFloat(tokens1[2]);
  35.             lightPos = new Vector3D(v_x, v_y, v_z);
  36.  
  37.             // read in each line of the file
  38.             while ((line = br.readLine()) != null) {
  39.                 // tokenise the line by splitting it at the tabs.
  40.                 String tokens[] = line.split("\\s+");
  41.                 float points[] = new float[9];
  42.                 int col[] = new int[3];
  43.  
  44.                 // process the tokens
  45.                 points[0] = Float.parseFloat(tokens[0]);
  46.                 points[1] = Float.parseFloat(tokens[1]);
  47.                 points[2] = Float.parseFloat(tokens[2]);
  48.  
  49.                 points[3] = Float.parseFloat(tokens[3]);
  50.                 points[4] = Float.parseFloat(tokens[4]);
  51.                 points[5] = Float.parseFloat(tokens[5]);
  52.  
  53.                 points[6] = Float.parseFloat(tokens[6]);
  54.                 points[7] = Float.parseFloat(tokens[7]);
  55.                 points[8] = Float.parseFloat(tokens[8]);
  56.  
  57.                 col[0] = Integer.valueOf(tokens[9]);
  58.                 col[1] = Integer.valueOf(tokens[10]);
  59.                 col[2] = Integer.valueOf(tokens[11]);
  60.  
  61.                 Polygon newPoly = new Polygon(points, col);
  62.                
  63.                
  64.  
  65.                
  66.                     poly.add(newPoly);
  67.                
  68.             }
  69.  
  70.             br.close();
  71.         } catch (IOException e) {
  72.             throw new RuntimeException("file reading failed.");
  73.         }
  74.         s = new Scene(poly, lightPos);
  75.         this.s = Pipeline.translateScene(this.s);
  76.  
  77.         /*
  78.          * This method should parse the given file into a Scene object, which
  79.          * you store and use to render an image.
  80.          */
  81.     }
  82.  
  83.     @Override
  84.     protected void onKeyPress(KeyEvent ev) {
  85.         if (ev.getKeyCode() == KeyEvent.VK_A) {
  86.             this.s = Pipeline.rotateScene(this.s, 0f, 0.1f);
  87.             this.redraw();
  88.         }
  89.         if (ev.getKeyCode() == KeyEvent.VK_D) {
  90.             this.s = Pipeline.rotateScene(this.s, 0f, -0.1f);
  91.             this.redraw();
  92.         }
  93.         if (ev.getKeyCode() == KeyEvent.VK_W) {
  94.             this.s = Pipeline.rotateScene(this.s, -0.1f,0f);
  95.             this.redraw();
  96.         }
  97.         if (ev.getKeyCode() == KeyEvent.VK_S) {
  98.             this.s = Pipeline.rotateScene(this.s, 0.1f, 0f);
  99.             this.redraw();
  100.         }
  101.         // TODO fill this in.
  102.  
  103.         /*
  104.          * This method should be used to rotate the user's viewpoint.
  105.          */
  106.     }
  107.  
  108.     @Override
  109.     protected BufferedImage render() {
  110.         if (this.s == null) {
  111.             return null;
  112.         }
  113.  
  114.         //this.s = Pipeline.translateScene(this.s);
  115.  
  116.         // this.s=Pipeline.scaleScene(this.s);
  117.        
  118.  
  119.         int ambientColor[] = getAmbientLight();
  120.         this.ambientLightColor = new Color(ambientColor[0], ambientColor[1], ambientColor[2]);
  121.         this.lightColor = Color.white.darker().darker();;
  122.  
  123.         // TODO fill this in.
  124.         Color zBuffer[][] = new Color[CANVAS_WIDTH][CANVAS_HEIGHT];
  125.         float zDepth[][] = new float[CANVAS_WIDTH][CANVAS_HEIGHT];
  126.         for (int x = 0; x < CANVAS_WIDTH; x++) {
  127.             for (int y = 0; y < CANVAS_HEIGHT; y++) {
  128.                 zBuffer[x][y] = Color.white.darker();
  129.                 zDepth[x][y] = Float.POSITIVE_INFINITY;
  130.             }
  131.         }
  132.  
  133.         for (Polygon poly : s.getPolygons()) {
  134.             if (!Pipeline.isHidden(poly)) {
  135.                 EdgeList EL = Pipeline.computeEdgeList(poly);
  136.                 // Color zBuffer[][]=new Color[2][2];
  137.                 // float zDepth[][]=new float[2][2];
  138.                 Color polyCol = Pipeline.getShading(poly, s.getLight(), this.lightColor, this.ambientLightColor);
  139.                 Pipeline.computeZBuffer(zBuffer, zDepth, EL, polyCol);
  140.             }
  141.  
  142.         }
  143.         BufferedImage scene = this.convertBitmapToImage(zBuffer);
  144.         return scene;
  145.  
  146.         /*
  147.          * This method should put together the pieces of your renderer, as
  148.          * described in the lecture. This will involve calling each of the
  149.          * static method stubs in the Pipeline class, which you also need to
  150.          * fill in.
  151.          */
  152.         // return null;
  153.     }
  154.  
  155.     /**
  156.      * Converts a 2D array of Colors to a BufferedImage. Assumes that bitmap is
  157.      * indexed by column then row and has imageHeight rows and imageWidth
  158.      * columns. Note that image.setRGB requires x (col) and y (row) are given in
  159.      * that order.
  160.      */
  161.     private BufferedImage convertBitmapToImage(Color[][] bitmap) {
  162.         BufferedImage image = new BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_INT_RGB);
  163.         for (int x = 0; x < CANVAS_WIDTH; x++) {
  164.             for (int y = 0; y < CANVAS_HEIGHT; y++) {
  165.                 image.setRGB(x, y, bitmap[x][y].getRGB());
  166.             }
  167.         }
  168.         return image;
  169.     }
  170.  
  171.     public static void main(String[] args) {
  172.         new Renderer();
  173.     }
  174. }
  175.  
  176. // code for comp261 assignments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement