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.Color; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.Texture; 23 import com.badlogic.gdx.graphics.VertexAttribute; 24 import com.badlogic.gdx.graphics.VertexAttributes; 25 import com.badlogic.gdx.graphics.glutils.IndexBufferObject; 26 import com.badlogic.gdx.graphics.glutils.ShaderProgram; 27 import com.badlogic.gdx.graphics.glutils.VertexBufferObject; 28 import com.badlogic.gdx.tests.utils.GdxTest; 29 30 public class IndexBufferObjectShaderTest extends GdxTest { 31 Texture texture; 32 ShaderProgram shader; 33 VertexBufferObject vbo; 34 IndexBufferObject ibo; 35 36 @Override dispose()37 public void dispose () { 38 texture.dispose(); 39 shader.dispose(); 40 vbo.dispose(); 41 ibo.dispose(); 42 } 43 44 @Override render()45 public void render () { 46 // System.out.println( "render"); 47 48 Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight()); 49 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 50 51 Gdx.gl.glEnable(GL20.GL_TEXTURE_2D); 52 shader.begin(); 53 shader.setUniformi("u_texture", 0); 54 texture.bind(); 55 vbo.bind(shader); 56 ibo.bind(); 57 Gdx.gl20.glDrawElements(GL20.GL_TRIANGLES, 3, GL20.GL_UNSIGNED_SHORT, 0); 58 ibo.unbind(); 59 vbo.unbind(shader); 60 shader.end(); 61 } 62 63 @Override create()64 public void create () { 65 String vertexShader = "attribute vec4 a_position; \n" + "attribute vec4 a_color;\n" + "attribute vec2 a_texCoords;\n" 66 + "varying vec4 v_color;" + "varying vec2 v_texCoords;" + "void main() \n" 67 + "{ \n" + " v_color = vec4(a_color.x, a_color.y, a_color.z, 1); \n" 68 + " v_texCoords = a_texCoords; \n" + " gl_Position = a_position; \n" + "} \n"; 69 String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec4 v_color;\n" 70 + "varying vec2 v_texCoords;\n" + "uniform sampler2D u_texture;\n" + "void main() \n" 71 + "{ \n" + " gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" 72 + "}"; 73 74 shader = new ShaderProgram(vertexShader, fragmentShader); 75 vbo = new VertexBufferObject(true, 3, new VertexAttribute(VertexAttributes.Usage.Position, 2, "a_position"), 76 new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoords"), new VertexAttribute( 77 VertexAttributes.Usage.ColorPacked, 4, "a_color")); 78 float[] vertices = new float[] {-1, -1, 0, 0, Color.toFloatBits(1f, 0f, 0f, 1f), 0, 1, 0.5f, 1.0f, 79 Color.toFloatBits(0f, 1f, 0f, 1f), 1, -1, 1, 0, Color.toFloatBits(0f, 0f, 1f, 1f)}; 80 vbo.setVertices(vertices, 0, vertices.length); 81 82 ibo = new IndexBufferObject(true, 3); 83 ibo.setIndices(new short[] {0, 1, 2}, 0, 3); 84 85 texture = new Texture(Gdx.files.internal("data/badlogic.jpg")); 86 } 87 88 @Override resume()89 public void resume () { 90 vbo.invalidate(); 91 ibo.invalidate(); 92 } 93 94 } 95