Block.pde 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. class Block {
  2. private int id;
  3. private PVector position;
  4. private PVector rotation;
  5. float scale;
  6. public Block(int id, float x, float y, float z) {
  7. this.id = id;
  8. this.position = new PVector(x, y, z);
  9. this.rotation = new PVector(0, 0, 0);
  10. this.scale = 1;
  11. }
  12. public void rotate(float deltaX, float deltaY) {
  13. this.rotation.x += deltaX;
  14. this.rotation.y += deltaY;
  15. }
  16. public void zoom(float delta) {
  17. this.scale += delta;
  18. }
  19. public void draw() {
  20. pushMatrix();
  21. translate(position.x, position.y, position.z);
  22. rotateX(this.rotation.x);
  23. rotateY(this.rotation.y);
  24. scale(this.scale);
  25. beginShape(QUADS);
  26. texture(blockTextures.get(this.id));
  27. // Given one texture and six faces, we can easily set up the uv coordinates
  28. // such that four of the faces tile "perfectly" along either u or v, but the other
  29. // two faces cannot be so aligned. This code tiles "along" u, "around" the X/Z faces
  30. // and fudges the Y faces - the Y faces are arbitrarily aligned such that a
  31. // rotation along the X axis will put the "top" of either texture at the "top"
  32. // of the screen, but is not otherwised aligned with the X/Z faces. (This
  33. // just affects what type of symmetry is required if you need seamless
  34. // tiling all the way around the cube)
  35. // +Z "front" face
  36. vertex(-1, -1, 1, 0, 0);
  37. vertex( 1, -1, 1, 1, 0);
  38. vertex( 1, 1, 1, 1, 1);
  39. vertex(-1, 1, 1, 0, 1);
  40. // -Z "back" face
  41. vertex( 1, -1, -1, 0, 0);
  42. vertex(-1, -1, -1, 1, 0);
  43. vertex(-1, 1, -1, 1, 1);
  44. vertex( 1, 1, -1, 0, 1);
  45. // +Y "bottom" face
  46. vertex(-1, 1, 1, 0, 0);
  47. vertex( 1, 1, 1, 1, 0);
  48. vertex( 1, 1, -1, 1, 1);
  49. vertex(-1, 1, -1, 0, 1);
  50. // -Y "top" face
  51. vertex(-1, -1, -1, 0, 0);
  52. vertex( 1, -1, -1, 1, 0);
  53. vertex( 1, -1, 1, 1, 1);
  54. vertex(-1, -1, 1, 0, 1);
  55. // +X "right" face
  56. vertex( 1, -1, 1, 0, 0);
  57. vertex( 1, -1, -1, 1, 0);
  58. vertex( 1, 1, -1, 1, 1);
  59. vertex( 1, 1, 1, 0, 1);
  60. // -X "left" face
  61. vertex(-1, -1, -1, 0, 0);
  62. vertex(-1, -1, 1, 1, 0);
  63. vertex(-1, 1, 1, 1, 1);
  64. vertex(-1, 1, -1, 0, 1);
  65. endShape();
  66. popMatrix();
  67. }
  68. }