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.gles2; 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.VertexAttributes.Usage; 23 import com.badlogic.gdx.graphics.glutils.ShaderProgram; 24 import com.badlogic.gdx.math.Matrix4; 25 import com.badlogic.gdx.math.Vector3; 26 import com.badlogic.gdx.tests.utils.GdxTest; 27 28 public class SimpleVertexShader extends GdxTest { 29 ShaderProgram shader; 30 Mesh mesh; 31 Matrix4 projection = new Matrix4(); 32 Matrix4 view = new Matrix4(); 33 Matrix4 model = new Matrix4(); 34 Matrix4 combined = new Matrix4(); 35 Vector3 axis = new Vector3(1, 0, 1).nor(); 36 float angle = 45; 37 38 @Override create()39 public void create () { 40 // @off 41 String vertexShader = 42 "uniform mat4 u_mvpMatrix; \n" 43 + "attribute vec4 a_position; \n" 44 + "void main() \n" 45 + "{ \n" 46 + " gl_Position = u_mvpMatrix * a_position; \n" 47 + "} \n"; 48 String fragmentShader = 49 "#ifdef GL_ES\n" 50 + "precision mediump float;\n" 51 + "#endif\n" 52 + "void main() \n" 53 + "{ \n" 54 + " gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n" 55 + "}"; 56 // @on 57 58 shader = new ShaderProgram(vertexShader, fragmentShader); 59 mesh = Shapes.genCube(); 60 mesh.getVertexAttribute(Usage.Position).alias = "a_position"; 61 } 62 63 @Override render()64 public void render () { 65 angle += Gdx.graphics.getDeltaTime() * 40.0f; 66 float aspect = Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight(); 67 projection.setToProjection(1.0f, 20.0f, 60.0f, aspect); 68 view.idt().trn(0, 0, -2.0f); 69 model.setToRotation(axis, angle); 70 combined.set(projection).mul(view).mul(model); 71 72 Gdx.gl20.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight()); 73 Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); 74 shader.begin(); 75 shader.setUniformMatrix("u_mvpMatrix", combined); 76 mesh.render(shader, GL20.GL_TRIANGLES); 77 shader.end(); 78 79 Gdx.app.log("angle", "" + angle); 80 } 81 } 82