Search in sources :

Example 6 with Mesh

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

the class TriangleRaycastTest method tap.

@Override
public boolean tap(float screenX, float screenY, int count, int button) {
    Ray ray = camera.getPickRay(screenX, screenY);
    rayFrom.set(ray.origin);
    rayTo.set(ray.direction).scl(100).add(rayFrom);
    // Clean the callback object for reuse.
    triangleRaycastCallback.setHitFraction(1);
    triangleRaycastCallback.clearReport();
    triangleRaycastCallback.setFrom(rayFrom);
    triangleRaycastCallback.setTo(rayTo);
    // Ray casting is performed directly on the collision shape.
    // The callback specifies the intersected MeshPart as well as triangle.
    triangleShape.performRaycast(triangleRaycastCallback, rayFrom, rayTo);
    int currentTriangleIndex = triangleRaycastCallback.triangleIndex;
    int currentPartId = triangleRaycastCallback.partId;
    if (currentTriangleIndex == -1 || currentPartId == -1) {
        // No intersection was found.
        return false;
    }
    // Get the position coordinates of the vertices belonging to intersected triangle.
    Mesh mesh = model.meshParts.get(currentPartId).mesh;
    FloatBuffer verticesBuffer = mesh.getVerticesBuffer();
    ShortBuffer indicesBuffer = mesh.getIndicesBuffer();
    int posOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4;
    int vertexSize = mesh.getVertexSize() / 4;
    int currentTriangleFirstVertexIndex = currentTriangleIndex * 3;
    // Store the three vertices belonging to the selected triangle.
    for (int i = 0; i < 3; i++) {
        int currentVertexIndex = indicesBuffer.get(currentTriangleFirstVertexIndex + i);
        int j = currentVertexIndex * vertexSize + posOffset;
        float x = verticesBuffer.get(j++);
        float y = verticesBuffer.get(j++);
        float z = verticesBuffer.get(j);
        selectedTriangleVertices[i].set(x, y, z);
    }
    return true;
}
Also used : Mesh(com.badlogic.gdx.graphics.Mesh) FloatBuffer(java.nio.FloatBuffer) Ray(com.badlogic.gdx.math.collision.Ray) ShortBuffer(java.nio.ShortBuffer)

Example 7 with Mesh

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

the class MultipleRenderTargetTest method createFullScreenQuad.

public Mesh createFullScreenQuad() {
    float[] verts = new float[20];
    int i = 0;
    verts[i++] = -1;
    verts[i++] = -1;
    verts[i++] = 0;
    verts[i++] = 0f;
    verts[i++] = 0f;
    verts[i++] = 1f;
    verts[i++] = -1;
    verts[i++] = 0;
    verts[i++] = 1f;
    verts[i++] = 0f;
    verts[i++] = 1f;
    verts[i++] = 1f;
    verts[i++] = 0;
    verts[i++] = 1f;
    verts[i++] = 1f;
    verts[i++] = -1;
    verts[i++] = 1f;
    verts[i++] = 0;
    verts[i++] = 0f;
    verts[i++] = 1f;
    Mesh mesh = new Mesh(true, 4, 0, new VertexAttribute(VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
    mesh.setVertices(verts);
    return mesh;
}
Also used : VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) Mesh(com.badlogic.gdx.graphics.Mesh)

Example 8 with Mesh

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

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

the class VoxelWorld method getRenderables.

@Override
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) {
    renderedChunks = 0;
    for (int i = 0; i < chunks.length; i++) {
        VoxelChunk chunk = chunks[i];
        Mesh mesh = meshes[i];
        if (dirty[i]) {
            int numVerts = chunk.calculateVertices(vertices);
            numVertices[i] = numVerts / 4 * 6;
            mesh.setVertices(vertices, 0, numVerts * VoxelChunk.VERTEX_SIZE);
            dirty[i] = false;
        }
        if (numVertices[i] == 0)
            continue;
        Renderable renderable = pool.obtain();
        renderable.material = materials[i];
        renderable.meshPart.mesh = mesh;
        renderable.meshPart.offset = 0;
        renderable.meshPart.size = numVertices[i];
        renderable.meshPart.primitiveType = GL20.GL_TRIANGLES;
        renderables.add(renderable);
        renderedChunks++;
    }
}
Also used : Renderable(com.badlogic.gdx.graphics.g3d.Renderable) Mesh(com.badlogic.gdx.graphics.Mesh)

Example 10 with Mesh

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

the class HelloTriangle method create.

@Override
public void create() {
    String vertexShader = "attribute vec4 vPosition;    \n" + "void main()                  \n" + "{                            \n" + "   gl_Position = vPosition;  \n" + "}                            \n";
    String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "void main()                                  \n" + "{                                            \n" + "  gl_FragColor = vec4 ( 1.0, 1.0, 1.0, 1.0 );\n" + "}";
    shader = new ShaderProgram(vertexShader, fragmentShader);
    mesh = new Mesh(true, 3, 0, new VertexAttribute(Usage.Position, 3, "vPosition"));
    float[] vertices = { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f };
    mesh.setVertices(vertices);
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) 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