Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package code.renderer;
- import java.awt.Color;
- import java.awt.event.KeyEvent;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import code.renderer.Scene.Polygon;
- public class Renderer extends GUI {
- Scene s;
- Color ambientLightColor;
- Color lightColor;
- @Override
- protected void onLoad(File file) {
- ArrayList<Polygon> poly = new ArrayList<Polygon>();
- Vector3D lightPos;
- // TODO fill this in.
- try {
- // make a reader
- BufferedReader br = new BufferedReader(new FileReader(file));
- String line;
- String tokens1[] = br.readLine().split("\\s+");
- float v_x = Float.parseFloat(tokens1[0]);
- float v_y = Float.parseFloat(tokens1[1]);
- float v_z = Float.parseFloat(tokens1[2]);
- lightPos = new Vector3D(v_x, v_y, v_z);
- // read in each line of the file
- while ((line = br.readLine()) != null) {
- // tokenise the line by splitting it at the tabs.
- String tokens[] = line.split("\\s+");
- float points[] = new float[9];
- int col[] = new int[3];
- // process the tokens
- points[0] = Float.parseFloat(tokens[0]);
- points[1] = Float.parseFloat(tokens[1]);
- points[2] = Float.parseFloat(tokens[2]);
- points[3] = Float.parseFloat(tokens[3]);
- points[4] = Float.parseFloat(tokens[4]);
- points[5] = Float.parseFloat(tokens[5]);
- points[6] = Float.parseFloat(tokens[6]);
- points[7] = Float.parseFloat(tokens[7]);
- points[8] = Float.parseFloat(tokens[8]);
- col[0] = Integer.valueOf(tokens[9]);
- col[1] = Integer.valueOf(tokens[10]);
- col[2] = Integer.valueOf(tokens[11]);
- Polygon newPoly = new Polygon(points, col);
- poly.add(newPoly);
- }
- br.close();
- } catch (IOException e) {
- throw new RuntimeException("file reading failed.");
- }
- s = new Scene(poly, lightPos);
- this.s = Pipeline.translateScene(this.s);
- /*
- * This method should parse the given file into a Scene object, which
- * you store and use to render an image.
- */
- }
- @Override
- protected void onKeyPress(KeyEvent ev) {
- if (ev.getKeyCode() == KeyEvent.VK_A) {
- this.s = Pipeline.rotateScene(this.s, 0f, 0.1f);
- this.redraw();
- }
- if (ev.getKeyCode() == KeyEvent.VK_D) {
- this.s = Pipeline.rotateScene(this.s, 0f, -0.1f);
- this.redraw();
- }
- if (ev.getKeyCode() == KeyEvent.VK_W) {
- this.s = Pipeline.rotateScene(this.s, -0.1f,0f);
- this.redraw();
- }
- if (ev.getKeyCode() == KeyEvent.VK_S) {
- this.s = Pipeline.rotateScene(this.s, 0.1f, 0f);
- this.redraw();
- }
- // TODO fill this in.
- /*
- * This method should be used to rotate the user's viewpoint.
- */
- }
- @Override
- protected BufferedImage render() {
- if (this.s == null) {
- return null;
- }
- //this.s = Pipeline.translateScene(this.s);
- // this.s=Pipeline.scaleScene(this.s);
- int ambientColor[] = getAmbientLight();
- this.ambientLightColor = new Color(ambientColor[0], ambientColor[1], ambientColor[2]);
- this.lightColor = Color.white.darker().darker();;
- // TODO fill this in.
- Color zBuffer[][] = new Color[CANVAS_WIDTH][CANVAS_HEIGHT];
- float zDepth[][] = new float[CANVAS_WIDTH][CANVAS_HEIGHT];
- for (int x = 0; x < CANVAS_WIDTH; x++) {
- for (int y = 0; y < CANVAS_HEIGHT; y++) {
- zBuffer[x][y] = Color.white.darker();
- zDepth[x][y] = Float.POSITIVE_INFINITY;
- }
- }
- for (Polygon poly : s.getPolygons()) {
- if (!Pipeline.isHidden(poly)) {
- EdgeList EL = Pipeline.computeEdgeList(poly);
- // Color zBuffer[][]=new Color[2][2];
- // float zDepth[][]=new float[2][2];
- Color polyCol = Pipeline.getShading(poly, s.getLight(), this.lightColor, this.ambientLightColor);
- Pipeline.computeZBuffer(zBuffer, zDepth, EL, polyCol);
- }
- }
- BufferedImage scene = this.convertBitmapToImage(zBuffer);
- return scene;
- /*
- * This method should put together the pieces of your renderer, as
- * described in the lecture. This will involve calling each of the
- * static method stubs in the Pipeline class, which you also need to
- * fill in.
- */
- // return null;
- }
- /**
- * Converts a 2D array of Colors to a BufferedImage. Assumes that bitmap is
- * indexed by column then row and has imageHeight rows and imageWidth
- * columns. Note that image.setRGB requires x (col) and y (row) are given in
- * that order.
- */
- private BufferedImage convertBitmapToImage(Color[][] bitmap) {
- BufferedImage image = new BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_INT_RGB);
- for (int x = 0; x < CANVAS_WIDTH; x++) {
- for (int y = 0; y < CANVAS_HEIGHT; y++) {
- image.setRGB(x, y, bitmap[x][y].getRGB());
- }
- }
- return image;
- }
- public static void main(String[] args) {
- new Renderer();
- }
- }
- // code for comp261 assignments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement