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.ApplicationListener; 20 import com.badlogic.gdx.Gdx; 21 import com.badlogic.gdx.InputMultiplexer; 22 import com.badlogic.gdx.graphics.Color; 23 import com.badlogic.gdx.graphics.GL20; 24 import com.badlogic.gdx.graphics.PerspectiveCamera; 25 import com.badlogic.gdx.graphics.VertexAttributes.Usage; 26 import com.badlogic.gdx.graphics.g3d.Environment; 27 import com.badlogic.gdx.graphics.g3d.Material; 28 import com.badlogic.gdx.graphics.g3d.Model; 29 import com.badlogic.gdx.graphics.g3d.ModelBatch; 30 import com.badlogic.gdx.graphics.g3d.ModelInstance; 31 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; 32 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; 33 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController; 34 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; 35 import com.badlogic.gdx.tests.utils.GdxTest; 36 37 public class FogTest extends GdxTest implements ApplicationListener { 38 public PerspectiveCamera cam; 39 public CameraInputController inputController; 40 public ModelBatch modelBatch; 41 public Model model; 42 public ModelInstance instance; 43 public Environment environment; 44 45 @Override create()46 public void create () { 47 modelBatch = new ModelBatch(); 48 environment = new Environment(); 49 environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f)); 50 environment.set(new ColorAttribute(ColorAttribute.Fog, 0.13f, 0.13f, 0.13f, 1f)); 51 environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); 52 53 cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 54 cam.position.set(30f, 10f, 30f); 55 cam.lookAt(0, 0, 0); 56 cam.near = 0.1f; 57 cam.far = 45f; 58 cam.update(); 59 60 ModelBuilder modelBuilder = new ModelBuilder(); 61 model = modelBuilder.createBox(5f, 5f, 5f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), Usage.Position 62 | Usage.Normal); 63 instance = new ModelInstance(model); 64 65 Gdx.input.setInputProcessor(new InputMultiplexer(this, inputController = new CameraInputController(cam))); 66 } 67 68 @Override render()69 public void render () { 70 71 animate(); 72 73 inputController.update(); 74 75 Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight()); 76 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 77 78 Gdx.gl.glClearColor(0.13f, 0.13f, 0.13f, 1); 79 80 modelBatch.begin(cam); 81 modelBatch.render(instance, environment); 82 modelBatch.end(); 83 } 84 85 float delta = 0f, dir = 1; 86 animate()87 private void animate () { 88 89 delta = Gdx.graphics.getDeltaTime(); 90 91 instance.transform.val[14] += delta * 4 * dir; 92 93 if (Math.abs(instance.transform.val[14]) > 5) { 94 dir *= -1; 95 } 96 } 97 98 @Override dispose()99 public void dispose () { 100 modelBatch.dispose(); 101 model.dispose(); 102 } 103 needsGL20()104 public boolean needsGL20 () { 105 return true; 106 } 107 resume()108 public void resume () { 109 } 110 resize(int width, int height)111 public void resize (int width, int height) { 112 } 113 pause()114 public void pause () { 115 } 116 } 117