1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Mesh; import com.badlogic.gdx.graphics.VertexAttribute; import com.badlogic.gdx.graphics.VertexAttributes.Usage; import com.badlogic.gdx.graphics.glutils.ShaderProgram;
public class ErrorShader implements ApplicationListener { MeshHelper meshHelper;
public static void main(String[] args) { LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration(); cfg.title = "Error"; cfg.useGL20 = true; cfg.width = 500; cfg.height = 500; cfg.resizable = false; new LwjglApplication(new ErrorShader(), cfg); } @Override public void create() { meshHelper = new MeshHelper(); meshHelper.createMesh(new float[] { -1.0f, -1.0f, Color.toFloatBits(0, 0, 255, 0), 0.0f, 1f, Color.toFloatBits(0, 0, 255, 0), 1f, -1.0f, Color.toFloatBits(0, 0, 255, 0) }); }
@Override public void resize(int width, int height) { }
@Override public void render() { Gdx.graphics.getGL20().glClearColor(0.2f, 0.2f, 0.2f, 1); Gdx.graphics.getGL20().glClear(GL10.GL_COLOR_BUFFER_BIT); Gdx.graphics.getGL20().glEnable(GL20.GL_BLEND); Gdx.gl.glBlendFunc(GL10.GL_ONE_MINUS_SRC_ALPHA, GL10.GL_SRC_ALPHA); meshHelper.drawMesh(); }
@Override public void pause() { }
@Override public void resume() { }
@Override public void dispose() { meshHelper.dispose(); } }
class MeshHelper { private Mesh mesh; private ShaderProgram redShader; private ShaderProgram blueShader;
public MeshHelper() { createShader(); }
public void createMesh(float[] vertices) { mesh = new Mesh(true, vertices.length, 0, new VertexAttribute(Usage.Position, 2, "a_position"), new VertexAttribute(Usage.ColorPacked, 4, "a_color")); mesh.setVertices(vertices); }
public void drawMesh() { if (mesh == null) throw new IllegalStateException("drawMesh called before a mesh has been created."); redShader.begin(); mesh.render(redShader, GL20.GL_TRIANGLES); redShader.end(); blueShader.begin(); mesh.render(blueShader, GL20.GL_TRIANGLES); blueShader.end(); }
private void createShader() { String vertexShader = "attribute vec4 a_position; \n" + "attribute vec4 a_color; \n" + "varying vec4 v_color; \n" + "void main() \n" + "{ \n" + " v_color = a_color; \n" + " gl_Position = a_position; \n" + "} \n";
String fragmentBLUEShader = "#ifdef GL_ES\n" + "#define LOWP lowp\n" + "precision mediump float;\n" + "#else\n" + "#define LOWP \n" + "#endif\n" + "\n" + "void main() {\n" + " gl_FragColor = vec4(0.0,0.0,1.0, 0.0);\n" + "}"; blueShader = new ShaderProgram(vertexShader, fragmentBLUEShader); if (blueShader.isCompiled() == false) throw new IllegalStateException(blueShader.getLog()); String fragmentREDShader = "#ifdef GL_ES\n" + "#define LOWP lowp\n" + "precision mediump float;\n" + "#else\n" + "#define LOWP \n" + "#endif\n" + "\n" + "void main() {\n" + " gl_FragColor = vec4(1,0,0,1);\n" + "}"; redShader = new ShaderProgram(vertexShader, fragmentREDShader); if (redShader.isCompiled() == false) throw new IllegalStateException(redShader.getLog()); } public void dispose() { mesh.dispose(); blueShader.dispose(); redShader.dispose(); }
} |