1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests.g3d; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.VertexAttributes.Usage; 21 import com.badlogic.gdx.graphics.g3d.Material; 22 import com.badlogic.gdx.graphics.g3d.Model; 23 import com.badlogic.gdx.graphics.g3d.ModelBatch; 24 import com.badlogic.gdx.graphics.g3d.ModelInstance; 25 import com.badlogic.gdx.graphics.g3d.Renderable; 26 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; 27 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; 28 import com.badlogic.gdx.graphics.g3d.environment.PointLight; 29 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; 30 import com.badlogic.gdx.math.Vector3; 31 import com.badlogic.gdx.math.collision.BoundingBox; 32 import com.badlogic.gdx.utils.Array; 33 34 public class LightsTest extends ModelTest { 35 DirectionalLight dirLight; 36 PointLight pointLight; 37 Model lightModel; 38 Renderable pLight; 39 Vector3 center = new Vector3(), transformedCenter = new Vector3(), tmpV = new Vector3(); 40 float radius = 1f; 41 42 @Override create()43 public void create () { 44 super.create(); 45 environment.clear(); 46 environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.2f, 0.2f, 1.0f)); 47 environment.add(dirLight = new DirectionalLight().set(0.8f, 0.2f, 0.2f, -1f, -2f, -0.5f)); 48 environment.add(pointLight = new PointLight().set(0.2f, 0.8f, 0.2f, 0f, 0f, 0f, 100f)); 49 50 ModelBuilder mb = new ModelBuilder(); 51 lightModel = mb.createSphere(1, 1, 1, 10, 10, new Material(ColorAttribute.createDiffuse(1, 1, 1, 1)), Usage.Position); 52 lightModel.nodes.get(0).parts.get(0).setRenderable(pLight = new Renderable()); 53 } 54 55 @Override onLoaded()56 protected void onLoaded () { 57 super.onLoaded(); 58 BoundingBox bounds = instances.get(0).calculateBoundingBox(new BoundingBox()); 59 bounds.getCenter(center); 60 radius = bounds.getDimensions(tmpV).len() * .5f; 61 pointLight.position.set(0, radius, 0).add(transformedCenter.set(center).mul(transform)); 62 pointLight.intensity = radius * radius; 63 ((ColorAttribute)pLight.material.get(ColorAttribute.Diffuse)).color.set(pointLight.color); 64 final float s = 0.2f * radius; 65 pLight.worldTransform.setToScaling(s, s, s); 66 } 67 68 @Override render(ModelBatch batch, Array<ModelInstance> instances)69 protected void render (ModelBatch batch, Array<ModelInstance> instances) { 70 final float delta = Gdx.graphics.getDeltaTime(); 71 dirLight.direction.rotate(Vector3.X, delta * 45f); 72 dirLight.direction.rotate(Vector3.Y, delta * 25f); 73 dirLight.direction.rotate(Vector3.Z, delta * 33f); 74 75 pointLight.position.sub(transformedCenter); 76 pointLight.position.rotate(Vector3.X, delta * 50f); 77 pointLight.position.rotate(Vector3.Y, delta * 13f); 78 pointLight.position.rotate(Vector3.Z, delta * 3f); 79 pointLight.position.add(transformedCenter.set(center).mul(transform)); 80 81 pLight.worldTransform.setTranslation(pointLight.position); 82 batch.render(pLight); 83 84 super.render(batch, instances); 85 } 86 87 @Override dispose()88 public void dispose () { 89 lightModel.dispose(); 90 super.dispose(); 91 } 92 } 93