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; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.Input; 21 import com.badlogic.gdx.InputAdapter; 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.ModelBuilder; 34 import com.badlogic.gdx.tests.utils.GdxTest; 35 import com.badlogic.gdx.utils.Array; 36 import com.badlogic.gdx.utils.Scaling; 37 import com.badlogic.gdx.utils.viewport.ExtendViewport; 38 import com.badlogic.gdx.utils.viewport.ScalingViewport; 39 import com.badlogic.gdx.utils.viewport.ScreenViewport; 40 import com.badlogic.gdx.utils.viewport.Viewport; 41 42 /** Cycles viewports while rendering with SpriteBatch. */ 43 public class ViewportTest3 extends GdxTest { 44 Array<Viewport> viewports; 45 Viewport viewport; 46 Array<String> names; 47 String name; 48 49 private PerspectiveCamera camera; 50 public Environment environment; 51 public DirectionalLight shadowLight; 52 public ModelBuilder modelBuilder; 53 public ModelBatch modelBatch; 54 public ModelInstance boxInstance; 55 create()56 public void create () { 57 modelBatch = new ModelBatch(); 58 modelBuilder = new ModelBuilder(); 59 60 environment = new Environment(); 61 environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.3f, 0.3f, 0.3f, 1.f)); 62 shadowLight = new DirectionalLight(); 63 shadowLight.set(0.8f, 0.8f, 0.8f, -0.5f, -1f, 0.7f); 64 environment.add(shadowLight); 65 66 modelBatch = new ModelBatch(); 67 68 camera = new PerspectiveCamera(); 69 camera.fieldOfView = 67; 70 camera.near = 0.1f; 71 camera.far = 300f; 72 camera.position.set(0, 0, 100); 73 camera.lookAt(0, 0, 0); 74 75 viewports = ViewportTest1.getViewports(camera); 76 viewport = viewports.first(); 77 78 names = ViewportTest1.getViewportNames(); 79 name = names.first(); 80 81 ModelBuilder modelBuilder = new ModelBuilder(); 82 Model boxModel = modelBuilder.createBox(50f, 50f, 50f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), 83 Usage.Position | Usage.Normal); 84 boxInstance = new ModelInstance(boxModel); 85 boxInstance.transform.rotate(1, 0, 0, 30); 86 boxInstance.transform.rotate(0, 1, 0, 30); 87 88 Gdx.input.setInputProcessor(new InputAdapter() { 89 public boolean keyDown (int keycode) { 90 if (keycode == Input.Keys.SPACE) { 91 int index = (viewports.indexOf(viewport, true) + 1) % viewports.size; 92 name = names.get(index); 93 viewport = viewports.get(index); 94 resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 95 } 96 return false; 97 } 98 }); 99 } 100 render()101 public void render () { 102 Gdx.gl.glClearColor(0, 0, 0, 1); 103 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 104 105 modelBatch.begin(camera); 106 modelBatch.render(boxInstance, environment); 107 modelBatch.end(); 108 } 109 resize(int width, int height)110 public void resize (int width, int height) { 111 System.out.println(name); 112 viewport.update(width, height); 113 } 114 } 115