Search in sources :

Example 31 with VertexAttribute

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

the class VertexBufferObjectWithVAO method bindAttributes.

private void bindAttributes(ShaderProgram shader, int[] locations) {
    boolean stillValid = this.cachedLocations.size != 0;
    final int numAttributes = attributes.size();
    if (stillValid) {
        if (locations == null) {
            for (int i = 0; stillValid && i < numAttributes; i++) {
                VertexAttribute attribute = attributes.get(i);
                int location = shader.getAttributeLocation(attribute.alias);
                stillValid = location == this.cachedLocations.get(i);
            }
        } else {
            stillValid = locations.length == this.cachedLocations.size;
            for (int i = 0; stillValid && i < numAttributes; i++) {
                stillValid = locations[i] == this.cachedLocations.get(i);
            }
        }
    }
    if (!stillValid) {
        Gdx.gl.glBindBuffer(GL20.GL_ARRAY_BUFFER, bufferHandle);
        unbindAttributes(shader);
        this.cachedLocations.clear();
        for (int i = 0; i < numAttributes; i++) {
            VertexAttribute attribute = attributes.get(i);
            if (locations == null) {
                this.cachedLocations.add(shader.getAttributeLocation(attribute.alias));
            } else {
                this.cachedLocations.add(locations[i]);
            }
            int location = this.cachedLocations.get(i);
            if (location < 0) {
                continue;
            }
            shader.enableVertexAttribute(location);
            shader.setVertexAttribute(location, attribute.numComponents, attribute.type, attribute.normalized, attributes.vertexSize, attribute.offset);
        }
    }
}
Also used : VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute)

Example 32 with VertexAttribute

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

the class VertexBufferObject method bind.

@Override
public void bind(ShaderProgram shader, int[] locations) {
    final GL20 gl = Gdx.gl20;
    gl.glBindBuffer(GL20.GL_ARRAY_BUFFER, bufferHandle);
    if (isDirty) {
        byteBuffer.limit(buffer.limit() * 4);
        gl.glBufferData(GL20.GL_ARRAY_BUFFER, byteBuffer.limit(), byteBuffer, usage);
        isDirty = false;
    }
    final int numAttributes = attributes.size();
    if (locations == null) {
        for (int i = 0; i < numAttributes; i++) {
            final VertexAttribute attribute = attributes.get(i);
            final int location = shader.getAttributeLocation(attribute.alias);
            if (location < 0)
                continue;
            shader.enableVertexAttribute(location);
            shader.setVertexAttribute(location, attribute.numComponents, attribute.type, attribute.normalized, attributes.vertexSize, attribute.offset);
        }
    } else {
        for (int i = 0; i < numAttributes; i++) {
            final VertexAttribute attribute = attributes.get(i);
            final int location = locations[i];
            if (location < 0)
                continue;
            shader.enableVertexAttribute(location);
            shader.setVertexAttribute(location, attribute.numComponents, attribute.type, attribute.normalized, attributes.vertexSize, attribute.offset);
        }
    }
    isBound = true;
}
Also used : GL20(com.badlogic.gdx.graphics.GL20) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute)

Example 33 with VertexAttribute

use of com.badlogic.gdx.graphics.VertexAttribute in project nhglib by VoidZombie.

the class ShaderUtils method createPrefix.

public static String createPrefix(Renderable renderable, boolean skinned) {
    String prefix = "";
    final int n = renderable.meshPart.mesh.getVertexAttributes().size();
    for (int i = 0; i < n; i++) {
        final VertexAttribute attr = renderable.meshPart.mesh.getVertexAttributes().get(i);
        if (attr.usage == VertexAttributes.Usage.BoneWeight) {
            prefix += "#define boneWeight" + attr.unit + "Flag\n";
        }
    }
    if (skinned) {
        prefix += "#define skinningFlag\n";
    }
    Environment environment = renderable.environment;
    // Ambient lighting
    ColorAttribute ambientLightAttribute = (ColorAttribute) environment.get(ColorAttribute.AmbientLight);
    if (ambientLightAttribute != null) {
        prefix += "#define ambientLighting\n";
    }
    // Directional lighting
    DirectionalLightsAttribute directionalLightsAttribute = (DirectionalLightsAttribute) environment.get(DirectionalLightsAttribute.Type);
    if (directionalLightsAttribute != null) {
        Array<DirectionalLight> directionalLights = directionalLightsAttribute.lights;
        if (directionalLights.size > 0) {
            prefix += "#define numDirectionalLights " + directionalLights.size + "\n";
        }
    }
    // Point lighting
    PointLightsAttribute pointLightsAttribute = (PointLightsAttribute) environment.get(PointLightsAttribute.Type);
    if (pointLightsAttribute != null) {
        Array<PointLight> pointLights = pointLightsAttribute.lights;
        if (pointLights.size > 0) {
            prefix += "#define numPointLights " + pointLights.size + "\n";
        }
    }
    // Spot lighting
    SpotLightsAttribute spotLightsAttribute = (SpotLightsAttribute) environment.get(SpotLightsAttribute.Type);
    if (spotLightsAttribute != null) {
        Array<SpotLight> spotLights = spotLightsAttribute.lights;
        if (spotLights.size > 0) {
            prefix += "#define numSpotLights " + spotLights.size + "\n";
        }
    }
    return prefix;
}
Also used : SpotLight(com.badlogic.gdx.graphics.g3d.environment.SpotLight) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) Environment(com.badlogic.gdx.graphics.g3d.Environment) PointLight(com.badlogic.gdx.graphics.g3d.environment.PointLight)

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