Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ass2.spec;
- import java.util.ArrayList;
- import java.util.List;
- import com.jogamp.opengl.GL2;
- /**
- * COMMENT: Comment Road
- *
- * @author malcolmr
- */
- public class Road {
- private List<Double> myPoints;
- private double myWidth;
- private Terrain myTerrain;
- private String textureFileName = "/Users/macbookpro/Desktop/JavaProjects/3421ass2/src/ass2/spec/textures/pavement.jpg";
- // private String textureFileName = "/Users/Anna/Desktop/COMP3421-Ass2/src/ass2/spec/textures/pavement.jpg";
- private String textureExt = "jpg";
- private MyTexture myTextures[];
- /**
- * Create a new road starting at the specified point
- */
- public Road(double width, double x0, double y0) {
- myWidth = width;
- myPoints = new ArrayList<Double>();
- myPoints.add(x0);
- myPoints.add(y0);
- }
- public Terrain getTerrain() {
- return this.myTerrain;
- }
- public void addTerrain(Terrain t) {
- this.myTerrain = t;
- }
- /**
- * Create a new road with the specified spine
- *
- * @param width
- * @param spine
- */
- public Road(double width, double[] spine) {
- myWidth = width;
- myPoints = new ArrayList<Double>();
- for (int i = 0; i < spine.length; i++) {
- myPoints.add(spine[i]);
- }
- }
- /**
- * The width of the road.
- *
- * @return
- */
- public double width() {
- return myWidth;
- }
- /**
- * Add a new segment of road, beginning at the last point added and ending at (x3, y3).
- * (x1, y1) and (x2, y2) are interpolated as bezier control points.
- *
- * @param x1
- * @param y1
- * @param x2
- * @param y2
- * @param x3
- * @param y3
- */
- public void addSegment(double x1, double y1, double x2, double y2, double x3, double y3) {
- myPoints.add(x1);
- myPoints.add(y1);
- myPoints.add(x2);
- myPoints.add(y2);
- myPoints.add(x3);
- myPoints.add(y3);
- }
- /**
- * Get the number of segments in the curve
- *
- * @return
- */
- public int size() {
- return myPoints.size() / 6;
- }
- /**
- * Get the specified control point.
- *
- * @param i
- * @return
- */
- public double[] controlPoint(int i) {
- double[] p = new double[2];
- p[0] = myPoints.get(i*2);
- p[1] = myPoints.get(i*2+1);
- return p;
- }
- /**
- * Get a point on the spine. The parameter t may vary from 0 to size().
- * Points on the kth segment take have parameters in the range (k, k+1).
- *
- * @param t
- * @return
- */
- public double[] point(double t) {
- int i = (int)Math.floor(t);
- t = t - i;
- i *= 6;
- double x0 = myPoints.get(i++);
- double y0 = myPoints.get(i++);
- double x1 = myPoints.get(i++);
- double y1 = myPoints.get(i++);
- double x2 = myPoints.get(i++);
- double y2 = myPoints.get(i++);
- double x3 = myPoints.get(i++);
- double y3 = myPoints.get(i++);
- double[] p = new double[2];
- p[0] = b(0, t) * x0 + b(1, t) * x1 + b(2, t) * x2 + b(3, t) * x3;
- p[1] = b(0, t) * y0 + b(1, t) * y1 + b(2, t) * y2 + b(3, t) * y3;
- return p;
- }
- /**
- * Calculate the Bezier coefficients
- *
- * @param i
- * @param t
- * @return
- */
- private double b(int i, double t) {
- switch(i) {
- case 0:
- return (1-t) * (1-t) * (1-t);
- case 1:
- return 3 * (1-t) * (1-t) * t;
- case 2:
- return 3 * (1-t) * t * t;
- case 3:
- return t * t * t;
- }
- // this should never happen
- throw new IllegalArgumentException("" + i);
- }
- public void loadTexture (GL2 gl){
- myTextures = new MyTexture[1];
- myTextures[0] = new MyTexture(gl,textureFileName,textureExt,true);
- }
- public void draw(GL2 gl, double height, double step) {
- gl.glColor3f(0,0,0);
- addMaterialLighting(gl);
- double halfWidth = this.width()/2;
- // Little epsion in height so it isn't IN the ground, but ON the ground
- double eps = 0.001;
- gl.glEnable(GL2.GL_TEXTURE_2D);
- gl.glBindTexture(GL2.GL_TEXTURE_2D, myTextures[0].getTextureId());
- gl.glBegin(GL2.GL_TRIANGLE_STRIP);
- for (float i = 0; i < this.size(); i+=step) {
- double[] point = this.point(i);
- double[] normal = getNormal(i);
- //gl.glNormal3d(/*help*/, /*me*/, /*please*/);
- double xNorm = halfWidth*normal[0];
- double zNorm = halfWidth*normal[1];
- gl.glNormal3d(0, 1, 0);
- gl.glTexCoord2d(-xNorm + point[0], -zNorm + point[1]);
- gl.glVertex3d(-xNorm + point[0], eps + height, -zNorm + point[1]);
- gl.glTexCoord2d(xNorm + point[0], zNorm + point[1]);
- gl.glVertex3d(xNorm + point[0], eps + height, zNorm + point[1]);
- }
- gl.glEnd();
- }
- public void addMaterialLighting(GL2 gl){
- float matAmbAndDif[] = {0.6f, 0.6f, 0.6f, 1.0f};
- float matSpec[] = { 0.4f, 0.4f, 0.4f, 1.0f };
- float matShine[] = { 10.0f };
- float emm[] = {0.0f, 0.0f, 0.0f, 1.0f};
- // Material properties of road.
- gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, matAmbAndDif,0);
- gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, matSpec,0);
- gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SHININESS, matShine,0);
- gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_EMISSION, emm,0);
- }
- public double[] getNormal (double t) {
- double[] tangent = this.tangent(t);
- double[] normal = new double[]{-tangent[1], tangent[0]};
- // Normalize:
- double norm = normal[0]*normal[0] + normal[1]*normal[1];
- norm = Math.sqrt(norm);
- normal[0] /= norm;
- normal[1] /= norm;
- return normal;
- }
- /**
- * Return the 2D tangent vector of the Bezier curve at instant t
- */
- public double[] tangent(double t) {
- double[] tangent = new double[3];
- int i = (int)Math.floor(t);
- t = t - i;
- i *= 6;
- double x0 = myPoints.get(i++);
- double y0 = myPoints.get(i++);
- double x1 = myPoints.get(i++);
- double y1 = myPoints.get(i++);
- double x2 = myPoints.get(i++);
- double y2 = myPoints.get(i++);
- double x3 = myPoints.get(i++);
- double y3 = myPoints.get(i++);
- double[] a = {b2(0, t), b2(1, t), b2(2, t)};
- double[] b = {(x1-x0), (x2-x1), (x3-x2)};
- double[] c = {(y1-y0), (y2-y1), (y3-y2)};
- double calculation1 = (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
- double calculation2 = (a[0] * c[0]) + (a[1] * c[1]) + (a[2] * c[2]);
- tangent[0] = 3 * calculation1;
- tangent[1] = 3 * calculation2;
- return tangent;
- }
- /**
- * Calculate the coeficients for the bezier tangent
- */
- private double b2(int i, double t) {
- switch(i) {
- case 0: return (1-t) * (1-t);
- case 1: return 2 * (1-t) * t;
- case 2: return t * t;
- }
- // this should never happen
- throw new IllegalArgumentException("" + i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement