Search in sources :

Example 11 with DirectionalLight

use of com.badlogic.gdx.graphics.g3d.environment.DirectionalLight 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 12 with DirectionalLight

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

the class Light method on.

public void on(boolean on) {
    this.on = on;
    if (type == Type.POINT) {
        PointLightsAttribute la = (PointLightsAttribute) scene.environment.get(PointLightsAttribute.Type);
        PointLight pl = (PointLight) lightData;
        if (on && !la.lights.contains(pl, true))
            la.lights.add(pl);
        else if (!on && la.lights.contains(pl, true))
            la.lights.removeValue(pl, true);
    } else if (type == Type.SUN) {
        DirectionalLightsAttribute la = (DirectionalLightsAttribute) scene.environment.get(DirectionalLightsAttribute.Type);
        DirectionalLight dl = (DirectionalLight) lightData;
        if (on && !la.lights.contains(dl, true))
            la.lights.add(dl);
        else if (!on && la.lights.contains(dl, true))
            la.lights.removeValue(dl, true);
    } else if (type == Type.SPOT) {
        SpotLightsAttribute la = (SpotLightsAttribute) scene.environment.get(SpotLightsAttribute.Type);
        SpotLight sl = (SpotLight) lightData;
        if (on && !la.lights.contains(sl, true))
            la.lights.add(sl);
        else if (!on && la.lights.contains(sl, true))
            la.lights.removeValue(sl, true);
    }
}
Also used : DirectionalLightsAttribute(com.badlogic.gdx.graphics.g3d.attributes.DirectionalLightsAttribute) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) 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 13 with DirectionalLight

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

the class BasicBulletTest method create.

@Override
public void create() {
    super.create();
    instructions = "Swipe for next test";
    lights = new Environment();
    lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.2f, 0.2f, 1.f));
    lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1f, -0.7f));
    // Set up the camera
    final float width = Gdx.graphics.getWidth();
    final float height = Gdx.graphics.getHeight();
    if (width > height)
        camera = new PerspectiveCamera(67f, 3f * width / height, 3f);
    else
        camera = new PerspectiveCamera(67f, 3f, 3f * height / width);
    camera.position.set(10f, 10f, 10f);
    camera.lookAt(0, 0, 0);
    camera.update();
    // Create the model batch
    modelBatch = new ModelBatch();
    // Create some basic models
    final Model groundModel = modelBuilder.createRect(20f, 0f, -20f, -20f, 0f, -20f, -20f, 0f, 20f, 20f, 0f, 20f, 0, 1, 0, new Material(ColorAttribute.createDiffuse(Color.BLUE), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(16f)), Usage.Position | Usage.Normal);
    models.add(groundModel);
    final Model sphereModel = modelBuilder.createSphere(1f, 1f, 1f, 10, 10, new Material(ColorAttribute.createDiffuse(Color.RED), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(64f)), Usage.Position | Usage.Normal);
    models.add(sphereModel);
    // Load the bullet library
    // Normally use: Bullet.init();
    BaseBulletTest.init();
    // Create the bullet world
    collisionConfiguration = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfiguration);
    broadphase = new btDbvtBroadphase();
    solver = new btSequentialImpulseConstraintSolver();
    collisionWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    collisionWorld.setGravity(gravity);
    // Create the shapes and body construction infos
    btCollisionShape groundShape = new btBoxShape(tempVector.set(20, 0, 20));
    shapes.add(groundShape);
    btRigidBodyConstructionInfo groundInfo = new btRigidBodyConstructionInfo(0f, null, groundShape, Vector3.Zero);
    bodyInfos.add(groundInfo);
    btCollisionShape sphereShape = new btSphereShape(0.5f);
    shapes.add(sphereShape);
    sphereShape.calculateLocalInertia(1f, tempVector);
    btRigidBodyConstructionInfo sphereInfo = new btRigidBodyConstructionInfo(1f, null, sphereShape, tempVector);
    bodyInfos.add(sphereInfo);
    // Create the ground
    ModelInstance ground = new ModelInstance(groundModel);
    instances.add(ground);
    btDefaultMotionState groundMotionState = new btDefaultMotionState();
    groundMotionState.setWorldTransform(ground.transform);
    motionStates.add(groundMotionState);
    btRigidBody groundBody = new btRigidBody(groundInfo);
    groundBody.setMotionState(groundMotionState);
    bodies.add(groundBody);
    collisionWorld.addRigidBody(groundBody);
    // Create the spheres
    for (float x = -10f; x <= 10f; x += 2f) {
        for (float y = 5f; y <= 15f; y += 2f) {
            for (float z = 0f; z <= 0f; z += 2f) {
                ModelInstance sphere = new ModelInstance(sphereModel);
                instances.add(sphere);
                sphere.transform.trn(x + 0.1f * MathUtils.random(), y + 0.1f * MathUtils.random(), z + 0.1f * MathUtils.random());
                btDefaultMotionState sphereMotionState = new btDefaultMotionState();
                sphereMotionState.setWorldTransform(sphere.transform);
                motionStates.add(sphereMotionState);
                btRigidBody sphereBody = new btRigidBody(sphereInfo);
                sphereBody.setMotionState(sphereMotionState);
                bodies.add(sphereBody);
                collisionWorld.addRigidBody(sphereBody);
            }
        }
    }
}
Also used : com.badlogic.gdx.physics.bullet.collision.btBoxShape(com.badlogic.gdx.physics.bullet.collision.btBoxShape) com.badlogic.gdx.physics.bullet.collision.btCollisionDispatcher(com.badlogic.gdx.physics.bullet.collision.btCollisionDispatcher) Material(com.badlogic.gdx.graphics.g3d.Material) com.badlogic.gdx.physics.bullet.collision.btDefaultCollisionConfiguration(com.badlogic.gdx.physics.bullet.collision.btDefaultCollisionConfiguration) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) com.badlogic.gdx.physics.bullet.linearmath.btDefaultMotionState(com.badlogic.gdx.physics.bullet.linearmath.btDefaultMotionState) com.badlogic.gdx.physics.bullet.collision.btCollisionShape(com.badlogic.gdx.physics.bullet.collision.btCollisionShape) com.badlogic.gdx.physics.bullet.dynamics.btRigidBody.btRigidBodyConstructionInfo(com.badlogic.gdx.physics.bullet.dynamics.btRigidBody.btRigidBodyConstructionInfo) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) com.badlogic.gdx.physics.bullet.dynamics.btRigidBody(com.badlogic.gdx.physics.bullet.dynamics.btRigidBody) com.badlogic.gdx.physics.bullet.collision.btSphereShape(com.badlogic.gdx.physics.bullet.collision.btSphereShape) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) Model(com.badlogic.gdx.graphics.g3d.Model) com.badlogic.gdx.physics.bullet.collision.btDbvtBroadphase(com.badlogic.gdx.physics.bullet.collision.btDbvtBroadphase) Environment(com.badlogic.gdx.graphics.g3d.Environment) com.badlogic.gdx.physics.bullet.dynamics.btDiscreteDynamicsWorld(com.badlogic.gdx.physics.bullet.dynamics.btDiscreteDynamicsWorld) com.badlogic.gdx.physics.bullet.dynamics.btSequentialImpulseConstraintSolver(com.badlogic.gdx.physics.bullet.dynamics.btSequentialImpulseConstraintSolver) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)

Example 14 with DirectionalLight

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

the class Basic3DSceneTest method create.

@Override
public void create() {
    modelBatch = new ModelBatch();
    lights = new Environment();
    lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
    lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(0f, 7f, 10f);
    cam.lookAt(0, 0, 0);
    cam.near = 0.1f;
    cam.far = 300f;
    cam.update();
    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);
    assets = new AssetManager();
    assets.load("data/g3d/invaders.g3dj", Model.class);
    loading = true;
}
Also used : CameraInputController(com.badlogic.gdx.graphics.g3d.utils.CameraInputController) AssetManager(com.badlogic.gdx.assets.AssetManager) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) Environment(com.badlogic.gdx.graphics.g3d.Environment) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)

Example 15 with DirectionalLight

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

Aggregations

DirectionalLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalLight)22 Environment (com.badlogic.gdx.graphics.g3d.Environment)17 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)16 ModelBatch (com.badlogic.gdx.graphics.g3d.ModelBatch)11 Material (com.badlogic.gdx.graphics.g3d.Material)10 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)9 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)7 PointLight (com.badlogic.gdx.graphics.g3d.environment.PointLight)7 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)7 SpotLight (com.badlogic.gdx.graphics.g3d.environment.SpotLight)5 CameraInputController (com.badlogic.gdx.graphics.g3d.utils.CameraInputController)5 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)4 Texture (com.badlogic.gdx.graphics.Texture)4 DefaultShaderProvider (com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider)4 Model (com.badlogic.gdx.graphics.g3d.Model)3 DirectionalLightsAttribute (com.badlogic.gdx.graphics.g3d.attributes.DirectionalLightsAttribute)3 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 Renderable (com.badlogic.gdx.graphics.g3d.Renderable)2 PointLightsAttribute (com.badlogic.gdx.graphics.g3d.attributes.PointLightsAttribute)2 SpotLightsAttribute (com.badlogic.gdx.graphics.g3d.attributes.SpotLightsAttribute)2