Java tutorial
/* * Copyright (C) 2014 Helix Engine Developers (http://github.com/fauu/HelixEngine) * * This software is licensed under the GNU General Public License * (version 3 or later). See the COPYING file in this distribution. * * You should have received a copy of the GNU Library General Public License * along with this software. If not, see <http://www.gnu.org/licenses/>. * * Authored by: Piotr Grabowski <fau999@gmail.com> */ package com.github.fauu.helix.core; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Camera; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g3d.Renderable; import com.badlogic.gdx.graphics.g3d.Shader; import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; import com.badlogic.gdx.graphics.g3d.utils.RenderContext; import com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor; import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.utils.GdxRuntimeException; public class GeneralShader implements Shader { private ShaderProgram program; private Camera camera; private RenderContext context; private int u_projTrans; private int u_worldTrans; private int u_color; private int u_texture; @Override public void init() { String vert = Gdx.files.internal("assets/shaders/default.vert").readString(); String frag = Gdx.files.internal("assets/shaders/default.frag").readString(); program = new ShaderProgram(vert, frag); if (!program.isCompiled()) { throw new GdxRuntimeException(program.getLog()); } u_projTrans = program.getUniformLocation("u_projTrans"); u_worldTrans = program.getUniformLocation("u_worldTrans"); u_color = program.getUniformLocation("u_color"); u_texture = program.getUniformLocation("u_texture"); } @Override public int compareTo(Shader other) { return 0; } @Override public boolean canRender(Renderable instance) { return true; } @Override public void begin(Camera camera, RenderContext context) { this.camera = camera; this.context = context; program.begin(); program.setUniformMatrix(u_projTrans, camera.combined); context.setBlending(true, GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); context.setCullFace(GL20.GL_BACK); context.setDepthTest(GL20.GL_LEQUAL, 0, 1); context.setDepthMask(true); } @Override public void render(Renderable renderable) { program.setUniformMatrix(u_worldTrans, renderable.worldTransform); final Color color = ((ColorAttribute) renderable.material.get(ColorAttribute.Diffuse)).color; program.setUniformf(u_color, color.r, color.g, color.b, color.a); final TextureDescriptor textureDescriptor = ((TextureAttribute) renderable.material .get(TextureAttribute.Diffuse)).textureDescription; program.setUniformi(u_texture, context.textureBinder.bind(textureDescriptor)); if (!(renderable.mesh instanceof MapRegionMesh)) { program.setAttributef("a_color", 1, 1, 1, 1); } renderable.mesh.render(program, renderable.primitiveType, renderable.meshPartOffset, renderable.meshPartSize, true); } @Override public void end() { program.end(); } @Override public void dispose() { program.dispose(); } public ShaderProgram getProgram() { return program; } }