Java Code Examples for org.lwjgl.util.vector.Matrix4f#mul()

The following examples show how to use org.lwjgl.util.vector.Matrix4f#mul() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Trackball.java    From OpenModsLib with MIT License 6 votes vote down vote up
private Matrix4f getTransform(float mouseX, float mouseY) {
	Preconditions.checkNotNull(dragStart, "Draging not started");
	Vector3f current = calculateSpherePoint(mouseX, mouseY);

	float dot = Vector3f.dot(dragStart, current);
	if (Math.abs(dot - 1) < 0.0001) return lastTransform;

	Vector3f axis = Vector3f.cross(dragStart, current, null);

	try {
		axis.normalise();
	} catch (IllegalStateException e) { // Zero length vector
		return lastTransform;
	}

	float angle = 2 * (float)(Math.acos(dot));

	Matrix4f rotation = new Matrix4f();
	rotation.rotate(angle, axis);
	return Matrix4f.mul(rotation, lastTransform, null);

}
 
Example 2
Source File: GUIRoot.java    From tribaltrouble with GNU General Public License v2.0 6 votes vote down vote up
protected final void displayChangedNotify(int width, int height) {
	//Reset The Current Viewport And Perspective Transformation
	setDim(width, height);
	if (width != 0) {
		float scale = getUnitsPerPixel(Globals.GUI_Z);
		Matrix4f m1 = new Matrix4f();
		m1.setIdentity();
		Matrix4f m2 = new Matrix4f();
		m2.setIdentity();
		Matrix4f m3 = new Matrix4f();
		m1.scale(new Vector3f(scale, scale, scale));
		m2.translate(new Vector3f(0f, 0f, -Globals.GUI_Z));
		Matrix4f.mul(m2, m1, m3);
		m2.load(m3);
		m3.setIdentity();
		m3.translate(new Vector3f(-width/2f, -height/2f, 0f));
		Matrix4f.mul(m2, m3, m1);
		m1.store(matrix_buf);
		matrix_buf.rewind();
	}
	for (int i = 0; i < delegate_stack.size(); i++) {
		((CameraDelegate)delegate_stack.get(i)).displayChanged(width, height);
	}
}
 
Example 3
Source File: AnimationLoader.java    From OpenGL-Animation with The Unlicense 6 votes vote down vote up
private void processTransforms(String jointName, String[] rawData, KeyFrameData[] keyFrames, boolean root){
	FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
	float[] matrixData = new float[16];
	for(int i=0;i<keyFrames.length;i++){
		for(int j=0;j<16;j++){
			matrixData[j] = Float.parseFloat(rawData[i*16 + j]);
		}
		buffer.clear();
		buffer.put(matrixData);
		buffer.flip();
		Matrix4f transform = new Matrix4f();
		transform.load(buffer);
		transform.transpose();
		if(root){
			//because up axis in Blender is different to up axis in game
			Matrix4f.mul(CORRECTION, transform, transform);
		}
		keyFrames[i].addJointTransform(new JointTransformData(jointName, transform));
	}
}
 
Example 4
Source File: GData5.java    From ldparteditor with MIT License 6 votes vote down vote up
public void isShown(Matrix4f viewport, ThreadsafeHashMap<GData1, Matrix4f> CACHE_viewByProjection, float zoom) {

        if (wasShown) {
            return;
        }

        final Matrix4f M2 = CACHE_viewByProjection.get(parent);
        if (M2 == null) {
            Matrix4f.mul(viewport, parent.productMatrix, M);
            CACHE_viewByProjection.put(parent, M);
        } else {
            M = M2;
        }

        // Calculate the real coordinates
        Matrix4f.transform(M, A2, A);
        Matrix4f.transform(M, B2, B);
        Matrix4f.transform(M, C2, C);
        Matrix4f.transform(M, D2, D);

        N.x = A.y - B.y;
        N.y = B.x - A.x;
        N.z = 0f;
        N.w = 1f;
        wasShown = zoom / Vector4f.dot(N, Vector4f.sub(C, A, null)) * Vector4f.dot(N, Vector4f.sub(D, A, null)) > -1e-20f;
    }
 
Example 5
Source File: MatrixUtil.java    From tectonicus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Matrix4f createLookAt(Vector3f eye, Vector3f lookAt, Vector3f up)
{
	Matrix4f matrix = new Matrix4f();
	matrix.setIdentity();
	
	// Create the basis vectors
	Vector3f forwards = Vector3f.sub(eye, lookAt, null);
	forwards.normalise();
	
	Vector3f right = Vector3f.cross(up, forwards, null);
	right.normalise();
	
	Vector3f actualUp = Vector3f.cross(forwards, right, null);
	actualUp.normalise();
	
	// Right vector across the top
	matrix.m00 = right.x;
	matrix.m10 = right.y;
	matrix.m20 = right.z;
	
	// Up vector across the middle row
	matrix.m01 = actualUp.x;
	matrix.m11 = actualUp.y;
	matrix.m21 = actualUp.z;
	
	// Forwards vector across the bottom row
	matrix.m02 = forwards.x;
	matrix.m12 = forwards.y;
	matrix.m22 = forwards.z;
	
	// Negative translation in the last column
	Matrix4f translation = new Matrix4f();
	translation.setIdentity();
	translation.translate(new Vector3f(-eye.x, -eye.y, -eye.z));
	
	return Matrix4f.mul(matrix, translation, null);
}
 
Example 6
Source File: SkeletonLoader.java    From OpenGL-Animation with The Unlicense 5 votes vote down vote up
private JointData extractMainJointData(XmlNode jointNode, boolean isRoot){
	String nameId = jointNode.getAttribute("id");
	int index = boneOrder.indexOf(nameId);
	String[] matrixData = jointNode.getChild("matrix").getData().split(" ");
	Matrix4f matrix = new Matrix4f();
	matrix.load(convertData(matrixData));
	matrix.transpose();
	if(isRoot){
		//because in Blender z is up, but in our game y is up.
		Matrix4f.mul(CORRECTION, matrix, matrix);
	}
	jointCount++;
	return new JointData(index, nameId, matrix);
}
 
Example 7
Source File: PerspectiveCalculator.java    From ldparteditor with MIT License 5 votes vote down vote up
/**
 * @return The real transformation matrix of the viewport
 */
public Matrix4f getRealViewport() {
    Matrix4f viewport_transform = new Matrix4f();
    Matrix4f.setIdentity(viewport_transform);
    float zoom = c3d.getZoom();
    Matrix4f.scale(new Vector3f(zoom, zoom, zoom), viewport_transform, viewport_transform);
    Matrix4f viewport_rotation = c3d.getRotation();
    Matrix4f.mul(viewport_rotation, viewport_transform, viewport_transform);
    Matrix4f viewport_translation = c3d.getTranslation();
    Matrix4f.mul(viewport_transform, viewport_translation, viewport_transform);
    return viewport_transform;
}
 
Example 8
Source File: Camera.java    From LowPolyWater with The Unlicense 5 votes vote down vote up
@Override
public Matrix4f getProjectionViewMatrix() {
	if(reflected){
		return Matrix4f.mul(projectionMatrix, reflectedMatrix, null);
	}else{
		return Matrix4f.mul(projectionMatrix, viewMatrix, null);
	}
}
 
Example 9
Source File: GLMatrixStack.java    From ldparteditor with MIT License 5 votes vote down vote up
public void glMultMatrixf(Matrix4f matrix) {

        Matrix4f.mul(currentMatrix, matrix, currentMatrix);

        final FloatBuffer buf = BufferUtils.createFloatBuffer(16);
        currentMatrix.store(buf);
        buf.position(0);

        int model = shader.getUniformLocation("model" ); //$NON-NLS-1$
        GL20.glUniformMatrix4fv(model, false, buf);
    }
 
Example 10
Source File: GDataPNG.java    From ldparteditor with MIT License 4 votes vote down vote up
public GDataPNG(String text, Vertex offset, BigDecimal angleA, BigDecimal angleB, BigDecimal angleC, Vertex scale, String texturePath, GData1 parent) {
    super(parent);
    this.text = text;
    this.texturePath = texturePath;
    this.texture = new GTexture(TexType.PLANAR, texturePath, null, 0, new Vector3f(), new Vector3f(), new Vector3f(), 0, 0);
    this.offset = offset;
    this.scale = scale;
    this.angleA = angleA;
    this.angleB = angleB;
    this.angleC = angleC;

    {
        Matrix4f tMatrix2 = new Matrix4f();
        tMatrix2.setIdentity();
        tMatrix = tMatrix2.scale(new Vector3f(scale.x, scale.y, scale.z));
    }

    Matrix4f dMatrix = new Matrix4f();
    dMatrix.setIdentity();

    Vector4f direction = new Vector4f(0f, 0f, -1f, 1f);
    // Matrix4f.rotate((float) (angleC.doubleValue() / 180.0 * Math.PI), new Vector3f(0f, 0f, 1f), dMatrix, dMatrix);
    Matrix4f.rotate((float) (angleB.doubleValue() / 180.0 * Math.PI), new Vector3f(1f, 0f, 0f), dMatrix, dMatrix);
    Matrix4f.rotate((float) (angleA.doubleValue() / 180.0 * Math.PI), new Vector3f(0f, 1f, 0f), dMatrix, dMatrix);

    Matrix4f.transform(dMatrix, direction, direction);
    direction.w = 0f;
    direction.normalise();
    direction.w = 1f;
    this.direction = direction;

    dMatrix.setIdentity();

    Matrix4f.rotate((float) (angleC.doubleValue() / 180.0 * Math.PI), new Vector3f(0f, 0f, 1f), dMatrix, dMatrix);
    Matrix4f.rotate((float) (angleB.doubleValue() / 180.0 * Math.PI), new Vector3f(1f, 0f, 0f), dMatrix, dMatrix);
    Matrix4f.rotate((float) (angleA.doubleValue() / 180.0 * Math.PI), new Vector3f(0f, 1f, 0f), dMatrix, dMatrix);

    Matrix4f.mul(dMatrix, tMatrix, tMatrix);

    Vector4f vx = Matrix4f.transform(dMatrix, new Vector4f(offset.x, 0f, 0f, 1f), null);
    Vector4f vy = Matrix4f.transform(dMatrix, new Vector4f(0f, offset.y, 0f, 1f), null);
    Vector4f vz = Matrix4f.transform(dMatrix, new Vector4f(0f, 0f, offset.z, 1f), null);
    tMatrix.m30 = vx.x;
    tMatrix.m31 = vx.y;
    tMatrix.m32 = vx.z;
    tMatrix.m30 += vy.x;
    tMatrix.m31 += vy.y;
    tMatrix.m32 += vy.z;
    tMatrix.m30 += vz.x;
    tMatrix.m31 += vz.y;
    tMatrix.m32 += vz.z;

    matrix = BufferUtils.createFloatBuffer(16);
    tMatrix.store(matrix);

    matrix.position(0);
}
 
Example 11
Source File: SubMesh.java    From tectonicus with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static Matrix4f createTransform(Rotation horizontalRotation, final float horizontalAngleDeg,
										Rotation verticalRotation, final float verticalAngleDeg)
{
	if (horizontalRotation == Rotation.None && verticalRotation == Rotation.None)
		return null;
	
	float horizontalAngleInRads = horizontalAngleDeg / 360.0f * 2.0f * (float)Math.PI;
	if (horizontalRotation == Rotation.AntiClockwise)
		horizontalAngleInRads *= -1.0f;
	
	float verticalAngleInRads = verticalAngleDeg / 360.0f * 2.0f * (float)Math.PI;
	if (verticalRotation == Rotation.AntiClockwise)
		verticalAngleInRads *= -1.0f;
	
	Matrix4f trans0 = new Matrix4f();
	trans0.translate(new Vector3f(+0.5f, +0.5f, +0.5f));
	
	Matrix4f horizontalRotate = new Matrix4f();
	horizontalRotate.rotate(horizontalAngleInRads, new Vector3f(0, 1, 0));
	
	Matrix4f verticalRotate = new Matrix4f();
	verticalRotate.rotate(verticalAngleInRads, new Vector3f(0, 0, 1));
	
	Matrix4f trans1 = new Matrix4f();
	trans1.translate(new Vector3f(-0.5f, -0.5f, -0.5f));
	
	Matrix4f combinedRotate = new Matrix4f();
	Matrix4f.mul(horizontalRotate, verticalRotate, combinedRotate);
	
	Matrix4f working = new Matrix4f();
	Matrix4f.mul(trans0, combinedRotate, working);
	
	Matrix4f actual = new Matrix4f();
	Matrix4f.mul(working, trans1, actual);
	
	
	
/*	Matrix4f working = new Matrix4f();
	Matrix4f.mul(trans0, rotate, working);
	
	Matrix4f actual = new Matrix4f();
	Matrix4f.mul(working, trans1, actual);
*/
	
	return actual;
}
 
Example 12
Source File: Transform.java    From ldparteditor with MIT License 4 votes vote down vote up
public Transform apply(Matrix4f matrix) {
    Matrix4f.mul(m, matrix, m);
    return this;
}
 
Example 13
Source File: GData5.java    From ldparteditor with MIT License 4 votes vote down vote up
@Override
public void drawGL20_BFC_Textured(Composite3D c3d) {
    // done :)
    if (GData.globalDrawObjects) {
        final OpenGLRenderer20 r = (OpenGLRenderer20) c3d.getRenderer();
        GL20.glUniform1f(r.getNormalSwitchLoc(), GData.globalNegativeDeterminant ^ GData.globalInvertNext ? 1f : 0f);
        GL20.glUniform1f(r.getNoTextureSwitch(), 1f);
        GL20.glUniform1f(r.getNoLightSwitch(), 1f);
        GL20.glUniform1f(r.getCubeMapSwitch(), 0f);

        if (!visible)
            return;
        if (!c3d.isDrawingSolidMaterials())
            return;

        float result;
        float zoom = c3d.getZoom();
        switch (c3d.getLineMode()) {
        case 1:
            result = 1f;
            break;
        case 2:
        case 4:
            return;
        default:
            final Matrix4f M2 = GData.CACHE_viewByProjection.get(parent);
            if (M2 == null) {
                Matrix4f.mul(c3d.getViewport(), parent.productMatrix, M);
                GData.CACHE_viewByProjection.put(parent, M);
            } else {
                M = M2;
            }
            // Calculate the real coordinates
            Matrix4f.transform(M, A2, A);
            Matrix4f.transform(M, B2, B);
            Matrix4f.transform(M, C2, C);
            Matrix4f.transform(M, D2, D);

            N.x = A.y - B.y;
            N.y = B.x - A.x;

            result = zoom / Vector4f.dot(N, Vector4f.sub(C, A, null)) * Vector4f.dot(N, Vector4f.sub(D, A, null));
            break;
        }

        if (result > -1e-20f) {

            float r2;
            float g2;
            float b2;
            int cn;
            if (colourNumber == 24 && (cn = parent.r == .5f && parent.g == .5f && parent.b == .5f && (parent.a == 1.1f || parent.a == -1)  ? 16 : View.getLDConfigIndex(parent.r,  parent.g,  parent.b)) != 16) {
                GColour c = View.getLDConfigEdgeColour(cn, c3d);
                r2 = c.getR();
                g2 = c.getG();
                b2 = c.getB();
            } else {
                r2 = this.r;
                g2 = this.g;
                b2 = this.b;
            }

            GL11.glLineWidth(View.lineWidthGL[0]);
            GL11.glColor4f(r2, g2, b2, 1f);
            GL11.glBegin(GL11.GL_LINES);
            GraphicalDataTools.setVertex(x1, y1, z1, this, true);
            GraphicalDataTools.setVertex(x2, y2, z2, this, true);
            GL11.glEnd();
        }
    }
    if (GData.globalFoundTEXMAPNEXT) {
        GData.globalFoundTEXMAPStack.pop();
        GData.globalTextureStack.pop();
        GData.globalFoundTEXMAPStack.push(false);
        GData.globalFoundTEXMAPNEXT = false;
    }
}
 
Example 14
Source File: GDataCSG.java    From ldparteditor with MIT License 4 votes vote down vote up
@Override
public synchronized String transformAndColourReplace(String colour2, Matrix matrix) {
    boolean notChoosen = true;
    String t = null;
    switch (type) {
    case CSG.QUAD:
        if (notChoosen) {
            t = " CSG_QUAD "; //$NON-NLS-1$
            notChoosen = false;
        }
    case CSG.CIRCLE:
        if (notChoosen) {
            t = " CSG_CIRCLE "; //$NON-NLS-1$
            notChoosen = false;
        }
    case CSG.ELLIPSOID:
        if (notChoosen) {
            t = " CSG_ELLIPSOID "; //$NON-NLS-1$
            notChoosen = false;
        }
    case CSG.CUBOID:
        if (notChoosen) {
            t = " CSG_CUBOID "; //$NON-NLS-1$
            notChoosen = false;
        }
    case CSG.CYLINDER:
        if (notChoosen) {
            t = " CSG_CYLINDER "; //$NON-NLS-1$
            notChoosen = false;
        }
    case CSG.MESH:
        if (notChoosen) {
            t = " CSG_MESH "; //$NON-NLS-1$
            notChoosen = false;
        }
    case CSG.EXTRUDE:
        if (notChoosen) {
            t = " CSG_EXTRUDE "; //$NON-NLS-1$
            notChoosen = false;
        }
    case CSG.TRANSFORM:
        if (notChoosen) {
            t = " CSG_TRANSFORM "; //$NON-NLS-1$
            notChoosen = false;
        }
    case CSG.CONE:
        if (notChoosen) {
            t = " CSG_CONE "; //$NON-NLS-1$
            notChoosen = false;
        }
        StringBuilder colourBuilder = new StringBuilder();
        if (colour == null) {
            colourBuilder.append(16);
        } else if (colour.getColourNumber() == -1) {
            colourBuilder.append("0x2"); //$NON-NLS-1$
            colourBuilder.append(MathHelper.toHex((int) (255f * colour.getR())).toUpperCase());
            colourBuilder.append(MathHelper.toHex((int) (255f * colour.getG())).toUpperCase());
            colourBuilder.append(MathHelper.toHex((int) (255f * colour.getB())).toUpperCase());
        } else {
            colourBuilder.append(colour.getColourNumber());
        }
        Matrix4f newMatrix = new Matrix4f(this.matrix);
        Matrix4f newMatrix2 = new Matrix4f(matrix.getMatrix4f());
        Matrix4f.transpose(newMatrix, newMatrix);
        newMatrix.m30 = newMatrix.m03;
        newMatrix.m31 = newMatrix.m13;
        newMatrix.m32 = newMatrix.m23;
        newMatrix.m03 = 0f;
        newMatrix.m13 = 0f;
        newMatrix.m23 = 0f;
        Matrix4f.mul(newMatrix2, newMatrix, newMatrix);
        String col = colourBuilder.toString();
        if (col.equals(colour2))
            col = "16"; //$NON-NLS-1$
        String tag = ref1.substring(0, ref1.lastIndexOf("#>")); //$NON-NLS-1$
        return "0 !LPE" + t + tag + " " + col + " " + MathHelper.matrixToString(newMatrix); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    default:
        return text;
    }
}
 
Example 15
Source File: Camera.java    From OpenGL-Animation with The Unlicense 4 votes vote down vote up
@Override
public Matrix4f getProjectionViewMatrix() {
	return Matrix4f.mul(projectionMatrix, viewMatrix, null);
}
 
Example 16
Source File: Joint.java    From OpenGL-Animation with The Unlicense 3 votes vote down vote up
/**
 * This is called during set-up, after the joints hierarchy has been
 * created. This calculates the model-space bind transform of this joint
 * like so: </br>
 * </br>
 * {@code bindTransform = parentBindTransform * localBindTransform}</br>
 * </br>
 * where "bindTransform" is the model-space bind transform of this joint,
 * "parentBindTransform" is the model-space bind transform of the parent
 * joint, and "localBindTransform" is the bone-space bind transform of this
 * joint. It then calculates and stores the inverse of this model-space bind
 * transform, for use when calculating the final animation transform each
 * frame. It then recursively calls the method for all of the children
 * joints, so that they too calculate and store their inverse bind-pose
 * transform.
 * 
 * @param parentBindTransform
 *            - the model-space bind transform of the parent joint.
 */
protected void calcInverseBindTransform(Matrix4f parentBindTransform) {
	Matrix4f bindTransform = Matrix4f.mul(parentBindTransform, localBindTransform, null);
	Matrix4f.invert(bindTransform, inverseBindTransform);
	for (Joint child : children) {
		child.calcInverseBindTransform(bindTransform);
	}
}
 
Example 17
Source File: JointTransform.java    From OpenGL-Animation with The Unlicense 3 votes vote down vote up
/**
 * In this method the bone-space transform matrix is constructed by
 * translating an identity matrix using the position variable and then
 * applying the rotation. The rotation is applied by first converting the
 * quaternion into a rotation matrix, which is then multiplied with the
 * transform matrix.
 * 
 * @return This bone-space joint transform as a matrix. The exact same
 *         transform as represented by the position and rotation in this
 *         instance, just in matrix form.
 */
protected Matrix4f getLocalTransform() {
	Matrix4f matrix = new Matrix4f();
	matrix.translate(position);
	Matrix4f.mul(matrix, rotation.toRotationMatrix(), matrix);
	return matrix;
}
 
Example 18
Source File: Animator.java    From OpenGL-Animation with The Unlicense 3 votes vote down vote up
/**
 * This is the method where the animator calculates and sets those all-
 * important "joint transforms" that I talked about so much in the tutorial.
 * 
 * This method applies the current pose to a given joint, and all of its
 * descendants. It does this by getting the desired local-transform for the
 * current joint, before applying it to the joint. Before applying the
 * transformations it needs to be converted from local-space to model-space
 * (so that they are relative to the model's origin, rather than relative to
 * the parent joint). This can be done by multiplying the local-transform of
 * the joint with the model-space transform of the parent joint.
 * 
 * The same thing is then done to all the child joints.
 * 
 * Finally the inverse of the joint's bind transform is multiplied with the
 * model-space transform of the joint. This basically "subtracts" the
 * joint's original bind (no animation applied) transform from the desired
 * pose transform. The result of this is then the transform required to move
 * the joint from its original model-space transform to it's desired
 * model-space posed transform. This is the transform that needs to be
 * loaded up to the vertex shader and used to transform the vertices into
 * the current pose.
 * 
 * @param currentPose
 *            - a map of the local-space transforms for all the joints for
 *            the desired pose. The map is indexed by the name of the joint
 *            which the transform corresponds to.
 * @param joint
 *            - the current joint which the pose should be applied to.
 * @param parentTransform
 *            - the desired model-space transform of the parent joint for
 *            the pose.
 */
private void applyPoseToJoints(Map<String, Matrix4f> currentPose, Joint joint, Matrix4f parentTransform) {
	Matrix4f currentLocalTransform = currentPose.get(joint.name);
	Matrix4f currentTransform = Matrix4f.mul(parentTransform, currentLocalTransform, null);
	for (Joint childJoint : joint.children) {
		applyPoseToJoints(currentPose, childJoint, currentTransform);
	}
	Matrix4f.mul(currentTransform, joint.getInverseBindTransform(), currentTransform);
	joint.setAnimationTransform(currentTransform);
}
 
Example 19
Source File: Transform.java    From ldparteditor with MIT License 2 votes vote down vote up
/**
 * Applies the specified transform to this transform.
 * 
 * @param t
 *            transform to apply
 * 
 * @return this transform
 */
public Transform apply(Transform t) {
    Matrix4f.mul(m, t.m, m);
    return this;
}