Search in sources :

Example 6 with Matrix4

use of com.badlogic.gdx.math.Matrix4 in project libgdx by libgdx.

the class MeshBuilderTest method create.

@Override
public void create() {
    super.create();
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1.0f, -0.8f));
    modelsWindow.setVisible(false);
    Texture texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
    Material material = new Material(TextureAttribute.createDiffuse(texture));
    MeshBuilder meshBuilder = new MeshBuilder();
    meshBuilder.begin(Usage.Position | Usage.Normal | Usage.ColorPacked | Usage.TextureCoordinates, GL20.GL_TRIANGLES);
    meshBuilder.box(1f, 1f, 1f);
    Mesh mesh = new Mesh(true, meshBuilder.getNumVertices(), meshBuilder.getNumIndices(), meshBuilder.getAttributes());
    mesh = meshBuilder.end(mesh);
    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();
    modelBuilder.manage(texture);
    modelBuilder.node().id = "box";
    MeshPartBuilder mpb = modelBuilder.part("box", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates | Usage.ColorPacked, material);
    mpb.setColor(Color.RED);
    mpb.box(1f, 1f, 1f);
    modelBuilder.node().id = "sphere";
    mpb = modelBuilder.part("sphere", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates | Usage.ColorPacked, material);
    mpb.sphere(2f, 2f, 2f, 10, 5);
    modelBuilder.node().id = "cone";
    mpb = modelBuilder.part("cone", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates | Usage.ColorPacked, material);
    mpb.setVertexTransform(new Matrix4().rotate(Vector3.X, -45f));
    mpb.cone(2f, 3f, 1f, 8);
    modelBuilder.node().id = "cylinder";
    mpb = modelBuilder.part("cylinder", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates | Usage.ColorPacked, material);
    mpb.setUVRange(1f, 1f, 0f, 0f);
    mpb.cylinder(2f, 4f, 3f, 15);
    modelBuilder.node().id = "mesh";
    mpb = modelBuilder.part("mesh", GL20.GL_TRIANGLES, mesh.getVertexAttributes(), material);
    Matrix4 transform = new Matrix4();
    mpb.setVertexTransform(transform.setToTranslation(0, 2, 0));
    mpb.addMesh(mesh);
    mpb.setColor(Color.BLUE);
    mpb.setVertexTransform(transform.setToTranslation(1, 1, 0));
    mpb.addMesh(mesh);
    mpb.setColor(null);
    mpb.setVertexTransform(transform.setToTranslation(-1, 1, 0).rotate(Vector3.X, 45));
    mpb.addMesh(mesh);
    mpb.setVertexTransform(transform.setToTranslation(0, 1, 1));
    mpb.setUVRange(0.75f, 0.75f, 0.25f, 0.25f);
    mpb.addMesh(mesh);
    model = modelBuilder.end();
    instances.add(new ModelInstance(model, new Matrix4().trn(0f, 0f, 0f), "mesh", true));
    instances.add(new ModelInstance(model, new Matrix4().trn(-5f, 0f, -5f), "box", true));
    instances.add(new ModelInstance(model, new Matrix4().trn(5f, 0f, -5f), "sphere", true));
    instances.add(new ModelInstance(model, new Matrix4().trn(-5f, 0f, 5f), "cone", true));
    instances.add(new ModelInstance(model, new Matrix4().trn(5f, 0f, 5f), "cylinder", true));
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) MeshBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshBuilder) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) Environment(com.badlogic.gdx.graphics.g3d.Environment) Mesh(com.badlogic.gdx.graphics.Mesh) Material(com.badlogic.gdx.graphics.g3d.Material) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute) Texture(com.badlogic.gdx.graphics.Texture) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 7 with Matrix4

use of com.badlogic.gdx.math.Matrix4 in project libgdx by libgdx.

the class ModelInstance method invalidate.

/** Makes sure that each {@link NodePart} of the {@link Node} and its sub-nodes, doesn't reference a node outside this node
	 * tree and that all materials are listed in the {@link #materials} array. */
private void invalidate(Node node) {
    for (int i = 0, n = node.parts.size; i < n; ++i) {
        NodePart part = node.parts.get(i);
        ArrayMap<Node, Matrix4> bindPose = part.invBoneBindTransforms;
        if (bindPose != null) {
            for (int j = 0; j < bindPose.size; ++j) {
                bindPose.keys[j] = getNode(bindPose.keys[j].id);
            }
        }
        if (!materials.contains(part.material, true)) {
            final int midx = materials.indexOf(part.material, false);
            if (midx < 0)
                materials.add(part.material = part.material.copy());
            else
                part.material = materials.get(midx);
        }
    }
    for (int i = 0, n = node.getChildCount(); i < n; ++i) {
        invalidate(node.getChild(i));
    }
}
Also used : Node(com.badlogic.gdx.graphics.g3d.model.Node) NodePart(com.badlogic.gdx.graphics.g3d.model.NodePart) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 8 with Matrix4

use of com.badlogic.gdx.math.Matrix4 in project libgdx by libgdx.

the class SplitPane method draw.

@Override
public void draw(Batch batch, float parentAlpha) {
    validate();
    Color color = getColor();
    Drawable handle = style.handle;
    applyTransform(batch, computeTransform());
    Matrix4 transform = batch.getTransformMatrix();
    if (firstWidget != null) {
        batch.flush();
        getStage().calculateScissors(firstWidgetBounds, firstScissors);
        if (ScissorStack.pushScissors(firstScissors)) {
            if (firstWidget.isVisible())
                firstWidget.draw(batch, parentAlpha * color.a);
            batch.flush();
            ScissorStack.popScissors();
        }
    }
    if (secondWidget != null) {
        batch.flush();
        getStage().calculateScissors(secondWidgetBounds, secondScissors);
        if (ScissorStack.pushScissors(secondScissors)) {
            if (secondWidget.isVisible())
                secondWidget.draw(batch, parentAlpha * color.a);
            batch.flush();
            ScissorStack.popScissors();
        }
    }
    batch.setColor(color.r, color.g, color.b, parentAlpha * color.a);
    handle.draw(batch, handleBounds.x, handleBounds.y, handleBounds.width, handleBounds.height);
    resetTransform(batch);
}
Also used : Color(com.badlogic.gdx.graphics.Color) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 9 with Matrix4

use of com.badlogic.gdx.math.Matrix4 in project libgdx by libgdx.

the class MatrixJNITest method bench.

private void bench() {
    Matrix4 mata = new Matrix4();
    Matrix4 matb = new Matrix4();
    long start = TimeUtils.nanoTime();
    for (int i = 0; i < 1000000; i++) {
        mata.mul(matb);
    }
    Gdx.app.log("MatrixJNITest", "java matrix * matrix took: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    start = TimeUtils.nanoTime();
    for (int i = 0; i < 1000000; i++) {
        Matrix4.mul(mata.val, matb.val);
    }
    Gdx.app.log("MatrixJNITest", "jni matrix * matrix took: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    Vector3 vec = new Vector3();
    start = TimeUtils.nanoTime();
    for (int i = 0; i < 500000; i++) {
        vec.mul(mata);
    }
    Gdx.app.log("MatrixJNITest", "java vecs * matrix took: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    float[] fvec = new float[3];
    start = TimeUtils.nanoTime();
    for (int i = 0; i < 500000; i++) {
        Matrix4.mulVec(mata.val, fvec);
    }
    Gdx.app.log("MatrixJNITest", "jni vecs * matrix took: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    float[] fvecs = new float[3 * 500000];
    start = TimeUtils.nanoTime();
    Matrix4.mulVec(mata.val, fvecs, 0, 500000, 3);
    Gdx.app.log("MatrixJNITest", "jni bulk vecs * matrix took: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    start = TimeUtils.nanoTime();
    for (int i = 0; i < 1000000; i++) {
        mata.inv();
    }
    Gdx.app.log("MatrixJNITest", "java inv(matrix): " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    start = TimeUtils.nanoTime();
    for (int i = 0; i < 1000000; i++) {
        Matrix4.inv(mata.val);
    }
    Gdx.app.log("MatrixJNITest", "jni inv(matrix): " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
}
Also used : Vector3(com.badlogic.gdx.math.Vector3) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 10 with Matrix4

use of com.badlogic.gdx.math.Matrix4 in project libgdx by libgdx.

the class MatrixJNITest method create.

@Override
public void create() {
    Matrix4 mat1 = new Matrix4();
    Matrix4 mat2 = new Matrix4();
    Matrix4 mat3 = new Matrix4();
    Vector3 vec = new Vector3(1, 2, 3);
    float[] fvec = { 1, 2, 3 };
    float[] fvecs = { 1, 2, 3, 0, 0, 1, 2, 3, 0, 0, 1, 2, 3, 0, 0 };
    mat1.setToRotation(0, 1, 0, 45);
    mat2.setToRotation(1, 0, 0, 45);
    vec.mul(mat1);
    Matrix4.mulVec(mat1.val, fvec);
    Matrix4.mulVec(mat1.val, fvecs, 0, 3, 5);
    check(vec, fvec);
    check(vec, fvecs, 3, 5);
    vec.prj(mat1);
    Matrix4.prj(mat1.val, fvec);
    Matrix4.prj(mat1.val, fvecs, 0, 3, 5);
    check(vec, fvec);
    check(vec, fvecs, 3, 5);
    vec.rot(mat1);
    Matrix4.rot(mat1.val, fvec);
    Matrix4.rot(mat1.val, fvecs, 0, 3, 5);
    check(vec, fvec);
    check(vec, fvecs, 3, 5);
    if (mat1.det() != Matrix4.det(mat1.val))
        throw new GdxRuntimeException("det doesn't work");
    mat2.set(mat1);
    mat1.inv();
    Matrix4.inv(mat2.val);
    check(mat1, mat2);
    mat3.set(mat1);
    mat1.mul(mat2);
    Matrix4.mul(mat3.val, mat2.val);
    check(mat1, mat3);
    bench();
    System.out.println("All tests passed.");
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Vector3(com.badlogic.gdx.math.Vector3) Matrix4(com.badlogic.gdx.math.Matrix4)

Aggregations

Matrix4 (com.badlogic.gdx.math.Matrix4)31 Vector3 (com.badlogic.gdx.math.Vector3)10 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)5 Texture (com.badlogic.gdx.graphics.Texture)4 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)3 Material (com.badlogic.gdx.graphics.g3d.Material)3 Model (com.badlogic.gdx.graphics.g3d.Model)3 Quaternion (com.badlogic.gdx.math.Quaternion)3 JsonValue (com.badlogic.gdx.utils.JsonValue)3 InputAdapter (com.badlogic.gdx.InputAdapter)2 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)2 Environment (com.badlogic.gdx.graphics.g3d.Environment)2 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)2 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)2 com.badlogic.gdx.physics.bullet.collision.btBvhTriangleMeshShape (com.badlogic.gdx.physics.bullet.collision.btBvhTriangleMeshShape)2 com.badlogic.gdx.physics.bullet.collision.btSphereShape (com.badlogic.gdx.physics.bullet.collision.btSphereShape)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 NodeComponent (io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent)2 ArrayList (java.util.ArrayList)2