Advertisement
Marmik

Road.java

Oct 21st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. package ass2.spec;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.jogamp.opengl.GL2;
  7.  
  8. /**
  9. * COMMENT: Comment Road
  10. *
  11. * @author malcolmr
  12. */
  13. public class Road {
  14.  
  15. private List<Double> myPoints;
  16. private double myWidth;
  17. private Terrain myTerrain;
  18.  
  19. /**
  20. * Create a new road starting at the specified point
  21. */
  22. public Road(double width, double x0, double y0) {
  23. myWidth = width;
  24. myPoints = new ArrayList<Double>();
  25. myPoints.add(x0);
  26. myPoints.add(y0);
  27. }
  28.  
  29. public Terrain getTerrain() {
  30. return this.myTerrain;
  31. }
  32.  
  33. public void addTerrain(Terrain t) {
  34. this.myTerrain = t;
  35. }
  36.  
  37. /**
  38. * Create a new road with the specified spine
  39. *
  40. * @param width
  41. * @param spine
  42. */
  43. public Road(double width, double[] spine) {
  44. myWidth = width;
  45. myPoints = new ArrayList<Double>();
  46. for (int i = 0; i < spine.length; i++) {
  47. myPoints.add(spine[i]);
  48. }
  49. }
  50.  
  51. /**
  52. * The width of the road.
  53. *
  54. * @return
  55. */
  56. public double width() {
  57. return myWidth;
  58. }
  59.  
  60. /**
  61. * Add a new segment of road, beginning at the last point added and ending at (x3, y3).
  62. * (x1, y1) and (x2, y2) are interpolated as bezier control points.
  63. *
  64. * @param x1
  65. * @param y1
  66. * @param x2
  67. * @param y2
  68. * @param x3
  69. * @param y3
  70. */
  71. public void addSegment(double x1, double y1, double x2, double y2, double x3, double y3) {
  72. myPoints.add(x1);
  73. myPoints.add(y1);
  74. myPoints.add(x2);
  75. myPoints.add(y2);
  76. myPoints.add(x3);
  77. myPoints.add(y3);
  78. }
  79.  
  80. /**
  81. * Get the number of segments in the curve
  82. *
  83. * @return
  84. */
  85. public int size() {
  86. return myPoints.size() / 6;
  87. }
  88.  
  89. /**
  90. * Get the specified control point.
  91. *
  92. * @param i
  93. * @return
  94. */
  95. public double[] controlPoint(int i) {
  96. double[] p = new double[2];
  97. p[0] = myPoints.get(i*2);
  98. p[1] = myPoints.get(i*2+1);
  99. return p;
  100. }
  101.  
  102. /**
  103. * Get a point on the spine. The parameter t may vary from 0 to size().
  104. * Points on the kth segment take have parameters in the range (k, k+1).
  105. *
  106. * @param t
  107. * @return
  108. */
  109. public double[] point(double t) {
  110. int i = (int)Math.floor(t);
  111. t = t - i;
  112.  
  113. i *= 6;
  114.  
  115. double x0 = myPoints.get(i++);
  116. double y0 = myPoints.get(i++);
  117. double x1 = myPoints.get(i++);
  118. double y1 = myPoints.get(i++);
  119. double x2 = myPoints.get(i++);
  120. double y2 = myPoints.get(i++);
  121. double x3 = myPoints.get(i++);
  122. double y3 = myPoints.get(i++);
  123.  
  124. double[] p = new double[2];
  125.  
  126. p[0] = b(0, t) * x0 + b(1, t) * x1 + b(2, t) * x2 + b(3, t) * x3;
  127. p[1] = b(0, t) * y0 + b(1, t) * y1 + b(2, t) * y2 + b(3, t) * y3;
  128.  
  129. return p;
  130. }
  131.  
  132. /**
  133. * Calculate the Bezier coefficients
  134. *
  135. * @param i
  136. * @param t
  137. * @return
  138. */
  139. private double b(int i, double t) {
  140.  
  141. switch(i) {
  142.  
  143. case 0:
  144. return (1-t) * (1-t) * (1-t);
  145.  
  146. case 1:
  147. return 3 * (1-t) * (1-t) * t;
  148.  
  149. case 2:
  150. return 3 * (1-t) * t * t;
  151.  
  152. case 3:
  153. return t * t * t;
  154. }
  155.  
  156. // this should never happen
  157. throw new IllegalArgumentException("" + i);
  158. }
  159.  
  160.  
  161. public void draw(GL2 gl, double height, double step) {
  162. gl.glColor3f(0,0,0);
  163.  
  164. double halfWidth = this.width()/2;
  165. // Little epsion in height so it isn't IN the ground, but ON the ground
  166. double eps = 0.001;
  167.  
  168. gl.glBegin(GL2.GL_TRIANGLE_STRIP);
  169. for (float i = 0; i < this.size(); i+=step) {
  170. double[] point = this.point(i);
  171. double[] normal = getNormal(i);
  172.  
  173. //gl.glNormal3d(/*help*/, /*me*/, /*please*/);
  174. double xNorm = halfWidth*normal[0];
  175. double zNorm = halfWidth*normal[1];
  176. gl.glVertex3d(-xNorm + point[0], eps + height, -zNorm + point[1]);
  177. gl.glVertex3d(xNorm + point[0], eps + height, zNorm + point[1]);
  178.  
  179. }
  180. gl.glEnd();
  181.  
  182. }
  183.  
  184. public double[] getNormal (double t) {
  185. double[] tangent = this.tangent(t);
  186. double[] normal = new double[]{-tangent[1], tangent[0]};
  187.  
  188.  
  189. // Normalize:
  190. double norm = normal[0]*normal[0] + normal[1]*normal[1];
  191. norm = Math.sqrt(norm);
  192. normal[0] /= norm;
  193. normal[1] /= norm;
  194.  
  195. return normal;
  196. }
  197.  
  198. /****************************************
  199. * STOLE ALL FOLLOWING CODE FROM GITHUB
  200. **************************************/
  201.  
  202.  
  203. /**
  204. * Return the 2D tangent vector of the Bezier curve at instant t
  205. */
  206.  
  207. public double[] tangent(double t) {
  208. double[] tangent = new double[3];
  209. int i = (int)Math.floor(t);
  210. t = t - i;
  211. i *= 6;
  212. double x0 = myPoints.get(i++);
  213. double y0 = myPoints.get(i++);
  214. double x1 = myPoints.get(i++);
  215. double y1 = myPoints.get(i++);
  216. double x2 = myPoints.get(i++);
  217. double y2 = myPoints.get(i++);
  218. double x3 = myPoints.get(i++);
  219. double y3 = myPoints.get(i++);
  220.  
  221. tangent[0] = 3*( b2(0, t) *(x1-x0) + b2(1, t) *(x2 - x1) + b2(2, t) *(x3-x2)) ;
  222. tangent[1] = 3*( b2(0, t) *(y1-y0) + b2(1, t) *(y2 - y1) + b2(2, t) *(y3-y2));
  223.  
  224. return tangent;
  225. }
  226.  
  227. /**
  228. * Calculate the coeficients for the bezier tangent
  229. */
  230.  
  231. private double b2(int i, double t) {
  232. switch(i) {
  233. case 0: return (1-t) * (1-t);
  234. case 1: return 2 * (1-t) * t;
  235. case 2: return t * t;
  236. }
  237. // this should never happen
  238. throw new IllegalArgumentException("" + i);
  239. }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement