Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package code.renderer;
- import java.awt.Color;
- import java.util.List;
- /**
- * The Scene class is where we store data about a 3D model and light source
- * inside our renderer. It also contains a static inner class that represents
- * one single polygon.
- *
- * Method stubs have been provided, but you'll need to fill them in.
- *
- * If you were to implement more fancy rendering, e.g. Phong shading, you'd want
- * to store more information in this class.
- */
- public class Scene {
- List<Polygon> polygons;
- Vector3D lightPos;
- public Scene(List<Polygon> polygons, Vector3D lightPos) {
- this.polygons = polygons;
- this.lightPos = lightPos;
- }
- public Vector3D getLight() {
- // TODO fill this in.
- return this.lightPos;
- }
- public List<Polygon> getPolygons() {
- // TODO fill this in.
- return this.polygons;
- }
- /**
- * Polygon stores data about a single polygon in a scene, keeping track of
- * (at least!) its three vertices and its reflectance.
- *
- * This class has been done for you.
- */
- public static class Polygon {
- Vector3D[] vertices;
- Color reflectance;
- /**
- * @param points
- * An array of floats with 9 elements, corresponding to the
- * (x,y,z) coordinates of the three vertices that make up
- * this polygon. If the three vertices are A, B, C then the
- * array should be [A_x, A_y, A_z, B_x, B_y, B_z, C_x, C_y,
- * C_z].
- * @param color
- * An array of three ints corresponding to the RGB values of
- * the polygon, i.e. [r, g, b] where all values are between 0
- * and 255.
- */
- public Polygon(float[] points, int[] color) {
- this.vertices = new Vector3D[3];
- float x, y, z;
- for (int i = 0; i < 3; i++) {
- x = points[i * 3];
- y = points[i * 3 + 1];
- z = points[i * 3 + 2];
- this.vertices[i] = new Vector3D(x, y, z);
- }
- int r = color[0];
- int g = color[1];
- int b = color[2];
- this.reflectance = new Color(r, g, b);
- }
- /**
- * An alternative constructor that directly takes three Vector3D objects
- * and a Color object.
- */
- public Polygon(Vector3D a, Vector3D b, Vector3D c, Color color) {
- this.vertices = new Vector3D[] { a, b, c };
- this.reflectance = color;
- }
- public Vector3D[] getVertices() {
- return vertices;
- }
- public Vector3D[] orderAntiClockWise() {
- Vector3D vert[] = this.vertices;
- float topmostPoint = Math.max(Math.max(vert[0].y, vert[1].y), vert[2].y);
- Vector3D v1;
- Vector3D v2;
- Vector3D v3;
- if (vert[0].y == topmostPoint) {
- v1 = vert[0];
- v2 = vert[1];
- v3 = vert[2];
- } else if (vert[1].y == topmostPoint) {
- v1 = vert[1];
- v2 = vert[0];
- v3 = vert[2];
- } else {
- v1 = vert[2];
- v2 = vert[1];
- v3 = vert[0];
- }
- if (v2.x >= v3.x) {
- // v2 = vert[1];
- // v3 = vert[2];
- } else {
- Vector3D temp = v2;
- v2 = v3;
- v3 = temp;
- }
- Vector3D finalList[] = new Vector3D[3];
- finalList[0] = v1;
- finalList[1] = v2;
- finalList[2] = v3;
- return finalList;
- }
- public Color getReflectance() {
- return reflectance;
- }
- public Vector3D getMinX() {
- Vector3D minX = vertices[0];
- for (Vector3D current : vertices) {
- if (current.x < minX.x) {
- minX = current;
- }
- }
- return minX;
- }
- public Vector3D getMaxX() {
- Vector3D maxX = vertices[0];
- for (Vector3D current : vertices) {
- if (current.x > maxX.x) {
- maxX = current;
- }
- }
- return maxX;
- }
- public Vector3D getMinY() {
- Vector3D minY = vertices[0];
- for (Vector3D current : vertices) {
- if (current.y < minY.y) {
- minY = current;
- }
- }
- return minY;
- }
- public Vector3D getMaxY() {
- Vector3D maxY = vertices[0];
- for (Vector3D current : vertices) {
- if (current.y > maxY.y) {
- maxY = current;
- }
- }
- return maxY;
- }
- @Override
- public String toString() {
- String str = "polygon:";
- for (Vector3D p : vertices)
- str += "\n " + p.toString();
- str += "\n " + reflectance.toString();
- return str;
- }
- }
- }
- // code for COMP261 assignments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement