package de.wb3player.fileaccess.output.drawstyle.jogl; import java.util.ArrayList; import javax.media.opengl.GL; import javax.media.opengl.GL2; import javax.media.opengl.glu.GLU; import javax.media.opengl.glu.GLUquadric; import javax.vecmath.Vector3d; import de.wb3player.fileaccess.output.drawstyle.DrawableMethods; import de.wb3player.fileaccess.output.objects.Drawable; import de.wb3player.fileaccess.output.objects.drawable.Line; import de.wb3player.fileaccess.output.objects.drawable.Point; import de.wb3player.fileaccess.output.objects.drawable.Polygon; import de.wb3player.fileaccess.output.objects.drawable.sql.BoundingBox; import de.wb3player.fileaccess.output.objects.drawable.sql.Lamp3D; import de.wb3player.fileaccess.output.objects.drawable.sql.Pipe3D; import de.wb3player.fileaccess.output.objects.drawable.sql.Plant3D; import de.wb3player.model.exception.DrawableMethodsException; import de.wb3player.toolbox.Toolbox; public class JoglDrawableMethodsNormal extends AbstractJoglDrawableMethods implements DrawableMethods { protected String nameSpecific = "Normal"; protected String descriptionSpecific = "JoglDrawableMethodsNormal: methods for drawing WB3 objects in JOGL"; public JoglDrawableMethodsNormal() { super(); this.name = nameSpecific; this.description = descriptionSpecific; } @Override public void draw(Object object) throws DrawableMethodsException{ if (this.gl == null && this.glu == null) { throw new DrawableMethodsException("JoglDrawableMethodsExtended: GL and/or GLU not initialized"); } if (object instanceof Drawable) { Drawable drawable = (Drawable)object; ArrayList coordinates = (ArrayList)drawable.getCoordinates(); if (object instanceof Point) { Point point = (Point)object; gl.glPointSize(point.getPointSize()); gl.glBegin(GL.GL_POINTS); setMaterial(gl, point.getColor3f()); //gl.glColor3f(point.getColor3f().x, point.getColor3f().y, point.getColor3f().z); gl.glVertex3f((float)coordinates.get(0).x, (float)coordinates.get(0).y, (float)coordinates.get(0).z); gl.glEnd(); } else if (object instanceof Plant3D) { Plant3D plant = (Plant3D)object; double trunkHeight = plant.getHeight() - plant.getCrownDiameter(); //trunk gl.glPushMatrix(); gl.glTranslated(coordinates.get(0).x, coordinates.get(0).y, coordinates.get(0).z); setMaterial(gl, plant.getTrunkColor()); //gl.glColor3f(plant.getTrunkColor().x, plant.getTrunkColor().y, plant.getTrunkColor().z); GLUquadric gluTrunkQuadric = glu.gluNewQuadric(); glu.gluQuadricDrawStyle(gluTrunkQuadric, GLU.GLU_FILL); glu.gluQuadricNormals(gluTrunkQuadric, GLU.GLU_SMOOTH); glu.gluCylinder(gluTrunkQuadric, plant.getTrunkDiameter(), plant.getTrunkDiameter(), trunkHeight, 15, 5); glu.gluDeleteQuadric(gluTrunkQuadric); gl.glPopMatrix(); //crown gl.glPushMatrix(); gl.glTranslated(coordinates.get(0).x, coordinates.get(0).y, (coordinates.get(0).z + (plant.getHeight()-plant.getCrownDiameter()/2))); setMaterial(gl, plant.getColor3f()); //gl.glColor3f(plant.getColor3f().x, plant.getColor3f().y, plant.getColor3f().z); GLUquadric gluCrownQuadric = glu.gluNewQuadric(); glu.gluQuadricDrawStyle(gluCrownQuadric, GLU.GLU_FILL); glu.gluQuadricNormals(gluCrownQuadric, GLU.GLU_SMOOTH); glu.gluSphere(gluCrownQuadric, (plant.getCrownDiameter()/2), 16, 16); glu.gluDeleteQuadric(gluCrownQuadric); gl.glPopMatrix(); } else if (object instanceof Lamp3D) { Lamp3D lamp3D = (Lamp3D)object; gl.glPushMatrix(); gl.glTranslated(coordinates.get(0).x, coordinates.get(0).y, coordinates.get(0).z); setMaterial(gl, lamp3D.getColor3f()); //gl.glColor3f(plant.getTrunkColor().x, plant.getTrunkColor().y, plant.getTrunkColor().z); GLUquadric gluTrunkQuadric = glu.gluNewQuadric(); glu.gluQuadricDrawStyle(gluTrunkQuadric, GLU.GLU_FILL); glu.gluQuadricNormals(gluTrunkQuadric, GLU.GLU_SMOOTH); glu.gluCylinder(gluTrunkQuadric, 0.2, 0.2, 3.0, 15, 5); glu.gluDeleteQuadric(gluTrunkQuadric); gl.glPopMatrix(); } else if (object instanceof Line) { Line line = (Line)object; for (int i = 0; i < coordinates.size()-1; i++) { setMaterial(gl, line.getColor3f()); gl.glLineWidth(line.getLineWidth()); gl.glBegin(GL2.GL_LINES); gl.glVertex3f((float)coordinates.get(i).x, (float)coordinates.get(i).y, (float)coordinates.get(i).z); gl.glVertex3f((float)coordinates.get(i+1).x, (float)coordinates.get(i+1).y, (float)coordinates.get(i+1).z); gl.glEnd(); } } else if (object instanceof Polygon) { Polygon polygon = (Polygon)object; if (!polygon.isClockwise()) gl.glFrontFace (GL2.GL_CCW); // counter clockwise else gl.glFrontFace(GL2.GL_CW); // clockwise gl.glShadeModel(GL2.GL_SMOOTH); gl.glBegin(GL2.GL_POLYGON); setMaterial(gl, polygon.getColor3f()); //gl.glColor3f(polygon.getColor3f().x, polygon.getColor3f().y, polygon.getColor3f().z); for (int i = 0; i < coordinates.size(); i++) { gl.glVertex3f((float)coordinates.get(i).x, (float)coordinates.get(i).y, (float)coordinates.get(i).z); } gl.glEnd(); } else if (object instanceof BoundingBox) { BoundingBox boundingBox = (BoundingBox)object; if (coordinates.size() == 2) { setMaterial(gl, boundingBox.getColor3f(),0.2f); //gl.glColor3f(boundingBox.getColor3f().x, boundingBox.getColor3f().y, boundingBox.getColor3f().z); gl.glBegin(GL2.GL_POLYGON); gl.glVertex3f((float)coordinates.get(0).x, (float)coordinates.get(0).y, (float)coordinates.get(0).z); gl.glVertex3f((float)coordinates.get(0).x, (float)coordinates.get(1).y, (float)coordinates.get(0).z); gl.glVertex3f((float)coordinates.get(1).x, (float)coordinates.get(1).y, (float)coordinates.get(1).z); gl.glVertex3f((float)coordinates.get(1).x, (float)coordinates.get(0).y, (float)coordinates.get(1).z); gl.glEnd(); } } else if (object instanceof Pipe3D) { Pipe3D pipe3D = (Pipe3D)object; if (coordinates.size() >= 2) { setMaterial(gl, pipe3D.getColor3f()); //draw a sphere at each coordinate for (Vector3d coordinate : coordinates) { gl.glPushMatrix(); gl.glTranslated(coordinate.x, coordinate.y, coordinate.z); setMaterial(gl, pipe3D.getColor3f()); //gl.glColor3f(boundingBox.getColor3f().x, boundingBox.getColor3f().y, boundingBox.getColor3f().z); GLUquadric gluEdgeQuadric = glu.gluNewQuadric(); glu.gluQuadricDrawStyle(gluEdgeQuadric, GLU.GLU_FILL); glu.gluQuadricNormals(gluEdgeQuadric, GLU.GLU_SMOOTH); glu.gluSphere(gluEdgeQuadric, (pipe3D.getDiameter()/2), 16, 16); glu.gluDeleteQuadric(gluEdgeQuadric); gl.glPopMatrix(); } //draw the cylinders between coordinates for (int i = 0; i < coordinates.size()-1; i++) { Vector3d fromCoordinate = coordinates.get(i); Vector3d toCoordinate = coordinates.get(i+1); double length = Toolbox.getDistanceBetweenPoints(fromCoordinate, toCoordinate); //Standard cylinder orientation Vector3d upVector = new Vector3d(0.0,0.0,1.0); //desired cylinder orientation Vector3d toVector = new Vector3d(toCoordinate.x-fromCoordinate.x, toCoordinate.y-fromCoordinate.y, toCoordinate.z-fromCoordinate.z); toVector.normalize(); //cross product Vector3d crossVector = new Vector3d(); crossVector.cross(upVector, toVector); double angle = 180 / Math.PI * Math.acos(upVector.dot(toVector)); gl.glPushMatrix(); gl.glTranslated(fromCoordinate.x, fromCoordinate.y, fromCoordinate.z); gl.glRotated(angle,crossVector.x,crossVector.y,crossVector.z); setMaterial(gl, pipe3D.getColor3f()); //gl.glColor3f(plant.getTrunkColor().x, plant.getTrunkColor().y, plant.getTrunkColor().z); GLUquadric gluPipeQuadric = glu.gluNewQuadric(); glu.gluQuadricOrientation(gluPipeQuadric,GLU.GLU_OUTSIDE); glu.gluQuadricDrawStyle(gluPipeQuadric, GLU.GLU_FILL); glu.gluQuadricNormals(gluPipeQuadric, GLU.GLU_SMOOTH); glu.gluCylinder(gluPipeQuadric, pipe3D.getDiameter()/2, pipe3D.getDiameter()/2, length, 15, 5); glu.gluDeleteQuadric(gluPipeQuadric); gl.glPopMatrix(); } } } } } }