use of javax.vecmath.Matrix3f in project bdx by GoranM.
the class GameObject method alignAxisToVec.
public void alignAxisToVec(String axisName, Vector3f vec) {
Vector3f alignAxis = axis(axisName);
Vector3f rotAxis = new Vector3f();
rotAxis.cross(alignAxis, vec);
if (rotAxis.length() == 0)
rotAxis = axis(("XYZ".indexOf(axisName) + 1) % 3);
Matrix3f rotMatrix = Matrix3f.rotation(rotAxis, alignAxis.angle(vec));
Matrix3f ori = orientation();
rotMatrix.mul(ori);
orientation(rotMatrix);
}
use of javax.vecmath.Matrix3f in project bdx by GoranM.
the class GameObject method velocityLocal.
public Vector3f velocityLocal() {
Vector3f v = new Vector3f();
body.getLinearVelocity(v);
Matrix3f invOri = orientation();
invOri.invert();
return invOri.mult(v);
}
use of javax.vecmath.Matrix3f in project bdx by GoranM.
the class GameObject method join.
public void join(HashMap<Mesh, ArrayList<Matrix4f>> map) {
// Collect transformed vertex arrays for each material & calculate number of indices
int VERT_STRIDE = Bdx.VERT_STRIDE;
HashMap<Material, ArrayList<float[]>> tvaMap = new HashMap<Material, ArrayList<float[]>>();
HashMap<Material, Integer> lenMap = new HashMap<Material, Integer>();
Mesh m;
Node node;
Material mat;
MeshPart meshPart;
float[] va, tva;
int numIndices, numVertices, offset, j, len;
Vector3f p = new Vector3f();
Vector3f s = new Vector3f();
Matrix3f o = new Matrix3f();
Vector3f vP = new Vector3f();
Vector3f nP = new Vector3f();
Vector3f vPT = new Vector3f();
Vector3f nPT = new Vector3f();
Vector3f pos = position();
Vector3f sca = scale();
Matrix3f oriInv = orientation().inverted();
for (Map.Entry<Mesh, ArrayList<Matrix4f>> e : map.entrySet()) {
m = e.getKey();
node = m.model.nodes.get(0);
for (Matrix4f t : e.getValue()) {
t.get(p);
p.sub(pos);
p = oriInv.mult(p.div(sca));
t.getRotationScale(o);
o = oriInv.mult(o);
s.set(t.m30, t.m31, t.m32);
if (s.length() == 0) {
s.set(1, 1, 1);
}
s = s.div(sca);
for (NodePart nodePart : node.parts) {
meshPart = nodePart.meshPart;
numIndices = meshPart.size;
numVertices = numIndices * VERT_STRIDE;
offset = meshPart.offset * VERT_STRIDE;
va = meshPart.mesh.getVertices(offset, numVertices, new float[numVertices]);
tva = new float[numVertices];
j = 0;
for (int i = 0; i < numIndices; i++) {
vP.set(va[j], va[j + 1], va[j + 2]);
nP.set(va[j + 3], va[j + 4], va[j + 5]);
vPT.set(o.mult(vP.mul(s)));
vPT.add(p);
nPT.set(o.mult(vP.plus(nP)));
nPT.sub(o.mult(vP));
tva[j] = vPT.x;
tva[j + 1] = vPT.y;
tva[j + 2] = vPT.z;
tva[j + 3] = nPT.x;
tva[j + 4] = nPT.y;
tva[j + 5] = nPT.z;
tva[j + 6] = va[j + 6];
tva[j + 7] = va[j + 7];
j += VERT_STRIDE;
}
mat = m.materials.get(nodePart.material.id);
ArrayList<float[]> l;
if (tvaMap.containsKey(mat)) {
l = tvaMap.get(mat);
len = lenMap.get(mat);
} else {
l = new ArrayList<float[]>();
tvaMap.put(mat, l);
len = 0;
}
l.add(tva);
lenMap.put(mat, len + tva.length);
}
}
}
// Build a unique model out of meshparts for each material
ModelBuilder builder = new ModelBuilder();
builder.begin();
short idx = 0;
MeshPartBuilder mpb;
for (Map.Entry<Material, ArrayList<float[]>> e : tvaMap.entrySet()) {
mat = e.getKey();
len = lenMap.get(mat);
tva = new float[len];
j = 0;
for (float[] verts : e.getValue()) {
numVertices = verts.length;
for (int i = 0; i < numVertices; i++) {
tva[i + j] = verts[i];
}
j += numVertices;
}
mpb = builder.part(mat.name(), GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, mat);
mpb.vertex(tva);
try {
for (short i = 0; i < len / VERT_STRIDE; i++) {
mpb.index(idx);
idx += 1;
}
} catch (Error error) {
throw new RuntimeException("MODEL ERROR: Models with more than 32767 vertices are not supported. Decrease the number of objects to join.");
}
}
Model finishedModel = builder.end();
// Update mesh
mesh(new Mesh(finishedModel, scene));
// Update dimensionsNoScale and origin
com.badlogic.gdx.graphics.Mesh mesh = finishedModel.meshes.first();
BoundingBox bbox = mesh.calculateBoundingBox();
Vector3 dimensions = bbox.getDimensions(new Vector3());
Vector3 center = bbox.getCenter(new Vector3());
dimensionsNoScale = new Vector3f(dimensions.x, dimensions.y, dimensions.z);
origin = new Vector3f(center.x, center.y, center.z);
// Update body
updateBody();
if (json.get("mesh_name").asString() == null) {
visible = json.get("visible").asBoolean();
}
}
use of javax.vecmath.Matrix3f in project MinecraftForge by MinecraftForge.
the class TRSRTransformation method stepJacobi.
private static Quat4f stepJacobi(Matrix3f m) {
Matrix3f t = new Matrix3f();
Quat4f qt = new Quat4f(), ret = new Quat4f(0, 0, 0, 1);
Pair<Float, Float> p;
// 01
if (m.m01 * m.m01 + m.m10 * m.m10 > eps) {
p = approxGivensQuat(m.m00, .5f * (m.m01 + m.m10), m.m11);
qt.set(0, 0, p.getLeft(), p.getRight());
//qt.normalize();
ret.mul(qt);
//t.set(qt);
t.setIdentity();
t.m00 = qt.w * qt.w - qt.z * qt.z;
t.m11 = t.m00;
t.m10 = 2 * qt.z * qt.w;
t.m01 = -t.m10;
t.m22 = qt.w * qt.w + qt.z * qt.z;
m.mul(m, t);
t.transpose();
m.mul(t, m);
}
// 02
if (m.m02 * m.m02 + m.m20 * m.m20 > eps) {
p = approxGivensQuat(m.m00, .5f * (m.m02 + m.m20), m.m22);
qt.set(0, -p.getLeft(), 0, p.getRight());
//qt.normalize();
ret.mul(qt);
//t.set(qt);
t.setIdentity();
t.m00 = qt.w * qt.w - qt.y * qt.y;
t.m22 = t.m00;
t.m20 = -2 * qt.y * qt.w;
t.m02 = -t.m20;
t.m11 = qt.w * qt.w + qt.y * qt.y;
m.mul(m, t);
t.transpose();
m.mul(t, m);
}
// 12
if (m.m12 * m.m12 + m.m21 * m.m21 > eps) {
p = approxGivensQuat(m.m11, .5f * (m.m12 + m.m21), m.m22);
qt.set(p.getLeft(), 0, 0, p.getRight());
//qt.normalize();
ret.mul(qt);
//t.set(qt);
t.setIdentity();
t.m11 = qt.w * qt.w - qt.x * qt.x;
t.m22 = t.m11;
t.m21 = 2 * qt.x * qt.w;
t.m12 = -t.m21;
t.m00 = qt.w * qt.w + qt.x * qt.x;
m.mul(m, t);
t.transpose();
m.mul(t, m);
}
return ret;
}
use of javax.vecmath.Matrix3f in project MinecraftForge by MinecraftForge.
the class TRSRTransformation method svdDecompose.
/*
* Performs SVD decomposition of m, accumulating reflection in the scale (U and V are pure rotations).
*/
public static Triple<Quat4f, Vector3f, Quat4f> svdDecompose(Matrix3f m) {
// determine V by doing 5 steps of Jacobi iteration on MT * M
Quat4f u = new Quat4f(0, 0, 0, 1), v = new Quat4f(0, 0, 0, 1), qt = new Quat4f();
Matrix3f b = new Matrix3f(m), t = new Matrix3f();
t.transpose(m);
b.mul(t, b);
for (int i = 0; i < 5; i++) v.mul(stepJacobi(b));
v.normalize();
t.set(v);
b.set(m);
b.mul(t);
// FIXME: this doesn't work correctly for some reason; not crucial, so disabling for now; investigate in the future.
//sortSingularValues(b, v);
Pair<Float, Float> p;
float ul = 1f;
p = qrGivensQuat(b.m00, b.m10);
qt.set(0, 0, p.getLeft(), p.getRight());
u.mul(qt);
t.setIdentity();
t.m00 = qt.w * qt.w - qt.z * qt.z;
t.m11 = t.m00;
t.m10 = -2 * qt.z * qt.w;
t.m01 = -t.m10;
t.m22 = qt.w * qt.w + qt.z * qt.z;
ul *= t.m22;
b.mul(t, b);
p = qrGivensQuat(b.m00, b.m20);
qt.set(0, -p.getLeft(), 0, p.getRight());
u.mul(qt);
t.setIdentity();
t.m00 = qt.w * qt.w - qt.y * qt.y;
t.m22 = t.m00;
t.m20 = 2 * qt.y * qt.w;
t.m02 = -t.m20;
t.m11 = qt.w * qt.w + qt.y * qt.y;
ul *= t.m11;
b.mul(t, b);
p = qrGivensQuat(b.m11, b.m21);
qt.set(p.getLeft(), 0, 0, p.getRight());
u.mul(qt);
t.setIdentity();
t.m11 = qt.w * qt.w - qt.x * qt.x;
t.m22 = t.m11;
t.m21 = -2 * qt.x * qt.w;
t.m12 = -t.m21;
t.m00 = qt.w * qt.w + qt.x * qt.x;
ul *= t.m00;
b.mul(t, b);
ul = 1f / ul;
u.scale((float) Math.sqrt(ul));
Vector3f s = new Vector3f(b.m00 * ul, b.m11 * ul, b.m22 * ul);
return Triple.of(u, s, v);
}
Aggregations