/** * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return same as multiply(-1, store) */ @Override public Vector4 negate(final Vector4 store) { return multiply(-1, store); }
@Test public void testMultiply() { final Vector4 vec1 = new Vector4(1, -1, 2, -2); final Vector4 vec2 = vec1.multiply(2.0, null); final Vector4 vec2B = vec1.multiply(2.0, new Vector4()); assertEquals(new Vector4(2.0, -2.0, 4.0, -4.0), vec2); assertEquals(new Vector4(2.0, -2.0, 4.0, -4.0), vec2B); vec2.multiplyLocal(0.5); assertEquals(new Vector4(1.0, -1.0, 2.0, -2.0), vec2); final Vector4 vec3 = vec1.multiply(vec2, null); final Vector4 vec3B = vec1.multiply(vec2, new Vector4()); assertEquals(new Vector4(1, 1, 4, 4), vec3); assertEquals(new Vector4(1, 1, 4, 4), vec3B); final Vector4 vec4 = vec1.multiply(2, 3, 2, 3, null); final Vector4 vec4B = vec1.multiply(2, 3, 2, 3, new Vector4()); assertEquals(new Vector4(2, -3, 4, -6), vec4); assertEquals(new Vector4(2, -3, 4, -6), vec4B); vec1.multiplyLocal(0.5, 0.5, 0.5, 0.5); assertEquals(new Vector4(0.5, -0.5, 1.0, -1.0), vec1); vec1.multiplyLocal(vec2); assertEquals(new Vector4(0.5, 0.5, 2.0, 2.0), vec1); }
/** * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return same as multiply(-1, store) */ @Override public Vector4 negate(final Vector4 store) { return multiply(-1, store); }
/** * Creates a new unit length vector from this one by dividing by length. If the length is 0, (ie, if the vector is * 0, 0, 0, 0) then a new vector (0, 0, 0, 0) is returned. * * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return a new unit vector (or 0, 0, 0, 0 if this unit is 0 length) */ @Override public Vector4 normalize(final Vector4 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector4.ZERO) : new Vector4(Vector4.ZERO); }
/** * Creates a new unit length vector from this one by dividing by length. If the length is 0, (ie, if the vector is * 0, 0, 0, 0) then a new vector (0, 0, 0, 0) is returned. * * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return a new unit vector (or 0, 0, 0, 0 if this unit is 0 length) */ @Override public Vector4 normalize(final Vector4 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector4.ZERO) : new Vector4(Vector4.ZERO); }
private void modifyProjectionMatrix(final Vector4 clipPlane) { // Get the current projection matrix projectionMatrix = cam.getProjectionMatrix().toArray(projectionMatrix); // Get the inverse transpose of the current modelview matrix final ReadOnlyMatrix4 modelViewMatrixInvTrans = tRenderer.getCamera().getModelViewMatrix().invert(tmpMatrix) .transposeLocal(); modelViewMatrixInvTrans.applyPre(clipPlane, clipPlane); // Calculate the clip-space corner point opposite the clipping plane // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and // transform it into camera space by multiplying it // by the inverse of the projection matrix cornerPoint.setX((sign(clipPlane.getX()) + projectionMatrix[8]) / projectionMatrix[0]); cornerPoint.setY((sign(clipPlane.getY()) + projectionMatrix[9]) / projectionMatrix[5]); cornerPoint.setZ(-1.0); cornerPoint.setW((1.0 + projectionMatrix[10]) / projectionMatrix[14]); // Calculate the scaled plane vector final Vector4 scaledPlaneVector = clipPlane.multiply((2.0 / clipPlane.dot(cornerPoint)), cornerPoint); // Replace the third row of the projection matrix projectionMatrix[2] = scaledPlaneVector.getX(); projectionMatrix[6] = scaledPlaneVector.getY(); projectionMatrix[10] = scaledPlaneVector.getZ() + 1.0; projectionMatrix[14] = scaledPlaneVector.getW(); // Load it back into OpenGL final Matrix4 newProjectionMatrix = tmpMatrix.fromArray(projectionMatrix); tRenderer.getCamera().setProjectionMatrix(newProjectionMatrix); }
private void modifyProjectionMatrix(final Vector4 clipPlane) { // Get the current projection matrix projectionMatrix = cam.getProjectionMatrix().toArray(projectionMatrix); // Get the inverse transpose of the current modelview matrix final ReadOnlyMatrix4 modelViewMatrixInvTrans = tRenderer.getCamera().getViewMatrix().invert(tmpMatrix) .transposeLocal(); modelViewMatrixInvTrans.applyPre(clipPlane, clipPlane); // Calculate the clip-space corner point opposite the clipping plane // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and // transform it into camera space by multiplying it // by the inverse of the projection matrix cornerPoint.setX((sign(clipPlane.getX()) + projectionMatrix[8]) / projectionMatrix[0]); cornerPoint.setY((sign(clipPlane.getY()) + projectionMatrix[9]) / projectionMatrix[5]); cornerPoint.setZ(-1.0); cornerPoint.setW((1.0 + projectionMatrix[10]) / projectionMatrix[14]); // Calculate the scaled plane vector final Vector4 scaledPlaneVector = clipPlane.multiply((2.0 / clipPlane.dot(cornerPoint)), cornerPoint); // Replace the third row of the projection matrix projectionMatrix[2] = scaledPlaneVector.getX(); projectionMatrix[6] = scaledPlaneVector.getY(); projectionMatrix[10] = scaledPlaneVector.getZ() + 1.0; projectionMatrix[14] = scaledPlaneVector.getW(); // Load it back into OpenGL final Matrix4 newProjectionMatrix = tmpMatrix.fromArray(projectionMatrix); tRenderer.getCamera().setProjectionMatrix(newProjectionMatrix); }