Search in sources :

Example 1 with PointLight

use of com.badlogic.gdx.graphics.g3d.environment.PointLight in project libgdx by libgdx.

the class Benchmark3DTest method randomizeLights.

protected void randomizeLights() {
    int pointLights = MathUtils.random(5);
    int directionalLights = MathUtils.random(5);
    DefaultShader.Config config = new Config();
    config.numDirectionalLights = directionalLights;
    config.numPointLights = pointLights;
    config.numSpotLights = 0;
    modelBatch.dispose();
    modelBatch = new ModelBatch(new DefaultShaderProvider(config));
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
    for (int i = 0; i < pointLights; i++) {
        environment.add(new PointLight().set(randomColor(), randomPosition(), MathUtils.random(10f)));
    }
    for (int i = 0; i < directionalLights; i++) {
        environment.add(new DirectionalLight().set(randomColor(), randomPosition()));
    }
}
Also used : DefaultShaderProvider(com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider) Config(com.badlogic.gdx.graphics.g3d.shaders.DefaultShader.Config) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) Environment(com.badlogic.gdx.graphics.g3d.Environment) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute) PointLight(com.badlogic.gdx.graphics.g3d.environment.PointLight) Config(com.badlogic.gdx.graphics.g3d.shaders.DefaultShader.Config) DefaultShader(com.badlogic.gdx.graphics.g3d.shaders.DefaultShader)

Example 2 with PointLight

use of com.badlogic.gdx.graphics.g3d.environment.PointLight in project libgdx by libgdx.

the class LightsTest method create.

@Override
public void create() {
    super.create();
    environment.clear();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.2f, 0.2f, 1.0f));
    environment.add(dirLight = new DirectionalLight().set(0.8f, 0.2f, 0.2f, -1f, -2f, -0.5f));
    environment.add(pointLight = new PointLight().set(0.2f, 0.8f, 0.2f, 0f, 0f, 0f, 100f));
    ModelBuilder mb = new ModelBuilder();
    lightModel = mb.createSphere(1, 1, 1, 10, 10, new Material(ColorAttribute.createDiffuse(1, 1, 1, 1)), Usage.Position);
    lightModel.nodes.get(0).parts.get(0).setRenderable(pLight = new Renderable());
}
Also used : ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) Renderable(com.badlogic.gdx.graphics.g3d.Renderable) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) Material(com.badlogic.gdx.graphics.g3d.Material) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute) PointLight(com.badlogic.gdx.graphics.g3d.environment.PointLight)

Example 3 with PointLight

use of com.badlogic.gdx.graphics.g3d.environment.PointLight in project bdx by GoranM.

the class Light method updateLight.

public void updateLight() {
    if (lightData != null) {
        if (type.equals(Type.POINT)) {
            PointLight p = (PointLight) lightData;
            p.set(color.r, color.g, color.b, position().x, position().y, position().z, energy);
        } else if (type.equals(Type.SUN)) {
            DirectionalLight d = (DirectionalLight) lightData;
            Vector3f dir = axis(2).negated();
            d.set(color.r, color.g, color.b, dir.x, dir.y, dir.z);
        } else if (type.equals(Type.SPOT)) {
            SpotLight s = (SpotLight) lightData;
            Vector3f down = axis(2).negated();
            s.set(color.r, color.g, color.b, position().x, position().y, position().z, down.x, down.y, down.z, energy, spotSize, exponent);
        }
    }
}
Also used : DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) Vector3f(javax.vecmath.Vector3f) PointLight(com.badlogic.gdx.graphics.g3d.environment.PointLight) SpotLight(com.badlogic.gdx.graphics.g3d.environment.SpotLight)

Example 4 with PointLight

use of com.badlogic.gdx.graphics.g3d.environment.PointLight in project libgdx by libgdx.

the class DefaultShader method bindLights.

protected void bindLights(final Renderable renderable, final Attributes attributes) {
    final Environment lights = renderable.environment;
    final DirectionalLightsAttribute dla = attributes.get(DirectionalLightsAttribute.class, DirectionalLightsAttribute.Type);
    final Array<DirectionalLight> dirs = dla == null ? null : dla.lights;
    final PointLightsAttribute pla = attributes.get(PointLightsAttribute.class, PointLightsAttribute.Type);
    final Array<PointLight> points = pla == null ? null : pla.lights;
    final SpotLightsAttribute sla = attributes.get(SpotLightsAttribute.class, SpotLightsAttribute.Type);
    final Array<SpotLight> spots = sla == null ? null : sla.lights;
    if (dirLightsLoc >= 0) {
        for (int i = 0; i < directionalLights.length; i++) {
            if (dirs == null || i >= dirs.size) {
                if (lightsSet && directionalLights[i].color.r == 0f && directionalLights[i].color.g == 0f && directionalLights[i].color.b == 0f)
                    continue;
                directionalLights[i].color.set(0, 0, 0, 1);
            } else if (lightsSet && directionalLights[i].equals(dirs.get(i)))
                continue;
            else
                directionalLights[i].set(dirs.get(i));
            int idx = dirLightsLoc + i * dirLightsSize;
            program.setUniformf(idx + dirLightsColorOffset, directionalLights[i].color.r, directionalLights[i].color.g, directionalLights[i].color.b);
            program.setUniformf(idx + dirLightsDirectionOffset, directionalLights[i].direction.x, directionalLights[i].direction.y, directionalLights[i].direction.z);
            if (dirLightsSize <= 0)
                break;
        }
    }
    if (pointLightsLoc >= 0) {
        for (int i = 0; i < pointLights.length; i++) {
            if (points == null || i >= points.size) {
                if (lightsSet && pointLights[i].intensity == 0f)
                    continue;
                pointLights[i].intensity = 0f;
            } else if (lightsSet && pointLights[i].equals(points.get(i)))
                continue;
            else
                pointLights[i].set(points.get(i));
            int idx = pointLightsLoc + i * pointLightsSize;
            program.setUniformf(idx + pointLightsColorOffset, pointLights[i].color.r * pointLights[i].intensity, pointLights[i].color.g * pointLights[i].intensity, pointLights[i].color.b * pointLights[i].intensity);
            program.setUniformf(idx + pointLightsPositionOffset, pointLights[i].position.x, pointLights[i].position.y, pointLights[i].position.z);
            if (pointLightsIntensityOffset >= 0)
                program.setUniformf(idx + pointLightsIntensityOffset, pointLights[i].intensity);
            if (pointLightsSize <= 0)
                break;
        }
    }
    if (spotLightsLoc >= 0) {
        for (int i = 0; i < spotLights.length; i++) {
            if (spots == null || i >= spots.size) {
                if (lightsSet && spotLights[i].intensity == 0f)
                    continue;
                spotLights[i].intensity = 0f;
            } else if (lightsSet && spotLights[i].equals(spots.get(i)))
                continue;
            else
                spotLights[i].set(spots.get(i));
            int idx = spotLightsLoc + i * spotLightsSize;
            program.setUniformf(idx + spotLightsColorOffset, spotLights[i].color.r * spotLights[i].intensity, spotLights[i].color.g * spotLights[i].intensity, spotLights[i].color.b * spotLights[i].intensity);
            program.setUniformf(idx + spotLightsPositionOffset, spotLights[i].position);
            program.setUniformf(idx + spotLightsDirectionOffset, spotLights[i].direction);
            program.setUniformf(idx + spotLightsCutoffAngleOffset, spotLights[i].cutoffAngle);
            program.setUniformf(idx + spotLightsExponentOffset, spotLights[i].exponent);
            if (spotLightsIntensityOffset >= 0)
                program.setUniformf(idx + spotLightsIntensityOffset, spotLights[i].intensity);
            if (spotLightsSize <= 0)
                break;
        }
    }
    if (attributes.has(ColorAttribute.Fog)) {
        set(u_fogColor, ((ColorAttribute) attributes.get(ColorAttribute.Fog)).color);
    }
    if (lights != null && lights.shadowMap != null) {
        set(u_shadowMapProjViewTrans, lights.shadowMap.getProjViewTrans());
        set(u_shadowTexture, lights.shadowMap.getDepthMap());
        set(u_shadowPCFOffset, 1.f / (2f * lights.shadowMap.getDepthMap().texture.getWidth()));
    }
    lightsSet = true;
}
Also used : DirectionalLightsAttribute(com.badlogic.gdx.graphics.g3d.attributes.DirectionalLightsAttribute) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) Environment(com.badlogic.gdx.graphics.g3d.Environment) PointLightsAttribute(com.badlogic.gdx.graphics.g3d.attributes.PointLightsAttribute) SpotLightsAttribute(com.badlogic.gdx.graphics.g3d.attributes.SpotLightsAttribute) PointLight(com.badlogic.gdx.graphics.g3d.environment.PointLight) SpotLight(com.badlogic.gdx.graphics.g3d.environment.SpotLight)

Example 5 with PointLight

use of com.badlogic.gdx.graphics.g3d.environment.PointLight in project libgdx by libgdx.

the class DefaultShader method begin.

@Override
public void begin(final Camera camera, final RenderContext context) {
    super.begin(camera, context);
    for (final DirectionalLight dirLight : directionalLights) dirLight.set(0, 0, 0, 0, -1, 0);
    for (final PointLight pointLight : pointLights) pointLight.set(0, 0, 0, 0, 0, 0, 0);
    for (final SpotLight spotLight : spotLights) spotLight.set(0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0);
    lightsSet = false;
    if (has(u_time))
        set(u_time, time += Gdx.graphics.getDeltaTime());
}
Also used : DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) PointLight(com.badlogic.gdx.graphics.g3d.environment.PointLight) SpotLight(com.badlogic.gdx.graphics.g3d.environment.SpotLight)

Aggregations

DirectionalLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalLight)7 PointLight (com.badlogic.gdx.graphics.g3d.environment.PointLight)7 SpotLight (com.badlogic.gdx.graphics.g3d.environment.SpotLight)5 Environment (com.badlogic.gdx.graphics.g3d.Environment)3 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)2 DirectionalLightsAttribute (com.badlogic.gdx.graphics.g3d.attributes.DirectionalLightsAttribute)2 PointLightsAttribute (com.badlogic.gdx.graphics.g3d.attributes.PointLightsAttribute)2 SpotLightsAttribute (com.badlogic.gdx.graphics.g3d.attributes.SpotLightsAttribute)2 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)1 Material (com.badlogic.gdx.graphics.g3d.Material)1 ModelBatch (com.badlogic.gdx.graphics.g3d.ModelBatch)1 Renderable (com.badlogic.gdx.graphics.g3d.Renderable)1 DefaultShader (com.badlogic.gdx.graphics.g3d.shaders.DefaultShader)1 Config (com.badlogic.gdx.graphics.g3d.shaders.DefaultShader.Config)1 DefaultShaderProvider (com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider)1 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)1 Vector3f (javax.vecmath.Vector3f)1