Advertisement
Guest User

Untitled

a guest
May 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. package se.teamgroup.tggps;
  2.  
  3. import java.awt.geom.Point2D;
  4. import java.nio.FloatBuffer;
  5. import java.nio.IntBuffer;
  6. import org.jon3d.math.Quaternion;
  7. import org.jon3d.math.Vector3;
  8. import org.jon3d.renderer.scene.Geometry;
  9. import org.jon3d.renderer.scene.GeometryBuffer;
  10. import org.jon3d.renderer.scene.GeometryNode;
  11. import org.jon3d.renderer.scene.Material;
  12. import org.jon3d.renderer.scene.Shader;
  13. import org.jon3d.renderer.scene.ShaderProgram;
  14. import org.jon3d.renderer.scene.TextureMap;
  15. import org.jon3d.renderer.scene.Transform;
  16. import org.jon3d.renderer.scene.Shader.Type;
  17. import org.lwjgl.BufferUtils;
  18.  
  19. /**
  20. * Will take care of any given roads geometry. This functionality is specific to
  21. * Jon3D.
  22. * <p>
  23. * TODO: Create a uniform so that the road knows where its neighbors are so that
  24. * it can draw the road appropriately.
  25. *
  26. * @author Jon Kristensen
  27. */
  28. public class RoadGeometryNode extends GeometryNode
  29. {
  30. private static final Material ROAD_MATERIAL;
  31.  
  32. // Initialize the roadMaterial object by setting up the shader program.
  33. static
  34. {
  35. Shader[] shaders = new Shader[2];
  36. Shader shader0 =
  37. new Shader("RoadVertexShader",
  38. "void main()\n{\n\tgl_Position = ftransform();\n}\n",
  39. Type.VERTEX);
  40. Shader shader1 =
  41. new Shader(
  42. "RoadFragmentShader",
  43. "void main()\n{\n gl_FragColor = gl_FragColor = vec4(1.0, 1.0, 1.0, 0.2);\n}\n",
  44. Type.FRAGMENT);
  45. shaders[0] = shader0;
  46. shaders[1] = shader1;
  47.  
  48. ShaderProgram shaderProgram =
  49. new ShaderProgram("RoadShaderProgram", shaders);
  50.  
  51. TextureMap[] textureMaps = new TextureMap[0];
  52.  
  53. ROAD_MATERIAL = new Material(shaderProgram, textureMaps);
  54. }
  55.  
  56. public static final float ROAD_WIDTH = 0.003f;
  57.  
  58. /**
  59. * Constructor.
  60. *
  61. * @param start the position of the start position
  62. * @param end the position of the end position
  63. */
  64. public RoadGeometryNode(Point2D start, Point2D end)
  65. {
  66. super(createGeometryBuffer(start, end), ROAD_MATERIAL, new Transform());
  67. }
  68.  
  69. /**
  70. * Creates a GeometryBuffer for this given road.
  71. *
  72. * @param start the position of the start position
  73. * @param end the position of the end position
  74. * @return a GeometryNode for this road
  75. */
  76. private static GeometryBuffer createGeometryBuffer(Point2D start,
  77. Point2D end)
  78. {
  79. // Zero degree angle is to the right. Counter-clockwise.
  80. double angle =
  81. Math
  82. .atan2(end.getY() - start.getY(), end.getX()
  83. - start.getX());
  84.  
  85. IntBuffer indices = BufferUtils.createIntBuffer(6);
  86. FloatBuffer vertexPositions = BufferUtils.createFloatBuffer(12);
  87. FloatBuffer normals = BufferUtils.createFloatBuffer(12);
  88. FloatBuffer textureCoordinates = BufferUtils.createFloatBuffer(8);
  89.  
  90. indices.put(0);
  91. indices.put(1);
  92. indices.put(2);
  93. indices.put(2);
  94. indices.put(1);
  95. indices.put(3);
  96.  
  97. /*
  98. * Here is the nearest right vertex looking down the road from the start
  99. * perspective.
  100. *
  101. * "start.getX()" is the initial x value. "ROAD_WIDTH / 2 *
  102. * Math.cos(angle)" is the amount to add due to the angle. "ROAD_WIDTH
  103. * / 2 * Math.sin(angle)" is the amount to add to the y value.
  104. */
  105. vertexPositions.put((float) (start.getX() + (ROAD_WIDTH / 2 * Math
  106. .sin(angle))));
  107. vertexPositions.put((float) (start.getY() + (ROAD_WIDTH / 2 * Math
  108. .cos(angle))));
  109. vertexPositions.put(0f);
  110.  
  111. /*
  112. * The nearest left vertex looking down the road from the start
  113. * perspective.
  114. */
  115. vertexPositions.put((float) (start.getX() - (ROAD_WIDTH / 2 * Math
  116. .sin(angle))));
  117. vertexPositions.put((float) (start.getY() - (ROAD_WIDTH / 2 * Math
  118. .cos(angle))));
  119. vertexPositions.put(0f);
  120.  
  121. /*
  122. * The farthest right vertex looking down the road from the start
  123. * perspective.
  124. */
  125. vertexPositions.put((float) (end.getX() + (ROAD_WIDTH / 2 * Math
  126. .sin(angle))));
  127. vertexPositions.put((float) (end.getY() + (ROAD_WIDTH / 2 * Math
  128. .cos(angle))));
  129. vertexPositions.put(0f);
  130.  
  131. /*
  132. * The farthest left vertex looking down the road from the start
  133. * perspective.
  134. */
  135. vertexPositions.put((float) (end.getX() - (ROAD_WIDTH / 2 * Math
  136. .sin(angle))));
  137. vertexPositions.put((float) (end.getY() - (ROAD_WIDTH / 2 * Math
  138. .cos(angle))));
  139. vertexPositions.put(0f);
  140.  
  141. for(int i = 0; i < 4; i++)
  142. {
  143. normals.put(0f);
  144. normals.put(0f);
  145. normals.put(1f);
  146. }
  147.  
  148. textureCoordinates.put(1);
  149. textureCoordinates.put(0);
  150. textureCoordinates.put(0);
  151. textureCoordinates.put(0);
  152. textureCoordinates.put(1);
  153. textureCoordinates.put(1);
  154. textureCoordinates.put(0);
  155. textureCoordinates.put(1);
  156.  
  157. indices.flip();
  158. vertexPositions.flip();
  159. normals.flip();
  160. textureCoordinates.flip();
  161.  
  162. return new GeometryBuffer("RoadGeometry", new Geometry(indices,
  163. vertexPositions, normals, textureCoordinates));
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement