Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.teamgroup.tggps;
- import java.awt.geom.Point2D;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import org.jon3d.math.Quaternion;
- import org.jon3d.math.Vector3;
- import org.jon3d.renderer.scene.Geometry;
- import org.jon3d.renderer.scene.GeometryBuffer;
- import org.jon3d.renderer.scene.GeometryNode;
- import org.jon3d.renderer.scene.Material;
- import org.jon3d.renderer.scene.Shader;
- import org.jon3d.renderer.scene.ShaderProgram;
- import org.jon3d.renderer.scene.TextureMap;
- import org.jon3d.renderer.scene.Transform;
- import org.jon3d.renderer.scene.Shader.Type;
- import org.lwjgl.BufferUtils;
- /**
- * Will take care of any given roads geometry. This functionality is specific to
- * Jon3D.
- * <p>
- * TODO: Create a uniform so that the road knows where its neighbors are so that
- * it can draw the road appropriately.
- *
- * @author Jon Kristensen
- */
- public class RoadGeometryNode extends GeometryNode
- {
- private static final Material ROAD_MATERIAL;
- // Initialize the roadMaterial object by setting up the shader program.
- static
- {
- Shader[] shaders = new Shader[2];
- Shader shader0 =
- new Shader("RoadVertexShader",
- "void main()\n{\n\tgl_Position = ftransform();\n}\n",
- Type.VERTEX);
- Shader shader1 =
- new Shader(
- "RoadFragmentShader",
- "void main()\n{\n gl_FragColor = gl_FragColor = vec4(1.0, 1.0, 1.0, 0.2);\n}\n",
- Type.FRAGMENT);
- shaders[0] = shader0;
- shaders[1] = shader1;
- ShaderProgram shaderProgram =
- new ShaderProgram("RoadShaderProgram", shaders);
- TextureMap[] textureMaps = new TextureMap[0];
- ROAD_MATERIAL = new Material(shaderProgram, textureMaps);
- }
- public static final float ROAD_WIDTH = 0.003f;
- /**
- * Constructor.
- *
- * @param start the position of the start position
- * @param end the position of the end position
- */
- public RoadGeometryNode(Point2D start, Point2D end)
- {
- super(createGeometryBuffer(start, end), ROAD_MATERIAL, new Transform());
- }
- /**
- * Creates a GeometryBuffer for this given road.
- *
- * @param start the position of the start position
- * @param end the position of the end position
- * @return a GeometryNode for this road
- */
- private static GeometryBuffer createGeometryBuffer(Point2D start,
- Point2D end)
- {
- // Zero degree angle is to the right. Counter-clockwise.
- double angle =
- Math
- .atan2(end.getY() - start.getY(), end.getX()
- - start.getX());
- IntBuffer indices = BufferUtils.createIntBuffer(6);
- FloatBuffer vertexPositions = BufferUtils.createFloatBuffer(12);
- FloatBuffer normals = BufferUtils.createFloatBuffer(12);
- FloatBuffer textureCoordinates = BufferUtils.createFloatBuffer(8);
- indices.put(0);
- indices.put(1);
- indices.put(2);
- indices.put(2);
- indices.put(1);
- indices.put(3);
- /*
- * Here is the nearest right vertex looking down the road from the start
- * perspective.
- *
- * "start.getX()" is the initial x value. "ROAD_WIDTH / 2 *
- * Math.cos(angle)" is the amount to add due to the angle. "ROAD_WIDTH
- * / 2 * Math.sin(angle)" is the amount to add to the y value.
- */
- vertexPositions.put((float) (start.getX() + (ROAD_WIDTH / 2 * Math
- .sin(angle))));
- vertexPositions.put((float) (start.getY() + (ROAD_WIDTH / 2 * Math
- .cos(angle))));
- vertexPositions.put(0f);
- /*
- * The nearest left vertex looking down the road from the start
- * perspective.
- */
- vertexPositions.put((float) (start.getX() - (ROAD_WIDTH / 2 * Math
- .sin(angle))));
- vertexPositions.put((float) (start.getY() - (ROAD_WIDTH / 2 * Math
- .cos(angle))));
- vertexPositions.put(0f);
- /*
- * The farthest right vertex looking down the road from the start
- * perspective.
- */
- vertexPositions.put((float) (end.getX() + (ROAD_WIDTH / 2 * Math
- .sin(angle))));
- vertexPositions.put((float) (end.getY() + (ROAD_WIDTH / 2 * Math
- .cos(angle))));
- vertexPositions.put(0f);
- /*
- * The farthest left vertex looking down the road from the start
- * perspective.
- */
- vertexPositions.put((float) (end.getX() - (ROAD_WIDTH / 2 * Math
- .sin(angle))));
- vertexPositions.put((float) (end.getY() - (ROAD_WIDTH / 2 * Math
- .cos(angle))));
- vertexPositions.put(0f);
- for(int i = 0; i < 4; i++)
- {
- normals.put(0f);
- normals.put(0f);
- normals.put(1f);
- }
- textureCoordinates.put(1);
- textureCoordinates.put(0);
- textureCoordinates.put(0);
- textureCoordinates.put(0);
- textureCoordinates.put(1);
- textureCoordinates.put(1);
- textureCoordinates.put(0);
- textureCoordinates.put(1);
- indices.flip();
- vertexPositions.flip();
- normals.flip();
- textureCoordinates.flip();
- return new GeometryBuffer("RoadGeometry", new Geometry(indices,
- vertexPositions, normals, textureCoordinates));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement