Search in sources :

Example 26 with Matrix4f

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();
    }
}
Also used : HashMap(java.util.HashMap) Node(com.badlogic.gdx.graphics.g3d.model.Node) ArrayList(java.util.ArrayList) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) Matrix3f(javax.vecmath.Matrix3f) BoundingBox(com.badlogic.gdx.math.collision.BoundingBox) Mesh(com.nilunder.bdx.gl.Mesh) Material(com.nilunder.bdx.gl.Material) Vector3(com.badlogic.gdx.math.Vector3) ManifoldPoint(com.bulletphysics.collision.narrowphase.ManifoldPoint) Matrix4f(javax.vecmath.Matrix4f) Vector3f(javax.vecmath.Vector3f) Model(com.badlogic.gdx.graphics.g3d.Model) NodePart(com.badlogic.gdx.graphics.g3d.model.NodePart) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with Matrix4f

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);
}
Also used : Matrix4f(javax.vecmath.Matrix4f) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 28 with Matrix4f

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));
}
Also used : TRSRTransformation(net.minecraftforge.common.model.TRSRTransformation) Matrix4f(javax.vecmath.Matrix4f) Vector3f(javax.vecmath.Vector3f) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 29 with Matrix4f

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;
}
Also used : TRSRTransformation(net.minecraftforge.common.model.TRSRTransformation) Group(com.almuradev.content.model.obj.geometry.Group) Vertex(com.almuradev.content.model.obj.geometry.Vertex) UnpackedBakedQuad(net.minecraftforge.client.model.pipeline.UnpackedBakedQuad) IMixinTextureAtlasSprite(com.almuradev.content.mixin.iface.IMixinTextureAtlasSprite) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) VertexNormal(com.almuradev.content.model.obj.geometry.VertexNormal) VertexDefinition(com.almuradev.content.model.obj.geometry.VertexDefinition) VertexTextureCoordinate(com.almuradev.content.model.obj.geometry.VertexTextureCoordinate) Matrix4f(javax.vecmath.Matrix4f) Vector4f(javax.vecmath.Vector4f) MaterialDefinition(com.almuradev.content.model.obj.material.MaterialDefinition) ResourceLocation(net.minecraft.util.ResourceLocation) Face(com.almuradev.content.model.obj.geometry.Face)

Aggregations

Matrix4f (javax.vecmath.Matrix4f)29 Vector3f (javax.vecmath.Vector3f)10 TRSRTransformation (net.minecraftforge.common.model.TRSRTransformation)4 Matrix3f (javax.vecmath.Matrix3f)3 Matrix4 (com.badlogic.gdx.math.Matrix4)2 Vector3 (com.badlogic.gdx.math.Vector3)2 ManifoldPoint (com.bulletphysics.collision.narrowphase.ManifoldPoint)2 CollisionShape (com.bulletphysics.collision.shapes.CollisionShape)2 Transform (com.bulletphysics.linearmath.Transform)2 Mesh (com.nilunder.bdx.gl.Mesh)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Vector4f (javax.vecmath.Vector4f)2 TextureAtlasSprite (net.minecraft.client.renderer.texture.TextureAtlasSprite)2 ConveyorDirection (blusunrize.immersiveengineering.api.tool.ConveyorHandler.ConveyorDirection)1 Matrix4 (blusunrize.immersiveengineering.common.util.chickenbones.Matrix4)1 IMixinTextureAtlasSprite (com.almuradev.content.mixin.iface.IMixinTextureAtlasSprite)1 Face (com.almuradev.content.model.obj.geometry.Face)1 Group (com.almuradev.content.model.obj.geometry.Group)1 Vertex (com.almuradev.content.model.obj.geometry.Vertex)1