[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl AbstractViewListener.java,1.2,1.3
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2005-07-22 11:20:19
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19282 Modified Files: AbstractViewListener.java Log Message: Added the common functionality from the views and added drawing of the generated edges and surfaces Index: AbstractViewListener.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/AbstractViewListener.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** AbstractViewListener.java 18 Jul 2005 10:44:33 -0000 1.2 --- AbstractViewListener.java 22 Jul 2005 11:18:32 -0000 1.3 *************** *** 7,10 **** --- 7,18 ---- package net.sourceforge.bprocessor.gl; + import net.sourceforge.bprocessor.model.Edge; + import net.sourceforge.bprocessor.model.EdgeFacade; + import net.sourceforge.bprocessor.model.Vertex; + import net.sourceforge.bprocessor.model.Surface; + + import java.util.Iterator; + import java.util.Set; + import net.java.games.jogl.GL; import net.java.games.jogl.GLDrawable; *************** *** 20,23 **** --- 28,55 ---- private static Logger log = Logger.getLogger(AbstractViewListener.class); + /** the color of the x axis */ + public static final double[] X_AXIS_COLOR = new double[] {0.0, 1.0, 0.0}; + + /** the color of the y axis */ + public static final double[] Y_AXIS_COLOR = new double[] {1.0, 0.0, 0.0}; + + /** the color of the z axis */ + public static final double[] Z_AXIS_COLOR = new double[] {0.0, 0.0, 1.0}; + + /** the std line color*/ + public static final double[] STD_LINE_COLOR = new double[] {0.0, 0.0, 0.0}; + + /** Used for the actual drawing line */ + public static final double[] DRAW_COLOR = new double[] {1.0, 1.0, 0.0}; + + /** Used for the actual drawing line */ + public static final double[] GRID_COLOR = new double[] {0.85, 0.85, 0.85}; + + /** The view transformation matrix (it is shown transposed) see p. 187 [GL BIBLE]*/ + protected static double[] viewTrans = new double[] {1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0}; + /** The width of the window */ protected static double width; *************** *** 32,41 **** protected static double size = 25.0; ! /** The view transformation matrix (it is shown transposed) see p. 187 [GL BIBLE]*/ ! protected static double[] viewTrans = new double[] {1.0, 0.0, 0.0, 0.0, ! 0.0, 1.0, 0.0, 0.0, ! 0.0, 0.0, 1.0, 0.0, ! 0.0, 0.0, 0.0, 1.0}; /** * The initialization part of the 3D view --- 64,85 ---- protected static double size = 25.0; ! /** The Active edge */ ! protected static Edge activeEdge = null; ! ! /** The Snap vertex */ ! protected static Vertex snapVertex = null; ! ! /** For clever snapping */ ! protected static double[] alignPoint = null; + /** The alignment vertex */ + protected static Vertex alignVertex = null; + + /** The GL */ + protected GL gl = null; + + /** The zoomFactor in the parent view */ + protected double zoomFactor = 1.0; + /** * The initialization part of the 3D view *************** *** 46,50 **** log.debug("[init]"); } ! GL gl = gld.getGL(); this.width = gld.getSize().getWidth(); --- 90,94 ---- log.debug("[init]"); } ! gl = gld.getGL(); this.width = gld.getSize().getWidth(); *************** *** 56,59 **** --- 100,123 ---- gl.glViewport(0, 0, (int)width, (int)height); } + + /** + * The function responsible for drawing at each update + * @param gld The GLDrawable object + */ + public void display(GLDrawable gld) { + gl = gld.getGL(); + + gl.glClear(GL.GL_COLOR_BUFFER_BIT); + gl.glViewport(0, 0, (int)width, (int)height); + + camera(gld); + + grid(); + coords(); + gl.glColor3dv(STD_LINE_COLOR); + gl.glLineWidth(1.0f); + drawAll(); + gl.glFlush(); + } /** *************** *** 85,88 **** --- 149,268 ---- } + /** + * Draw all the existing elements + */ + protected void drawAll() { + Set edges = EdgeFacade.getInstance().findAll(); + Iterator it = edges.iterator(); + gl.glColor3dv(STD_LINE_COLOR); + while (it.hasNext()) { + Edge e = (Edge)it.next(); + drawEdge(e); + } + + // draw activeEdge different if it is parallel with axis + if (activeEdge != null) { + Vertex to = activeEdge.getTo(); + Vertex from = activeEdge.getFrom(); + if (from.getY() == to.getY() && from.getZ() == to.getZ()) { + gl.glColor3dv(X_AXIS_COLOR); + } else if (from.getX() == to.getX() && from.getZ() == to.getZ()) { + gl.glColor3dv(Y_AXIS_COLOR); + } else if (from.getX() == to.getX() && from.getY() == to.getY()) { + gl.glColor3dv(Z_AXIS_COLOR); + } else { + gl.glColor3dv(STD_LINE_COLOR); + } + gl.glBegin(GL.GL_LINES); + gl.glVertex3d(from.getX(), from.getY(), from.getZ()); + gl.glVertex3d(to.getX(), to.getY(), to.getZ()); + gl.glEnd(); + } + + // draw alignment line + if (alignPoint != null && alignVertex != null) { + if (alignPoint[1] == alignVertex.getY() && alignPoint[2] == alignVertex.getZ()) { + gl.glColor3dv(X_AXIS_COLOR); + } else if (alignPoint[0] == alignVertex.getX() && alignPoint[2] == alignVertex.getZ()) { + gl.glColor3dv(Y_AXIS_COLOR); + } else if (alignPoint[0] == alignVertex.getX() && alignPoint[1] == alignVertex.getY()) { + gl.glColor3dv(Z_AXIS_COLOR); + } else { + gl.glColor3dv(DRAW_COLOR); + } + gl.glBegin(GL.GL_LINES); + gl.glVertex3d(alignPoint[0], alignPoint[1], alignPoint[2]); + gl.glVertex3d(alignVertex.getX(), alignVertex.getY(), alignVertex.getZ()); + gl.glEnd(); + } + + // draw snap point if any + if (snapVertex != null) { + gl.glColor3d(0.0, 1.0, 0.0); + gl.glPointSize(5.0f); + gl.glBegin(GL.GL_POINTS); + gl.glVertex3d(snapVertex.getX(), snapVertex.getY(), snapVertex.getZ()); + gl.glEnd(); + gl.glPointSize(1.0f); + } + } + + /** + * Draw a surface + * @param s The surface to draw + */ + private void drawSurface(Surface s) { + Set edges = s.getEdges(); + Iterator it = edges.iterator(); + while (it.hasNext()) { + Edge e = (Edge)it.next(); + drawEdge(e); + } + } + + /** + * Draw an Edge + * @param e The Edge to draw + */ + private void drawEdge(Edge e) { + Vertex to = e.getTo(); + Vertex from = e.getFrom(); + if (to != null && from != null) { + gl.glBegin(GL.GL_LINES); + gl.glVertex3d(to.getX(), to.getY(), to.getZ()); + gl.glVertex3d(from.getX(), from.getY(), from.getZ()); + gl.glEnd(); + } + } + + /** + * Check if v is aligned horizontally or vertically with alignVertex + * @param coord The coordinate to check for alignment with + * @return The alignmentpoint, if there ain't any the original point is returned + */ + protected double[] aligned(double[] coord) { + double[] res = new double[] {coord[0], coord[1], coord[2]}; + if (alignVertex != null) { + if (Math.abs(coord[0] - alignVertex.getX()) < GLView.EPSILON) { + res[0] = alignVertex.getX(); + } + if (Math.abs(coord[1] - alignVertex.getY()) < GLView.EPSILON) { + res[1] = alignVertex.getY(); + } + if (Math.abs(coord[2] - alignVertex.getZ()) < GLView.EPSILON) { + res[2] = alignVertex.getZ(); + } + if (res[0] != coord[0] && res[1] != coord[1] && res[2] != coord[2]) { + alignPoint = null; + return coord; + } else { + alignPoint = res; + return res; + } + } + alignPoint = null; + return coord; + } + /** * Overwrite the view matrix *************** *** 122,126 **** } - /** * Change the zoom factor of the camera take only R+ --- 302,305 ---- *************** *** 128,138 **** * @param zoom the zoom factor change */ ! public abstract void zoom(double zoom); /** ! * The drawing function * @param gld The GLDrawable object */ ! public abstract void display(GLDrawable gld); /** --- 307,396 ---- * @param zoom the zoom factor change */ ! public void zoom(double zoom) { ! if (zoom > 0) { ! zoom *= getZoomFactor(); ! setZoomFactor(zoom); ! } ! } /** ! * setter for zoomFactor ! * @param z The new zoomFactor ! */ ! public void setZoomFactor(double z) { ! zoomFactor = z; ! } ! ! /** ! * getter for zoomFactor ! * @return The parent zoomFactor ! */ ! public double getZoomFactor() { ! return zoomFactor; ! } ! ! /** ! * Getter for activeEdge ! * @return activeEdge ! */ ! public Edge getActiveEdge() { ! return activeEdge; ! } ! ! /** ! * Set activeEdge ! * @param e The activeEdge ! */ ! public void setActiveEdge(Edge e) { ! activeEdge = e; ! } ! ! /** ! * Getter for snapVertex ! * @return snapVertex ! */ ! public Vertex getSnapVertex() { ! return snapVertex; ! } ! ! /** ! * Setter for snapVertex ! * @param v The new snapVertex ! */ ! public void setSnapVertex(Vertex v) { ! snapVertex = v; ! } ! ! /** ! * Getter for alignVertex ! * @return alignVertex ! */ ! public Vertex getAlignVertex() { ! return alignVertex; ! } ! ! /** ! * Setter for alignVertex ! * @param v The new alignVertex ! */ ! public void setAlignVertex(Vertex v) { ! alignVertex = v; ! } ! ! /** ! * The camera init function * @param gld The GLDrawable object */ ! public abstract void camera(GLDrawable gld); ! ! /** ! * The drawing of cordinatesystem ! */ ! protected abstract void coords(); ! ! /** ! * The drawing of grid ! */ ! protected abstract void grid(); /** *************** *** 141,143 **** --- 399,408 ---- */ public abstract void translateCenter(double [] mv); + + /** + * Return the 3D coordinates for the canvas matching the given 2D coords + * @return The relative 3D coordinates + * @param coords The window coordinates + */ + public abstract double[] toCanvasCoords(double[] coords); } |