Advertisement
Marmik

Road.java

Oct 22nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 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. private String textureFileName = "/Users/macbookpro/Desktop/JavaProjects/3421ass2/src/ass2/spec/textures/pavement.jpg";
  19. // private String textureFileName = "/Users/Anna/Desktop/COMP3421-Ass2/src/ass2/spec/textures/pavement.jpg";
  20. private String textureExt = "jpg";
  21. private MyTexture myTextures[];
  22.  
  23. /**
  24. * Create a new road starting at the specified point
  25. */
  26. public Road(double width, double x0, double y0) {
  27. myWidth = width;
  28. myPoints = new ArrayList<Double>();
  29. myPoints.add(x0);
  30. myPoints.add(y0);
  31. }
  32.  
  33. public Terrain getTerrain() {
  34. return this.myTerrain;
  35. }
  36.  
  37. public void addTerrain(Terrain t) {
  38. this.myTerrain = t;
  39. }
  40.  
  41. /**
  42. * Create a new road with the specified spine
  43. *
  44. * @param width
  45. * @param spine
  46. */
  47. public Road(double width, double[] spine) {
  48. myWidth = width;
  49. myPoints = new ArrayList<Double>();
  50. for (int i = 0; i < spine.length; i++) {
  51. myPoints.add(spine[i]);
  52. }
  53. }
  54.  
  55. /**
  56. * The width of the road.
  57. *
  58. * @return
  59. */
  60. public double width() {
  61. return myWidth;
  62. }
  63.  
  64. /**
  65. * Add a new segment of road, beginning at the last point added and ending at (x3, y3).
  66. * (x1, y1) and (x2, y2) are interpolated as bezier control points.
  67. *
  68. * @param x1
  69. * @param y1
  70. * @param x2
  71. * @param y2
  72. * @param x3
  73. * @param y3
  74. */
  75. public void addSegment(double x1, double y1, double x2, double y2, double x3, double y3) {
  76. myPoints.add(x1);
  77. myPoints.add(y1);
  78. myPoints.add(x2);
  79. myPoints.add(y2);
  80. myPoints.add(x3);
  81. myPoints.add(y3);
  82. }
  83.  
  84. /**
  85. * Get the number of segments in the curve
  86. *
  87. * @return
  88. */
  89. public int size() {
  90. return myPoints.size() / 6;
  91. }
  92.  
  93. /**
  94. * Get the specified control point.
  95. *
  96. * @param i
  97. * @return
  98. */
  99. public double[] controlPoint(int i) {
  100. double[] p = new double[2];
  101. p[0] = myPoints.get(i*2);
  102. p[1] = myPoints.get(i*2+1);
  103. return p;
  104. }
  105.  
  106. /**
  107. * Get a point on the spine. The parameter t may vary from 0 to size().
  108. * Points on the kth segment take have parameters in the range (k, k+1).
  109. *
  110. * @param t
  111. * @return
  112. */
  113. public double[] point(double t) {
  114. int i = (int)Math.floor(t);
  115. t = t - i;
  116.  
  117. i *= 6;
  118.  
  119. double x0 = myPoints.get(i++);
  120. double y0 = myPoints.get(i++);
  121. double x1 = myPoints.get(i++);
  122. double y1 = myPoints.get(i++);
  123. double x2 = myPoints.get(i++);
  124. double y2 = myPoints.get(i++);
  125. double x3 = myPoints.get(i++);
  126. double y3 = myPoints.get(i++);
  127.  
  128. double[] p = new double[2];
  129.  
  130. p[0] = b(0, t) * x0 + b(1, t) * x1 + b(2, t) * x2 + b(3, t) * x3;
  131. p[1] = b(0, t) * y0 + b(1, t) * y1 + b(2, t) * y2 + b(3, t) * y3;
  132.  
  133. return p;
  134. }
  135.  
  136. /**
  137. * Calculate the Bezier coefficients
  138. *
  139. * @param i
  140. * @param t
  141. * @return
  142. */
  143. private double b(int i, double t) {
  144.  
  145. switch(i) {
  146.  
  147. case 0:
  148. return (1-t) * (1-t) * (1-t);
  149.  
  150. case 1:
  151. return 3 * (1-t) * (1-t) * t;
  152.  
  153. case 2:
  154. return 3 * (1-t) * t * t;
  155.  
  156. case 3:
  157. return t * t * t;
  158. }
  159.  
  160. // this should never happen
  161. throw new IllegalArgumentException("" + i);
  162. }
  163.  
  164. public void loadTexture (GL2 gl){
  165. myTextures = new MyTexture[1];
  166. myTextures[0] = new MyTexture(gl,textureFileName,textureExt,true);
  167. }
  168.  
  169.  
  170. public void draw(GL2 gl, double height, double step) {
  171. gl.glColor3f(0,0,0);
  172. addMaterialLighting(gl);
  173. double halfWidth = this.width()/2;
  174. // Little epsion in height so it isn't IN the ground, but ON the ground
  175. double eps = 0.001;
  176.  
  177. gl.glEnable(GL2.GL_TEXTURE_2D);
  178. gl.glBindTexture(GL2.GL_TEXTURE_2D, myTextures[0].getTextureId());
  179.  
  180. gl.glBegin(GL2.GL_TRIANGLE_STRIP);
  181. for (float i = 0; i < this.size(); i+=step) {
  182. double[] point = this.point(i);
  183. double[] normal = getNormal(i);
  184.  
  185. //gl.glNormal3d(/*help*/, /*me*/, /*please*/);
  186. double xNorm = halfWidth*normal[0];
  187. double zNorm = halfWidth*normal[1];
  188. gl.glNormal3d(0, 1, 0);
  189. gl.glTexCoord2d(-xNorm + point[0], -zNorm + point[1]);
  190. gl.glVertex3d(-xNorm + point[0], eps + height, -zNorm + point[1]);
  191. gl.glTexCoord2d(xNorm + point[0], zNorm + point[1]);
  192. gl.glVertex3d(xNorm + point[0], eps + height, zNorm + point[1]);
  193.  
  194. }
  195. gl.glEnd();
  196.  
  197. }
  198.  
  199. public void addMaterialLighting(GL2 gl){
  200. float matAmbAndDif[] = {0.6f, 0.6f, 0.6f, 1.0f};
  201. float matSpec[] = { 0.4f, 0.4f, 0.4f, 1.0f };
  202. float matShine[] = { 10.0f };
  203. float emm[] = {0.0f, 0.0f, 0.0f, 1.0f};
  204. // Material properties of road.
  205. gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, matAmbAndDif,0);
  206. gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, matSpec,0);
  207. gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SHININESS, matShine,0);
  208. gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_EMISSION, emm,0);
  209. }
  210.  
  211. public double[] getNormal (double t) {
  212. double[] tangent = this.tangent(t);
  213. double[] normal = new double[]{-tangent[1], tangent[0]};
  214.  
  215.  
  216. // Normalize:
  217. double norm = normal[0]*normal[0] + normal[1]*normal[1];
  218. norm = Math.sqrt(norm);
  219. normal[0] /= norm;
  220. normal[1] /= norm;
  221.  
  222. return normal;
  223. }
  224.  
  225.  
  226. /**
  227. * Return the 2D tangent vector of the Bezier curve at instant t
  228. */
  229.  
  230. public double[] tangent(double t) {
  231. double[] tangent = new double[3];
  232. int i = (int)Math.floor(t);
  233. t = t - i;
  234. i *= 6;
  235. double x0 = myPoints.get(i++);
  236. double y0 = myPoints.get(i++);
  237. double x1 = myPoints.get(i++);
  238. double y1 = myPoints.get(i++);
  239. double x2 = myPoints.get(i++);
  240. double y2 = myPoints.get(i++);
  241. double x3 = myPoints.get(i++);
  242. double y3 = myPoints.get(i++);
  243.  
  244. double[] a = {b2(0, t), b2(1, t), b2(2, t)};
  245. double[] b = {(x1-x0), (x2-x1), (x3-x2)};
  246. double[] c = {(y1-y0), (y2-y1), (y3-y2)};
  247.  
  248. double calculation1 = (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
  249. double calculation2 = (a[0] * c[0]) + (a[1] * c[1]) + (a[2] * c[2]);
  250.  
  251. tangent[0] = 3 * calculation1;
  252. tangent[1] = 3 * calculation2;
  253. return tangent;
  254. }
  255.  
  256. /**
  257. * Calculate the coeficients for the bezier tangent
  258. */
  259.  
  260. private double b2(int i, double t) {
  261. switch(i) {
  262. case 0: return (1-t) * (1-t);
  263. case 1: return 2 * (1-t) * t;
  264. case 2: return t * t;
  265. }
  266. // this should never happen
  267. throw new IllegalArgumentException("" + i);
  268. }
  269.  
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement