Anonymous avatar Anonymous committed de8b9d3

updated HelloShader to properly report output

Comments (0)

Files changed (1)

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

 package sandbox.misc;
 
 import org.lwjgl.BufferUtils;
+import org.lwjgl.opengl.GL20;
 import org.lwjgl.util.vector.Vector2f;
 import org.lwjgl.util.vector.Vector3f;
 import org.lwjgl.util.vector.Vector4f;
 import static org.lwjgl.opengl.GL11.*;
 import static org.lwjgl.opengl.GL20.*;
 
+// Draws a green triangle.
+// A red triangle means the shaders did NOT run.
+// Any other color means something weird happened.
 public class HelloShader extends BaseApp {
     VBO vbo = new VBO();
 
+    // starts off gold
     public static final String VERTEX_SHADER = "" +
             "varying vec4 vertColor;\n" +
             "void main(){\n" +
             "    vertColor = vec4(0.9, 0.8, 0.1, 1.0);\n" +
             "}";
 
+    // this will turn it green
     public static final String FRAGMENT_SHADER = "" +
             "varying vec4 vertColor;\n" +
             "void main(){\n" +
-            "    gl_FragColor = vertColor;\n" +
+            "    gl_FragColor = vertColor + vec4(-1, 0, 0, 0);\n" +
             "}";
 
+
     private int shader = 0;
     private int vertShader = 0;
     private int fragShader = 0;
 
     @Override
     protected void setup() {
+        // This uses our VBO class, which is a quick hack to hide details, but not anything I
+        // recommend for real-world use.
         vbo.addVertex(
-                new Vector3f(-50, -50, 0),
-                new Vector3f(0, 1, 0),
-                new Vector4f(0, 0, 1, 1),
-                new Vector2f(0, 0)
+                new Vector3f(-50, -50, 0),  // position
+                new Vector3f(0, 1, 0),      // normal
+                new Vector4f(1, 0, 0, 1),   // color
+                new Vector2f(0, 0)          // texture coord
         );
 
         vbo.addVertex(
                 new Vector3f(50, -50, 0),
                 new Vector3f(0, 1, 0),
-                new Vector4f(0, 0, 1, 1),
+                new Vector4f(1, 0, 0, 1),
                 new Vector2f(0, 0)
         );
 
         vbo.addVertex(
                 new Vector3f(50, 50, 0),
                 new Vector3f(0, 1, 0),
-                new Vector4f(0, 0, 1, 1),
+                new Vector4f(1, 0, 0, 1),
                 new Vector2f(0, 0)
         );
 
 
         glAttachShader(shader, vertShader);
         glAttachShader(shader, fragShader);
+
         glLinkProgram(shader);
+        checkLink(shader);
         glValidateProgram(shader);
-        programPostValidate(shader);
+        checkValidate(shader);
     }
 
     @Override
         glUseProgram(0);
     }
 
-
     private void buildVertexShader(String source) {
-        vertShader = glCreateShader(GL_VERTEX_SHADER);
-        glShaderSource(vertShader, source);
-        glCompileShader(vertShader);
-        if (!shaderPostCompile(vertShader))
-            vertShader = 0;
+        vertShader = buildShader(glCreateShader(GL_VERTEX_SHADER), source);
     }
 
     private void buildFragmentShader(String source) {
-        fragShader = glCreateShader(GL_FRAGMENT_SHADER);
-        glShaderSource(fragShader, source);
-        glCompileShader(fragShader);
-        if (!shaderPostCompile(fragShader))
-            fragShader = 0;
+        fragShader = buildShader(glCreateShader(GL_FRAGMENT_SHADER), source);
     }
 
-    private boolean shaderPostCompile(int obj) {
+    private int buildShader(int shader, String source) {
+        glShaderSource(shader, source);
+        glCompileShader(shader);
+        if (checkCompile(shader)) {
+            return shader;
+        } else {
+            glDeleteShader(shader);
+            return 0;
+        }
+    }
+
+    private boolean checkCompile(int obj) {
+        if (glGetShader(obj, GL_COMPILE_STATUS) == GL_TRUE)
+            return true;
+
+        // Error compiling -- print the log to stderr
         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);
+        if (length > 1) {
+            // 1 is for the null byte
+            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)
+    private boolean checkLink(int obj) {
+        return checkProgram(obj, GL_LINK_STATUS);
+    }
+
+    private boolean checkValidate(int obj) {
+        return checkProgram(obj, GL_VALIDATE_STATUS);
+    }
+
+    // a copy and paste job from checkCompile
+    private boolean checkProgram(int obj, int status) {
+        if (glGetProgram(obj, status) == GL_TRUE)
             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);
+        // Error compiling -- print the log to stderr
+        IntBuffer iVal = BufferUtils.createIntBuffer(1);
+        glGetShader(obj, GL_INFO_LOG_LENGTH, iVal);
+        int length = iVal.get();
+        if (length > 1) {
+            // 1 is for the null byte
+            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.