Search in sources :

Example 26 with ModelInstance

use of com.badlogic.gdx.graphics.g3d.ModelInstance in project libgdx by libgdx.

the class BasicBulletTest method create.

@Override
public void create() {
    super.create();
    instructions = "Swipe for next test";
    lights = new Environment();
    lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.2f, 0.2f, 1.f));
    lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1f, -0.7f));
    // Set up the camera
    final float width = Gdx.graphics.getWidth();
    final float height = Gdx.graphics.getHeight();
    if (width > height)
        camera = new PerspectiveCamera(67f, 3f * width / height, 3f);
    else
        camera = new PerspectiveCamera(67f, 3f, 3f * height / width);
    camera.position.set(10f, 10f, 10f);
    camera.lookAt(0, 0, 0);
    camera.update();
    // Create the model batch
    modelBatch = new ModelBatch();
    // Create some basic models
    final Model groundModel = modelBuilder.createRect(20f, 0f, -20f, -20f, 0f, -20f, -20f, 0f, 20f, 20f, 0f, 20f, 0, 1, 0, new Material(ColorAttribute.createDiffuse(Color.BLUE), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(16f)), Usage.Position | Usage.Normal);
    models.add(groundModel);
    final Model sphereModel = modelBuilder.createSphere(1f, 1f, 1f, 10, 10, new Material(ColorAttribute.createDiffuse(Color.RED), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(64f)), Usage.Position | Usage.Normal);
    models.add(sphereModel);
    // Load the bullet library
    // Normally use: Bullet.init();
    BaseBulletTest.init();
    // Create the bullet world
    collisionConfiguration = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfiguration);
    broadphase = new btDbvtBroadphase();
    solver = new btSequentialImpulseConstraintSolver();
    collisionWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    collisionWorld.setGravity(gravity);
    // Create the shapes and body construction infos
    btCollisionShape groundShape = new btBoxShape(tempVector.set(20, 0, 20));
    shapes.add(groundShape);
    btRigidBodyConstructionInfo groundInfo = new btRigidBodyConstructionInfo(0f, null, groundShape, Vector3.Zero);
    bodyInfos.add(groundInfo);
    btCollisionShape sphereShape = new btSphereShape(0.5f);
    shapes.add(sphereShape);
    sphereShape.calculateLocalInertia(1f, tempVector);
    btRigidBodyConstructionInfo sphereInfo = new btRigidBodyConstructionInfo(1f, null, sphereShape, tempVector);
    bodyInfos.add(sphereInfo);
    // Create the ground
    ModelInstance ground = new ModelInstance(groundModel);
    instances.add(ground);
    btDefaultMotionState groundMotionState = new btDefaultMotionState();
    groundMotionState.setWorldTransform(ground.transform);
    motionStates.add(groundMotionState);
    btRigidBody groundBody = new btRigidBody(groundInfo);
    groundBody.setMotionState(groundMotionState);
    bodies.add(groundBody);
    collisionWorld.addRigidBody(groundBody);
    // Create the spheres
    for (float x = -10f; x <= 10f; x += 2f) {
        for (float y = 5f; y <= 15f; y += 2f) {
            for (float z = 0f; z <= 0f; z += 2f) {
                ModelInstance sphere = new ModelInstance(sphereModel);
                instances.add(sphere);
                sphere.transform.trn(x + 0.1f * MathUtils.random(), y + 0.1f * MathUtils.random(), z + 0.1f * MathUtils.random());
                btDefaultMotionState sphereMotionState = new btDefaultMotionState();
                sphereMotionState.setWorldTransform(sphere.transform);
                motionStates.add(sphereMotionState);
                btRigidBody sphereBody = new btRigidBody(sphereInfo);
                sphereBody.setMotionState(sphereMotionState);
                bodies.add(sphereBody);
                collisionWorld.addRigidBody(sphereBody);
            }
        }
    }
}
Also used : com.badlogic.gdx.physics.bullet.collision.btBoxShape(com.badlogic.gdx.physics.bullet.collision.btBoxShape) com.badlogic.gdx.physics.bullet.collision.btCollisionDispatcher(com.badlogic.gdx.physics.bullet.collision.btCollisionDispatcher) Material(com.badlogic.gdx.graphics.g3d.Material) com.badlogic.gdx.physics.bullet.collision.btDefaultCollisionConfiguration(com.badlogic.gdx.physics.bullet.collision.btDefaultCollisionConfiguration) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) com.badlogic.gdx.physics.bullet.linearmath.btDefaultMotionState(com.badlogic.gdx.physics.bullet.linearmath.btDefaultMotionState) com.badlogic.gdx.physics.bullet.collision.btCollisionShape(com.badlogic.gdx.physics.bullet.collision.btCollisionShape) com.badlogic.gdx.physics.bullet.dynamics.btRigidBody.btRigidBodyConstructionInfo(com.badlogic.gdx.physics.bullet.dynamics.btRigidBody.btRigidBodyConstructionInfo) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) com.badlogic.gdx.physics.bullet.dynamics.btRigidBody(com.badlogic.gdx.physics.bullet.dynamics.btRigidBody) com.badlogic.gdx.physics.bullet.collision.btSphereShape(com.badlogic.gdx.physics.bullet.collision.btSphereShape) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) Model(com.badlogic.gdx.graphics.g3d.Model) com.badlogic.gdx.physics.bullet.collision.btDbvtBroadphase(com.badlogic.gdx.physics.bullet.collision.btDbvtBroadphase) Environment(com.badlogic.gdx.graphics.g3d.Environment) com.badlogic.gdx.physics.bullet.dynamics.btDiscreteDynamicsWorld(com.badlogic.gdx.physics.bullet.dynamics.btDiscreteDynamicsWorld) com.badlogic.gdx.physics.bullet.dynamics.btSequentialImpulseConstraintSolver(com.badlogic.gdx.physics.bullet.dynamics.btSequentialImpulseConstraintSolver) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)

Example 27 with ModelInstance

use of com.badlogic.gdx.graphics.g3d.ModelInstance in project libgdx by libgdx.

the class SoftBodyTest method create.

@Override
public void create() {
    super.create();
    world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 1f);
    float x0 = -2f, y0 = 6f, z0 = -2f;
    float x1 = 8f, y1 = 6f, z1 = 8f;
    Vector3 patch00 = new Vector3(x0, y0, z0);
    Vector3 patch10 = new Vector3(x1, y1, z0);
    Vector3 patch01 = new Vector3(x0, y0, z1);
    Vector3 patch11 = new Vector3(x1, y1, z1);
    softBody = btSoftBodyHelpers.CreatePatch(worldInfo, patch00, patch10, patch01, patch11, 15, 15, 15, false);
    softBody.takeOwnership();
    softBody.setTotalMass(100f);
    ((btSoftRigidDynamicsWorld) (world.collisionWorld)).addSoftBody(softBody);
    final int vertCount = softBody.getNodeCount();
    final int faceCount = softBody.getFaceCount();
    mesh = new Mesh(false, vertCount, faceCount * 3, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
    final int vertSize = mesh.getVertexSize() / 4;
    mesh.getVerticesBuffer().position(0);
    mesh.getVerticesBuffer().limit(vertCount * vertSize);
    mesh.getIndicesBuffer().position(0);
    mesh.getIndicesBuffer().limit(faceCount * 3);
    softBody.getVertices(mesh.getVerticesBuffer(), vertCount, mesh.getVertexSize(), 0);
    softBody.getIndices(mesh.getIndicesBuffer(), faceCount);
    final float[] verts = new float[vertCount * vertSize];
    final int uvOffset = mesh.getVertexAttribute(Usage.TextureCoordinates).offset / 4;
    final int normalOffset = mesh.getVertexAttribute(Usage.Normal).offset / 4;
    mesh.getVertices(verts);
    for (int i = 0; i < vertCount; i++) {
        verts[i * vertSize + normalOffset] = 0f;
        verts[i * vertSize + normalOffset + 1] = 1f;
        verts[i * vertSize + normalOffset + 2] = 0f;
        verts[i * vertSize + uvOffset] = (verts[i * vertSize] - x0) / (x1 - x0);
        verts[i * vertSize + uvOffset + 1] = (verts[i * vertSize + 2] - z0) / (z1 - z0);
    }
    mesh.setVertices(verts);
    texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    builder.part(new MeshPart("", mesh, 0, mesh.getNumIndices(), GL20.GL_TRIANGLES), new Material(TextureAttribute.createDiffuse(texture), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(64f), IntAttribute.createCullFace(0)));
    model = builder.end();
    instance = new ModelInstance(model);
    world.add(new BulletEntity(instance, null));
}
Also used : com.badlogic.gdx.physics.bullet.softbody.btSoftRigidDynamicsWorld(com.badlogic.gdx.physics.bullet.softbody.btSoftRigidDynamicsWorld) Mesh(com.badlogic.gdx.graphics.Mesh) Vector3(com.badlogic.gdx.math.Vector3) Material(com.badlogic.gdx.graphics.g3d.Material) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart) Texture(com.badlogic.gdx.graphics.Texture) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute)

Example 28 with ModelInstance

use of com.badlogic.gdx.graphics.g3d.ModelInstance in project libgdx by libgdx.

the class BaseG3dTest method createAxes.

private void createAxes() {
    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();
    MeshPartBuilder builder = modelBuilder.part("grid", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
    builder.setColor(Color.LIGHT_GRAY);
    for (float t = GRID_MIN; t <= GRID_MAX; t += GRID_STEP) {
        builder.line(t, 0, GRID_MIN, t, 0, GRID_MAX);
        builder.line(GRID_MIN, 0, t, GRID_MAX, 0, t);
    }
    builder = modelBuilder.part("axes", GL20.GL_LINES, Usage.Position | Usage.ColorUnpacked, new Material());
    builder.setColor(Color.RED);
    builder.line(0, 0, 0, 100, 0, 0);
    builder.setColor(Color.GREEN);
    builder.line(0, 0, 0, 0, 100, 0);
    builder.setColor(Color.BLUE);
    builder.line(0, 0, 0, 0, 0, 100);
    axesModel = modelBuilder.end();
    axesInstance = new ModelInstance(axesModel);
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) Material(com.badlogic.gdx.graphics.g3d.Material) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder)

Example 29 with ModelInstance

use of com.badlogic.gdx.graphics.g3d.ModelInstance in project libgdx by libgdx.

the class Basic3DSceneTest method doneLoading.

private void doneLoading() {
    Model model = assets.get("data/g3d/invaders.g3dj", Model.class);
    for (int i = 0; i < model.nodes.size; i++) {
        String id = model.nodes.get(i).id;
        ModelInstance instance = new ModelInstance(model, id);
        Node node = instance.getNode(id);
        instance.transform.set(node.globalTransform);
        node.translation.set(0, 0, 0);
        node.scale.set(1, 1, 1);
        node.rotation.idt();
        instance.calculateTransforms();
        if (id.equals("space")) {
            space = instance;
            continue;
        }
        instances.add(instance);
        if (id.equals("ship"))
            ship = instance;
        else if (id.startsWith("block"))
            blocks.add(instance);
        else if (id.startsWith("invader"))
            invaders.add(instance);
    }
    loading = false;
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) Node(com.badlogic.gdx.graphics.g3d.model.Node) Model(com.badlogic.gdx.graphics.g3d.Model)

Example 30 with ModelInstance

use of com.badlogic.gdx.graphics.g3d.ModelInstance in project libgdx by libgdx.

the class Basic3DSceneTest method render.

@Override
public void render() {
    if (loading && assets.update())
        doneLoading();
    camController.update();
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    modelBatch.begin(cam);
    for (ModelInstance instance : instances) modelBatch.render(instance, lights);
    if (space != null)
        modelBatch.render(space);
    modelBatch.end();
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance)

Aggregations

ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)55 Material (com.badlogic.gdx.graphics.g3d.Material)29 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)26 Model (com.badlogic.gdx.graphics.g3d.Model)22 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)17 ModelBatch (com.badlogic.gdx.graphics.g3d.ModelBatch)16 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)15 MeshPartBuilder (com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder)11 Texture (com.badlogic.gdx.graphics.Texture)10 Environment (com.badlogic.gdx.graphics.g3d.Environment)10 Vector3 (com.badlogic.gdx.math.Vector3)10 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)9 AnimationController (com.badlogic.gdx.graphics.g3d.utils.AnimationController)9 DungeonTileModelInstance (util.DungeonTileModelInstance)8 BlendingAttribute (com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute)7 DirectionalLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalLight)7 CameraInputController (com.badlogic.gdx.graphics.g3d.utils.CameraInputController)6 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)5 AssetManager (com.badlogic.gdx.assets.AssetManager)4 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4