Search in sources :

Example 36 with ModelInstance

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

the class MultipleRenderTargetTest method create.

@Override
public void create() {
    //use default prepend shader code for batch, some gpu drivers are less forgiving
    batch = new SpriteBatch();
    //depth texture not currently sampled
    ShaderProgram.pedantic = false;
    modelCache = new ModelCache();
    ShaderProgram.prependVertexCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_ARB_explicit_attrib_location : enable\n" : "#version 300 es\n";
    ShaderProgram.prependFragmentCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_ARB_explicit_attrib_location : enable\n" : "#version 300 es\n";
    renderContext = new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.ROUNDROBIN));
    shaderProvider = new BaseShaderProvider() {

        @Override
        protected Shader createShader(Renderable renderable) {
            return new MRTShader(renderable);
        }
    };
    renderableSorter = new DefaultRenderableSorter() {

        @Override
        public int compare(Renderable o1, Renderable o2) {
            return o1.shader.compareTo(o2.shader);
        }
    };
    mrtSceneShader = new ShaderProgram(Gdx.files.internal("data/g3d/shaders/mrtscene.vert"), Gdx.files.internal("data/g3d/shaders/mrtscene.frag"));
    if (!mrtSceneShader.isCompiled()) {
        System.out.println(mrtSceneShader.getLog());
    }
    quad = createFullScreenQuad();
    camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.near = 1f;
    camera.far = 100f;
    camera.position.set(3, 5, 10);
    camera.lookAt(0, 2, 0);
    camera.up.set(0, 1, 0);
    camera.update();
    cameraController = new FirstPersonCameraController(camera);
    cameraController.setVelocity(50);
    Gdx.input.setInputProcessor(cameraController);
    frameBuffer = new MRTFrameBuffer(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 3);
    AssetManager assetManager = new AssetManager();
    assetManager.load("data/g3d/materials/cannon.g3db", Model.class);
    assetManager.finishLoading();
    Model scene = assetManager.get("data/g3d/materials/cannon.g3db");
    cannon = new ModelInstance(scene, "Cannon_LP");
    cannon.transform.setToTranslationAndScaling(0, 0, 0, 0.001f, 0.001f, 0.001f);
    ModelBuilder modelBuilder = new ModelBuilder();
    for (int i = 0; i < NUM_LIGHTS; i++) {
        modelBuilder.begin();
        Light light = new Light();
        light.color.set(MathUtils.random(1f), MathUtils.random(1f), MathUtils.random(1f));
        light.position.set(MathUtils.random(-10f, 10f), MathUtils.random(10f, 15f), MathUtils.random(-10f, 10f));
        light.vy = MathUtils.random(10f, 20f);
        light.vx = MathUtils.random(-10f, 10f);
        light.vz = MathUtils.random(-10f, 10f);
        MeshPartBuilder meshPartBuilder = modelBuilder.part("light", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.ColorPacked | VertexAttributes.Usage.Normal, new Material());
        meshPartBuilder.setColor(light.color.x, light.color.y, light.color.z, 1f);
        meshPartBuilder.sphere(0.2f, 0.2f, 0.2f, 10, 10);
        light.lightInstance = new ModelInstance(modelBuilder.end());
        lights.add(light);
    }
    modelBuilder.begin();
    MeshPartBuilder meshPartBuilder = modelBuilder.part("floor", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.ColorPacked | VertexAttributes.Usage.Normal, new Material());
    meshPartBuilder.setColor(0.2f, 0.2f, 0.2f, 1f);
    meshPartBuilder.box(0, -0.1f, 0f, 20f, 0.1f, 20f);
    floorInstance = new ModelInstance(modelBuilder.end());
    Gdx.input.setInputProcessor(new InputMultiplexer(this, cameraController));
}
Also used : RenderContext(com.badlogic.gdx.graphics.g3d.utils.RenderContext) AssetManager(com.badlogic.gdx.assets.AssetManager) Material(com.badlogic.gdx.graphics.g3d.Material) DefaultRenderableSorter(com.badlogic.gdx.graphics.g3d.utils.DefaultRenderableSorter) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder) Shader(com.badlogic.gdx.graphics.g3d.Shader) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) BaseShaderProvider(com.badlogic.gdx.graphics.g3d.utils.BaseShaderProvider) ModelCache(com.badlogic.gdx.graphics.g3d.ModelCache) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) InputMultiplexer(com.badlogic.gdx.InputMultiplexer) Renderable(com.badlogic.gdx.graphics.g3d.Renderable) ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) Model(com.badlogic.gdx.graphics.g3d.Model) FirstPersonCameraController(com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController) DefaultTextureBinder(com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder)

Example 37 with ModelInstance

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

the class ShaderTest method create.

@Override
public void create() {
    modelBatch = new ModelBatch(new DefaultShaderProvider() {

        @Override
        protected Shader createShader(Renderable renderable) {
            if (renderable.material.has(TestAttribute.ID))
                return new TestShader(renderable);
            return super.createShader(renderable);
        }
    });
    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(0f, 0f, 20f);
    cam.lookAt(0, 0, 0);
    cam.near = 1f;
    cam.far = 300f;
    cam.update();
    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);
    Material testMaterial1 = new Material("TestMaterial1", new TestAttribute(1f));
    Material redMaterial = new Material("RedMaterial", ColorAttribute.createDiffuse(Color.RED));
    Material testMaterial2 = new Material("TestMaterial2", new TestAttribute(1f), ColorAttribute.createDiffuse(Color.BLUE));
    ModelBuilder builder = new ModelBuilder();
    Node node;
    builder.begin();
    node = builder.node();
    node.id = "testCone1";
    node.translation.set(-10, 0f, 0f);
    builder.part("testCone", GL20.GL_TRIANGLES, Usage.Position, testMaterial1).cone(5, 5, 5, 20);
    node = builder.node();
    node.id = "redSphere";
    builder.part("redSphere", GL20.GL_TRIANGLES, Usage.Position, redMaterial).sphere(5, 5, 5, 20, 20);
    node = builder.node();
    node.id = "testCone1";
    node.translation.set(10, 0f, 0f);
    builder.part("testCone", GL20.GL_TRIANGLES, Usage.Position, testMaterial2).cone(5, 5, 5, 20);
    model = builder.end();
    ModelInstance modelInstance;
    modelInstance = new ModelInstance(model);
    testAttribute1 = (TestAttribute) modelInstance.getMaterial("TestMaterial1").get(TestAttribute.ID);
    testAttribute2 = (TestAttribute) modelInstance.getMaterial("TestMaterial2").get(TestAttribute.ID);
    instances.add(modelInstance);
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) DefaultShaderProvider(com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider) CameraInputController(com.badlogic.gdx.graphics.g3d.utils.CameraInputController) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) Renderable(com.badlogic.gdx.graphics.g3d.Renderable) Node(com.badlogic.gdx.graphics.g3d.model.Node) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) Material(com.badlogic.gdx.graphics.g3d.Material) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera)

Example 38 with ModelInstance

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

the class ShadowMappingTest method create.

@Override
public void create() {
    modelBatch = new ModelBatch();
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
    environment.add((shadowLight = new DirectionalShadowLight(1024, 1024, 30f, 30f, 1f, 100f)).set(0.8f, 0.8f, 0.8f, -1f, -.8f, -.2f));
    environment.shadowMap = shadowLight;
    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(0f, 7f, 10f);
    cam.lookAt(0, 0, 0);
    cam.near = 1f;
    cam.far = 50f;
    cam.update();
    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();
    MeshPartBuilder mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.ColorUnpacked, new Material(ColorAttribute.createDiffuse(Color.WHITE)));
    mpb.setColor(1f, 1f, 1f, 1f);
    mpb.box(0, -1.5f, 0, 10, 1, 10);
    mpb.setColor(1f, 0f, 1f, 1f);
    mpb.sphere(2f, 2f, 2f, 10, 10);
    model = modelBuilder.end();
    instance = new ModelInstance(model);
    shadowBatch = new ModelBatch(new DepthShaderProvider());
    Gdx.input.setInputProcessor(camController = new CameraInputController(cam));
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) CameraInputController(com.badlogic.gdx.graphics.g3d.utils.CameraInputController) DirectionalShadowLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) Environment(com.badlogic.gdx.graphics.g3d.Environment) Material(com.badlogic.gdx.graphics.g3d.Material) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute) DepthShaderProvider(com.badlogic.gdx.graphics.g3d.utils.DepthShaderProvider)

Example 39 with ModelInstance

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

the class SkeletonTest method onLoaded.

@Override
protected void onLoaded() {
    if (currentlyLoading == null || currentlyLoading.isEmpty())
        return;
    instances.clear();
    animationControllers.clear();
    final ModelInstance instance = new ModelInstance(assets.get(currentlyLoading, Model.class));
    for (Material m : instance.materials) m.set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, 0.8f));
    instances.add(instance);
    if (instance.animations.size > 0)
        animationControllers.put(instance, new AnimationController(instance));
    currentlyLoading = null;
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) BlendingAttribute(com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute) AnimationController(com.badlogic.gdx.graphics.g3d.utils.AnimationController) Model(com.badlogic.gdx.graphics.g3d.Model) Material(com.badlogic.gdx.graphics.g3d.Material)

Example 40 with ModelInstance

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

the class Benchmark3DTest method onLoaded.

@Override
protected void onLoaded() {
    if (currentlyLoading == null || currentlyLoading.length() == 0)
        return;
    final ModelInstance instance = new ModelInstance(assets.get(currentlyLoading, Model.class));
    instance.transform = new Matrix4().idt();
    instance.transform.setToTranslation(MathUtils.random(-10, 10), MathUtils.random(-10, 10), MathUtils.random(-10, 10));
    instance.transform.rotate(Vector3.X, MathUtils.random(-180, 180));
    instance.transform.rotate(Vector3.Y, MathUtils.random(-180, 180));
    instance.transform.rotate(Vector3.Z, MathUtils.random(-180, 180));
    instances.add(instance);
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) Model(com.badlogic.gdx.graphics.g3d.Model) Matrix4(com.badlogic.gdx.math.Matrix4)

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