• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.GL20;
21 import com.badlogic.gdx.graphics.Mesh;
22 import com.badlogic.gdx.graphics.Texture;
23 import com.badlogic.gdx.graphics.VertexAttribute;
24 import com.badlogic.gdx.graphics.glutils.ShaderProgram;
25 import com.badlogic.gdx.math.Matrix4;
26 import com.badlogic.gdx.math.Vector3;
27 import com.badlogic.gdx.tests.utils.GdxTest;
28 
29 public class MeshShaderTest extends GdxTest {
30 	ShaderProgram shader;
31 	Mesh mesh;
32 	Texture texture;
33 	Matrix4 matrix = new Matrix4();
34 
35 	@Override
create()36 	public void create () {
37 		String vertexShader = "attribute vec4 a_position;    \n" + "attribute vec4 a_color;\n" + "attribute vec2 a_texCoord0;\n"
38 			+ "uniform mat4 u_worldView;\n" + "varying vec4 v_color;" + "varying vec2 v_texCoords;"
39 			+ "void main()                  \n" + "{                            \n" + "   v_color = vec4(1, 1, 1, 1); \n"
40 			+ "   v_texCoords = a_texCoord0; \n" + "   gl_Position =  u_worldView * a_position;  \n"
41 			+ "}                            \n";
42 		String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec4 v_color;\n"
43 			+ "varying vec2 v_texCoords;\n" + "uniform sampler2D u_texture;\n" + "void main()                                  \n"
44 			+ "{                                            \n" + "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n"
45 			+ "}";
46 
47 		shader = new ShaderProgram(vertexShader, fragmentShader);
48 		if (shader.isCompiled() == false) {
49 			Gdx.app.log("ShaderTest", shader.getLog());
50 			Gdx.app.exit();
51 		}
52 
53 		mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.TexCoords(0));
54 		mesh.setVertices(new float[] {-0.5f, -0.5f, 0, 1, 1, 1, 1, 0, 1, 0.5f, -0.5f, 0, 1, 1, 1, 1, 1, 1, 0.5f, 0.5f, 0, 1, 1, 1,
55 			1, 1, 0, -0.5f, 0.5f, 0, 1, 1, 1, 1, 0, 0});
56 		mesh.setIndices(new short[] {0, 1, 2, 2, 3, 0});
57 		texture = new Texture(Gdx.files.internal("data/bobrgb888-32x32.png"));
58 	}
59 
60 	Vector3 axis = new Vector3(0, 0, 1);
61 	float angle = 0;
62 
63 	@Override
render()64 	public void render () {
65 		angle += Gdx.graphics.getDeltaTime() * 45;
66 		matrix.setToRotation(axis, angle);
67 
68 		Gdx.gl20.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
69 		Gdx.gl20.glClearColor(0.2f, 0.2f, 0.2f, 1);
70 		Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
71 		Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
72 		Gdx.gl20.glEnable(GL20.GL_BLEND);
73 		Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
74 		texture.bind();
75 		shader.begin();
76 		shader.setUniformMatrix("u_worldView", matrix);
77 		shader.setUniformi("u_texture", 0);
78 		mesh.render(shader, GL20.GL_TRIANGLES);
79 		shader.end();
80 	}
81 
82 	@Override
dispose()83 	public void dispose () {
84 		mesh.dispose();
85 		texture.dispose();
86 		shader.dispose();
87 	}
88 }
89