Search in sources :

Example 1 with Mesh

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

the class Model method convertMesh.

protected void convertMesh(ModelMesh modelMesh) {
    int numIndices = 0;
    for (ModelMeshPart part : modelMesh.parts) {
        numIndices += part.indices.length;
    }
    VertexAttributes attributes = new VertexAttributes(modelMesh.attributes);
    int numVertices = modelMesh.vertices.length / (attributes.vertexSize / 4);
    Mesh mesh = new Mesh(true, numVertices, numIndices, attributes);
    meshes.add(mesh);
    disposables.add(mesh);
    BufferUtils.copy(modelMesh.vertices, mesh.getVerticesBuffer(), modelMesh.vertices.length, 0);
    int offset = 0;
    mesh.getIndicesBuffer().clear();
    for (ModelMeshPart part : modelMesh.parts) {
        MeshPart meshPart = new MeshPart();
        meshPart.id = part.id;
        meshPart.primitiveType = part.primitiveType;
        meshPart.offset = offset;
        meshPart.size = part.indices.length;
        meshPart.mesh = mesh;
        mesh.getIndicesBuffer().put(part.indices);
        offset += meshPart.size;
        meshParts.add(meshPart);
    }
    mesh.getIndicesBuffer().position(0);
    for (MeshPart part : meshParts) part.update();
}
Also used : ModelMeshPart(com.badlogic.gdx.graphics.g3d.model.data.ModelMeshPart) VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes) ModelMesh(com.badlogic.gdx.graphics.g3d.model.data.ModelMesh) Mesh(com.badlogic.gdx.graphics.Mesh) ModelMeshPart(com.badlogic.gdx.graphics.g3d.model.data.ModelMeshPart) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart)

Example 2 with Mesh

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

the class BillboardParticleBatch method allocRenderable.

protected Renderable allocRenderable() {
    Renderable renderable = new Renderable();
    renderable.meshPart.primitiveType = GL20.GL_TRIANGLES;
    renderable.meshPart.offset = 0;
    renderable.material = new Material(this.blendingAttribute, this.depthTestAttribute, TextureAttribute.createDiffuse(texture));
    renderable.meshPart.mesh = new Mesh(false, MAX_VERTICES_PER_MESH, MAX_PARTICLES_PER_MESH * 6, currentAttributes);
    renderable.meshPart.mesh.setIndices(indices);
    renderable.shader = shader;
    return renderable;
}
Also used : Renderable(com.badlogic.gdx.graphics.g3d.Renderable) Mesh(com.badlogic.gdx.graphics.Mesh) Material(com.badlogic.gdx.graphics.g3d.Material)

Example 3 with Mesh

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

the class SpriteBatch method flush.

@Override
public void flush() {
    if (idx == 0)
        return;
    renderCalls++;
    totalRenderCalls++;
    int spritesInBatch = idx / 20;
    if (spritesInBatch > maxSpritesInBatch)
        maxSpritesInBatch = spritesInBatch;
    int count = spritesInBatch * 6;
    lastTexture.bind();
    Mesh mesh = this.mesh;
    mesh.setVertices(vertices, 0, idx);
    mesh.getIndicesBuffer().position(0);
    mesh.getIndicesBuffer().limit(count);
    if (blendingDisabled) {
        Gdx.gl.glDisable(GL20.GL_BLEND);
    } else {
        Gdx.gl.glEnable(GL20.GL_BLEND);
        if (blendSrcFunc != -1)
            Gdx.gl.glBlendFunc(blendSrcFunc, blendDstFunc);
    }
    mesh.render(customShader != null ? customShader : shader, GL20.GL_TRIANGLES, 0, count);
    idx = 0;
}
Also used : Mesh(com.badlogic.gdx.graphics.Mesh)

Example 4 with Mesh

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

the class RenderableShapeBuilder method buildNormals.

/** Builds normal, tangent and binormal of a Renderable.
	 * @param builder
	 * @param renderable
	 * @param vectorSize Size of the normal vector
	 * @param normalColor Normal vector's color
	 * @param tangentColor Tangent vector's color
	 * @param binormalColor Binormal vector's color */
public static void buildNormals(MeshPartBuilder builder, Renderable renderable, float vectorSize, Color normalColor, Color tangentColor, Color binormalColor) {
    Mesh mesh = renderable.meshPart.mesh;
    // Position
    int positionOffset = -1;
    if (mesh.getVertexAttribute(Usage.Position) != null)
        positionOffset = mesh.getVertexAttribute(Usage.Position).offset / FLOAT_BYTES;
    // Normal
    int normalOffset = -1;
    if (mesh.getVertexAttribute(Usage.Normal) != null)
        normalOffset = mesh.getVertexAttribute(Usage.Normal).offset / FLOAT_BYTES;
    // Tangent
    int tangentOffset = -1;
    if (mesh.getVertexAttribute(Usage.Tangent) != null)
        tangentOffset = mesh.getVertexAttribute(Usage.Tangent).offset / FLOAT_BYTES;
    // Binormal
    int binormalOffset = -1;
    if (mesh.getVertexAttribute(Usage.BiNormal) != null)
        binormalOffset = mesh.getVertexAttribute(Usage.BiNormal).offset / FLOAT_BYTES;
    int attributesSize = mesh.getVertexSize() / FLOAT_BYTES;
    int verticesOffset = 0;
    int verticesQuantity = 0;
    if (mesh.getNumIndices() > 0) {
        // Get min vertice to max vertice in indices array
        ensureIndicesCapacity(mesh.getNumIndices());
        mesh.getIndices(renderable.meshPart.offset, renderable.meshPart.size, indices, 0);
        short minVertice = minVerticeInIndices();
        short maxVertice = maxVerticeInIndices();
        verticesOffset = minVertice;
        verticesQuantity = maxVertice - minVertice;
    } else {
        verticesOffset = renderable.meshPart.offset;
        verticesQuantity = renderable.meshPart.size;
    }
    ensureVerticesCapacity(verticesQuantity * attributesSize);
    mesh.getVertices(verticesOffset * attributesSize, verticesQuantity * attributesSize, vertices, 0);
    for (int i = verticesOffset; i < verticesQuantity; i++) {
        int id = i * attributesSize;
        // Vertex position
        tmpV0.set(vertices[id + positionOffset], vertices[id + positionOffset + 1], vertices[id + positionOffset + 2]);
        // Vertex normal, tangent, binormal
        if (normalOffset != -1) {
            tmpV1.set(vertices[id + normalOffset], vertices[id + normalOffset + 1], vertices[id + normalOffset + 2]);
            tmpV2.set(tmpV0).add(tmpV1.scl(vectorSize));
        }
        if (tangentOffset != -1) {
            tmpV3.set(vertices[id + tangentOffset], vertices[id + tangentOffset + 1], vertices[id + tangentOffset + 2]);
            tmpV4.set(tmpV0).add(tmpV3.scl(vectorSize));
        }
        if (binormalOffset != -1) {
            tmpV5.set(vertices[id + binormalOffset], vertices[id + binormalOffset + 1], vertices[id + binormalOffset + 2]);
            tmpV6.set(tmpV0).add(tmpV5.scl(vectorSize));
        }
        // World transform
        tmpV0.mul(renderable.worldTransform);
        tmpV2.mul(renderable.worldTransform);
        tmpV4.mul(renderable.worldTransform);
        tmpV6.mul(renderable.worldTransform);
        // Draws normal, tangent, binormal
        if (normalOffset != -1) {
            builder.setColor(normalColor);
            builder.line(tmpV0, tmpV2);
        }
        if (tangentOffset != -1) {
            builder.setColor(tangentColor);
            builder.line(tmpV0, tmpV4);
        }
        if (binormalOffset != -1) {
            builder.setColor(binormalColor);
            builder.line(tmpV0, tmpV6);
        }
    }
}
Also used : Mesh(com.badlogic.gdx.graphics.Mesh)

Example 5 with Mesh

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

the class MeshShaderTest method create.

@Override
public void create() {
    String vertexShader = "attribute vec4 a_position;    \n" + "attribute vec4 a_color;\n" + "attribute vec2 a_texCoord0;\n" + "uniform mat4 u_worldView;\n" + "varying vec4 v_color;" + "varying vec2 v_texCoords;" + "void main()                  \n" + "{                            \n" + "   v_color = vec4(1, 1, 1, 1); \n" + "   v_texCoords = a_texCoord0; \n" + "   gl_Position =  u_worldView * a_position;  \n" + "}                            \n";
    String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec4 v_color;\n" + "varying vec2 v_texCoords;\n" + "uniform sampler2D u_texture;\n" + "void main()                                  \n" + "{                                            \n" + "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" + "}";
    shader = new ShaderProgram(vertexShader, fragmentShader);
    if (shader.isCompiled() == false) {
        Gdx.app.log("ShaderTest", shader.getLog());
        Gdx.app.exit();
    }
    mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.TexCoords(0));
    mesh.setVertices(new float[] { -0.5f, -0.5f, 0, 1, 1, 1, 1, 0, 1, 0.5f, -0.5f, 0, 1, 1, 1, 1, 1, 1, 0.5f, 0.5f, 0, 1, 1, 1, 1, 1, 0, -0.5f, 0.5f, 0, 1, 1, 1, 1, 0, 0 });
    mesh.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });
    //Mesh with texCoords wearing a pair of shorts. :)
    meshCustomVA = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorPacked(), new VertexAttribute(Usage.TextureCoordinates, 2, GL20.GL_UNSIGNED_SHORT, true, ShaderProgram.TEXCOORD_ATTRIBUTE + "0", 0));
    meshCustomVA.setVertices(new float[] { -0.5f, -0.5f, 0, FLOAT_WHITE, toSingleFloat(0, 1), 0.5f, -0.5f, 0, FLOAT_WHITE, toSingleFloat(1, 1), 0.5f, 0.5f, 0, FLOAT_WHITE, toSingleFloat(1, 0), -0.5f, 0.5f, 0, FLOAT_WHITE, toSingleFloat(0, 0) });
    meshCustomVA.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });
    texture = new Texture(Gdx.files.internal("data/bobrgb888-32x32.png"));
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) Mesh(com.badlogic.gdx.graphics.Mesh) Texture(com.badlogic.gdx.graphics.Texture)

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