Search in sources :

Example 1 with MeshPartBuilder

use of com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder 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 2 with MeshPartBuilder

use of com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder in project bdx by GoranM.

the class Scene method createModel.

public Model createModel(JsonValue model) {
    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    short idx = 0;
    for (JsonValue mat : model) {
        Material m = materials.get(mat.name);
        if (mat.name.equals(defaultMaterial.id))
            m = new Material(m);
        MeshPartBuilder mpb = builder.part(model.name, GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, m);
        float[] verts = mat.asFloatArray();
        mpb.vertex(verts);
        int len = verts.length / Bdx.VERT_STRIDE;
        try {
            for (short i = 0; i < len; ++i) {
                mpb.index(idx);
                idx += 1;
            }
        } catch (Error e) {
            throw new RuntimeException("MODEL ERROR: Models with more than 32767 vertices are not supported. " + model.name + " has " + Integer.toString(len) + " vertices.");
        }
    }
    return builder.end();
}
Also used : ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) JsonValue(com.badlogic.gdx.utils.JsonValue) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder)

Example 3 with MeshPartBuilder

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

the class Animation3DTest method create.

@Override
public void create() {
    super.create();
    lights = new Environment();
    lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
    lights.add((shadowLight = new DirectionalShadowLight(1024, 1024, 30f, 30f, 1f, 100f)).set(0.8f, 0.8f, 0.8f, -.4f, -.4f, -.4f));
    lights.shadowMap = shadowLight;
    inputController.rotateLeftKey = inputController.rotateRightKey = inputController.forwardKey = inputController.backwardKey = 0;
    cam.position.set(25, 25, 25);
    cam.lookAt(0, 0, 0);
    cam.update();
    modelsWindow.setVisible(false);
    assets.load("data/g3d/skydome.g3db", Model.class);
    assets.load("data/g3d/concrete.png", Texture.class);
    assets.load("data/tree.png", Texture.class);
    assets.load("data/g3d/ship.obj", Model.class);
    loading = true;
    trForward.translation.set(0, 0, 8f);
    trBackward.translation.set(0, 0, -8f);
    trLeft.rotation.setFromAxis(Vector3.Y, 90);
    trRight.rotation.setFromAxis(Vector3.Y, -90);
    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    builder.node().id = "floor";
    MeshPartBuilder part = builder.part("floor", GL20.GL_TRIANGLES, Usage.Position | Usage.TextureCoordinates | Usage.Normal, new Material("concrete"));
    ((MeshBuilder) part).ensureRectangles(1600);
    for (float x = -200f; x < 200f; x += 10f) {
        for (float z = -200f; z < 200f; z += 10f) {
            part.rect(x, 0, z + 10f, x + 10f, 0, z + 10f, x + 10f, 0, z, x, 0, z, 0, 1, 0);
        }
    }
    builder.node().id = "tree";
    part = builder.part("tree", GL20.GL_TRIANGLES, Usage.Position | Usage.TextureCoordinates | Usage.Normal, new Material("tree"));
    part.rect(0f, 0f, -10f, 10f, 0f, -10f, 10f, 10f, -10f, 0f, 10f, -10f, 0, 0, 1f);
    part.setUVRange(1, 0, 0, 1);
    part.rect(10f, 0f, -10f, 0f, 0f, -10f, 0f, 10f, -10f, 10f, 10f, -10f, 0, 0, -1f);
    floorModel = builder.end();
    shadowBatch = new ModelBatch(new DepthShaderProvider());
}
Also used : ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) DirectionalShadowLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight) MeshBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshBuilder) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) Environment(com.badlogic.gdx.graphics.g3d.Environment) Material(com.badlogic.gdx.graphics.g3d.Material) 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 4 with MeshPartBuilder

use of com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder 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 5 with MeshPartBuilder

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

the class FrustumCullingTest method createFrustumModel.

public static Model createFrustumModel(final Vector3... p) {
    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    MeshPartBuilder mpb = builder.part("", GL20.GL_LINES, Usage.Position | Usage.Normal, new Material(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)));
    mpb.vertex(p[0].x, p[0].y, p[0].z, 0, 0, 1, p[1].x, p[1].y, p[1].z, 0, 0, 1, p[2].x, p[2].y, p[2].z, 0, 0, 1, p[3].x, p[3].y, p[3].z, 0, 0, // near
    1, p[4].x, p[4].y, p[4].z, 0, 0, -1, p[5].x, p[5].y, p[5].z, 0, 0, -1, p[6].x, p[6].y, p[6].z, 0, 0, -1, p[7].x, p[7].y, p[7].z, 0, 0, -1);
    mpb.index((short) 0, (short) 1, (short) 1, (short) 2, (short) 2, (short) 3, (short) 3, (short) 0);
    mpb.index((short) 4, (short) 5, (short) 5, (short) 6, (short) 6, (short) 7, (short) 7, (short) 4);
    mpb.index((short) 0, (short) 4, (short) 1, (short) 5, (short) 2, (short) 6, (short) 3, (short) 7);
    return builder.end();
}
Also used : ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) Material(com.badlogic.gdx.graphics.g3d.Material) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)

Aggregations

MeshPartBuilder (com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder)9 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)9 Material (com.badlogic.gdx.graphics.g3d.Material)7 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)5 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)5 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)3 Environment (com.badlogic.gdx.graphics.g3d.Environment)3 ModelBatch (com.badlogic.gdx.graphics.g3d.ModelBatch)3 Texture (com.badlogic.gdx.graphics.Texture)2 Model (com.badlogic.gdx.graphics.g3d.Model)2 DirectionalShadowLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight)2 DepthShaderProvider (com.badlogic.gdx.graphics.g3d.utils.DepthShaderProvider)2 MeshBuilder (com.badlogic.gdx.graphics.g3d.utils.MeshBuilder)2 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1 AssetManager (com.badlogic.gdx.assets.AssetManager)1 Mesh (com.badlogic.gdx.graphics.Mesh)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 ModelCache (com.badlogic.gdx.graphics.g3d.ModelCache)1 Renderable (com.badlogic.gdx.graphics.g3d.Renderable)1 Shader (com.badlogic.gdx.graphics.g3d.Shader)1