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);
}
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());
}
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;
}
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);
}
}
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();
}
Aggregations