public Matrix4f getTransformation() { Matrix4f translationMatrix = new Matrix4f().translation(translation); Matrix4f scaleMatrix = new Matrix4f().scale(scale); Matrix4f rotationMatrix = new Matrix4f().rotate(rotation); return translationMatrix.mul(rotationMatrix.mul(scaleMatrix)); }
public void keyPressed(KeyEvent e) { keyMove = new Matrix4f(); keyMove.setIdentity(); Vector3f keyMovement = new Vector3f(); switch (e.getKeyChar()) { case 's': keyMovement.z = -1 * scale; keyMove.setTranslation(keyMovement); break; case 'w': keyMovement.z = 1 * scale; keyMove.setTranslation(keyMovement); break; case 'a': keyMovement.x = 1 * scale; keyMove.setTranslation(keyMovement); break; case 'd': keyMovement.x = -1 * scale; keyMove.setTranslation(keyMovement); break; default: System.out.println("invalid key"); break; } }
public Matrix4f getViewProjection() { Matrix4f cameraRotation = new Matrix4f().initRotation(forward, up); Matrix4f cameraTranslation = new Matrix4f().initTranslation(-pos.getX(), -pos.getY(), -pos.getZ()); return projection.mul(cameraRotation.mul(cameraTranslation)); }
public void mouseDragged(MouseEvent e) { oldMoveVector = new Vector3f(fixOldX, oldY, oldZ); oldWorldZMoveVector = new Vector3f(oldX, oldY, oldZ); // get x,y newX = e.getX() / (adjustToScreenSize / 2) - 1; // used to be (float)jframe.getWidth() newY = 1 - e.getY() / (adjustToScreenSize / 2); // used to be (float)jframe.getWidth() newZ = calculateZ(newX, newY); newMoveVector = new Vector3f(fixNewX, newY, newZ); newWorldZMoveVector = new Vector3f(newX, newY, newZ); // now calculate the Camera movement Vector3f moveAxis = new Vector3f(); // moving around this axis moveAxis.cross( oldMoveVector, newMoveVector); // TODO identify, if the axis points down or upwards => adjust the angle float moveAngle = oldMoveVector.angle(newMoveVector); // moving by this angle // special Vector including the angle AxisAngle4f moveAxisAngle = new AxisAngle4f(); moveAxisAngle.set(moveAxis, moveAngle); // calculate turning axis mouseTurn = new Matrix4f(); mouseTurn.setIdentity(); mouseTurn.set(moveAxisAngle); // the new Multiplication-Matrix // now calculate the World movement Vector3f worldMoveAxis = new Vector3f(); // moving around this axis worldMoveAxis.x = 0; worldMoveAxis.y = 0; worldMoveAxis.z = 1; float worldMoveAngle = oldWorldZMoveVector.angle(newWorldZMoveVector); // moving by this angle // adjust world Angle for turning back Vector3f worldMoveAxisTemp = new Vector3f(); worldMoveAxisTemp.cross(oldWorldZMoveVector, newWorldZMoveVector); if (worldMoveAxisTemp.z < 0) { worldMoveAngle = -worldMoveAngle; } // special Vector including the angle AxisAngle4f worldMoveAxisAngle = new AxisAngle4f(); worldMoveAxisAngle.set(worldMoveAxis, worldMoveAngle); // calculate turning axis mouseWorldTurn = new Matrix4f(); mouseWorldTurn.setIdentity(); mouseWorldTurn.set(worldMoveAxisAngle); // the new Multiplication-Matrix // override the initial starting values fixOldX = fixNewX; oldX = newX; oldY = newY; oldZ = newZ; }
public Matrix4f getTransformation() { Matrix4f translationMatrix = new Matrix4f().initTranslation(translation.getX(), translation.getY(), translation.getZ()); Matrix4f rotationMatrix = new Matrix4f().initRotation(rotation.getX(), rotation.getY(), rotation.getZ()); Matrix4f scaleMatrix = new Matrix4f().initScale(scale.getX(), scale.getY(), scale.getZ()); return translationMatrix.mul(rotationMatrix.mul(scaleMatrix)); }
/** * <code>toRotationMatrix</code> converts this quaternion to a rotational matrix. The result is * stored in result. 4th row and 4th column values are untouched. Note: the result is created from * a normalized version of this quat. * * @param result The Matrix4f to store the result in. * @return the rotation matrix representation of this quaternion. */ public Matrix4f toRotationMatrix(Matrix4f result) { float norm = norm(); // we explicitly test norm against one here, saving a division // at the cost of a test and branch. Is it worth it? float s = (norm == 1f) ? 2f : (norm > 0f) ? 2f / norm : 0; // compute xs/ys/zs first to save 6 multiplications, since xs/ys/zs // will be used 2-4 times each. float xs = x * s; float ys = y * s; float zs = z * s; float xx = x * xs; float xy = x * ys; float xz = x * zs; float xw = w * xs; float yy = y * ys; float yz = y * zs; float yw = w * ys; float zz = z * zs; float zw = w * zs; // using s=2/norm (instead of 1/norm) saves 9 multiplications by 2 here result.m00 = 1 - (yy + zz); result.m01 = (xy - zw); result.m02 = (xz + yw); result.m10 = (xy + zw); result.m11 = 1 - (xx + zz); result.m12 = (yz - xw); result.m20 = (xz - yw); result.m21 = (yz + xw); result.m22 = 1 - (xx + yy); return result; }
/** Multiply this Matrix for a Scaling Matrix, where sx,sy and sz are the scaling factors */ public void scale(float sx, float sy, float sz) { Transform3f t = new Transform3f(); t.mM.A = sx; t.mM.F = sy; t.mM.M = sz; t.mM.R = 1; mult(t); }
/** * Multiply this Matrix for a Translation Matrix, where the vector (x,y,z) the translation entity */ public void translate(float x, float y, float z) { Transform3f t = new Transform3f(); t.mM.D = x; t.mM.H = y; t.mM.N = z; t.mM.R = 1; mult(t); }
@API.Constr public Translation(@API.Param(name = "loc") ObservableReadOnlyRef<Vector3f> location) { this.modelMatrix = new MakeOnGet<>( location, (loc) -> { Matrix4f matrix = new Matrix4f(); matrix.setIdentity(); matrix.translate(loc); return matrix; }); }
/** * 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; }
/** Multiply this Matrix for a Rotation of an angle alpha (in radians) around the Z ax */ public void rotateZ(float alpha) { Transform3f t = new Transform3f(); float cos = (float) (Math.cos(alpha)); float sin = (float) (Math.sin(alpha)); t.mM.A = cos; t.mM.B = -sin; t.mM.E = sin; t.mM.F = cos; mult(t); }
public BoundingVolume transform(Matrix4f trans, BoundingVolume store) { BoundingSphere sphere; if (store == null || store.getType() != BoundingVolume.Type.Sphere) { sphere = new BoundingSphere(1, new Vector3f(0, 0, 0)); } else { sphere = (BoundingSphere) store; } trans.mult(center, sphere.center); Vector3f axes = new Vector3f(1, 1, 1); trans.mult(axes, axes); float ax = getMaxAxis(axes); sphere.radius = FastMath.abs(ax * radius) + RADIUS_EPSILON - 1f; return sphere; }
public static Matrix4f rxinvert(Matrix4f m) { /* This assumes that m is merely a composition of rotations * and translations. */ return (m.trim3(1) .transpose() .mul1(makexlate(new Matrix4f(), new Coord3f(-m.m[12], -m.m[13], -m.m[14])))); }
/** * Sets the value of this axis-angle to the rotational component of the passed matrix. If the * specified matrix has no rotational component, the value of this AxisAngle4f is set to an angle * of 0 about an axis of (0,1,0). * * @param m1 the matrix4f */ public final void set(Matrix4f m1) { Matrix3f m3f = new Matrix3f(); m1.get(m3f); x = m3f.m21 - m3f.m12; y = m3f.m02 - m3f.m20; z = m3f.m10 - m3f.m01; double mag = x * x + y * y + z * z; if (mag > EPS) { mag = Math.sqrt(mag); double sin = 0.5 * mag; double cos = 0.5 * (m3f.m00 + m3f.m11 + m3f.m22 - 1.0); angle = (float) Math.atan2(sin, cos); double invMag = 1.0 / mag; x = (float) (x * invMag); y = (float) (y * invMag); z = (float) (z * invMag); } else { x = 0.0f; y = 1.0f; z = 0.0f; angle = 0.0f; } }
private void init() { mBox = new BoundingBox(mVertexBuffers.getVertexBuffer()); t = new Matrix4f(); t.setIdentity(); mZeroVector = new Vector3f(0, 0, 0); mEpsilon = 0.001f; }
/** * Sets the value of this axis-angle to the rotational component of the passed matrix. If the * specified matrix has no rotational component, the value of this AxisAngle4d is set to an angle * of 0 about an axis of (0,1,0). * * @param m1 the matrix4f */ public final void set(Matrix4f m1) { Matrix3d m3d = new Matrix3d(); m1.get(m3d); x = (float) (m3d.m21 - m3d.m12); y = (float) (m3d.m02 - m3d.m20); z = (float) (m3d.m10 - m3d.m01); double mag = x * x + y * y + z * z; if (mag > EPS) { mag = Math.sqrt(mag); double sin = 0.5 * mag; double cos = 0.5 * (m3d.m00 + m3d.m11 + m3d.m22 - 1.0); angle = (float) Math.atan2(sin, cos); double invMag = 1.0 / mag; x = x * invMag; y = y * invMag; z = z * invMag; } else { x = 0.0f; y = 1.0f; z = 0.0f; angle = 0.0f; } }
public Matrix4f mul(Matrix4f r) { Matrix4f res = new Matrix4f(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { res.set( i, j, m[i][0] * r.get(0, j) + m[i][1] * r.get(1, j) + m[i][2] * r.get(2, j) + m[i][3] * r.get(3, j)); } } return res; }
@Override public void resize(int width, int height) { super.resize(width, height); mWorldState.width = width; mWorldState.height = height; mWorldState.rotate = width > height ? 1 : 0; mScript.set_g_glWidth(mWorldState.width); mScript.set_g_glHeight(mWorldState.height); mScript.set_g_rotate(mWorldState.rotate); mScript.invoke_initLeaves(); Matrix4f proj = new Matrix4f(); proj.loadProjectionNormalized(mWidth, mHeight); mPvOrthoAlloc.setProjection(proj); }
public void loadProjectionNormalized(int i, int j) { Matrix4f matrix4f = new Matrix4f(); Matrix4f matrix4f1 = new Matrix4f(); if (i > j) { float f = (float) i / (float) j; matrix4f.loadFrustum(-f, f, -1F, 1.0F, 1.0F, 100F); } else { float f1 = (float) j / (float) i; matrix4f.loadFrustum(-1F, 1.0F, -f1, f1, 1.0F, 100F); } matrix4f1.loadRotate(180F, 0.0F, 1.0F, 0.0F); matrix4f.loadMultiply(matrix4f, matrix4f1); matrix4f1.loadScale(-2F, 2.0F, 1.0F); matrix4f.loadMultiply(matrix4f, matrix4f1); matrix4f1.loadTranslate(0.0F, 0.0F, 2.0F); matrix4f.loadMultiply(matrix4f, matrix4f1); load(matrix4f); }
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); }
public void loadMultiply(Matrix4f matrix4f, Matrix4f matrix4f1) { for (int i = 0; i < 4; i++) { float f3 = 0.0F; float f2 = 0.0F; float f1 = 0.0F; float f = 0.0F; for (int j = 0; j < 4; j++) { float f4 = matrix4f1.get(i, j); f3 += matrix4f.get(j, 0) * f4; f2 += matrix4f.get(j, 1) * f4; f1 += matrix4f.get(j, 2) * f4; f += matrix4f.get(j, 3) * f4; } set(i, 0, f3); set(i, 1, f2); set(i, 2, f1); set(i, 3, f); } }
/** * Multiplies this matrix with the specified right one. * * @param right - the right matrix to multiply with * @return this matrix instance */ public Matrix4f multiply(Matrix4f right) { float[] result = new float[16]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { result[i * 4 + j] += matrix[k + i * 4] * right.toFloatArray()[j + k * 4]; } } } matrix = result; return this; }
public void run() { adjustToScreenSize = (float) Math.min( jframe.getWidth(), jframe.getHeight()); // used here, since you can change the screen-Size Matrix4f newTranslation = new Matrix4f(); newTranslation.setIdentity(); Matrix4f oldcTranslation = new Matrix4f(); oldcTranslation = camera.getCameraMatrix(); // world z-Axis-turn if (mouseWorldTurn != null) { newTranslation.mul(mouseWorldTurn); mouseWorldTurn.setIdentity(); } // camera x-Axis-turn if (mouseTurn != null) { newTranslation.mul(mouseTurn); mouseTurn.setIdentity(); } // camera movement if (keyMove != null) { newTranslation.mul(keyMove); keyMove.setIdentity(); } newTranslation.mul(oldcTranslation); camera.setCameraMatrix(newTranslation); // something still appears to be wrong while turning // Trigger redrawing of the render window renderPanel.getCanvas().repaint(); }
@API.Constr public Size( @API.Param(name = "size") ObservableReadOnlyRef<Vector3f> size, @API.Param(name = "selfMode") Mode selfMode, @API.Param(name = "targetMode") Mode targetMode) { this.modelMatrix = new MakeOnGet<>( size, (s) -> { return Matrix4f.setIdentity(new Matrix4f()) .translate( new Vector3f( (selfMode.p.x - targetMode.p.x) * s.x, (selfMode.p.y - targetMode.p.y) * s.y, (selfMode.p.z - targetMode.p.z) * s.z)); }); }
public Matrix4f getProjectedTransformation() { Matrix4f transformationMatrix = getTransformation(); Matrix4f projectionMatrix = new Matrix4f().initProjection(fov, width, height, zNear, zFar); Matrix4f cameraRotation = new Matrix4f().initCamera(camera.getForward(), camera.getUp()); Matrix4f cameraTranslation = new Matrix4f() .initTranslation( -camera.getPos().getX(), -camera.getPos().getY(), -camera.getPos().getZ()); return projectionMatrix.mul(cameraRotation.mul(cameraTranslation.mul(transformationMatrix))); }
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(); } } }
private void createProgramVertex() { mPvOrthoAlloc = new ProgramVertexFixedFunction.Constants(mRS); Matrix4f proj = new Matrix4f(); proj.loadProjectionNormalized(mWidth, mHeight); mPvOrthoAlloc.setProjection(proj); ProgramVertexFixedFunction.Builder builder = new ProgramVertexFixedFunction.Builder(mRS); mPvSky = builder.create(); ((ProgramVertexFixedFunction) mPvSky).bindConstants(mPvOrthoAlloc); mScript.set_g_PVSky(mPvSky); mConstants = new ScriptField_Constants(mRS, 1); mUniformAlloc = mConstants.getAllocation(); ProgramVertex.Builder sb = new ProgramVertex.Builder(mRS); String t = "\n" + "varying vec4 varColor;\n" + "varying vec2 varTex0;\n" + "vec2 addDrop(vec4 d, vec2 pos, float dxMul) {\n" + " vec2 ret = vec2(0.0, 0.0);\n" + " vec2 delta = d.xy - pos;\n" + " delta.x *= dxMul;\n" + " float dist = length(delta);\n" + " if (dist < d.w) { \n" + " float amp = d.z * dist;\n" + " amp /= d.w * d.w;\n" + " amp *= sin(d.w - dist);\n" + " ret = delta * amp;\n" + " }\n" + " return ret;\n" + "}\n" + "void main() {\n" + " vec2 pos = ATTRIB_position.xy;\n" + " gl_Position = vec4(pos.x, pos.y, 0.0, 1.0);\n" + " float dxMul = 1.0;\n" + " varTex0 = vec2((pos.x + 1.0), (pos.y + 1.6666));\n" + " if (UNI_Rotate < 0.9) {\n" + " varTex0.xy *= vec2(0.25, 0.33);\n" + " varTex0.x += UNI_Offset.x * 0.5;\n" + " pos.x += UNI_Offset.x * 2.0;\n" + " } else {\n" + " varTex0.xy *= vec2(0.5, 0.3125);\n" + " dxMul = 2.5;\n" + " }\n" + " varColor = vec4(1.0, 1.0, 1.0, 1.0);\n" + " pos.xy += vec2(1.0, 1.0);\n" + " pos.xy *= vec2(25.0, 42.0);\n" + " varTex0.xy += addDrop(UNI_Drop01, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop02, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop03, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop04, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop05, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop06, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop07, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop08, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop09, pos, dxMul);\n" + " varTex0.xy += addDrop(UNI_Drop10, pos, dxMul);\n" + "}\n"; sb.setShader(t); sb.addConstant(mUniformAlloc.getType()); sb.addInput(mMesh.getVertexAllocation(0).getType().getElement()); mPvWater = sb.create(); mPvWater.bindConstants(mUniformAlloc, 0); mScript.set_g_PVWater(mPvWater); }
/** 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 void translate(float f, float f1, float f2) { Matrix4f matrix4f = new Matrix4f(); matrix4f.loadTranslate(f, f1, f2); multiply(matrix4f); }
public Matrix4f initRotation(float x, float y, float z) { Matrix4f rx = new Matrix4f(); Matrix4f ry = new Matrix4f(); Matrix4f rz = new Matrix4f(); x = (float) Math.toRadians(x); y = (float) Math.toRadians(y); z = (float) Math.toRadians(z); rz.m[0][0] = (float) Math.cos(z); rz.m[0][1] = -(float) Math.sin(z); rz.m[0][2] = 0; rz.m[0][3] = 0; rz.m[1][0] = (float) Math.sin(z); rz.m[1][1] = (float) Math.cos(z); rz.m[1][2] = 0; rz.m[1][3] = 0; rz.m[2][0] = 0; rz.m[2][1] = 0; rz.m[2][2] = 1; rz.m[2][3] = 0; rz.m[3][0] = 0; rz.m[3][1] = 0; rz.m[3][2] = 0; rz.m[3][3] = 1; rx.m[0][0] = 1; rx.m[0][1] = 0; rx.m[0][2] = 0; rx.m[0][3] = 0; rx.m[1][0] = 0; rx.m[1][1] = (float) Math.cos(x); rx.m[1][2] = -(float) Math.sin(x); rx.m[1][3] = 0; rx.m[2][0] = 0; rx.m[2][1] = (float) Math.sin(x); rx.m[2][2] = (float) Math.cos(x); rx.m[2][3] = 0; rx.m[3][0] = 0; rx.m[3][1] = 0; rx.m[3][2] = 0; rx.m[3][3] = 1; ry.m[0][0] = (float) Math.cos(y); ry.m[0][1] = 0; ry.m[0][2] = -(float) Math.sin(y); ry.m[0][3] = 0; ry.m[1][0] = 0; ry.m[1][1] = 1; ry.m[1][2] = 0; ry.m[1][3] = 0; ry.m[2][0] = (float) Math.sin(y); ry.m[2][1] = 0; ry.m[2][2] = (float) Math.cos(y); ry.m[2][3] = 0; ry.m[3][0] = 0; ry.m[3][1] = 0; ry.m[3][2] = 0; ry.m[3][3] = 1; m = rz.mul(ry.mul(rx)).getM(); return this; }