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.InputMultiplexer; 21 import com.badlogic.gdx.graphics.Color; 22 import com.badlogic.gdx.graphics.Cubemap; 23 import com.badlogic.gdx.graphics.GL20; 24 import com.badlogic.gdx.graphics.OrthographicCamera; 25 import com.badlogic.gdx.graphics.PerspectiveCamera; 26 import com.badlogic.gdx.graphics.Texture; 27 import com.badlogic.gdx.graphics.Texture.TextureFilter; 28 import com.badlogic.gdx.graphics.VertexAttributes.Usage; 29 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 30 import com.badlogic.gdx.graphics.g3d.Environment; 31 import com.badlogic.gdx.graphics.g3d.Material; 32 import com.badlogic.gdx.graphics.g3d.Model; 33 import com.badlogic.gdx.graphics.g3d.ModelBatch; 34 import com.badlogic.gdx.graphics.g3d.ModelInstance; 35 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; 36 import com.badlogic.gdx.graphics.g3d.attributes.CubemapAttribute; 37 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; 38 import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader.Config; 39 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController; 40 import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider; 41 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; 42 import com.badlogic.gdx.graphics.glutils.KTXTextureData; 43 import com.badlogic.gdx.graphics.glutils.ShaderProgram; 44 import com.badlogic.gdx.math.MathUtils; 45 import com.badlogic.gdx.tests.utils.GdxTest; 46 47 /** Simple test and example for the KTX/ZKTX file format 48 * @author Vincent Bousquet */ 49 public class KTXTest extends GdxTest { 50 51 // 3D texture cubemap example 52 private PerspectiveCamera perspectiveCamera; 53 private CameraInputController inputController; 54 private ModelBatch modelBatch; 55 private Model model; 56 private ModelInstance instance; 57 private Environment environment; 58 private Cubemap cubemap; 59 60 // 2D texture alpha ETC1 example 61 private OrthographicCamera orthoCamera; 62 private Texture image; 63 private SpriteBatch batch; 64 private ShaderProgram etc1aShader; 65 66 // animation 67 private float time; 68 69 @Override create()70 public void create () { 71 72 // Cubemap test 73 74 String cubemapVS = "" // 75 + "attribute vec3 a_position;\n"// 76 + "uniform mat4 u_projViewTrans;\n"// 77 + "uniform mat4 u_worldTrans;\n"// 78 + "\n"// 79 + "varying vec3 v_cubeMapUV;\n"// 80 + "\n"// 81 + "void main() {\n"// 82 + " vec4 g_position = vec4(a_position, 1.0);\n"// 83 + " g_position = u_worldTrans * g_position;\n"// 84 + " v_cubeMapUV = normalize(g_position.xyz);\n"// 85 + " gl_Position = u_projViewTrans * g_position;\n"// 86 + "}"; 87 String cubemapFS = ""// 88 + "#ifdef GL_ES\n"// 89 + "precision mediump float;\n"// 90 + "#endif\n"// 91 + "uniform samplerCube u_environmentCubemap;\n"// 92 + "varying vec3 v_cubeMapUV;\n"// 93 + "void main() {\n" // 94 + " gl_FragColor = vec4(textureCube(u_environmentCubemap, v_cubeMapUV).rgb, 1.0);\n" // 95 + "}\n"; 96 modelBatch = new ModelBatch(new DefaultShaderProvider(new Config(cubemapVS, cubemapFS))); 97 98 cubemap = new Cubemap(new KTXTextureData(Gdx.files.internal("data/cubemap.zktx"), true)); 99 cubemap.setFilter(TextureFilter.MipMapLinearLinear, TextureFilter.Linear); 100 101 environment = new Environment(); 102 environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.1f, 0.1f, 0.1f, 1.f)); 103 environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1.0f, -0.8f)); 104 environment.set(new CubemapAttribute(CubemapAttribute.EnvironmentMap, cubemap)); 105 106 perspectiveCamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 107 perspectiveCamera.position.set(10f, 10f, 10f); 108 perspectiveCamera.lookAt(0, 0, 0); 109 perspectiveCamera.near = 0.1f; 110 perspectiveCamera.far = 300f; 111 perspectiveCamera.update(); 112 113 ModelBuilder modelBuilder = new ModelBuilder(); 114 model = modelBuilder.createBox(5f, 5f, 5f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), Usage.Position 115 | Usage.Normal); 116 instance = new ModelInstance(model); 117 118 Gdx.input.setInputProcessor(new InputMultiplexer(this, inputController = new CameraInputController(perspectiveCamera))); 119 120 // 2D texture test 121 String etc1aVS = "" // 122 + "uniform mat4 u_projTrans;\n"// 123 + "\n"// 124 + "attribute vec4 a_position;\n"// 125 + "attribute vec2 a_texCoord0;\n"// 126 + "attribute vec4 a_color;\n"// 127 + "\n"// 128 + "varying vec4 v_color;\n"// 129 + "varying vec2 v_texCoord;\n"// 130 + "\n"// 131 + "void main() {\n"// 132 + " gl_Position = u_projTrans * a_position;\n"// 133 + " v_texCoord = a_texCoord0;\n"// 134 + " v_color = a_color;\n"// 135 + "}\n";// 136 String etc1aFS = ""// 137 + "#ifdef GL_ES\n"// 138 + "precision mediump float;\n"// 139 + "#endif\n"// 140 + "uniform sampler2D u_texture;\n"// 141 + "\n"// 142 + "varying vec4 v_color;\n"// 143 + "varying vec2 v_texCoord;\n"// 144 + "\n"// 145 + "void main() {\n"// 146 + " vec3 col = texture2D(u_texture, v_texCoord.st).rgb;\n"// 147 + " float alpha = texture2D(u_texture, v_texCoord.st + vec2(0.0, 0.5)).r;\n"// 148 + " gl_FragColor = vec4(col, alpha) * v_color;\n"// 149 + "}\n";// 150 etc1aShader = new ShaderProgram(etc1aVS, etc1aFS); 151 orthoCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 152 image = new Texture("data/egg.zktx"); 153 batch = new SpriteBatch(100, etc1aShader); 154 155 } 156 157 @Override render()158 public void render () { 159 time += Gdx.graphics.getDeltaTime(); 160 inputController.update(); 161 int gw = Gdx.graphics.getWidth(), gh = Gdx.graphics.getHeight(); 162 int pw = gw > gh ? gw / 2 : gw, ph = gw > gh ? gh : gh / 2; 163 164 Gdx.gl.glClearColor(0, 0, 0, 1); 165 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 166 167 // cubemap 168 Gdx.gl.glViewport(gw - pw, gh - ph, pw, ph); 169 perspectiveCamera.viewportWidth = pw; 170 perspectiveCamera.viewportHeight = ph; 171 perspectiveCamera.update(); 172 modelBatch.begin(perspectiveCamera); 173 modelBatch.render(instance, environment); 174 modelBatch.end(); 175 176 // 2D texture with alpha & ETC1 177 Gdx.gl.glViewport(0, 0, pw, ph); 178 orthoCamera.viewportWidth = pw; 179 orthoCamera.viewportHeight = ph; 180 orthoCamera.update(); 181 batch.setProjectionMatrix(orthoCamera.combined); 182 batch.begin(); 183 float s = 0.1f + 0.5f * (1 + MathUtils.sinDeg(time * 90.0f)); 184 float w = s * image.getWidth(), h = s * image.getHeight() / 2, x = -w / 2, y = -h / 2; 185 batch.setShader(null); 186 batch.disableBlending(); 187 batch.draw(image, -pw / 2, -ph / 2, pw, ph, 0, 1, 1, 0); 188 batch.setShader(etc1aShader); 189 batch.enableBlending(); 190 batch.draw(image, x, y, w, h, 0, 0.5f, 1, 0); 191 batch.end(); 192 } 193 194 @Override dispose()195 public void dispose () { 196 modelBatch.dispose(); 197 model.dispose(); 198 cubemap.dispose(); 199 image.dispose(); 200 batch.dispose(); 201 etc1aShader.dispose(); 202 } 203 needsGL20()204 public boolean needsGL20 () { 205 return true; 206 } 207 resume()208 public void resume () { 209 } 210 resize(int width, int height)211 public void resize (int width, int height) { 212 } 213 pause()214 public void pause () { 215 } 216 217 } 218