Search in sources :

Example 1 with VertexAttributes

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

the class WeightMeshSpawnShapeValue method calculateWeights.

/** Calculate the weights of each triangle of the wrapped mesh. If the mesh has indices: the function will calculate the weight
	 * of those triangles. If the mesh has not indices: the function will consider the vertices as a triangle strip. */
public void calculateWeights() {
    distribution.clear();
    VertexAttributes attributes = mesh.getVertexAttributes();
    int indicesCount = mesh.getNumIndices();
    int vertexCount = mesh.getNumVertices();
    int vertexSize = (short) (attributes.vertexSize / 4), positionOffset = (short) (attributes.findByUsage(Usage.Position).offset / 4);
    float[] vertices = new float[vertexCount * vertexSize];
    mesh.getVertices(vertices);
    if (indicesCount > 0) {
        short[] indices = new short[indicesCount];
        mesh.getIndices(indices);
        // Calculate the Area
        for (int i = 0; i < indicesCount; i += 3) {
            int p1Offset = indices[i] * vertexSize + positionOffset, p2Offset = indices[i + 1] * vertexSize + positionOffset, p3Offset = indices[i + 2] * vertexSize + positionOffset;
            float x1 = vertices[p1Offset], y1 = vertices[p1Offset + 1], z1 = vertices[p1Offset + 2], x2 = vertices[p2Offset], y2 = vertices[p2Offset + 1], z2 = vertices[p2Offset + 2], x3 = vertices[p3Offset], y3 = vertices[p3Offset + 1], z3 = vertices[p3Offset + 2];
            float area = Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2f);
            distribution.add(new Triangle(x1, y1, z1, x2, y2, z2, x3, y3, z3), area);
        }
    } else {
        // Calculate the Area
        for (int i = 0; i < vertexCount; i += vertexSize) {
            int p1Offset = i + positionOffset, p2Offset = p1Offset + vertexSize, p3Offset = p2Offset + vertexSize;
            float x1 = vertices[p1Offset], y1 = vertices[p1Offset + 1], z1 = vertices[p1Offset + 2], x2 = vertices[p2Offset], y2 = vertices[p2Offset + 1], z2 = vertices[p2Offset + 2], x3 = vertices[p3Offset], y3 = vertices[p3Offset + 1], z3 = vertices[p3Offset + 2];
            float area = Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2f);
            distribution.add(new Triangle(x1, y1, z1, x2, y2, z2, x3, y3, z3), area);
        }
    }
    // Generate cumulative distribution
    distribution.generateNormalized();
}
Also used : VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes)

Example 2 with VertexAttributes

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

the class BaseShader method init.

/** Initialize this shader, causing all registered uniforms/attributes to be fetched. */
public void init(final ShaderProgram program, final Renderable renderable) {
    if (locations != null)
        throw new GdxRuntimeException("Already initialized");
    if (!program.isCompiled())
        throw new GdxRuntimeException(program.getLog());
    this.program = program;
    final int n = uniforms.size;
    locations = new int[n];
    for (int i = 0; i < n; i++) {
        final String input = uniforms.get(i);
        final Validator validator = validators.get(i);
        final Setter setter = setters.get(i);
        if (validator != null && !validator.validate(this, i, renderable))
            locations[i] = -1;
        else {
            locations[i] = program.fetchUniformLocation(input, false);
            if (locations[i] >= 0 && setter != null) {
                if (setter.isGlobal(this, i))
                    globalUniforms.add(i);
                else
                    localUniforms.add(i);
            }
        }
        if (locations[i] < 0) {
            validators.set(i, null);
            setters.set(i, null);
        }
    }
    if (renderable != null) {
        final VertexAttributes attrs = renderable.meshPart.mesh.getVertexAttributes();
        final int c = attrs.size();
        for (int i = 0; i < c; i++) {
            final VertexAttribute attr = attrs.get(i);
            final int location = program.getAttributeLocation(attr.alias);
            if (location >= 0)
                attributes.put(attr.getKey(), location);
        }
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute)

Example 3 with VertexAttributes

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

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

the class ModelCache method end.

/** Finishes creating the cache, must be called after a call to {@link #begin()}, only after this call the cache will be valid
	 * (until the next call to {@link #begin()}). Calling this method will process all renderables added using one of the add(...)
	 * methods and will combine them if possible. */
public void end() {
    if (!building)
        throw new GdxRuntimeException("Call begin() prior to calling end()");
    building = false;
    if (items.size == 0)
        return;
    sorter.sort(camera, items);
    int itemCount = items.size;
    int initCount = renderables.size;
    final Renderable first = items.get(0);
    VertexAttributes vertexAttributes = first.meshPart.mesh.getVertexAttributes();
    Material material = first.material;
    int primitiveType = first.meshPart.primitiveType;
    int offset = renderables.size;
    meshBuilder.begin(vertexAttributes);
    MeshPart part = meshBuilder.part("", primitiveType, meshPartPool.obtain());
    renderables.add(obtainRenderable(material, primitiveType));
    for (int i = 0, n = items.size; i < n; ++i) {
        final Renderable renderable = items.get(i);
        final VertexAttributes va = renderable.meshPart.mesh.getVertexAttributes();
        final Material mat = renderable.material;
        final int pt = renderable.meshPart.primitiveType;
        final boolean sameMesh = va.equals(vertexAttributes) && // comparing indices and vertices...
        renderable.meshPart.size + meshBuilder.getNumVertices() < Short.MAX_VALUE;
        final boolean samePart = sameMesh && pt == primitiveType && mat.same(material, true);
        if (!samePart) {
            if (!sameMesh) {
                final Mesh mesh = meshBuilder.end(meshPool.obtain(vertexAttributes, meshBuilder.getNumVertices(), meshBuilder.getNumIndices()));
                while (offset < renderables.size) renderables.get(offset++).meshPart.mesh = mesh;
                meshBuilder.begin(vertexAttributes = va);
            }
            final MeshPart newPart = meshBuilder.part("", pt, meshPartPool.obtain());
            final Renderable previous = renderables.get(renderables.size - 1);
            previous.meshPart.offset = part.offset;
            previous.meshPart.size = part.size;
            part = newPart;
            renderables.add(obtainRenderable(material = mat, primitiveType = pt));
        }
        meshBuilder.setVertexTransform(renderable.worldTransform);
        meshBuilder.addMesh(renderable.meshPart.mesh, renderable.meshPart.offset, renderable.meshPart.size);
    }
    final Mesh mesh = meshBuilder.end(meshPool.obtain(vertexAttributes, meshBuilder.getNumVertices(), meshBuilder.getNumIndices()));
    while (offset < renderables.size) renderables.get(offset++).meshPart.mesh = mesh;
    final Renderable previous = renderables.get(renderables.size - 1);
    previous.meshPart.offset = part.offset;
    previous.meshPart.size = part.size;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes) Mesh(com.badlogic.gdx.graphics.Mesh) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart)

Example 5 with VertexAttributes

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

the class TextureArrayTest method create.

@Override
public void create() {
    GL30Profiler.enable();
    ShaderProgram.prependVertexCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_EXT_texture_array : enable\n" : "#version 300 es\n";
    ShaderProgram.prependFragmentCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_EXT_texture_array : enable\n" : "#version 300 es\n";
    String[] texPaths = new String[] { "data/g3d/materials/Searing Gorge.jpg", "data/g3d/materials/Lava Cracks.jpg", "data/g3d/materials/Deep Fire.jpg" };
    camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.set(8, 10f, 20f);
    camera.lookAt(10, 0, 10);
    camera.up.set(0, 1, 0);
    camera.update();
    cameraController = new FirstPersonCameraController(camera);
    Gdx.input.setInputProcessor(cameraController);
    textureArray = new TextureArray(texPaths);
    textureArray.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
    shaderProgram = new ShaderProgram(Gdx.files.internal("data/shaders/texturearray.vert"), Gdx.files.internal("data/shaders/texturearray.frag"));
    System.out.println(shaderProgram.getLog());
    int vertexStride = 6;
    int vertexCount = 100 * 100;
    terrain = new Mesh(false, vertexCount * 6, 0, new VertexAttributes(VertexAttribute.Position(), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 3, ShaderProgram.TEXCOORD_ATTRIBUTE + 0)));
    Pixmap data = new Pixmap(Gdx.files.internal("data/g3d/heightmap.png"));
    float[] vertices = new float[vertexCount * vertexStride * 6];
    int idx = 0;
    for (int i = 0; i < 100 - 1; i++) {
        for (int j = 0; j < 100 - 1; j++) {
            idx = addVertex(i, j, vertices, data, idx);
            idx = addVertex(i, j + 1, vertices, data, idx);
            idx = addVertex(i + 1, j, vertices, data, idx);
            idx = addVertex(i, j + 1, vertices, data, idx);
            idx = addVertex(i + 1, j + 1, vertices, data, idx);
            idx = addVertex(i + 1, j, vertices, data, idx);
        }
    }
    terrain.setVertices(vertices);
    data.dispose();
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) TextureArray(com.badlogic.gdx.graphics.TextureArray) Mesh(com.badlogic.gdx.graphics.Mesh) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) FirstPersonCameraController(com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Aggregations

VertexAttributes (com.badlogic.gdx.graphics.VertexAttributes)5 Mesh (com.badlogic.gdx.graphics.Mesh)3 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)2 MeshPart (com.badlogic.gdx.graphics.g3d.model.MeshPart)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 TextureArray (com.badlogic.gdx.graphics.TextureArray)1 ModelMesh (com.badlogic.gdx.graphics.g3d.model.data.ModelMesh)1 ModelMeshPart (com.badlogic.gdx.graphics.g3d.model.data.ModelMeshPart)1 FirstPersonCameraController (com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController)1 ShaderProgram (com.badlogic.gdx.graphics.glutils.ShaderProgram)1