Advertisement
Guest User

Episode 19 error

a guest
Jan 29th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. package com.mine.minefront.graphics;
  2.  
  3. import java.util.Random;
  4.  
  5. import com.mine.minefront.Game;
  6. import com.mine.minefront.input.Controller;
  7.  
  8. public class Render3D extends Render {
  9.  
  10. public double[] zBuffer;
  11. private double renderDistance = 10000;
  12. private double forward, right, up, cosine, sine;
  13.  
  14. public Render3D(int width, int height) {
  15. super(width, height);
  16. zBuffer = new double[width * height];
  17. }
  18.  
  19. public void floor(Game game) {
  20.  
  21. double floorPosition = 8;
  22. double ceilingPosition = 8;
  23. forward = game.controls.z;
  24. right = game.controls.x;
  25. up = game.controls.y;
  26. double walking = Math.sin(game.time / 6.0) * 0.3;
  27. if (Controller.crouchWalk) {
  28. walking = Math.sin(game.time / 6.0) * 0.15;
  29. } else if (Controller.runWalk) {
  30. walking = Math.sin(game.time / 6.0) * 0.6;
  31. }
  32.  
  33. double rotation = 0;// Math.sin(game.time % 1000.0/
  34. // 80);//game.controls.rotation;
  35. cosine = Math.cos(rotation);
  36. sine = Math.sin(rotation);
  37.  
  38. for (int y = 0; y < height; y++) {
  39. double ceiling = (y - height / 2.0) / height;
  40.  
  41. double z = (floorPosition + up) / ceiling;
  42. if (Controller.walk) {
  43. z = (floorPosition + up + walking) / ceiling;
  44. }
  45.  
  46. if (ceiling < 0) {
  47. z = (ceilingPosition - up) / -ceiling;
  48. if (Controller.walk) {
  49. z = (ceilingPosition - up - walking) / -ceiling;
  50. }
  51. }
  52.  
  53. for (int x = 0; x < width; x++) {
  54. double depth = (x - width / 2.0) / height;
  55. depth *= z;
  56. double xx = depth * cosine + z * sine;
  57. double yy = z * cosine - depth * sine;
  58. int xPix = (int) (xx + right);
  59. int yPix = (int) (yy + forward);
  60. zBuffer[x + y * width] = z;
  61. // pixels[x + y * width] = ((xPix & 15) * 16) | ((yPix & 15) *
  62. // 16) << 8;
  63. pixels[x + y * width] = Texture.floor.pixels[(xPix & 7) + (yPix & 7) * 8];
  64. }
  65. }
  66. }
  67.  
  68. public void renderWall(double xLeft, double xRight, double zDistance, double yHeight) {
  69. double xcLeft = ((xLeft) - right) * 2;
  70. double zcLeft = ((zDistance) - forward) * 2;
  71.  
  72. double rotLeftSideX = xcLeft * cosine - zcLeft * sine;
  73. double yCornerTL = ((-yHeight) - up) * 2;
  74. double yCornerBL = ((+0.5 - yHeight) - up) * 2;
  75. double rotLeftSideZ = zcLeft * cosine * xcLeft * sine;
  76.  
  77. double xcRight = ((xRight) - right) * 2;
  78. double zcRight = ((zDistance) - forward) * 2;
  79.  
  80. double rotRightSideX = xcRight = xcRight * cosine - zcRight * sine;
  81. double yCornerTR = ((-yHeight) - up) * 2;
  82. double yCornerBR = ((+0.5 - yHeight) - up) * 2;
  83. double rotRightSideZ = zcRight * cosine + xcRight * sine;
  84.  
  85. double xPixelLeft = (rotLeftSideX / rotLeftSideZ * height + width / 2);
  86. double xPixelRight = (rotRightSideX / rotRightSideZ * height + width / 2);
  87.  
  88. if (xPixelLeft >= xPixelRight) {
  89. return;
  90. }
  91. int xPixelLeftInt = (int) (xPixelLeft);
  92. int xPixelRightInt = (int) (xPixelRight);
  93.  
  94. if (xPixelLeftInt < 0) {
  95. xPixelLeftInt = 0;
  96. }
  97.  
  98. if (xPixelRight > width) {
  99. xPixelRight = width;
  100. }
  101.  
  102. double yPixelLeftTop = (int) (yCornerTL / rotLeftSideZ * height + height / 2);
  103. double yPixelLeftBottom = (int) (yCornerBL / rotLeftSideZ * height + height / 2);
  104. double yPixelRightTop = (int) (yCornerTR / rotRightSideZ * height + height / 2);
  105. double yPixelRightBottom = (int) (yCornerBR / rotRightSideZ * height + height / 2);
  106.  
  107. for (int x = xPixelLeftInt; x < xPixelRightInt; x++) {
  108. double pixelRotation = (x - xPixelLeft) / (xPixelRight - xPixelLeft);
  109.  
  110. double yPixelTop = yPixelLeftTop + (yPixelRightTop - yPixelLeftTop) * pixelRotation;
  111. double yPixelBottom = yPixelLeftBottom + (yPixelRightBottom - yPixelLeftBottom) * pixelRotation;
  112.  
  113. int yPixelTopInt = (int) (yPixelTop);
  114. int yPixelBottomInt = (int) (yPixelBottom);
  115.  
  116. if (yPixelTopInt < 0) {
  117. yPixelTopInt = 0;
  118. }
  119. if (yPixelTopInt > height) {
  120. yPixelTopInt = height;
  121. }
  122.  
  123. for (int y = yPixelTopInt; y < yPixelBottomInt; y++) {
  124. pixels[x + y * width] = 0x1B91E0;
  125. zBuffer[x + y * width] = 0;
  126. }
  127. }
  128. }
  129.  
  130. public void renderDistanceLimiter() {
  131. for (int i = 0; i < width * height; i++) {
  132. int color = pixels[i];
  133. int brightness = (int) (renderDistance / (zBuffer[i]));
  134.  
  135. if (brightness < 0) {
  136. brightness = 0;
  137. }
  138.  
  139. if (brightness > 255) {
  140. brightness = 255;
  141. }
  142.  
  143. int r = (color >> 16) & 0xff;
  144. int g = (color >> 8) & 0xff;
  145. int b = (color) & 0xff;
  146.  
  147. r = r * brightness / 255;
  148. g = g * brightness / 255;
  149. b = b * brightness / 255;
  150.  
  151. pixels[i] = r << 16 | g << 8 | b;
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement