/** * Creates a transform matrix that will convert from this spatials' local coordinate space to the * world coordinate space based on the world transform. * * @param store Matrix where to store the result, if null, a new one will be created and returned. * @return store if not null, otherwise, a new matrix containing the result. * @see Spatial#getWorldTransform() */ public Matrix4f getLocalToWorldMatrix(Matrix4f store) { if (store == null) { store = new Matrix4f(); } else { store.loadIdentity(); } // multiply with scale first, then rotate, finally translate (cf. // Eberly) store.scale(getWorldScale()); store.multLocal(getWorldRotation()); store.setTranslation(getWorldTranslation()); return store; }
private void setTransformation(Matrix4f transformation) { // Compute the modelview matrix by multiplying the camera matrix and // the transformation matrix of the object Matrix4f modelview = new Matrix4f(sceneManager.getCamera().getCameraMatrix()); modelview.mul(transformation); // Set modelview and projection matrices in shader gl.glUniformMatrix4fv( gl.glGetUniformLocation(activeShaderID, "modelview"), 1, false, transformationToFloat16(modelview), 0); gl.glUniformMatrix4fv( gl.glGetUniformLocation(activeShaderID, "projection"), 1, false, transformationToFloat16(sceneManager.getFrustum().getProjectionMatrix()), 0); }
/** Convert a Transformation to a float array in column major ordering, as used by OpenGL. */ private static float[] transformationToFloat16(Matrix4f m) { float[] f = new float[16]; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) f[j * 4 + i] = m.getElement(i, j); return f; }
public static void zoomToFit(Viewport2 viewport) { ViewDefinition viewdef = viewport.getViewDefinition(); // if (MainFrame.getInstance().getMeshToolBar().getMode() != MeshToolBar.VIEW_ZOOM) { // MainFrame.getInstance().getJPatchScreen().removeAllMouseListeners(); // MainFrame.getInstance().getJPatchScreen().addMouseListeners(new // ChangeViewMouseListener(MouseEvent.BUTTON1,ChangeViewMouseListener.ZOOM)); // MainFrame.getInstance().getMeshToolBar().setMode(MeshToolBar.VIEW_ZOOM); // } else { // MainFrame.getInstance().getMeshToolBar().reset(); // } Selection selection = MainFrame.getInstance().getSelection(); float left = Float.MAX_VALUE; float right = -Float.MAX_VALUE; float bottom = Float.MAX_VALUE; float top = -Float.MAX_VALUE; Point3f p3 = new Point3f(); Matrix4f m4View = viewdef.getScreenMatrix(); // Matrix3f m3RotScale = new Matrix3f(); // m4View.getRotationScale(m3RotScale); boolean doit = true; if (selection != null && !selection.isSingle()) { for (Iterator it = selection.getObjects().iterator(); it.hasNext(); ) { Object object = it.next(); if (object instanceof ControlPoint) { p3.set(((ControlPoint) object).getPosition()); m4View.transform(p3); if (p3.x < left) left = p3.x; if (p3.x > right) right = p3.x; if (p3.y < bottom) bottom = p3.y; if (p3.y > top) top = p3.y; } } } else { ArrayList heads = MainFrame.getInstance().getModel().allHeads(); int p = 0; for (Iterator it = heads.iterator(); it.hasNext(); ) { ControlPoint cp = (ControlPoint) it.next(); if (!cp.isHidden()) { p3.set(cp.getPosition()); m4View.transform(p3); if (p3.x < left) left = p3.x; if (p3.x > right) right = p3.x; if (p3.y < bottom) bottom = p3.y; if (p3.y > top) top = p3.y; p++; } } doit = (p >= 2); } if (doit) { // System.out.println(left + " " + right + " " + top + " " + bottom + " " + // viewdef.getScale()); // System.out.println(viewdef.getTranslateX() + " " + viewdef.getTranslateY()); float centerX = (left + right) / 2f; float centerY = (top + bottom) / 2f; float dimX = viewdef.getWidth() / 2f; float dimY = viewdef.getHeight() / 2f; float sizeX = right - centerX; float sizeY = top - centerY; if (sizeX > 0 || sizeY > 0) { // System.out.println(centerX + ":" + centerY); float scaleX = dimX / sizeX; float scaleY = dimY / sizeY; float scale = Math.min(scaleX, scaleY) * 0.9f; // viewdef.setScale(viewdef.getScale() * scale); viewdef.setLock(null); viewdef.moveView(-centerX / dimX + 1, (dimY - centerY) / dimX, false); viewdef.scaleView(scale); // viewdef.setTranslation(centerX, centerY); // viewdef.computeMatrix(); // viewport.render(); } } }