Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vector {
- float[] pos;
- Vector (float[] pos_) {
- pos=pos_;
- }
- Vector () {}
- void changeVector (float[] pos_) {
- pos=pos_;
- }
- void addFloat (float pos_) {
- if (pos==null) {
- System.err.println("(!) Position of vector is null!");
- } else {
- pos=expand(pos,pos.length+1);
- pos[pos.length-1]=pos_;
- }
- }
- void logSelf() {
- for (int i=0;i<pos.length;i++) {
- println(pos[i] + " ");
- }
- println("-----------------------");
- }
- void logErrorSelf() {
- for (int i=0;i<pos.length;i++) {
- System.err.println(pos[i] + " ");
- }
- System.err.println("-----------------------");
- }
- }
- class Vectorlist {
- Vector[] v;
- Vectorlist (Vector[] v_) {
- v=v_;
- }
- Vectorlist () {}
- void changeVectorlist (Vector[] v_) {
- v=v_;
- }
- void addVector (Vector v_) {
- Vector[] temp;
- if (v==null) {
- System.err.println("(!) Vectorlist is null!");
- } else {
- temp = new Vector[v.length+1];
- for (int i=0;i<v.length+1;i++) {
- temp[i]=v[i];
- }
- temp[temp.length-1]=v_;
- v = new Vector[v.length+1];
- v = temp;
- }
- }
- Vector getVector (int v_) {
- return v[v_];
- }
- float[][] getFloats() {
- float[][] temp = new float[v.length][v[0].pos.length];
- for (int i=0;i<v.length+1;i++) {
- for (int j=0;j<v[0].pos.length+1;j++) {
- temp[i][j]=v[i].pos[j];
- }
- }
- return temp;
- }
- }
- class Matrix {
- float[][] matrix;
- Matrix (float[][] matrix_) {
- matrix=matrix_;
- }
- Matrix (Vectorlist v_) {
- if (v_==null) {
- System.err.println("(!) Vectorlist is null!");
- }else{
- matrix=v_.getFloats();
- }
- }
- Matrix () {}
- Vector multiply (Vector v) {
- if (v.pos.length!=matrix[0].length) {
- System.err.println("(!) Matrix incompatible with vector.");
- System.err.println("");
- System.err.println("Matrix:");
- System.err.println("Size: "+matrix[0].length+"x"+matrix.length);
- logErrorSelf();
- System.err.println("");
- System.err.println("Vector:");
- System.err.println("Size: "+v.pos.length);
- v.logErrorSelf();
- exit();
- return null;
- }
- float[] fo = new float[0];
- Vector vo = new Vector();
- for (int i=0;i<matrix.length;i++) {
- float sum = 0;
- for (int j=0;j<v.pos.length;j++) {
- sum += v.pos[j]*matrix[i][j];
- }
- fo = expand(fo,fo.length+1);
- fo[fo.length-1] = sum;
- }
- vo.changeVector(fo);
- return vo;
- }
- Vectorlist multiply (Vectorlist v_) {
- Vectorlist temp = new Vectorlist();
- if (v_.v==null) {
- System.err.println("(!) Vectorlist is null!");
- return null;
- }else{
- for (int i=0;i<v_.v.length;i++) {
- temp.addVector(multiply(v_.v[i]));
- }
- return temp;
- }
- }
- void logSelf() {
- for (int i=0;i<matrix.length;i++) {
- for (int j=0;j<matrix[0].length;j++) {
- print(matrix[i][j] + " ");
- }
- println();
- }
- println("-----------------------");
- }
- void logErrorSelf() {
- for (int i=0;i<matrix.length;i++) {
- for (int j=0;j<matrix[0].length;j++) {
- System.err.print(matrix[i][j] + " ");
- }
- System.err.println();
- }
- System.err.println("-----------------------");
- }
- }
- Matrix combineMatricies(Matrix a,Matrix b) {
- if (((a==null) || (b==null)) || ((a.matrix==null) || (b.matrix==null))) {
- System.err.println("(!) Matricies are null!");
- exit();
- return null;
- }
- if (a.matrix.length!=b.matrix[0].length) {
- System.err.println("(!) Matricies are incopmpatible.");
- System.err.println("");
- System.err.println("Matrix A:");
- System.err.println("Size: "+a.matrix[0].length+"x"+a.matrix.length);
- a.logErrorSelf();
- System.err.println("");
- System.err.println("Matrix B:");
- System.err.println("Size: "+b.matrix[0].length+"x"+b.matrix.length);
- b.logErrorSelf();
- exit();
- return null;
- }
- Vector vtemp = new Vector();
- Vectorlist ltemp = new Vectorlist();
- Matrix c;
- for (int j=0;j<a.matrix[0].length;j++) {
- for (int i=0;i<a.matrix.length;i++) {
- vtemp.addFloat(a.matrix[i][j]);
- }
- ltemp.addVector(vtemp);
- }
- c = new Matrix(b.multiply(ltemp));
- return c;
- }
- Matrix createTranslation(float x, float y, float z, Vectorlist v_) {
- float[][] temp = {
- {1,0,0,x},
- {0,1,0,y},
- {0,0,1,z}
- };
- for (int i=0;i<v_.v.length;i++) {
- v_.v[i].addFloat(1);
- }
- return new Matrix(temp);
- }
- Matrix createTranslation(float x, float y, float z, Vector v_) {
- float[][] temp = {
- {1,0,0,x},
- {0,1,0,y},
- {0,0,1,z}
- };
- v_.addFloat(1);
- return new Matrix(temp);
- }
- Matrix createRotation(float xt, float yt, float zt) {
- float xc=cos(xt),xs=sin(xt);
- float yc=cos(yt),ys=sin(yt);
- float zc=cos(zt),zs=sin(zt);
- float[][] x = {
- {1,0,0},
- {0,xc,-xs},
- {0,xs,xc}
- };
- float[][] y = {
- {yc,0,ys},
- {0,1,0},
- {-ys,0,yc}
- };
- float[][] z = {
- {zc,-zs,0},
- {zs,zc,0},
- {0,0,1}
- };
- Matrix xRot = new Matrix(x);
- Matrix yRot = new Matrix(y);
- Matrix zRot = new Matrix(z);
- Matrix Rot = combineMatricies(combineMatricies(zRot,yRot),xRot);
- return Rot;
- }
- Matrix createScale(float x, float y, float z) {
- float[][] m = {
- {x,0,0},
- {0,y,0},
- {0,0,z}
- };
- return new Matrix(m);
- }
- class Vertex {
- Matrix p;
- Vector pos;
- Vector renderPos;
- Vertex (Vector pos_) {
- pos=pos_;
- }
- Vertex () {}
- void beforeDraw() {
- renderPos=pos;
- }
- void multiplyMatrix (Matrix m) {
- renderPos = m.multiply(renderPos);
- }
- void render() {
- multiplyMatrix(createTranslation(pos.pos[0],pos.pos[1],pos.pos[2],renderPos));
- float[][] P = {
- {1/renderPos.pos[2],0,0},
- {0,1/renderPos.pos[2],0},
- {0,0,1}
- };
- p = new Matrix(P);
- multiplyMatrix(p);
- point(renderPos.pos[0],renderPos.pos[1]);
- }
- }
- class Object {
- Vertex[] vertexList;
- Vector rot = new Vector();
- Vector scl = new Vector();
- Object (Vertex[] vertexlist, Vector rot_, Vector scl_) {
- vertexList = new Vertex[vertexlist.length];
- vertexList = vertexlist;
- rot = rot_;
- scl = scl_;
- }
- void beforeDraw() {
- for (int i=0;i<vertexList.length+1;i++) {
- vertexList[i].beforeDraw();
- }
- }
- void render() {
- if (vertexList==null) {
- System.err.println("(!) Object can't be displayed!");
- return;
- }
- for (int i=0;i<vertexList.length;i++) {
- if (vertexList[i]==null) {
- System.err.println("(!) Vertex can't be displayed!");
- continue;
- }
- vertexList[i].multiplyMatrix(createRotation(rot.pos[0],rot.pos[1],rot.pos[2]));
- vertexList[i].multiplyMatrix(createScale(scl.pos[0],scl.pos[1],scl.pos[2]));
- vertexList[i].render();
- }
- }
- }
- Object Cube;
- void setup() {
- float[] Rot = {0,0,0};
- float[] Scl = {50,50,50};
- float[] a = {1,-1,1};
- float[] b = {1,-1,-1};
- float[] c = {-1,-1,1};
- float[] d = {-1,-1,-1};
- float[] e = {1,1,1};
- float[] f = {1,1,-1};
- float[] g = {-1,1,1};
- float[] h = {-1,1,-1};
- Vertex[] vertexlist = new Vertex[8];
- vertexlist[0] = new Vertex(new Vector(a));
- vertexlist[1] = new Vertex(new Vector(b));
- vertexlist[2] = new Vertex(new Vector(c));
- vertexlist[3] = new Vertex(new Vector(d));
- vertexlist[4] = new Vertex(new Vector(e));
- vertexlist[5] = new Vertex(new Vector(f));
- vertexlist[6] = new Vertex(new Vector(g));
- vertexlist[7] = new Vertex(new Vector(h));
- Vector rot = new Vector(Rot);
- Vector scl = new Vector(Scl);
- Cube = new Object(vertexlist, rot, scl);
- }
- void beginDraw() {
- Cube.beforeDraw();
- }
- void draw() {
- background(50);
- Cube.render();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement