[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/view AbstractView.java,1.56,1.57 View3D.jav
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2005-11-17 16:59:59
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/view In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22507 Modified Files: AbstractView.java View3D.java View.java Log Message: changed the entire camera initializisation and rotation mechanism Index: AbstractView.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/view/AbstractView.java,v retrieving revision 1.56 retrieving revision 1.57 diff -C2 -d -r1.56 -r1.57 *** AbstractView.java 15 Nov 2005 19:04:09 -0000 1.56 --- AbstractView.java 17 Nov 2005 16:59:45 -0000 1.57 *************** *** 48,56 **** private static Logger log = Logger.getLogger(AbstractView.class); - /** 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}; /** Select mode for selecting edges */ private static final int EDGES = 0; --- 48,51 ---- *************** *** 296,301 **** picking = 0; - - // grid and coords are always completely lit gl.glDisable(GL.GL_DEPTH_TEST); --- 291,294 ---- *************** *** 307,312 **** coords(); ! // draw the clipping planes ! Iterator clipit = clippingPlanes.iterator(); gl.glEnable(GL.GL_POLYGON_STIPPLE); --- 300,304 ---- coords(); ! // draw the clipping planes Iterator clipit = clippingPlanes.iterator(); gl.glEnable(GL.GL_POLYGON_STIPPLE); *************** *** 393,396 **** --- 385,389 ---- drawString(x, y, string); } + /** * Resets the working attributes of the view *************** *** 632,636 **** */ protected void drawAll(GLDrawable gld) { - Set surfaces = Project.getInstance().getSurfaces(); Set edges = Project.getInstance().getEdges(); --- 625,628 ---- *************** *** 1380,1384 **** gl.glGetIntegerv(GL.GL_MAX_CLIP_PLANES, maxclip); if (clippingPlanes.size() < maxclip[0] || maxclip[0] == 0) { - log.info("Added clipplane"); cp.setNumber(clippingPlanes.size()); clippingPlanes.add(cp); --- 1372,1375 ---- *************** *** 1427,1430 **** --- 1418,1427 ---- /** + * Add the change to the focal width; + * @param change the change to the focalwidth + */ + public abstract void changeFocalWidth(double change); + + /** * representation of a 3D point. */ *************** *** 1520,1523 **** --- 1517,1526 ---- /** + * Roll the camera + * @param angle The amount of roll negative left positive right + */ + public abstract void rollCamera(double angle); + + /** * Return the near plane * @return The near plane Index: View3D.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/view/View3D.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** View3D.java 1 Nov 2005 07:27:15 -0000 1.18 --- View3D.java 17 Nov 2005 16:59:45 -0000 1.19 *************** *** 23,36 **** /** The logger */ private static Logger log = Logger.getLogger(View3D.class); ! /** The relative center of the view */ ! private static double[] center = new double[] {0.0, 0.0, 0.0}; ! ! /** The rotaion about the X axis */ ! private static double rotationX = -30; ! /** The rotation about the Y axis */ ! private static double rotationZ = 130; ! /** * The constructor --- 23,36 ---- /** The logger */ private static Logger log = Logger.getLogger(View3D.class); + + /** double arrays for modelView setup */ + protected double[] center, roll, camera; ! /** The focal width */ ! private double focalwidth; ! /** Do a camera reset on repaint */ ! private boolean reset = true; ! /** * The constructor *************** *** 39,42 **** --- 39,45 ---- public View3D(GLView glv) { super(glv); + center = new double[]{.0, .0, .0}; + camera = new double[]{25.0, 0.0, 0.0}; + roll = new double[]{.0, .0, 1.0}; } *************** *** 51,91 **** gl = gld.getGL(); glu = gld.getGLU(); ! int[] viewport = new int[] {0, 0, (int)width, (int)height}; ! gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); ! //Set the PickMatrix if we are trying to pick something if (picking > 0) { glu.gluPickMatrix(x, viewport[3] - y, 5, 5, viewport); } double aspect = width / height; ! near = 4 * getZoomFactor(); ! far = 200.0 * getZoomFactor(); if (aspect < 1.0) { // fovy, aspect, near, far (relative to camera position) ! glu.gluPerspective(aspect * 60.0, 1.0, near, far); } else { ! glu.gluPerspective(60.0, aspect, near, far); } gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); - gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] {0.0f, 0.0f, 0.0f, 1.0f}); - glu.gluLookAt(0.0, 25.0 * getZoomFactor(), 0.0, - 0.0, 0.0, 0.0, - 0.0, 0.0, 1.0); - gl.glRotated(rotationX, 1.0, 0.0, 0.0); - gl.glRotated(rotationZ, 0.0, 0.0, 1.0); - gl.glMultMatrixd(viewTrans); - if (picking == 0) { - gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelMatrix); - gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projMatrix); gl.glGetIntegerv(GL.GL_VIEWPORT, screenport); } } /** * Move the center left/right, up/down, forward/backward * @param mv the move vector --- 54,120 ---- gl = gld.getGL(); glu = gld.getGLU(); ! gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); ! if (picking > 0) { + int[] viewport = new int[] {0, 0, (int)width, (int)height}; glu.gluPickMatrix(x, viewport[3] - y, 5, 5, viewport); } + double aspect = width / height; ! double near = 1 * getZoomFactor(); ! double far = 200.0 * getZoomFactor(); ! focalwidth = 60.0; if (aspect < 1.0) { // fovy, aspect, near, far (relative to camera position) ! glu.gluPerspective(aspect * focalwidth, 1.0, near, far); } else { ! glu.gluPerspective(focalwidth, aspect, near, far); } gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); + glu.gluLookAt(camera[0] * getZoomFactor(), + camera[1] * getZoomFactor(), + camera[2] * getZoomFactor(), + center[0], center[1], center[2], + roll[0], roll[1], roll[2]); + //gl.glMultMatrixd(modelMatrix); + + if (reset) { + reset = false; + resetCamera(gld); + } if (picking == 0) { gl.glGetIntegerv(GL.GL_VIEWPORT, screenport); + gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projMatrix); + gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelMatrix); } } /** + * reset the modelView and projectionMatrix on next repaint + */ + private void resetCamera() { + reset = true; + } + + /** + * Reset til view to "original" view + * @param gld The GL drawable + */ + private void resetCamera(GLDrawable gld) { + gl = gld.getGL(); + glu = gld.getGLU(); + + gl.glPushMatrix(); + gl.glLoadIdentity(); + gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] {0.0f, 0.0f, 0.0f, 1.0f}); + gl.glPopMatrix(); + } + + /** * Move the center left/right, up/down, forward/backward * @param mv the move vector *************** *** 93,102 **** public void translateCenter(double [] mv) { if (mv.length == 3) { ! this.viewTrans[12] -= modelMatrix[0] * mv[0] + modelMatrix[1] * mv[1] + ! modelMatrix[2] * mv[2]; ! this.viewTrans[13] -= modelMatrix[4] * mv[0] + modelMatrix[5] * mv[1] + ! modelMatrix[6] * mv[2]; ! this.viewTrans[14] -= modelMatrix[8] * mv[0] + modelMatrix[9] * mv[1] + ! modelMatrix[10] * mv[2]; } else { log.error("[translateViewMatrix] Wrong parameter size"); --- 122,134 ---- public void translateCenter(double [] mv) { if (mv.length == 3) { ! double dx = modelMatrix[0] * mv[0] + modelMatrix[1] * -mv[1] + modelMatrix[2] * mv[2]; ! double dy = modelMatrix[4] * mv[0] + modelMatrix[5] * -mv[1] + modelMatrix[6] * mv[2]; ! double dz = modelMatrix[8] * mv[0] + modelMatrix[9] * -mv[1] + modelMatrix[10] * mv[2]; ! center[0] += dx; ! center[1] += dy; ! center[2] += dz; ! camera[0] += dx; ! camera[1] += dy; ! camera[2] += dz; } else { log.error("[translateViewMatrix] Wrong parameter size"); *************** *** 105,113 **** /** * Change the rotation about the x axis * @param change The change in R+ (is divided with the window height) */ ! public void translateRotationX(double change) { ! rotationX += change / height; } --- 137,283 ---- /** + * Add the change to the focal width; + * @param change the change to the focalwidth + */ + public void changeFocalWidth(double change) { + focalwidth += change; + if (focalwidth > 180.0) { + focalwidth = 180; + } else if (focalwidth < 0.0) { + focalwidth = 0; + } + } + + /** * Change the rotation about the x axis * @param change The change in R+ (is divided with the window height) */ ! public void translateRotationX(double change) { ! translateRotationX(change, center); ! } ! ! /** ! * Change the rotation about the x axis ! * @param change The change in R+ (is divided with the window height) ! * @param c The point to rotate about ! */ ! public void translateRotationX(double change, double[] c) { ! double angle = change / width; ! rotate(-angle, c, modelMatrix[0], modelMatrix[4], modelMatrix[8]); ! } ! ! /** ! * Rotate the camera ! * @param angle The angle I would like to rotate ! * @param c The center of rotation ! * @param x The amount about the x axis ! * @param y The amount about the y axis ! * @param z The amount about the z axis ! */ ! public void rotate(double angle, double[] c, double x, double y, double z) { ! double rx, ry, rz; ! double dx = camera[0] - c[0]; ! double dy = camera[1] - c[1]; ! double dz = camera[2] - c[2]; ! double sinx, cosx, siny, sinz, cosy, cosz; ! double pro = x * y * z; ! ! sinx = Math.sin(x * angle); ! cosx = Math.cos(x * angle); ! siny = Math.sin(y * angle); ! cosy = Math.cos(y * angle); ! sinz = Math.sin(z * angle); ! cosz = Math.cos(z * angle); ! ! ! double[] rotx = new double[] {1, 0, 0, ! 0, cosx, sinx, ! 0, -sinx, cosx}; ! double[] roty = new double[] {cosy, 0, -siny, ! 0, 1, 0, ! siny, 0, cosy}; ! double[] rotz = new double[] {cosz, sinz, 0, ! -sinz, cosz, 0, ! 0, 0, 1}; ! ! double[] rot = multMatrix3(rotx, roty); ! rot = multMatrix3(rot, rotz); ! ! rx = dx * rot[0] + dy * rot[1] + dz * rot[2]; ! ry = dx * rot[3] + dy * rot[4] + dz * rot[5]; ! rz = dx * rot[6] + dy * rot[7] + dz * rot[8]; ! ! camera[0] = rx + c[0]; ! camera[1] = ry + c[1]; ! camera[2] = rz + c[2]; ! ! roll[0] = roll[0] * rot[0] + roll[1] * rot[1] + roll[2] * rot[2]; ! roll[1] = roll[0] * rot[3] + roll[1] * rot[4] + roll[2] * rot[5]; ! roll[2] = roll[0] * rot[6] + roll[1] * rot[7] + roll[2] * rot[8]; ! } ! ! /** ! * Roll the camera ! * @param angle The amount of roll negative left positive right ! */ ! public void rollCamera(double angle) { ! angle /= width; ! double dx = camera[0] - center[0]; ! double dy = camera[1] - center[1]; ! double dz = camera[2] - center[2]; ! double length = Math.sqrt(dx * dx + dy * dy + dz * dz); ! ! double sinx = Math.sin(dx / length * angle); ! double cosx = Math.cos(dx / length * angle); ! double siny = Math.sin(dy / length * angle); ! double cosy = Math.cos(dy / length * angle); ! double sinz = Math.sin(dz / length * angle); ! double cosz = Math.cos(dz / length * angle); ! ! double[] rotx = new double[] {1, 0, 0, ! 0, cosx, sinx, ! 0, -sinx, cosx}; ! double[] roty = new double[] {cosy, 0, -siny, ! 0, 1, 0, ! siny, 0, cosy}; ! double[] rotz = new double[] {cosz, sinz, 0, ! -sinz, cosz, 0, ! 0, 0, 1}; ! ! double[] rot = multMatrix3(rotx, roty); ! rot = multMatrix3(rot, rotz); ! ! double rx = roll[0] * rot[0] + roll[1] * rot[1] + roll[2] * rot[2]; ! double ry = roll[0] * rot[3] + roll[1] * rot[4] + roll[2] * rot[5]; ! double rz = roll[0] * rot[6] + roll[1] * rot[7] + roll[2] * rot[8]; ! ! roll[0] = rx; ! roll[1] = ry; ! roll[2] = rz; ! } ! ! /** ! * Matrix multiplication ! * @param matrix1 The first matrix ! * @param matrix2 The second matrix ! * @return The multiplication of matrix1 and matrix2 ! */ ! public double[] multMatrix3(double[] matrix1, double[] matrix2) { ! double[] result = null; ! if (matrix1.length == 9 && matrix2.length == 9) { ! result = new double[9]; ! result[0] = matrix1[0] * matrix2[0] + matrix1[1] * matrix2[3] + matrix1[2] * matrix2[6]; ! result[1] = matrix1[0] * matrix2[1] + matrix1[1] * matrix2[4] + matrix1[2] * matrix2[7]; ! result[2] = matrix1[0] * matrix2[2] + matrix1[1] * matrix2[5] + matrix1[2] * matrix2[8]; ! ! result[3] = matrix1[3] * matrix2[0] + matrix1[4] * matrix2[3] + matrix1[5] * matrix2[6]; ! result[4] = matrix1[3] * matrix2[1] + matrix1[4] * matrix2[4] + matrix1[5] * matrix2[7]; ! result[5] = matrix1[3] * matrix2[2] + matrix1[4] * matrix2[5] + matrix1[5] * matrix2[8]; ! ! result[6] = matrix1[6] * matrix2[0] + matrix1[7] * matrix2[3] + matrix1[8] * matrix2[6]; ! result[7] = matrix1[6] * matrix2[1] + matrix1[7] * matrix2[4] + matrix1[8] * matrix2[7]; ! result[8] = matrix1[6] * matrix2[2] + matrix1[7] * matrix2[5] + matrix1[8] * matrix2[8]; ! } ! return result; } *************** *** 115,121 **** * Change the rotation about the y axis * @param change The change in R+ (is divided with the window height) */ public void translateRotationY(double change) { ! rotationZ -= change / width; } --- 285,305 ---- * Change the rotation about the y axis * @param change The change in R+ (is divided with the window height) + * @param c The point to rotate about + */ + public void translateRotationY(double change, double[] c) { + double angle = change / height; + if (roll[2] < 0) { + rotate(-angle, c, 0, 0, -1); + } else { + rotate(-angle, c, 0, 0, 1); + } + } + + /** + * Change the rotation about the z axis + * @param change The change in R+ (is divided with the window height) */ public void translateRotationY(double change) { ! translateRotationY(change, center); } *************** *** 126,145 **** public void changeView(int value) { if (value == View.VIEW_XY) { ! rotationZ = 180; ! rotationX = -90; } if (value == View.VIEW_XZ) { ! rotationZ = 180; ! rotationX = 0; } if (value == View.VIEW_YZ) { ! rotationZ = 90; ! rotationX = 0; } if (value == View.VIEW_3D) { ! rotationZ = 130; ! rotationX = -30; } - } --- 310,364 ---- public void changeView(int value) { if (value == View.VIEW_XY) { ! //rotationZ = 180; ! //rotationX = -90; ! center[0] = .0; ! center[1] = .0; ! center[2] = .0; ! camera[0] = .0; ! camera[1] = .0; ! camera[2] = 25.0; ! roll[0] = .0; ! roll[1] = .1; ! roll[2] = .0; } if (value == View.VIEW_XZ) { ! //rotationZ = 180; ! //rotationX = 0; ! center[0] = .0; ! center[1] = .0; ! center[2] = .0; ! camera[0] = .0; ! camera[1] = 25.0; ! camera[2] = .0; ! roll[0] = .0; ! roll[1] = .0; ! roll[2] = .1; } if (value == View.VIEW_YZ) { ! //rotationZ = 90; ! //rotationX = 0; ! center[0] = .0; ! center[1] = .0; ! center[2] = .0; ! camera[0] = 25.0; ! camera[1] = .0; ! camera[2] = .0; ! roll[0] = .0; ! roll[1] = .0; ! roll[2] = .1; } if (value == View.VIEW_3D) { ! //rotationZ = 130; ! //rotationX = -30; ! center[0] = .0; ! center[1] = .0; ! center[2] = .0; ! camera[0] = 25.0; ! camera[1] = 5.0; ! camera[2] = 5.0; ! roll[0] = .0; ! roll[1] = .0; ! roll[2] = .1; } } *************** *** 150,153 **** --- 369,373 ---- super.reset(); } + /** * Draw a grid Index: View.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/view/View.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** View.java 6 Nov 2005 16:33:44 -0000 1.19 --- View.java 17 Nov 2005 16:59:45 -0000 1.20 *************** *** 135,138 **** --- 135,164 ---- /** + * Change the rotation about the y axis + * @param change The change in R+ (is divided with the window height) + * @param center The point to rotate about + */ + public void translateRotationY(double change, double[] center); + + /** + * Change the rotation about the x axis + * @param change The change in R+ (is divided with the window height) + * @param center The point to rotate about + */ + public void translateRotationX(double change, double[] center); + + /** + * Roll the camera + * @param angle The amount of roll negative left positive right + */ + public void rollCamera(double angle); + + /** + * Add the change to the focal width; + * @param change the change to the focalwidth + */ + public void changeFocalWidth(double change); + + /** * Change view to 3d, XY, XZ, or YZ * @param value One of 3d_VIEW, XY_VIEW, XZ_VIEW, YZ_VIEW *************** *** 153,156 **** --- 179,188 ---- /** + * getter for zoomFactor + * @return The parent zoomFactor + */ + public double getZoomFactor(); + + /** * Setter for snapVertex * @param v The new snapVertex |