Search in sources :

Example 6 with MeshPartBuilder

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

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

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

the class MaterialTest method create.

@Override
public void create() {
    texture = new Texture(Gdx.files.internal("data/badlogic.jpg"), true);
    // Create material attributes. Each material can contain x-number of attributes.
    textureAttribute = new TextureAttribute(TextureAttribute.Diffuse, texture);
    colorAttribute = new ColorAttribute(ColorAttribute.Diffuse, Color.ORANGE);
    blendingAttribute = new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    ModelBuilder builder = new ModelBuilder();
    model = builder.createBox(1, 1, 1, new Material(), Usage.Position | Usage.Normal | Usage.TextureCoordinates);
    model.manageDisposable(texture);
    modelInstance = new ModelInstance(model);
    modelInstance.transform.rotate(Vector3.X, 45);
    material = modelInstance.materials.get(0);
    builder.begin();
    MeshPartBuilder mpb = builder.part("back", GL20.GL_TRIANGLES, Usage.Position | Usage.TextureCoordinates, new Material(textureAttribute));
    mpb.rect(-2, -2, -2, 2, -2, -2, 2, 2, -2, -2, 2, -2, 0, 0, 1);
    backModel = builder.end();
    background = new ModelInstance(backModel);
    modelBatch = new ModelBatch();
    camera = new PerspectiveCamera(45, 4, 4);
    camera.position.set(0, 0, 3);
    camera.direction.set(0, 0, -1);
    camera.update();
    Gdx.input.setInputProcessor(this);
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) BlendingAttribute(com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) Material(com.badlogic.gdx.graphics.g3d.Material) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute) Texture(com.badlogic.gdx.graphics.Texture) TextureAttribute(com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute)

Example 9 with MeshPartBuilder

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

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