Home | History | Annotate | Download | only in tests
      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.GL20;
     22 import com.badlogic.gdx.graphics.Mesh;
     23 import com.badlogic.gdx.graphics.PerspectiveCamera;
     24 import com.badlogic.gdx.graphics.Pixmap.Format;
     25 import com.badlogic.gdx.graphics.Texture;
     26 import com.badlogic.gdx.graphics.Texture.TextureFilter;
     27 import com.badlogic.gdx.graphics.VertexAttribute;
     28 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
     29 import com.badlogic.gdx.graphics.glutils.MipMapGenerator;
     30 import com.badlogic.gdx.graphics.glutils.ShaderProgram;
     31 import com.badlogic.gdx.scenes.scene2d.Stage;
     32 import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
     33 import com.badlogic.gdx.scenes.scene2d.ui.Label;
     34 import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
     35 import com.badlogic.gdx.scenes.scene2d.ui.Table;
     36 import com.badlogic.gdx.scenes.scene2d.ui.SelectBox.SelectBoxStyle;
     37 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
     38 import com.badlogic.gdx.tests.utils.GdxTest;
     39 import com.badlogic.gdx.tests.utils.PerspectiveCamController;
     40 import com.badlogic.gdx.utils.GdxRuntimeException;
     41 
     42 public class MipMapTest extends GdxTest {
     43 	PerspectiveCamera camera;
     44 	PerspectiveCamController controller;
     45 	Mesh mesh;
     46 	Texture textureHW;
     47 	Texture textureSW;
     48 	Texture currTexture;
     49 	ShaderProgram shader;
     50 	Stage ui;
     51 	Skin skin;
     52 	InputMultiplexer multiplexer;
     53 	SelectBox<String> minFilter;
     54 	SelectBox<String> magFilter;
     55 	CheckBox hwMipMap;
     56 
     57 	@Override
     58 	public void create () {
     59 		camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
     60 		camera.position.set(0, 1.5f, 1.5f);
     61 		camera.lookAt(0, 0, 0);
     62 		camera.update();
     63 		controller = new PerspectiveCamController(camera);
     64 
     65 		mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(
     66 			Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
     67 		mesh.setVertices(new float[] {-1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, -1, 1, 0, -1, 0, -1, 0, 0,});
     68 		mesh.setIndices(new short[] {0, 1, 2, 3});
     69 
     70 		shader = new ShaderProgram(Gdx.files.internal("data/shaders/flattex-vert.glsl").readString(), Gdx.files.internal(
     71 			"data/shaders/flattex-frag.glsl").readString());
     72 		if (!shader.isCompiled()) throw new GdxRuntimeException("shader error: " + shader.getLog());
     73 
     74 		textureHW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true);
     75 		MipMapGenerator.setUseHardwareMipMap(false);
     76 		textureSW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true);
     77 		currTexture = textureHW;
     78 
     79 		createUI();
     80 
     81 		multiplexer = new InputMultiplexer();
     82 		Gdx.input.setInputProcessor(multiplexer);
     83 		multiplexer.addProcessor(ui);
     84 		multiplexer.addProcessor(controller);
     85 	}
     86 
     87 	private void createUI () {
     88 		skin = new Skin(Gdx.files.internal("data/uiskin.json"));
     89 		ui = new Stage();
     90 
     91 		String[] filters = new String[TextureFilter.values().length];
     92 		int idx = 0;
     93 		for (TextureFilter filter : TextureFilter.values()) {
     94 			filters[idx++] = filter.toString();
     95 		}
     96 		hwMipMap = new CheckBox("Hardware Mips", skin);
     97 		minFilter = new SelectBox(skin);
     98 		minFilter.setItems(filters);
     99 		magFilter = new SelectBox(skin.get(SelectBoxStyle.class));
    100 		magFilter.setItems("Nearest", "Linear");
    101 
    102 		Table table = new Table();
    103 		table.setSize(ui.getWidth(), 30);
    104 		table.setY(ui.getHeight() - 30);
    105 		table.add(hwMipMap).spaceRight(5);
    106 		table.add(new Label("Min Filter", skin)).spaceRight(5);
    107 		table.add(minFilter).spaceRight(5);
    108 		table.add(new Label("Mag Filter", skin)).spaceRight(5);
    109 		table.add(magFilter);
    110 
    111 		ui.addActor(table);
    112 	}
    113 
    114 	@Override
    115 	public void render () {
    116 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    117 		Gdx.gl.glEnable(GL20.GL_TEXTURE_2D);
    118 
    119 		camera.update();
    120 
    121 		currTexture = hwMipMap.isChecked() ? textureHW : textureSW;
    122 		currTexture.bind();
    123 		currTexture.setFilter(TextureFilter.valueOf(minFilter.getSelected()), TextureFilter.valueOf(magFilter.getSelected()));
    124 
    125 		shader.begin();
    126 		shader.setUniformMatrix("u_projTrans", camera.combined);
    127 		shader.setUniformi("s_texture", 0);
    128 		mesh.render(shader, GL20.GL_TRIANGLE_FAN);
    129 		shader.end();
    130 
    131 		ui.act();
    132 		ui.draw();
    133 	}
    134 
    135 	@Override
    136 	public void dispose () {
    137 		shader.dispose();
    138 		textureHW.dispose();
    139 		textureSW.dispose();
    140 		mesh.dispose();
    141 		ui.dispose();
    142 		skin.dispose();
    143 	}
    144 }
    145