use of javax.vecmath.Matrix4f 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.Matrix4f in project bdx by GoranM.
the class Mesh method vertTransform.
public void vertTransform(int materialSlot, Matrix4f matrix) {
Matrix4f m = matrix;
float[] vals = { m.m00, m.m10, m.m20, m.m30, m.m01, m.m11, m.m21, m.m31, m.m02, m.m12, m.m22, m.m32, m.m03, m.m13, m.m23, m.m33 };
model.meshes.first().transform(new Matrix4(vals), model.meshParts.get(materialSlot).offset, model.meshParts.get(materialSlot).size);
}
use of javax.vecmath.Matrix4f in project ImmersiveEngineering by BluSunrize.
the class TileEntityFloodlight method applyTransformations.
@SideOnly(Side.CLIENT)
@Override
public Optional<TRSRTransformation> applyTransformations(IBlockState object, String group, Optional<TRSRTransformation> transform) {
if (!transform.isPresent())
transform = Optional.of(new TRSRTransformation((Matrix4f) null));
Matrix4f mat = transform.get().getMatrix();
Vector3f transl = new Vector3f(.5f, .5f, .5f);
double yaw = 0;
double pitch = 0;
double roll = 0;
// pitch, yaw, roll
if (side.getAxis() == Axis.Y) {
yaw = facing == EnumFacing.SOUTH ? 180 : facing == EnumFacing.WEST ? 90 : facing == EnumFacing.EAST ? -90 : 0;
if (side == EnumFacing.DOWN)
roll = 180;
} else //It's a mess, but it works!
{
if (side == EnumFacing.NORTH) {
pitch = 90;
yaw = 180;
}
if (side == EnumFacing.SOUTH)
pitch = 90;
if (side == EnumFacing.WEST) {
pitch = 90;
yaw = -90;
}
if (side == EnumFacing.EAST) {
pitch = 90;
yaw = 90;
}
if (facing == EnumFacing.DOWN)
roll += 180;
else if (side.getAxis() == Axis.X && facing.getAxis() == Axis.Z)
roll += 90 * facing.getAxisDirection().getOffset() * side.getAxisDirection().getOffset();
else if (side.getAxis() == Axis.Z && facing.getAxis() == Axis.X)
roll += -90 * facing.getAxisDirection().getOffset() * side.getAxisDirection().getOffset();
}
transl.add(new Vector3f(side.getFrontOffsetX() * .125f, side.getFrontOffsetY() * .125f, side.getFrontOffsetZ() * .125f));
if ("axis".equals(group) || "light".equals(group) || "off".equals(group) || "glass".equals(group)) {
if (side.getAxis() == Axis.Y)
yaw += rotY;
else
roll += rotY;
if ("light".equals(group) || "off".equals(group) || "glass".equals(group))
pitch += rotX;
}
mat.setRotation(ClientUtils.degreeToQuaterion(pitch, yaw, roll));
mat.setTranslation(transl);
return Optional.of(new TRSRTransformation(mat));
}
use of javax.vecmath.Matrix4f in project Almura by AlmuraDev.
the class OBJBakedModel method getQuads.
@Override
public List<BakedQuad> getQuads(@Nullable final IBlockState state, @Nullable final EnumFacing side, final long rand) {
if (this.quads == null) {
this.quads = new LinkedList<>();
final TRSRTransformation transformation = this.state.apply(Optional.empty()).orElse(null);
Face particleFace = null;
TextureAtlasSprite particleAtlasSprite = null;
for (final Group group : this.model.getGroups()) {
final MaterialDefinition materialDefinition = group.getMaterialDefinition().orElse(null);
TextureAtlasSprite diffuseSprite = this.spriteOverride;
if (diffuseSprite == null) {
if (materialDefinition != null) {
if (materialDefinition.getDiffuseTexture().isPresent()) {
final ResourceLocation diffuseLocation = materialDefinition.getDiffuseTexture().orElse(null);
if (diffuseLocation != null) {
if (diffuseLocation.getResourcePath().endsWith(".png")) {
diffuseSprite = this.bakedTextureGetter.apply(new ResourceLocation(diffuseLocation.getResourceDomain(), diffuseLocation.getResourcePath().split("\\.")[0]));
} else {
diffuseSprite = this.bakedTextureGetter.apply(diffuseLocation);
}
}
}
}
}
if (diffuseSprite == null) {
diffuseSprite = ModelLoader.White.INSTANCE;
}
particleAtlasSprite = diffuseSprite;
for (final Face face : group.getFaces()) {
particleFace = face;
final UnpackedBakedQuad.Builder quadBuilder = new UnpackedBakedQuad.Builder(this.format);
quadBuilder.setContractUVs(true);
quadBuilder.setTexture(diffuseSprite);
VertexNormal normal = null;
for (final VertexDefinition vertexDef : face.getVertices()) {
if (normal == null) {
normal = vertexDef.getNormal().orElse(null);
}
for (int e = 0; e < this.format.getElementCount(); e++) {
switch(this.format.getElement(e).getUsage()) {
case POSITION:
final Vertex vertex = vertexDef.getVertex();
if (transformation != null) {
final Matrix4f transform = transformation.getMatrix();
final Vector4f position = new Vector4f(vertex.getX(), vertex.getY(), vertex.getZ(), 1f);
final Vector4f transformed = new Vector4f();
transform.transform(position, transformed);
quadBuilder.put(e, transformed.getX(), transformed.getY(), transformed.getZ());
} else {
quadBuilder.put(e, vertex.getX(), vertex.getY(), vertex.getZ());
}
break;
case UV:
final float u;
final float v;
if (this.spriteOverride == null) {
final VertexTextureCoordinate textureCoordinate = vertexDef.getTextureCoordinate().orElse(null);
if (textureCoordinate != null) {
u = textureCoordinate.getU();
v = 1f - textureCoordinate.getV();
} else {
u = 0f;
v = 1f;
}
} else {
switch(vertexDef.getIndex()) {
case 1:
u = 0f;
v = 0f;
break;
case 2:
u = 1f;
v = 0f;
break;
case 3:
u = 1f;
v = 1f;
break;
case 4:
u = 0f;
v = 1f;
break;
default:
u = 0f;
v = 0f;
}
}
quadBuilder.put(e, diffuseSprite.getInterpolatedU(u * 16f), diffuseSprite.getInterpolatedV(v * 16f));
break;
case NORMAL:
if (normal != null) {
quadBuilder.put(e, normal.getX(), normal.getY(), normal.getZ());
}
break;
case COLOR:
quadBuilder.put(e, 1f, 1f, 1f, 1f);
break;
default:
quadBuilder.put(e);
}
}
}
if (normal != null) {
quadBuilder.setQuadOrientation(EnumFacing.getFacingFromVector(normal.getX(), normal.getY(), normal.getZ()));
}
this.quads.add(quadBuilder.build());
}
}
if (particleFace != null && particleAtlasSprite != null) {
// For now, last face = particle generation
this.particleDiffuseSprite = this.createParticleSpriteFor(particleFace, particleAtlasSprite);
}
}
return this.quads;
}
Aggregations