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