Thread: [Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/view AbstractView.java,NONE,1.1
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2005-07-26 12:44:07
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/view In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15937 Added Files: AbstractView.java Log Message: implements common methods for the views --- NEW FILE: AbstractView.java --- //--------------------------------------------------------------------------------- // $Id: AbstractView.java,v 1.1 2005/07/26 12:43:56 rimestad Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gl.view; import net.sourceforge.bprocessor.kernel.notification.Notification; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.EdgeFacade; import net.sourceforge.bprocessor.model.Vertex; import net.sourceforge.bprocessor.model.VertexFacade; 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; import net.java.games.jogl.GLU; import net.java.games.jogl.GLUquadric; import net.java.games.jogl.util.GLUT; import org.apache.log4j.Logger; /** * The 3D view listener */ public abstract class AbstractView implements View { /** The logger */ private static Logger log = Logger.getLogger(AbstractView.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; /** The height of the window */ protected static double height; /** The screen aspect */ protected static double aspect; /** The size of the viewing area */ 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 selected vertex */ protected static Vertex selectedVertex = 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 * @param gld The GLDrawable object */ public void init(GLDrawable gld) { if (log.isDebugEnabled()) { log.debug("[init]"); } gl = gld.getGL(); this.width = gld.getSize().getWidth(); this.height = gld.getSize().getHeight(); this.aspect = this.width / this.height; gl.glClearColor(0.7f, 0.7f, 0.7f, 0.0f); 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(gld); } /** * Called by the drawable during the first repaint after the component has been resized. * @param gld The GLDrawable * @param x The new x coordinate * @param y The new y coordinate * @param width The new window width * @param height The new window height */ public void reshape(GLDrawable gld, int x, int y, int width, int height) { this.width = width; this.height = height; this.aspect = this.width / this.height; } /** * Called by the drawable when the display mode or the display device * associated with the GLDrawable has changed. * @param gld The GLDrawable * @param modeChanged The new graphical mode * @param deviceChanged The new graphical device */ public void displayChanged(GLDrawable gld, boolean modeChanged, boolean deviceChanged) { } /** * Draw all the existing elements * @param gld The GLDrawable */ protected void drawAll(GLDrawable gld) { Set edges = EdgeFacade.getInstance().findAll(); GL gl = gld.getGL(); GLU glu = gld.getGLU(); GLUT glut = new GLUT(); 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 selection if (selectedVertex != null) { gl.glColor3d(0.0, 1.0, 0.0); gl.glPushMatrix(); gl.glTranslated(selectedVertex.getX(), selectedVertex.getY(), selectedVertex.getZ()); GLUquadric quad = glu.gluNewQuadric(); glu.gluQuadricDrawStyle(quad, GLU.GLU_FILL); glu.gluSphere(quad, 0.3, 15, 15); glu.gluDeleteQuadric(quad); gl.glPopMatrix(); } } /** * 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(); } } /** * Change the rotation about the x axis. Only usable in 3D * @param change The change in R+ (is divided with the window height) */ public void translateRotationX(double change) { } /** * Change the rotation about the y axis. Only usable in 3D * @param change The change in R+ (is divided with the window height) */ public void translateRotationY(double change) { } /** * Change the zoom factor of the camera take only R+ * 1-oo is increase zoom and 0-1 decrease 1 keep the current zoom * @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; } /** * Setter for alignPoint * @param coord The new alignPoint */ public void setAlignPoint(double[] coord) { alignPoint = coord; } /** * Getter for alignPoint * @return alignPoint */ public double[] getAlignPoint() { return alignPoint; } /** * Handle a notification * @param n The notification */ public void handleNotification(Notification n) { if (n.getType().equals(Notification.VERTEX_SELECTED)) { selectedVertex = VertexFacade.getInstance().findById(n.getObject()); } } /** * Should the listener handle this notification * @param type The notification type * @return Returns true on SELECTED events; otherwise false */ public boolean isNotificationEnabled(String type) { if (type.equals(Notification.VERTEX_SELECTED)) { return true; } return false; } /** * 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(); /** * Move the center left/right, up/down, forward/backward * @param mv the move vector */ 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); } |