Search in sources :

Example 1 with VertexAttribute

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

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

the class G3dModelLoader method parseAttributes.

private VertexAttribute[] parseAttributes(JsonValue attributes) {
    Array<VertexAttribute> vertexAttributes = new Array<VertexAttribute>();
    int unit = 0;
    int blendWeightCount = 0;
    for (JsonValue value = attributes.child; value != null; value = value.next) {
        String attribute = value.asString();
        String attr = (String) attribute;
        if (attr.equals("POSITION")) {
            vertexAttributes.add(VertexAttribute.Position());
        } else if (attr.equals("NORMAL")) {
            vertexAttributes.add(VertexAttribute.Normal());
        } else if (attr.equals("COLOR")) {
            vertexAttributes.add(VertexAttribute.ColorUnpacked());
        } else if (attr.equals("COLORPACKED")) {
            vertexAttributes.add(VertexAttribute.ColorPacked());
        } else if (attr.equals("TANGENT")) {
            vertexAttributes.add(VertexAttribute.Tangent());
        } else if (attr.equals("BINORMAL")) {
            vertexAttributes.add(VertexAttribute.Binormal());
        } else if (attr.startsWith("TEXCOORD")) {
            vertexAttributes.add(VertexAttribute.TexCoords(unit++));
        } else if (attr.startsWith("BLENDWEIGHT")) {
            vertexAttributes.add(VertexAttribute.BoneWeight(blendWeightCount++));
        } else {
            throw new GdxRuntimeException("Unknown vertex attribute '" + attr + "', should be one of position, normal, uv, tangent or binormal");
        }
    }
    return vertexAttributes.toArray(VertexAttribute.class);
}
Also used : Array(com.badlogic.gdx.utils.Array) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 3 with VertexAttribute

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

the class ImmediateModeRenderer20 method buildVertexAttributes.

private VertexAttribute[] buildVertexAttributes(boolean hasNormals, boolean hasColor, int numTexCoords) {
    Array<VertexAttribute> attribs = new Array<VertexAttribute>();
    attribs.add(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE));
    if (hasNormals)
        attribs.add(new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE));
    if (hasColor)
        attribs.add(new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE));
    for (int i = 0; i < numTexCoords; i++) {
        attribs.add(new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + i));
    }
    VertexAttribute[] array = new VertexAttribute[attribs.size];
    for (int i = 0; i < attribs.size; i++) array[i] = attribs.get(i);
    return array;
}
Also used : Array(com.badlogic.gdx.utils.Array) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute)

Example 4 with VertexAttribute

use of com.badlogic.gdx.graphics.VertexAttribute 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)

Example 5 with VertexAttribute

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

the class MipMapTest method create.

@Override
public void create() {
    camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.set(0, 1.5f, 1.5f);
    camera.lookAt(0, 0, 0);
    camera.update();
    controller = new PerspectiveCamController(camera);
    mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
    mesh.setVertices(new float[] { -1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, -1, 1, 0, -1, 0, -1, 0, 0 });
    mesh.setIndices(new short[] { 0, 1, 2, 3 });
    shader = new ShaderProgram(Gdx.files.internal("data/shaders/flattex-vert.glsl").readString(), Gdx.files.internal("data/shaders/flattex-frag.glsl").readString());
    if (!shader.isCompiled())
        throw new GdxRuntimeException("shader error: " + shader.getLog());
    textureHW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true);
    MipMapGenerator.setUseHardwareMipMap(false);
    textureSW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true);
    currTexture = textureHW;
    createUI();
    multiplexer = new InputMultiplexer();
    Gdx.input.setInputProcessor(multiplexer);
    multiplexer.addProcessor(ui);
    multiplexer.addProcessor(controller);
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) PerspectiveCamController(com.badlogic.gdx.tests.utils.PerspectiveCamController) InputMultiplexer(com.badlogic.gdx.InputMultiplexer) ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) Mesh(com.badlogic.gdx.graphics.Mesh) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) Texture(com.badlogic.gdx.graphics.Texture)

Aggregations

VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)33 Mesh (com.badlogic.gdx.graphics.Mesh)15 ShaderProgram (com.badlogic.gdx.graphics.glutils.ShaderProgram)9 Texture (com.badlogic.gdx.graphics.Texture)8 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)6 Array (com.badlogic.gdx.utils.Array)4 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)3 Vector3 (com.badlogic.gdx.math.Vector3)3 GL20 (com.badlogic.gdx.graphics.GL20)2 VertexAttributes (com.badlogic.gdx.graphics.VertexAttributes)2 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 Attributes (com.badlogic.gdx.graphics.g3d.Attributes)2 IndexBufferObject (com.badlogic.gdx.graphics.glutils.IndexBufferObject)2 VertexBufferObject (com.badlogic.gdx.graphics.glutils.VertexBufferObject)2 PerspectiveCamController (com.badlogic.gdx.tests.utils.PerspectiveCamController)2 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 TextureArray (com.badlogic.gdx.graphics.TextureArray)1 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 Environment (com.badlogic.gdx.graphics.g3d.Environment)1