Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final double zoom = 300;
- int cx, cy;
- class Camera {
- double xpos;
- double ypos;
- double zpos;
- double vrot;
- double hrot;
- public Camera(int x, int y, int z, double v, double h) {
- this.xpos = x;
- this.ypos = y;
- this.zpos = z;
- this.vrot = v;
- this.hrot = h;
- }
- }
- class Vector2 {
- double x;
- double y;
- public Vector2(double axex, double axey) {
- this.x = axex;
- this.y = axey;
- }
- }
- class Vector3 {
- double x;
- double y;
- double z;
- public Vector3(double xv, double yv, double zv) {
- this.x = xv;
- this.y = yv;
- this.z = zv;
- }
- public double scal(Vector3 v) {
- return this.x * v.x + this.y * v.y + this.z*v.z;
- }
- public double absolute() {
- return sqrt((float)(this.x*this.x + this.y*this.y + this.z*this.z));
- }
- public Vector3 mlt(Vector3 v) {
- return new Vector3(this.x*v.x, this.y*v.y, this.z*v.z);
- }
- public Vector3 normalizeVec() {
- return new Vector3(this.x/this.absolute(), this.y/this.absolute(), this.z/this.absolute());
- }
- }
- class Vector2I {
- int x;
- int y;
- public Vector2I(int xv, int yv) {
- this.x = xv;
- this.y = yv;
- }
- }
- class Color3 {
- int red;
- int green;
- int blue;
- public Color3(int r, int g, int b) {
- this.red = r;
- this.green = g;
- this.blue = b;
- }
- }
- Camera cam = new Camera(0, 0, -5, 0, 0);
- Vector3 light = new Vector3(1, 1.2, -1.5);
- int ambiantLight = 50;
- //final int numVerts = 42;
- final int numVerts = 8;
- //final int numEdges = 120;
- final int numEdges = 12;
- //final int numFaces = 80;
- final int numFaces = 12;
- double vertices[][] = {{-1, -1, 1 , 1 , -1, -1, 1 , 1 },
- {-1, 1 , 1 , -1, -1, 1 , 1 , -1},
- {-1, -1, -1, -1, 1 , 1 , 1 , 1 }};
- int edges[][] = {{0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7},
- {1, 4, 2, 5, 3, 6, 0, 7, 5, 6, 7, 4}};
- int faces[][] = {{0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6},
- {1, 2, 4, 5, 3, 7, 2, 1, 7, 3, 5, 4},
- {2, 3, 5, 1, 7, 4, 1, 5, 3, 2, 4, 7}};
- Color3 facesColor[] = { new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255),
- new Color3(255, 255, 255)};
- Vector3 normals[] = new Vector3[numFaces];
- /*Vector3 normals[] = {new Vector3(0, 0, -1),
- new Vector3(0, 0, -1),
- new Vector3(-1, 0, 0),
- new Vector3(-1, 0, 0),
- new Vector3(0, -1, 0),
- new Vector3(0, -1, 0),
- new Vector3(0, 1, 0),
- new Vector3(0, 1, 0),
- new Vector3(1, 0, 0),
- new Vector3(1, 0, 0),
- new Vector3(0, 0, 1),
- new Vector3(0, 0, 1)};*/
- int sortedFaces[] = new int[numFaces];
- boolean drawnFaces[] = new boolean[numFaces];
- double lightPerFace[] = new double[numFaces];
- int vertexCoordinates[][] = new int[2][numVerts];
- double verticesCV[][] = new double[3][numVerts];
- void setup() {
- size(600, 610);
- cx = width/2;
- cy = height/2;
- noStroke();
- background(0);
- frameRate(500);
- light = light.normalizeVec();
- println(light.absolute());
- }
- void keyPressed() {
- double x = sin((float)cam.hrot);
- double y = cos((float)cam.hrot);
- switch (keyCode) {
- case 87: // w
- cam.xpos+=0.2*x;
- cam.zpos+=0.2*y;
- break;
- case 83: // s
- cam.xpos-=0.2*x;
- cam.zpos-=0.2*y;
- break;
- case 65:
- cam.xpos-=0.2*y;
- cam.zpos+=0.2*x;
- break;
- case 68:
- cam.xpos+=0.2*y;
- cam.zpos-=0.2*x;
- break;
- case 32:
- cam.ypos+=0.2;
- break;
- case 17:
- cam.ypos-=0.2;
- break;
- case 37:
- cam.hrot-=0.02;
- break;
- case 39:
- cam.hrot+=0.02;
- break;
- case 38:
- cam.vrot+=0.02;
- break;
- case 40:
- cam.vrot-=0.02;
- break;
- }
- //System.out.println(keyCode);
- }
- void animate() {
- vertices[0][0] = sqrt(2)*cos(radians(225+frameCount/2));
- vertices[0][1] = sqrt(2)*cos(radians(225+frameCount/2));
- vertices[0][2] = sqrt(2)*cos(radians(315+frameCount/2));
- vertices[0][3] = sqrt(2)*cos(radians(315+frameCount/2));
- vertices[0][4] = sqrt(2)*cos(radians(135+frameCount/2));
- vertices[0][5] = sqrt(2)*cos(radians(135+frameCount/2));
- vertices[0][6] = sqrt(2)*cos(radians(45+frameCount/2));
- vertices[0][7] = sqrt(2)*cos(radians(45+frameCount/2));
- vertices[2][0] = sqrt(2)*sin(radians(225+frameCount/2));
- vertices[2][1] = sqrt(2)*sin(radians(225+frameCount/2));
- vertices[2][2] = sqrt(2)*sin(radians(315+frameCount/2));
- vertices[2][3] = sqrt(2)*sin(radians(315+frameCount/2));
- vertices[2][4] = sqrt(2)*sin(radians(135+frameCount/2));
- vertices[2][5] = sqrt(2)*sin(radians(135+frameCount/2));
- vertices[2][6] = sqrt(2)*sin(radians(45+frameCount/2));
- vertices[2][7] = sqrt(2)*sin(radians(45+frameCount/2));
- }
- void draw() {
- background(0);
- noStroke();
- fill(255);
- text("FPS:", 0, 10);
- text(frameRate, 27 , 10);
- animate();
- computeNormals();
- calculate();
- //drawVerts();
- //drawEdges();
- drawFaces();
- drawVec3(new Vector3(1, 0, 0), new Vector3(0, 0, 0), 255, 0, 0);
- drawVec3(new Vector3(0, 1, 0), new Vector3(0, 0, 0), 0, 255, 0);
- drawVec3(new Vector3(0, 0, 1), new Vector3(0, 0, 0), 0, 0, 255);
- /*cam.hrot+=PI/60;
- cam.vrot+=PI/90;*/
- }
- void computeNormals() {
- for (int i = 0; i < numFaces; i++) {
- /*Vector3 vp1 = new Vector3(vertices[0][faces[0][i]], vertices[1][faces[0][i]], vertices[2][faces[0][i]]);
- Vector3 vp2 = new Vector3(vertices[0][faces[1][i]], vertices[1][faces[1][i]], vertices[2][faces[1][i]]);
- Vector3 vp3 = new Vector3(vertices[0][faces[2][i]], vertices[1][faces[2][i]], vertices[2][faces[2][i]]);
- Vector3 normal = (vp1.mlt(vp2)).mlt(vp3);
- normal.x /= normal.absolute();
- normal.y /= normal.absolute();
- normal.z /= normal.absolute();
- normals[i] = normal;*/
- //Vector3 S1S2 = new Vector3(vertices[0][faces[1][i]]-vertices[0][faces[0][i]], vertices[1][faces[1][i]]-vertices[1][faces[0][i]], vertices[2][faces[1][i]]-vertices[2][faces[0][i]]);
- //Vector3 S1S3 = new Vector3(vertices[0][faces[2][i]]-vertices[0][faces[0][i]], vertices[1][faces[2][i]]-vertices[1][faces[0][i]], vertices[2][faces[2][i]]-vertices[2][faces[0][i]]);
- double x1 = vertices[0][faces[0][i]];
- double y1 = vertices[1][faces[0][i]];
- double z1 = vertices[2][faces[0][i]];
- double x2 = vertices[0][faces[1][i]];
- double y2 = vertices[1][faces[1][i]];
- double z2 = vertices[2][faces[1][i]];
- double x3 = vertices[0][faces[2][i]];
- double y3 = vertices[1][faces[2][i]];
- double z3 = vertices[2][faces[2][i]];
- Vector3 normal = new Vector3(0, 0, 0);
- normal.x = (y2-y1)*(z3-z1) - (z2-z1)*(y3-y1);
- normal.y = (z2-z1)*(x3-x1) - (x2-x1)*(z3-z1);
- normal.z = (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1);
- normal.x /= normal.absolute();
- normal.y /= normal.absolute();
- normal.z /= normal.absolute();
- normals[i] = normal;
- }
- }
- Vector2 rotate2d(Vector2 pos, double ang) {
- double x = pos.x;
- double y = pos.y;
- double c = cos((float)ang);
- double s = sin((float)ang);
- return new Vector2(x*c-y*s, y*c+x*s);
- }
- void drawVerts() {
- for (int i = 0; i < numVerts; i++) {
- ellipse(vertexCoordinates[0][i], vertexCoordinates[1][i], 5, 5);
- }
- }
- void drawEdges() {
- for (int i = 0; i < numEdges; i++) {
- fill(255);
- stroke(255);
- line((float)vertexCoordinates[0][edges[0][i]],(float)vertexCoordinates[1][edges[0][i]],(float)vertexCoordinates[0][edges[1][i]],(float)vertexCoordinates[1][edges[1][i]]);
- /*System.out.println(vertexCoordinates[0][edges[0][i]]);
- System.out.println(vertexCoordinates[1][edges[0][i]]);
- System.out.println(vertexCoordinates[0][edges[1][i]]);
- System.out.println(vertexCoordinates[1][edges[1][i]]);*/
- }
- }
- void calculate() {
- double x, y, z;
- for (int i = 0; i < numVerts; i++) {
- x = vertices[0][i];
- y = vertices[1][i];
- z = vertices[2][i];
- x -= cam.xpos;
- y -= cam.ypos;
- z -= cam.zpos;
- Vector2 roth = rotate2d(new Vector2(x, z), cam.hrot);
- x = roth.x;
- z = roth.y;
- Vector2 rotv = rotate2d(new Vector2(y, z), cam.vrot);
- y = rotv.x;
- z = rotv.y;
- verticesCV[0][i] = x;
- verticesCV[1][i] = y;
- verticesCV[2][i] = z;
- double f = zoom/z;
- x *= f;
- y *= f;
- /*System.out.println("x="+x);
- System.out.println("y="+y);*/
- x+=cx;
- y=cy-y;
- vertexCoordinates[0][i] = (int)x;
- vertexCoordinates[1][i] = (int)y;
- }
- }
- Vector2I calculateVert(Vector3 vert) {
- Vector2I onscreen = new Vector2I(0, 0);
- double x = vert.x;
- double y = vert.y;
- double z = vert.z;
- x -= cam.xpos;
- y -= cam.ypos;
- z -= cam.zpos;
- Vector2 roth = rotate2d(new Vector2(x, z), cam.hrot);
- x = roth.x;
- z = roth.y;
- Vector2 rotv = rotate2d(new Vector2(y, z), cam.vrot);
- y = rotv.x;
- z = rotv.y;
- double f = zoom/z;
- x *= f;
- y *= f;
- x+=cx;
- y=cy-y;
- onscreen.x = (int)x;
- onscreen.y = (int)y;
- return onscreen;
- }
- void drawVec3(Vector3 vec, Vector3 origin, int r, int g, int b) {
- Vector2I onscreenorigin = calculateVert(origin);
- Vector2I onscreenEnd = calculateVert(new Vector3(origin.x + vec.x, origin.y + vec.y, origin.z + vec.z));
- stroke(r, g, b);
- line(onscreenorigin.x, onscreenorigin.y, onscreenEnd.x, onscreenEnd.y);
- }
- void drawFaces() {
- backfaceCull();
- depthSort();
- int x0, y0, x1, y1, x2, y2;
- for (int i = numFaces-1; i >= 0; i--) {
- double r = lightPerFace[sortedFaces[i]]*(facesColor[sortedFaces[i]].red);
- if (r < 0) r = 0;
- r+=ambiantLight;
- if (r > 255) r = 255;
- double g = lightPerFace[sortedFaces[i]]*(facesColor[sortedFaces[i]].green);
- if (g < 0) g = 0;
- g+=ambiantLight;
- if (g > 255) g = 255;
- double b = lightPerFace[sortedFaces[i] ]*(facesColor[sortedFaces[i]].blue);
- if (b < 0) b = 0;
- b+=ambiantLight;
- if (b > 255) b = 255;
- fill((int)r, (int)g, (int)b);
- stroke((int)r, (int)g, (int)b);
- stroke(255, 0, 0);
- x0 = vertexCoordinates[0][faces[0][sortedFaces[i]]];
- y0 = vertexCoordinates[1][faces[0][sortedFaces[i]]];
- x1 = vertexCoordinates[0][faces[1][sortedFaces[i]]];
- y1 = vertexCoordinates[1][faces[1][sortedFaces[i]]];
- x2 = vertexCoordinates[0][faces[2][sortedFaces[i]]];
- y2 = vertexCoordinates[1][faces[2][sortedFaces[i]]];
- //System.out.println("Drawed face: "+sortedFaces[i]);
- //System.out.println("Must be drawn? "+drawnFaces[sortedFaces[i]]);
- if (drawnFaces[sortedFaces[i]] || false)
- triangle(x0, y0, x1, y1, x2, y2);
- drawVec3(normals[sortedFaces[i]], new Vector3(vertices[0][faces[0][sortedFaces[i]]], vertices[1][faces[0][sortedFaces[i]]], vertices[2][faces[0][sortedFaces[i]]]), 0, 255, 255);
- drawVec3(normals[sortedFaces[i]], new Vector3(vertices[0][faces[1][sortedFaces[i]]], vertices[1][faces[1][sortedFaces[i]]], vertices[2][faces[1][sortedFaces[i]]]), 0, 255, 255);
- drawVec3(normals[sortedFaces[i]], new Vector3(vertices[0][faces[2][sortedFaces[i]]], vertices[1][faces[2][sortedFaces[i]]], vertices[2][faces[2][sortedFaces[i]]]), 0, 255, 255);
- }
- }
- void backfaceCull() {
- for (int i = 0; i < numFaces; i++) {
- Vector3 camVec = new Vector3(cam.xpos-vertices[0][faces[1][i]],cam.ypos-vertices[1][faces[1][i]],cam.zpos-vertices[2][faces[1][i]]);
- double scal = normals[i].scal(camVec);
- if (scal <= 0) drawnFaces[i] = false;
- else drawnFaces[i] = true;
- lightPerFace[i] = light.scal(normals[i]);
- }
- }
- void depthSort() {
- double mind = 500;
- for (int i = 0; i < numFaces; i++) {
- sortedFaces[i] = i;
- //System.out.println(sortedFaces[i]);
- }
- for (int i = 0; i < numFaces; i++) {
- mind = 500;
- for (int j = i; j < numFaces; j++) {
- //System.out.println(j);
- double depth = verticesCV[2][faces[0][sortedFaces[j]]]*verticesCV[2][faces[1][sortedFaces[j]]]*verticesCV[2][faces[2][sortedFaces[j]]];
- depth/=3;
- if (depth < mind) {
- mind = depth;
- int tmp = sortedFaces[i];
- sortedFaces[i] = sortedFaces[j];
- sortedFaces[j] = tmp;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment