Example usage for com.badlogic.gdx.graphics.g3d.materials Material getNumberOfAttributes

List of usage examples for com.badlogic.gdx.graphics.g3d.materials Material getNumberOfAttributes

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g3d.materials Material getNumberOfAttributes.

Prototype

public int getNumberOfAttributes() 

Source Link

Usage

From source file:com.badlogic.gdx.graphics.g3d.test.PrototypeRendererGL20.java

License:Apache License

private void flush() {

    // opaque is sorted front to back
    // transparent is sorted back to front
    drawableManager.drawables.sort(opaqueSorter);
    for (int i = drawableManager.drawables.size; --i >= 0;) {

        final Drawable drawable = drawableManager.drawables.get(i);

        final Vector3 center = drawable.sortCenter;
        lightManager.calculateLights(center.x, center.y, center.z);

        final Matrix4 modelMatrix = drawable.transform;
        normalMatrix.set(modelMatrix);//from  ww  w  .j av  a2s.c  om

        if (drawable.isAnimated)
            ((AnimatedModel) (drawable.model)).setAnimation(drawable.animation, drawable.animationTime,
                    drawable.isLooping);

        final SubMesh subMeshes[] = drawable.model.getSubMeshes();

        boolean matrixChanged = true;
        for (int j = 0; j < subMeshes.length; j++) {

            final SubMesh subMesh = subMeshes[j];
            final Material material = drawable.materials.get(j);

            // bind new shader if material can't use old one
            final boolean shaderChanged = bindShader(material);

            if (shaderChanged || matrixChanged) {
                currentShader.setUniformMatrix("u_normalMatrix", normalMatrix, false);
                currentShader.setUniformMatrix("u_modelMatrix", modelMatrix, false);
                matrixChanged = false;
            }

            for (int k = 0, len = material.getNumberOfAttributes(); k < len; k++) {
                final MaterialAttribute atrib = material.getAttribute(k);

                // special case for textures. really important to batch these
                if (atrib instanceof TextureAttribute) {
                    final TextureAttribute texAtrib = (TextureAttribute) atrib;
                    if (!texAtrib.texturePortionEquals(lastTexture[texAtrib.unit])) {
                        lastTexture[texAtrib.unit] = texAtrib;
                        texAtrib.bind(currentShader);
                    } else {
                        // need to be done, shader textureAtribute name could be changed.
                        currentShader.setUniformi(texAtrib.name, texAtrib.unit);
                    }
                } else if (atrib instanceof GpuSkinningAttribute) {
                    GpuSkinningAttribute gpuAttrib = (GpuSkinningAttribute) atrib;
                    gpuAttrib.setModelMatrix(modelMatrix);
                    gpuAttrib.bind(currentShader);
                } else {
                    atrib.bind(currentShader);
                }
            }

            // finally render current submesh
            subMesh.getMesh().render(currentShader, subMesh.primitiveType);
        }
    }

    // if transparent queue is not empty enable blending(this force gpu to
    // flush and there is some time to sort)
    if (drawableManager.drawablesBlended.size > 0)
        renderBlended();

    // cleaning

    if (currentShader != null) {
        currentShader.end();
        currentShader = null;
    }
    for (int i = 0, len = TextureAttribute.MAX_TEXTURE_UNITS; i < len; i++)
        lastTexture[i] = null;
    // clear all queus

    drawing = false;

    drawableManager.clear();
}

From source file:com.badlogic.gdx.graphics.g3d.test.PrototypeRendererGL20.java

License:Apache License

private void renderBlended() {

    Gdx.gl.glEnable(GL10.GL_BLEND);//from w  w  w.  j a  va  2 s  .c o  m
    final Array<Drawable> transparentDrawables = drawableManager.drawablesBlended;
    transparentDrawables.sort();

    // find N nearest lights per model
    // draw all models from opaque queue

    int lastSrcBlend = -1;
    int lastDstBlend = -1;

    for (int i = 0, size = transparentDrawables.size; i < size; i++) {

        final Drawable drawable = transparentDrawables.get(i);

        final Vector3 center = drawable.sortCenter;
        lightManager.calculateLights(center.x, center.y, center.z);

        final Matrix4 modelMatrix = drawable.transform;
        normalMatrix.set(modelMatrix);

        if (drawable.isAnimated)
            ((AnimatedModel) (drawable.model)).setAnimation(drawable.animation, drawable.animationTime,
                    drawable.isLooping);

        final SubMesh subMeshes[] = drawable.model.getSubMeshes();

        boolean matrixChanged = true;
        for (int j = 0; j < subMeshes.length; j++) {

            final SubMesh subMesh = subMeshes[j];
            final Material material = drawable.materials.get(j);

            // bind new shader if material can't use old one
            final boolean shaderChanged = bindShader(material);

            if (shaderChanged || matrixChanged) {
                currentShader.setUniformMatrix("u_normalMatrix", normalMatrix, false);
                currentShader.setUniformMatrix("u_modelMatrix", modelMatrix, false);
                matrixChanged = false;
            }

            for (int k = 0, len = material.getNumberOfAttributes(); k < len; k++) {
                final MaterialAttribute atrib = material.getAttribute(k);

                // yet another instanceof. TODO is there any better way to do this? maybe stuffing this to material
                if (atrib instanceof BlendingAttribute) {
                    final BlendingAttribute blending = (BlendingAttribute) atrib;
                    if (blending.blendSrcFunc != lastSrcBlend || blending.blendDstFunc != lastDstBlend) {
                        atrib.bind(currentShader);
                        lastSrcBlend = blending.blendSrcFunc;
                        lastDstBlend = blending.blendDstFunc;
                    }
                } else if (atrib instanceof TextureAttribute) {
                    // special case for textures. really important to batch these
                    final TextureAttribute texAtrib = (TextureAttribute) atrib;
                    if (!texAtrib.texturePortionEquals(lastTexture[texAtrib.unit])) {
                        lastTexture[texAtrib.unit] = texAtrib;
                        texAtrib.bind(currentShader);
                    } else {
                        // need to be done, shader textureAtribute name could be changed.
                        currentShader.setUniformi(texAtrib.name, texAtrib.unit);
                    }
                } else if (atrib instanceof GpuSkinningAttribute) {
                    final GpuSkinningAttribute gpuAtrib = (GpuSkinningAttribute) atrib;
                    gpuAtrib.setModelMatrix(modelMatrix);
                    gpuAtrib.bind(currentShader);
                } else {
                    atrib.bind(currentShader);
                }
            }
            // finally render current submesh
            subMesh.getMesh().render(currentShader, subMesh.primitiveType);
        }
    }
    Gdx.gl.glDisable(GL10.GL_BLEND);
}

From source file:de.redlion.badminton.render.ShaderFactory.java

License:Apache License

static public ShaderProgram createShader(Material material, LightManager lights) {

    final StringBuilder flags = new StringBuilder(128);
    flags.append(lightsNum);//from  w w  w .  jav a  2  s  . com
    flags.append(lights.maxLightsPerModel);
    flags.append("\n");

    if (material != null) {
        for (int i = 0; i < material.getNumberOfAttributes(); i++) {
            flags.append(define);
            flags.append(material.getAttribute(i).getShaderFlag());
            flags.append("\n");
        }
    }
    // TODO FIX light chose method
    String fileName;
    if (lights.quality == LightQuality.FRAGMENT)
        fileName = "light";
    else {
        fileName = "vertexpath";
    }
    final String vertexShader = Gdx.files.internal("data/shaders/" + fileName + ".vertex.glsl").readString();
    final String fragmentShader = Gdx.files.internal("data/shaders/" + fileName + ".fragment.glsl")
            .readString();

    ShaderProgram.pedantic = false;
    final ShaderProgram shader = new ShaderProgram(flags + vertexShader, flags + fragmentShader);
    if (!shader.isCompiled()) {
        System.out.println("error" + shader.getLog());
        Gdx.app.exit();
    }
    return shader;
}