Search in sources :

Example 21 with Mesh

use of com.badlogic.gdx.graphics.Mesh in project commons-gdx by gemserk.

the class MeshUtils method createVerticalGradient.

public static Mesh createVerticalGradient(final float width, final float height, Color[] gradientColors, float[] gradientStops) {
    if (gradientStops.length == 1) {
        Color color = gradientColors[0];
        Gdx2dMeshBuilder meshBuilder = new Gdx2dMeshBuilder(6, false, true, 0);
        //
        meshBuilder.color(color).vertex(0f, //
        1f).color(color).vertex(1f, //
        1f).color(color).vertex(0f, //
        0f).color(color).vertex(1f, //
        1f).color(color).vertex(0f, //
        0f).color(color).vertex(1f, 0f);
        Mesh mesh = meshBuilder.build();
        mesh.scale(width, height, 1f);
        return mesh;
    }
    Gdx2dMeshBuilder meshBuilder = new Gdx2dMeshBuilder((gradientStops.length - 1) * 6, false, true, 0);
    for (int i = 0; i < gradientStops.length - 1; i++) {
        float y0 = gradientStops[i];
        float y1 = gradientStops[i + 1];
        Color c0 = gradientColors[i];
        Color c1 = gradientColors[i + 1];
        //
        meshBuilder.color(c0).vertex(0f, //
        y0).color(c0).vertex(1f, //
        y0).color(c1).vertex(0f, //
        y1).color(c0).vertex(1f, //
        y0).color(c1).vertex(0f, //
        y1).color(c1).vertex(1f, y1);
    }
    Mesh mesh = meshBuilder.build();
    mesh.scale(width, height, 1f);
    return mesh;
}
Also used : Color(com.badlogic.gdx.graphics.Color) Mesh(com.badlogic.gdx.graphics.Mesh)

Example 22 with Mesh

use of com.badlogic.gdx.graphics.Mesh 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 23 with Mesh

use of com.badlogic.gdx.graphics.Mesh in project libgdx by libgdx.

the class ConvexHullDistanceTest method createConvexHullShape.

public static btConvexHullShape createConvexHullShape(final Model model, boolean optimize) {
    final Mesh mesh = model.meshes.get(0);
    final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize());
    if (!optimize)
        return shape;
    // now optimize the shape
    final btShapeHull hull = new btShapeHull(shape);
    hull.buildHull(shape.getMargin());
    final btConvexHullShape result = new btConvexHullShape(hull);
    // delete the temporary shape
    shape.dispose();
    hull.dispose();
    return result;
}
Also used : com.badlogic.gdx.physics.bullet.collision.btShapeHull(com.badlogic.gdx.physics.bullet.collision.btShapeHull) Mesh(com.badlogic.gdx.graphics.Mesh) com.badlogic.gdx.physics.bullet.collision.btConvexHullShape(com.badlogic.gdx.physics.bullet.collision.btConvexHullShape)

Example 24 with Mesh

use of com.badlogic.gdx.graphics.Mesh in project libgdx by libgdx.

the class SpawnInfluencerPanel method onTemplateChecked.

@Override
public void onTemplateChecked(Model model, boolean isChecked) {
    //Update the shapes
    SpawnShapeValue shapeValue = null;
    Mesh mesh = model.meshes.get(0);
    weightMeshSpawnShapeValue.setMesh(mesh, model);
    meshSpawnShapeValue.setMesh(mesh, model);
    if (shapeCombo.getSelectedItem() == SPAWN_SHAPE_WEIGHT_MESH) {
        SpawnInfluencer influencer = (SpawnInfluencer) editor.getEmitter().findInfluencer(SpawnInfluencer.class);
        influencer.spawnShapeValue = weightMeshSpawnShapeValue;
    } else if (shapeCombo.getSelectedItem() == SPAWN_SHAPE_MESH) {
        SpawnInfluencer influencer = (SpawnInfluencer) editor.getEmitter().findInfluencer(SpawnInfluencer.class);
        influencer.spawnShapeValue = meshSpawnShapeValue;
    }
    editor.restart();
}
Also used : Mesh(com.badlogic.gdx.graphics.Mesh) SpawnInfluencer(com.badlogic.gdx.graphics.g3d.particles.influencers.SpawnInfluencer) LineSpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.LineSpawnShapeValue) EllipseSpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.EllipseSpawnShapeValue) UnweightedMeshSpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.UnweightedMeshSpawnShapeValue) WeightMeshSpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.WeightMeshSpawnShapeValue) RectangleSpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.RectangleSpawnShapeValue) PointSpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.PointSpawnShapeValue) SpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.SpawnShapeValue) PrimitiveSpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.PrimitiveSpawnShapeValue) CylinderSpawnShapeValue(com.badlogic.gdx.graphics.g3d.particles.values.CylinderSpawnShapeValue)

Example 25 with Mesh

use of com.badlogic.gdx.graphics.Mesh in project libgdx by libgdx.

the class DecalBatch method initialize.

/** Initializes the batch with the given amount of decal objects the buffer is able to hold when full.
	 * 
	 * @param size Maximum size of decal objects to hold in memory */
public void initialize(int size) {
    vertices = new float[size * Decal.SIZE];
    Mesh.VertexDataType vertexDataType = Mesh.VertexDataType.VertexArray;
    if (Gdx.gl30 != null) {
        vertexDataType = Mesh.VertexDataType.VertexBufferObjectWithVAO;
    }
    mesh = new Mesh(vertexDataType, false, size * 4, size * 6, new VertexAttribute(VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(VertexAttributes.Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
    short[] indices = new short[size * 6];
    int v = 0;
    for (int i = 0; i < indices.length; i += 6, v += 4) {
        indices[i] = (short) (v);
        indices[i + 1] = (short) (v + 2);
        indices[i + 2] = (short) (v + 1);
        indices[i + 3] = (short) (v + 1);
        indices[i + 4] = (short) (v + 2);
        indices[i + 5] = (short) (v + 3);
    }
    mesh.setIndices(indices);
}
Also used : VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) Mesh(com.badlogic.gdx.graphics.Mesh)

Aggregations

Mesh (com.badlogic.gdx.graphics.Mesh)32 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)15 Texture (com.badlogic.gdx.graphics.Texture)8 ShaderProgram (com.badlogic.gdx.graphics.glutils.ShaderProgram)8 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)4 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)3 VertexAttributes (com.badlogic.gdx.graphics.VertexAttributes)3 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)3 Material (com.badlogic.gdx.graphics.g3d.Material)3 MeshPart (com.badlogic.gdx.graphics.g3d.model.MeshPart)3 Pixmap (com.badlogic.gdx.graphics.Pixmap)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)2 Renderable (com.badlogic.gdx.graphics.g3d.Renderable)2 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)2 Vector3 (com.badlogic.gdx.math.Vector3)2 com.badlogic.gdx.physics.bullet.collision.btConvexHullShape (com.badlogic.gdx.physics.bullet.collision.btConvexHullShape)2 com.badlogic.gdx.physics.bullet.collision.btShapeHull (com.badlogic.gdx.physics.bullet.collision.btShapeHull)2 PerspectiveCamController (com.badlogic.gdx.tests.utils.PerspectiveCamController)2 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1