MMBC

processing 3D engine (with some debug)

Jul 28th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.61 KB | None | 0 0
  1. final double zoom = 300;
  2. int cx, cy;
  3.  
  4. class Camera {
  5.   double xpos;
  6.   double ypos;
  7.   double zpos;
  8.   double vrot;
  9.   double hrot;
  10.  
  11.   public Camera(int x, int y, int z, double v, double h) {
  12.     this.xpos = x;
  13.     this.ypos = y;
  14.     this.zpos = z;
  15.     this.vrot = v;
  16.     this.hrot = h;
  17.   }
  18. }
  19.  
  20. class Vector2 {
  21.   double x;
  22.   double y;
  23.  
  24.   public Vector2(double axex, double axey) {
  25.     this.x = axex;
  26.     this.y = axey;
  27.   }
  28. }
  29.  
  30. class Vector3 {
  31.   double x;
  32.   double y;
  33.   double z;
  34.   public Vector3(double xv, double yv, double zv) {
  35.     this.x = xv;
  36.     this.y = yv;
  37.     this.z = zv;
  38.   }
  39.   public double scal(Vector3 v) {
  40.     return  this.x * v.x + this.y * v.y + this.z*v.z;
  41.   }
  42.   public double absolute() {
  43.     return sqrt((float)(this.x*this.x + this.y*this.y + this.z*this.z));
  44.   }
  45.   public Vector3 mlt(Vector3 v) {
  46.     return new Vector3(this.x*v.x, this.y*v.y, this.z*v.z);
  47.   }
  48.   public Vector3 normalizeVec() {
  49.     return new Vector3(this.x/this.absolute(), this.y/this.absolute(), this.z/this.absolute());
  50.   }
  51. }
  52.  
  53. class Vector2I {
  54.   int x;
  55.   int y;
  56.   public Vector2I(int xv, int yv) {
  57.     this.x = xv;
  58.     this.y = yv;
  59.   }
  60. }
  61.  
  62. class Color3 {
  63.   int red;
  64.   int green;
  65.   int blue;
  66.   public Color3(int r, int g, int b) {
  67.     this.red = r;
  68.     this.green = g;
  69.     this.blue = b;
  70.   }
  71. }
  72.  
  73. Camera cam = new Camera(0, 0, -5, 0, 0);
  74. Vector3 light = new Vector3(1, 1.2, -1.5);
  75. int ambiantLight = 50;
  76.  
  77. //final int numVerts = 42;
  78. final int numVerts = 8;
  79. //final int numEdges = 120;
  80. final int numEdges = 12;
  81. //final int numFaces = 80;
  82. final int numFaces = 12;
  83.  
  84. double vertices[][] = {{-1, -1, 1 , 1 , -1, -1, 1 , 1 },
  85.                        {-1, 1 , 1 , -1, -1, 1 , 1 , -1},
  86.                        {-1, -1, -1, -1, 1 , 1 , 1 , 1 }};
  87.  
  88. int edges[][] = {{0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7},
  89.                  {1, 4, 2, 5, 3, 6, 0, 7, 5, 6, 7, 4}};
  90.                  
  91. int faces[][] = {{0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6},
  92.                  {1, 2, 4, 5, 3, 7, 2, 1, 7, 3, 5, 4},
  93.                  {2, 3, 5, 1, 7, 4, 1, 5, 3, 2, 4, 7}};
  94.  
  95. Color3 facesColor[] = { new Color3(255, 255, 255),
  96.                        new Color3(255, 255, 255),
  97.                        new Color3(255, 255, 255),
  98.                        new Color3(255, 255, 255),
  99.                        new Color3(255, 255, 255),
  100.                        new Color3(255, 255, 255),
  101.                        new Color3(255, 255, 255),
  102.                        new Color3(255, 255, 255),
  103.                        new Color3(255, 255, 255),
  104.                        new Color3(255, 255, 255),
  105.                        new Color3(255, 255, 255),
  106.                        new Color3(255, 255, 255)};
  107.  
  108. Vector3 normals[] = new Vector3[numFaces];
  109. /*Vector3 normals[] = {new Vector3(0, 0, -1),
  110.                      new Vector3(0, 0, -1),
  111.                      new Vector3(-1, 0, 0),
  112.                      new Vector3(-1, 0, 0),
  113.                      new Vector3(0, -1, 0),
  114.                      new Vector3(0, -1, 0),
  115.                      new Vector3(0, 1, 0),
  116.                      new Vector3(0, 1, 0),
  117.                      new Vector3(1, 0, 0),
  118.                      new Vector3(1, 0, 0),
  119.                      new Vector3(0, 0, 1),
  120.                      new Vector3(0, 0, 1)};*/
  121.  
  122. int sortedFaces[] = new int[numFaces];
  123. boolean drawnFaces[] = new boolean[numFaces];
  124. double lightPerFace[] = new double[numFaces];
  125.  
  126. int vertexCoordinates[][] = new int[2][numVerts];
  127. double verticesCV[][] = new double[3][numVerts];
  128.  
  129. void setup() {
  130.   size(600, 610);
  131.   cx = width/2;
  132.   cy = height/2;
  133.   noStroke();
  134.   background(0);
  135.   frameRate(500);
  136.   light = light.normalizeVec();
  137.   println(light.absolute());
  138. }
  139.  
  140. void keyPressed() {
  141.  
  142.   double x = sin((float)cam.hrot);
  143.   double y = cos((float)cam.hrot);
  144.   switch (keyCode) {
  145.   case 87: // w
  146.     cam.xpos+=0.2*x;
  147.     cam.zpos+=0.2*y;
  148.     break;
  149.   case 83: // s
  150.     cam.xpos-=0.2*x;
  151.     cam.zpos-=0.2*y;
  152.     break;
  153.   case 65:
  154.     cam.xpos-=0.2*y;
  155.     cam.zpos+=0.2*x;
  156.     break;
  157.   case 68:
  158.     cam.xpos+=0.2*y;
  159.     cam.zpos-=0.2*x;
  160.     break;
  161.   case 32:
  162.     cam.ypos+=0.2;
  163.     break;
  164.   case 17:
  165.     cam.ypos-=0.2;
  166.     break;
  167.   case 37:
  168.     cam.hrot-=0.02;
  169.     break;
  170.   case 39:
  171.     cam.hrot+=0.02;
  172.     break;
  173.   case 38:
  174.     cam.vrot+=0.02;
  175.     break;
  176.   case 40:
  177.     cam.vrot-=0.02;
  178.     break;
  179.   }
  180.   //System.out.println(keyCode);
  181. }
  182.  
  183. void animate() {
  184.   vertices[0][0] = sqrt(2)*cos(radians(225+frameCount/2));
  185.   vertices[0][1] = sqrt(2)*cos(radians(225+frameCount/2));
  186.   vertices[0][2] = sqrt(2)*cos(radians(315+frameCount/2));
  187.   vertices[0][3] = sqrt(2)*cos(radians(315+frameCount/2));
  188.   vertices[0][4] = sqrt(2)*cos(radians(135+frameCount/2));
  189.   vertices[0][5] = sqrt(2)*cos(radians(135+frameCount/2));
  190.   vertices[0][6] = sqrt(2)*cos(radians(45+frameCount/2));
  191.   vertices[0][7] = sqrt(2)*cos(radians(45+frameCount/2));
  192.   vertices[2][0] = sqrt(2)*sin(radians(225+frameCount/2));
  193.   vertices[2][1] = sqrt(2)*sin(radians(225+frameCount/2));
  194.   vertices[2][2] = sqrt(2)*sin(radians(315+frameCount/2));
  195.   vertices[2][3] = sqrt(2)*sin(radians(315+frameCount/2));
  196.   vertices[2][4] = sqrt(2)*sin(radians(135+frameCount/2));
  197.   vertices[2][5] = sqrt(2)*sin(radians(135+frameCount/2));
  198.   vertices[2][6] = sqrt(2)*sin(radians(45+frameCount/2));
  199.   vertices[2][7] = sqrt(2)*sin(radians(45+frameCount/2));
  200. }
  201.  
  202. void draw() {
  203.   background(0);
  204.   noStroke();
  205.   fill(255);
  206.   text("FPS:", 0, 10);
  207.   text(frameRate, 27 , 10);
  208.   animate();
  209.   computeNormals();
  210.   calculate();
  211.   //drawVerts();
  212.   //drawEdges();
  213.   drawFaces();
  214.   drawVec3(new Vector3(1, 0, 0), new Vector3(0, 0, 0), 255, 0, 0);
  215.   drawVec3(new Vector3(0, 1, 0), new Vector3(0, 0, 0), 0, 255, 0);
  216.   drawVec3(new Vector3(0, 0, 1), new Vector3(0, 0, 0), 0, 0, 255);
  217.   /*cam.hrot+=PI/60;
  218.   cam.vrot+=PI/90;*/
  219. }
  220.  
  221. void computeNormals() {
  222.   for (int i = 0; i < numFaces; i++) {
  223.     /*Vector3 vp1 = new Vector3(vertices[0][faces[0][i]], vertices[1][faces[0][i]], vertices[2][faces[0][i]]);
  224.     Vector3 vp2 = new Vector3(vertices[0][faces[1][i]], vertices[1][faces[1][i]], vertices[2][faces[1][i]]);
  225.     Vector3 vp3 = new Vector3(vertices[0][faces[2][i]], vertices[1][faces[2][i]], vertices[2][faces[2][i]]);
  226.     Vector3 normal = (vp1.mlt(vp2)).mlt(vp3);
  227.     normal.x /= normal.absolute();
  228.     normal.y /= normal.absolute();
  229.     normal.z /= normal.absolute();
  230.     normals[i] = normal;*/
  231.     //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]]);
  232.     //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]]);
  233.     double x1 = vertices[0][faces[0][i]];
  234.     double y1 = vertices[1][faces[0][i]];
  235.     double z1 = vertices[2][faces[0][i]];
  236.     double x2 = vertices[0][faces[1][i]];
  237.     double y2 = vertices[1][faces[1][i]];
  238.     double z2 = vertices[2][faces[1][i]];
  239.     double x3 = vertices[0][faces[2][i]];
  240.     double y3 = vertices[1][faces[2][i]];
  241.     double z3 = vertices[2][faces[2][i]];
  242.     Vector3 normal = new Vector3(0, 0, 0);
  243.     normal.x = (y2-y1)*(z3-z1) - (z2-z1)*(y3-y1);
  244.     normal.y = (z2-z1)*(x3-x1) - (x2-x1)*(z3-z1);
  245.     normal.z = (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1);
  246.     normal.x /= normal.absolute();
  247.     normal.y /= normal.absolute();
  248.     normal.z /= normal.absolute();
  249.     normals[i] = normal;
  250.   }
  251. }
  252.  
  253. Vector2 rotate2d(Vector2 pos, double ang) {
  254.   double x = pos.x;
  255.   double y = pos.y;
  256.   double c = cos((float)ang);
  257.   double s = sin((float)ang);
  258.  
  259.   return new Vector2(x*c-y*s, y*c+x*s);
  260. }
  261.  
  262. void drawVerts() {
  263.   for (int i = 0; i < numVerts; i++) {
  264.     ellipse(vertexCoordinates[0][i], vertexCoordinates[1][i], 5, 5);
  265.   }
  266. }
  267.  
  268. void drawEdges() {
  269.   for (int i = 0; i < numEdges; i++) {
  270.     fill(255);
  271.     stroke(255);
  272.     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]]);
  273.     /*System.out.println(vertexCoordinates[0][edges[0][i]]);
  274.     System.out.println(vertexCoordinates[1][edges[0][i]]);
  275.     System.out.println(vertexCoordinates[0][edges[1][i]]);
  276.     System.out.println(vertexCoordinates[1][edges[1][i]]);*/
  277.   }
  278. }
  279.  
  280. void calculate() {
  281.   double x, y, z;
  282.   for (int i = 0; i < numVerts; i++) {
  283.     x = vertices[0][i];
  284.     y = vertices[1][i];
  285.     z = vertices[2][i];
  286.    
  287.    
  288.     x -= cam.xpos;
  289.     y -= cam.ypos;
  290.     z -= cam.zpos;
  291.    
  292.    
  293.     Vector2 roth = rotate2d(new Vector2(x, z), cam.hrot);
  294.     x = roth.x;
  295.     z = roth.y;
  296.     Vector2 rotv = rotate2d(new Vector2(y, z), cam.vrot);
  297.     y = rotv.x;
  298.     z = rotv.y;
  299.    
  300.     verticesCV[0][i] = x;
  301.     verticesCV[1][i] = y;
  302.     verticesCV[2][i] = z;
  303.    
  304.     double f = zoom/z;
  305.    
  306.     x *= f;
  307.     y *= f;
  308.    
  309.     /*System.out.println("x="+x);
  310.     System.out.println("y="+y);*/
  311.    
  312.     x+=cx;
  313.     y=cy-y;
  314.    
  315.    
  316.     vertexCoordinates[0][i] = (int)x;
  317.     vertexCoordinates[1][i] = (int)y;
  318.   }
  319. }
  320.  
  321. Vector2I calculateVert(Vector3 vert) {
  322.   Vector2I onscreen = new Vector2I(0, 0);
  323.   double x = vert.x;
  324.   double y = vert.y;
  325.   double z = vert.z;
  326.  
  327.   x -= cam.xpos;
  328.   y -= cam.ypos;
  329.   z -= cam.zpos;
  330.  
  331.   Vector2 roth = rotate2d(new Vector2(x, z), cam.hrot);
  332.   x = roth.x;
  333.   z = roth.y;
  334.   Vector2 rotv = rotate2d(new Vector2(y, z), cam.vrot);
  335.   y = rotv.x;
  336.   z = rotv.y;
  337.   double f = zoom/z;
  338.    
  339.   x *= f;
  340.   y *= f;
  341.    
  342.   x+=cx;
  343.   y=cy-y;
  344.  
  345.   onscreen.x = (int)x;
  346.   onscreen.y = (int)y;
  347.  
  348.   return onscreen;
  349. }
  350.  
  351. void drawVec3(Vector3 vec, Vector3 origin, int r, int g, int b) {
  352.   Vector2I onscreenorigin = calculateVert(origin);
  353.   Vector2I onscreenEnd = calculateVert(new Vector3(origin.x + vec.x, origin.y + vec.y, origin.z + vec.z));
  354.   stroke(r, g, b);
  355.   line(onscreenorigin.x, onscreenorigin.y, onscreenEnd.x, onscreenEnd.y);
  356. }
  357.  
  358. void drawFaces() {
  359.   backfaceCull();
  360.   depthSort();
  361.   int x0, y0, x1, y1, x2, y2;
  362.   for (int i = numFaces-1; i >= 0; i--) {
  363.     double r = lightPerFace[sortedFaces[i]]*(facesColor[sortedFaces[i]].red);
  364.     if (r < 0) r = 0;
  365.     r+=ambiantLight;
  366.     if (r > 255) r = 255;
  367.     double g = lightPerFace[sortedFaces[i]]*(facesColor[sortedFaces[i]].green);
  368.     if (g < 0) g = 0;
  369.     g+=ambiantLight;
  370.     if (g > 255) g = 255;
  371.     double b = lightPerFace[sortedFaces[i]  ]*(facesColor[sortedFaces[i]].blue);
  372.     if (b < 0) b = 0;
  373.     b+=ambiantLight;
  374.     if (b > 255) b = 255;
  375.     fill((int)r, (int)g, (int)b);
  376.     stroke((int)r, (int)g, (int)b);
  377.     stroke(255, 0, 0);
  378.     x0 = vertexCoordinates[0][faces[0][sortedFaces[i]]];
  379.     y0 = vertexCoordinates[1][faces[0][sortedFaces[i]]];
  380.     x1 = vertexCoordinates[0][faces[1][sortedFaces[i]]];
  381.     y1 = vertexCoordinates[1][faces[1][sortedFaces[i]]];
  382.     x2 = vertexCoordinates[0][faces[2][sortedFaces[i]]];
  383.     y2 = vertexCoordinates[1][faces[2][sortedFaces[i]]];
  384.     //System.out.println("Drawed face: "+sortedFaces[i]);
  385.     //System.out.println("Must be drawn? "+drawnFaces[sortedFaces[i]]);
  386.     if (drawnFaces[sortedFaces[i]] || false)
  387.     triangle(x0, y0, x1, y1, x2, y2);
  388.     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);
  389.     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);
  390.     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);
  391.   }
  392. }
  393.  
  394. void backfaceCull() {
  395.   for (int i = 0; i < numFaces; i++) {
  396.     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]]);
  397.     double scal = normals[i].scal(camVec);
  398.     if (scal <= 0) drawnFaces[i] = false;
  399.     else drawnFaces[i] = true;
  400.     lightPerFace[i] = light.scal(normals[i]);
  401.   }
  402. }
  403.  
  404. void depthSort() {
  405.   double mind = 500;
  406.   for (int i = 0; i < numFaces; i++) {
  407.     sortedFaces[i] = i;
  408.     //System.out.println(sortedFaces[i]);
  409.   }
  410.   for (int i = 0; i < numFaces; i++) {
  411.     mind = 500;
  412.     for (int j = i; j < numFaces; j++) {
  413.       //System.out.println(j);
  414.       double depth = verticesCV[2][faces[0][sortedFaces[j]]]*verticesCV[2][faces[1][sortedFaces[j]]]*verticesCV[2][faces[2][sortedFaces[j]]];
  415.       depth/=3;
  416.       if (depth < mind) {
  417.         mind = depth;
  418.         int tmp = sortedFaces[i];
  419.         sortedFaces[i] = sortedFaces[j];
  420.         sortedFaces[j] = tmp;
  421.       }
  422.     }
  423.   }
  424. }
Advertisement
Add Comment
Please, Sign In to add comment