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 java.util.LinkedList; 20 21 import com.badlogic.gdx.Gdx; 22 import com.badlogic.gdx.graphics.Camera; 23 import com.badlogic.gdx.graphics.GL20; 24 import com.badlogic.gdx.graphics.OrthographicCamera; 25 import com.badlogic.gdx.graphics.Texture; 26 import com.badlogic.gdx.graphics.g2d.TextureRegion; 27 import com.badlogic.gdx.graphics.g3d.decals.CameraGroupStrategy; 28 import com.badlogic.gdx.graphics.g3d.decals.Decal; 29 import com.badlogic.gdx.graphics.g3d.decals.DecalBatch; 30 import com.badlogic.gdx.math.WindowedMean; 31 import com.badlogic.gdx.tests.utils.GdxTest; 32 33 public class DecalTest extends GdxTest { 34 public static final int TARGET_FPS = 40; 35 public static final int INITIAL_RENDERED = 100; 36 private boolean willItBlend_that_is_the_question = true; 37 Texture egg; 38 Texture wheel; 39 LinkedList<Decal> toRender = new LinkedList<Decal>(); 40 DecalBatch batch; 41 float timePassed = 0; 42 int frames = 0; 43 Camera cam; 44 WindowedMean fps = new WindowedMean(5); 45 int idx = 0; 46 float w; 47 float h; 48 49 @Override create()50 public void create () { 51 Gdx.gl.glEnable(GL20.GL_DEPTH_TEST); 52 Gdx.gl.glDepthFunc(GL20.GL_LESS); 53 54 egg = new Texture(Gdx.files.internal("data/egg.png")); 55 egg.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); 56 egg.setWrap(Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge); 57 wheel = new Texture(Gdx.files.internal("data/wheel.png")); 58 wheel.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); 59 wheel.setWrap(Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge); 60 61 w = Gdx.graphics.getWidth() / 0.8f; 62 h = Gdx.graphics.getHeight() / 0.8f; 63 for (int i = 0; i < INITIAL_RENDERED; i++) { 64 toRender.add(makeDecal()); 65 } 66 cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 67 cam.near = 0.1f; 68 cam.far = 10f; 69 cam.position.set(0, 0, 0.1f); 70 cam.direction.set(0, 0, -1f); 71 batch = new DecalBatch(new CameraGroupStrategy(cam)); 72 73 Gdx.gl.glClearColor(1, 1, 0, 1); 74 } 75 76 @Override dispose()77 public void dispose () { 78 egg.dispose(); 79 wheel.dispose(); 80 batch.dispose(); 81 } 82 83 @Override render()84 public void render () { 85 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 86 87 float elapsed = Gdx.graphics.getDeltaTime(); 88 float scale = timePassed > 0.5 ? 1 - timePassed / 2 : 0.5f + timePassed / 2; 89 90 for (Decal decal : toRender) { 91 decal.rotateZ(elapsed * 45); 92 decal.setScale(scale); 93 batch.add(decal); 94 } 95 batch.flush(); 96 97 timePassed += elapsed; 98 frames++; 99 if (timePassed > 1.0f) { 100 System.out.println("DecalPerformanceTest2 fps: " + frames + " at spritecount: " + toRender.size()); 101 fps.addValue(frames); 102 if (fps.hasEnoughData()) { 103 float factor = fps.getMean() / (float)TARGET_FPS; 104 int target = (int)(toRender.size() * factor); 105 if (fps.getMean() > TARGET_FPS) { 106 int start = toRender.size(); 107 for (int i = start; toRender.size() < target; i++) { 108 toRender.add(makeDecal()); 109 } 110 fps.clear(); 111 } else { 112 while (toRender.size() > target) { 113 toRender.removeLast(); 114 } 115 fps.clear(); 116 } 117 } 118 timePassed = 0; 119 frames = 0; 120 } 121 } 122 123 @Override resize(int width, int height)124 public void resize (int width, int height) { 125 w = Gdx.graphics.getWidth() / 0.8f; 126 h = Gdx.graphics.getHeight() / 0.8f; 127 cam = new OrthographicCamera(width, height); 128 cam.near = 0.1f; 129 cam.far = 10f; 130 cam.position.set(0, 0, 0.1f); 131 cam.direction.set(0, 0, -1f); 132 } 133 makeDecal()134 private Decal makeDecal () { 135 Decal sprite = null; 136 switch (idx % 2) { 137 case 0: 138 sprite = Decal.newDecal(new TextureRegion(egg), willItBlend_that_is_the_question); 139 break; 140 case 1: 141 sprite = Decal.newDecal(new TextureRegion(wheel)); 142 break; 143 } 144 sprite.setPosition(-w / 2 + (float)Math.random() * w, h / 2 - (float)Math.random() * h, (float)-Math.random() * 10); 145 idx++; 146 return sprite; 147 } 148 } 149