Search in sources :

Example 1 with Quaternion

use of org.rajawali3d.math.Quaternion in project Rajawali by Rajawali.

the class Vector3Test method testRotateBy.

@Test
public void testRotateBy() throws Exception {
    final Quaternion q = new Quaternion();
    final Vector3 v = new Vector3(Vector3.X);
    v.multiply(2.0);
    final Vector3 out = q.multiply(v);
    assertNotNull(out);
    assertTrue(out != v);
    assertEquals(Double.doubleToRawLongBits(2d), Double.doubleToRawLongBits(out.x));
    assertEquals(Double.doubleToRawLongBits(0d), Double.doubleToRawLongBits(out.y));
    assertEquals(Double.doubleToRawLongBits(0d), Double.doubleToRawLongBits(out.z));
    q.fromAngleAxis(Axis.Z, 45.0);
    final Vector3 out1 = q.multiply(v);
    assertNotNull(out1);
    assertTrue(out1 != v);
    assertEquals(1.4142135623730951, out1.x, 1e-14);
    assertEquals(1.4142135623730951, out1.y, 1e-14);
    assertEquals(Double.doubleToRawLongBits(0d), Double.doubleToRawLongBits(out1.z));
    q.fromAngleAxis(1d, 0d, 1d, 45d);
    q.normalize();
    final Vector3 out2 = q.multiply(v);
    assertNotNull(out2);
    assertTrue(out2 != v);
    assertEquals(1.7071067811865477, out2.x, 1e-14);
    assertEquals(0.9999999999999998, out2.y, 1e-14);
    assertEquals(0.29289321881345237, out2.z, 1e-14);
}
Also used : Quaternion(org.rajawali3d.math.Quaternion) Vector3(org.rajawali3d.math.vector.Vector3) SmallTest(android.test.suitebuilder.annotation.SmallTest) Test(org.junit.Test)

Example 2 with Quaternion

use of org.rajawali3d.math.Quaternion in project Rajawali by Rajawali.

the class BlockMeshInstance method parseBlock.

public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    // Parse scene block
    RajLog.d("Parsing SceneGraph Block at position: " + dis.getPosition());
    mSceneGraphBlock = new SceneGraphBlock();
    mSceneGraphBlock.readGraphData(blockHeader, dis);
    // Block id for geometry
    mGeometryID = dis.readUnsignedInt();
    // Lookup the geometry or create it if it does not exist.
    final BlockHeader geomHeader = blockHeader.blockHeaders.get((short) mGeometryID);
    if (geomHeader == null) {
        mGeometry = new Object3D(mSceneGraphBlock.lookupName);
    } else {
        if (geomHeader.parser == null || !(geomHeader.parser instanceof ABaseObjectBlockParser))
            throw new ParsingException("Invalid block reference.");
        mGeometry = ((ABaseObjectBlockParser) geomHeader.parser).getBaseObject3D().clone(false, true);
        mGeometry.setName(mSceneGraphBlock.lookupName);
    }
    // Apply the materials
    final int materialCount = dis.readUnsignedShort();
    final Material[] materials = new Material[materialCount];
    for (int i = 0; i < materialCount; ++i) {
        final long materialID = dis.readUnsignedInt();
        if (materialID == 0) {
            materials[i] = getDefaultMaterial();
            materials[i].addTexture(getDefaultTexture());
        } else {
            final BlockHeader materialHeader = blockHeader.blockHeaders.get((short) materialID);
            if (materialHeader == null || materialHeader.parser == null || !(materialHeader.parser instanceof ATextureBlockParser))
                throw new ParsingException("Invalid block reference " + materialID);
            materials[i] = ((ATextureBlockParser) materialHeader.parser).getMaterial();
        }
    }
    // mesh instance properties; does it cast a shadow?
    AwdProperties properties = dis.readProperties(EXPECTED_PROPS);
    mCastsShadow = (boolean) properties.get(PROP_CASTS_SHADOW, true);
    final Matrix4 matrix = new Matrix4(mSceneGraphBlock.transformMatrix);
    // Set translation
    mGeometry.setPosition(matrix.getTranslation());
    // Set scale
    final Vector3 scale = matrix.getScaling();
    mGeometry.setScale(scale.y, scale.x, scale.z);
    // Set rotation
    mGeometry.setOrientation(new Quaternion().fromMatrix(matrix));
    int m = 0;
    if (!mGeometry.isContainer())
        mGeometry.setMaterial(materials[m++]);
    for (int i = 0; i < mGeometry.getNumChildren(); i++) mGeometry.getChildAt(i).setMaterial(materials[Math.min(materials.length - 1, m++)]);
    // ignore user properties, skip to end of block
    dis.skip(blockHeader.blockEnd - dis.getPosition());
}
Also used : Quaternion(org.rajawali3d.math.Quaternion) Material(org.rajawali3d.materials.Material) Vector3(org.rajawali3d.math.vector.Vector3) AwdProperties(org.rajawali3d.loader.LoaderAWD.AwdProperties) Matrix4(org.rajawali3d.math.Matrix4) Object3D(org.rajawali3d.Object3D) ParsingException(org.rajawali3d.loader.ParsingException) BlockHeader(org.rajawali3d.loader.LoaderAWD.BlockHeader)

Example 3 with Quaternion

use of org.rajawali3d.math.Quaternion in project Rajawali by Rajawali.

the class SlerpAnimation3D method quaternionFromVector.

protected Quaternion quaternionFromVector(Vector3 vec) {
    vec.normalize();
    final double angle = MathUtil.radiansToDegrees(Math.acos(Vector3.dot(mForwardVec, vec)));
    final Quaternion q = new Quaternion();
    q.fromAngleAxis(mTmpQuatVector.crossAndSet(mForwardVec, vec), angle);
    return q;
}
Also used : Quaternion(org.rajawali3d.math.Quaternion)

Example 4 with Quaternion

use of org.rajawali3d.math.Quaternion in project Rajawali by Rajawali.

the class ArcballCamera method applyRotation.

private void applyRotation() {
    if (mIsRotating) {
        mapToSphere((float) mPrevScreenCoord.getX(), (float) mPrevScreenCoord.getY(), mPrevSphereCoord);
        mapToSphere((float) mCurrScreenCoord.getX(), (float) mCurrScreenCoord.getY(), mCurrSphereCoord);
        Vector3 rotationAxis = mPrevSphereCoord.clone();
        rotationAxis.cross(mCurrSphereCoord);
        rotationAxis.normalize();
        double rotationAngle = Math.acos(Math.min(1, mPrevSphereCoord.dot(mCurrSphereCoord)));
        mCurrentOrientation.fromAngleAxis(rotationAxis, MathUtil.radiansToDegrees(rotationAngle));
        mCurrentOrientation.normalize();
        Quaternion q = new Quaternion(mStartOrientation);
        q.multiply(mCurrentOrientation);
        mEmpty.setOrientation(q);
    }
}
Also used : Quaternion(org.rajawali3d.math.Quaternion) Vector3(org.rajawali3d.math.vector.Vector3)

Example 5 with Quaternion

use of org.rajawali3d.math.Quaternion in project Rajawali by Rajawali.

the class ArcballCamera method initialize.

private void initialize() {
    mStartFOV = mFieldOfView;
    mLookAtEnabled = true;
    setLookAt(0, 0, 0);
    mEmpty = new Object3D();
    mScratchMatrix = new Matrix4();
    mScratchVector = new Vector3();
    mCameraStartPos = new Vector3();
    mPrevSphereCoord = new Vector3();
    mCurrSphereCoord = new Vector3();
    mPrevScreenCoord = new Vector2();
    mCurrScreenCoord = new Vector2();
    mStartOrientation = new Quaternion();
    mCurrentOrientation = new Quaternion();
}
Also used : Vector2(org.rajawali3d.math.vector.Vector2) Quaternion(org.rajawali3d.math.Quaternion) Vector3(org.rajawali3d.math.vector.Vector3) Matrix4(org.rajawali3d.math.Matrix4) Object3D(org.rajawali3d.Object3D)

Aggregations

Quaternion (org.rajawali3d.math.Quaternion)7 Vector3 (org.rajawali3d.math.vector.Vector3)4 SmallTest (android.test.suitebuilder.annotation.SmallTest)2 Test (org.junit.Test)2 Object3D (org.rajawali3d.Object3D)2 Matrix4 (org.rajawali3d.math.Matrix4)2 AwdProperties (org.rajawali3d.loader.LoaderAWD.AwdProperties)1 BlockHeader (org.rajawali3d.loader.LoaderAWD.BlockHeader)1 ParsingException (org.rajawali3d.loader.ParsingException)1 Material (org.rajawali3d.materials.Material)1 Vector2 (org.rajawali3d.math.vector.Vector2)1