Home | History | Annotate | Download | only in g3d
      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.Application;
     20 import com.badlogic.gdx.Gdx;
     21 import com.badlogic.gdx.graphics.Color;
     22 import com.badlogic.gdx.graphics.GL20;
     23 import com.badlogic.gdx.graphics.Mesh;
     24 import com.badlogic.gdx.graphics.PerspectiveCamera;
     25 import com.badlogic.gdx.graphics.Pixmap;
     26 import com.badlogic.gdx.graphics.Texture;
     27 import com.badlogic.gdx.graphics.TextureArray;
     28 import com.badlogic.gdx.graphics.VertexAttribute;
     29 import com.badlogic.gdx.graphics.VertexAttributes;
     30 import com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController;
     31 import com.badlogic.gdx.graphics.glutils.ShaderProgram;
     32 import com.badlogic.gdx.graphics.profiling.GL30Profiler;
     33 import com.badlogic.gdx.math.Matrix4;
     34 import com.badlogic.gdx.math.Quaternion;
     35 import com.badlogic.gdx.math.Vector3;
     36 import com.badlogic.gdx.tests.utils.GdxTest;
     37 
     38 /** @author Tomski **/
     39 public class TextureArrayTest extends GdxTest {
     40 
     41 	TextureArray textureArray;
     42 	Mesh terrain;
     43 
     44 	ShaderProgram shaderProgram;
     45 
     46 	PerspectiveCamera camera;
     47 	FirstPersonCameraController cameraController;
     48 
     49 	Matrix4 modelView = new Matrix4();
     50 
     51 	@Override
     52 	public void create () {
     53 		GL30Profiler.enable();
     54 		ShaderProgram.prependVertexCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_EXT_texture_array : enable\n" : "#version 300 es\n";
     55 		ShaderProgram.prependFragmentCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_EXT_texture_array : enable\n" : "#version 300 es\n";
     56 
     57 		String[] texPaths = new String[] {  "data/g3d/materials/Searing Gorge.jpg",  "data/g3d/materials/Lava Cracks.jpg", "data/g3d/materials/Deep Fire.jpg" };
     58 
     59 		camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
     60 		camera.position.set(8, 10f, 20f);
     61 		camera.lookAt(10, 0, 10);
     62 		camera.up.set(0, 1, 0);
     63 		camera.update();
     64 		cameraController = new FirstPersonCameraController(camera);
     65 		Gdx.input.setInputProcessor(cameraController);
     66 
     67 		textureArray = new TextureArray(texPaths);
     68 		textureArray.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
     69 		shaderProgram = new ShaderProgram(Gdx.files.internal("data/shaders/texturearray.vert"), Gdx.files.internal("data/shaders/texturearray.frag"));
     70 		System.out.println(shaderProgram.getLog());
     71 
     72 		int vertexStride = 6;
     73 		int vertexCount = 100 * 100;
     74 		terrain = new Mesh(false, vertexCount * 6, 0, new VertexAttributes(VertexAttribute.Position(), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 3, ShaderProgram.TEXCOORD_ATTRIBUTE + 0)));
     75 
     76 		Pixmap data = new Pixmap(Gdx.files.internal("data/g3d/heightmap.png"));
     77 		float[] vertices = new float[vertexCount * vertexStride * 6];
     78 		int idx = 0;
     79 		for (int i = 0; i < 100 - 1; i++) {
     80 			for (int j = 0; j < 100 - 1; j++) {
     81 				idx = addVertex(i, j, vertices, data, idx);
     82 				idx = addVertex(i, j + 1, vertices, data, idx);
     83 				idx = addVertex(i + 1, j, vertices, data, idx);
     84 
     85 				idx = addVertex(i, j + 1, vertices, data, idx);
     86 				idx = addVertex(i + 1, j + 1, vertices, data, idx);
     87 				idx = addVertex(i + 1, j, vertices, data, idx);
     88 			}
     89 		}
     90 		terrain.setVertices(vertices);
     91 
     92 		data.dispose();
     93 	}
     94 
     95 	Color tmpColor = new Color();
     96 	private int addVertex (int i, int j, float[] vertsOut, Pixmap heightmap, int idx) {
     97 		int pixel = heightmap.getPixel((int) (i/100f * heightmap.getWidth()), (int)(j/100f * heightmap.getHeight()));
     98 		tmpColor.set(pixel);
     99 		vertsOut[idx++] = i/5f;
    100 		vertsOut[idx++] = tmpColor.r * 25f/5f;
    101 		vertsOut[idx++] = j/ 5f;
    102 		vertsOut[idx++] = i/20f;
    103 		vertsOut[idx++] = j/20f;
    104 		vertsOut[idx++] = (tmpColor.r * 3f) - 0.5f;
    105 		return idx;
    106 	}
    107 
    108 	@Override
    109 	public void render () {
    110 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    111 		Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
    112 		Gdx.gl.glDepthFunc(GL20.GL_LEQUAL);
    113 		Gdx.gl.glCullFace(GL20.GL_BACK);
    114 
    115 		modelView.translate(10f, 0, 10f).rotate(0, 1f, 0, 2f * Gdx.graphics.getDeltaTime()).translate(-10f, 0, -10f);
    116 
    117 		cameraController.update();
    118 
    119 		textureArray.bind();
    120 
    121 		shaderProgram.begin();
    122 		shaderProgram.setUniformi("u_textureArray", 0);
    123 		shaderProgram.setUniformMatrix("u_projViewTrans", camera.combined);
    124 		shaderProgram.setUniformMatrix("u_modelView", modelView);
    125 		terrain.render(shaderProgram, GL20.GL_TRIANGLES);
    126 		shaderProgram.end();
    127 	}
    128 
    129 	@Override
    130 	public void dispose () {
    131 		terrain.dispose();
    132 		shaderProgram.dispose();
    133 	}
    134 }
    135