Anonymous avatar Anonymous committed b32f51a

Added HelloShader example

Comments (0)

Files changed (1)

src/main/java/sandbox/misc/HelloShader.java

+package sandbox.misc;
+
+import org.lwjgl.BufferUtils;
+import org.lwjgl.util.vector.Vector2f;
+import org.lwjgl.util.vector.Vector3f;
+import org.lwjgl.util.vector.Vector4f;
+import sandbox.common.BaseApp;
+import sandbox.util.VBO;
+
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
+
+import static org.lwjgl.opengl.GL11.*;
+import static org.lwjgl.opengl.GL20.*;
+
+public class HelloShader extends BaseApp {
+    VBO vbo = new VBO();
+
+    public static final String VERTEX_SHADER = "" +
+            "varying vec4 vertColor;\n" +
+            "void main(){\n" +
+            "    gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;\n" +
+            "    vertColor = vec4(0.9, 0.8, 0.1, 1.0);\n" +
+            "}";
+
+    public static final String FRAGMENT_SHADER = "" +
+            "varying vec4 vertColor;\n" +
+            "void main(){\n" +
+            "    gl_FragColor = vertColor;\n" +
+            "}";
+
+    private int shader = 0;
+    private int vertShader = 0;
+    private int fragShader = 0;
+
+    public HelloShader(String title) {
+        super(title);
+    }
+
+    public static void main(String[] args) {
+        HelloShader s = new HelloShader("Hello Shader");
+        s.doMain(args);
+    }
+
+    @Override
+    protected void setup() {
+        vbo.addVertex(
+                new Vector3f(-50, -50, 0),
+                new Vector3f(0, 1, 0),
+                new Vector4f(0, 0, 1, 1),
+                new Vector2f(0, 0)
+        );
+
+        vbo.addVertex(
+                new Vector3f(50, -50, 0),
+                new Vector3f(0, 1, 0),
+                new Vector4f(0, 0, 1, 1),
+                new Vector2f(0, 0)
+        );
+
+        vbo.addVertex(
+                new Vector3f(50, 50, 0),
+                new Vector3f(0, 1, 0),
+                new Vector4f(0, 0, 1, 1),
+                new Vector2f(0, 0)
+        );
+
+        vbo.compile();
+        shader = glCreateProgram();
+
+        buildVertexShader(VERTEX_SHADER);
+        buildFragmentShader(FRAGMENT_SHADER);
+
+        glAttachShader(shader, vertShader);
+        glAttachShader(shader, fragShader);
+        glLinkProgram(shader);
+        glValidateProgram(shader);
+        programPostValidate(shader);
+    }
+
+    @Override
+    protected void render() {
+        glUseProgram(shader);
+        glPushMatrix();
+        glTranslatef(0, 0, -200);
+        glClear(GL_COLOR_BUFFER_BIT);
+
+        glEnableClientState(GL_VERTEX_ARRAY);
+        glEnableClientState(GL_NORMAL_ARRAY);
+        glEnableClientState(GL_COLOR_ARRAY);
+
+        vbo.bind();
+        glDrawElements(GL_TRIANGLES, vbo.getSize(), GL_UNSIGNED_INT, 0);
+
+        glDisableClientState(GL_VERTEX_ARRAY);
+        glDisableClientState(GL_NORMAL_ARRAY);
+        glDisableClientState(GL_COLOR_ARRAY);
+        glPopMatrix();
+        glUseProgram(0);
+    }
+
+
+    private void buildVertexShader(String source) {
+        vertShader = glCreateShader(GL_VERTEX_SHADER);
+        glShaderSource(vertShader, source);
+        glCompileShader(vertShader);
+        if (!shaderPostCompile(vertShader))
+            vertShader = 0;
+    }
+
+    private void buildFragmentShader(String source) {
+        fragShader = glCreateShader(GL_FRAGMENT_SHADER);
+        glShaderSource(fragShader, source);
+        glCompileShader(fragShader);
+        if (!shaderPostCompile(fragShader))
+            fragShader = 0;
+    }
+
+    private boolean shaderPostCompile(int obj) {
+        IntBuffer iVal = BufferUtils.createIntBuffer(1);
+        glGetShader(obj, GL_INFO_LOG_LENGTH, iVal);
+        int length = iVal.get();
+        if (length <= 1)
+            return true;
+
+        ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
+        iVal.flip();
+        glGetShaderInfoLog(obj, iVal, infoLog);
+        byte[] infoBytes = new byte[length];
+        infoLog.get(infoBytes);
+        String out = new String(infoBytes);
+        System.err.println(out);
+        return false;
+    }
+
+    private boolean programPostValidate(int obj) {
+        IntBuffer iVal = BufferUtils.createIntBuffer(1);
+        glGetProgram(obj, GL_INFO_LOG_LENGTH, iVal);
+        int length = iVal.get();
+        if (length <= 1)
+            return true;
+
+        ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
+        iVal.flip();
+        glGetProgramInfoLog(obj, iVal, infoLog);
+        byte[] infoBytes = new byte[length];
+        infoLog.get(infoBytes);
+        String out = new String(infoBytes);
+        System.err.println(out);
+        return false;
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.