Anonymous avatar Anonymous committed 8f0266a

refactored wiki tutorial into single class

Comments (0)

Files changed (5)

src/main/java/tutorials/wiki/ShaderBox.java

+package tutorials.wiki.shader;
+
+import org.lwjgl.BufferUtils;
+import org.lwjgl.input.Keyboard;
+import org.lwjgl.opengl.*;
+import org.lwjgl.util.glu.GLU;
+
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
+
+public class ShaderBox {
+
+    Box box;
+
+    private boolean done=false;
+
+    public ShaderBox(){
+        init();
+
+        while(!done){
+            if ((Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) || Display.isCloseRequested())
+                done=true;
+            render();
+            Display.update();
+        }
+
+        Display.destroy();
+    }
+
+    private void render(){
+        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
+        GL11.glLoadIdentity();
+        box.draw();
+    }
+
+    private void init(){
+        int w=1024;
+        int h=768;
+
+        try{
+            Display.setDisplayMode(new DisplayMode(w, h));
+            Display.setVSyncEnabled(true);
+            Display.setTitle("Shader Setup");
+            Display.create();
+        }catch(Exception e){
+            System.out.println("Error setting up display");
+            System.exit(0);
+        }
+
+        GL11.glViewport(0,0,w,h);
+        GL11.glMatrixMode(GL11.GL_PROJECTION);
+        GL11.glLoadIdentity();
+        GLU.gluPerspective(45.0f, ((float) w / (float) h), 0.1f, 100.0f);
+        GL11.glMatrixMode(GL11.GL_MODELVIEW);
+        GL11.glLoadIdentity();
+        GL11.glShadeModel(GL11.GL_SMOOTH);
+        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+        GL11.glClearDepth(1.0f);
+        GL11.glEnable(GL11.GL_DEPTH_TEST);
+        GL11.glDepthFunc(GL11.GL_LEQUAL);
+        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
+
+        box = new Box();
+    }
+
+    public static void main(String[] args){
+        new ShaderBox();
+    }
+}
+
+
+/**
+* The vertex and fragment shaders are setup when the box object is
+* constructed. They are applied to the GL state prior to the box
+* being drawn, and released from that state after drawing.
+* @author Stephen Jones
+*/
+class Box {
+
+    /*
+    * if the shaders are setup ok we can use shaders, otherwise we just
+    * use default settings
+    */
+    private boolean useShader=true;
+
+    public static final String VERTEX_SHADER = "" +
+            "varying vec4 vertColor;\n" +
+            "void main(){\n" +
+            "    gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;\n" +
+            "    vertColor = vec4(0.6, 0.3, 0.4, 1.0);\n" +
+            "}";
+
+    public static final String FRAGMENT_SHADER = "" +
+            "varying vec4 vertColor;\n" +
+            "void main(){\n" +
+            "    gl_FragColor = vertColor;\n" +
+            "}";
+
+
+    /*
+    * program shader, to which is attached a vertex and fragment shaders.
+    * They are set to 0 as a check because GL will assign unique int
+    * values to each
+    */
+    private int shader=0;
+    private int vertShader=0;
+    private int fragShader=0;
+
+    public Box(){
+
+        /*
+        * create the shader program. If OK, create vertex
+        * and fragment shaders
+        */
+        shader= ARBShaderObjects.glCreateProgramObjectARB();
+
+        if(shader!=0){
+            vertShader=createVertShader(VERTEX_SHADER);
+            fragShader=createFragShader(FRAGMENT_SHADER);
+        }
+        else useShader=false;
+
+        /*
+        * if the vertex and fragment shaders setup sucessfully,
+        * attach them to the shader program, link the sahder program
+        * (into the GL context I suppose), and validate
+        */
+        if(vertShader !=0 && fragShader !=0){
+            ARBShaderObjects.glAttachObjectARB(shader, vertShader);
+            ARBShaderObjects.glAttachObjectARB(shader, fragShader);
+            ARBShaderObjects.glLinkProgramARB(shader);
+            ARBShaderObjects.glValidateProgramARB(shader);
+            useShader=printLogInfo(shader);
+        }else useShader=false;
+    }
+
+    /*
+    * If the shader was setup succesfully, we use the shader. Otherwise
+    * we run normal drawing code.
+    */
+    public void draw(){
+        if(useShader) {
+            ARBShaderObjects.glUseProgramObjectARB(shader);
+        }
+        GL11.glLoadIdentity();
+        GL11.glTranslatef(0.0f, 0.0f, -10.0f);
+        GL11.glColor3f(1.0f, 1.0f, 1.0f);//white
+
+        GL11.glBegin(GL11.GL_QUADS);
+        GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
+        GL11.glVertex3f(1.0f, 1.0f, 0.0f);
+        GL11.glVertex3f(1.0f, -1.0f, 0.0f);
+        GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
+        GL11.glEnd();
+
+        //release the shader
+        ARBShaderObjects.glUseProgramObjectARB(0);
+
+    }
+
+    /*
+    * With the exception of syntax, setting up vertex and fragment shaders
+    * is the same.
+    * @param the name and path to the vertex shader
+    */
+    private int createVertShader(String source){
+        //vertShader will be non zero if succefully created
+
+        vertShader=ARBShaderObjects.glCreateShaderObjectARB(ARBVertexShader.GL_VERTEX_SHADER_ARB);
+        //if created, convert the vertex shader code to a String
+        if(vertShader==0){return 0;}
+        /*
+        * associate the vertex code String with the created vertex shader
+        * and compile
+        */
+        ARBShaderObjects.glShaderSourceARB(vertShader, source);
+        ARBShaderObjects.glCompileShaderARB(vertShader);
+        //if there was a problem compiling, reset vertShader to zero
+        if(!printLogInfo(vertShader)){
+            vertShader=0;
+        }
+        //if zero we won't be using the shader
+        return vertShader;
+    }
+
+    //same as per the vertex shader except for method syntax
+    private int createFragShader(String source){
+
+        fragShader=ARBShaderObjects.glCreateShaderObjectARB(ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
+        if(fragShader==0){return 0;}
+            String fragCode="";
+            String line;
+        ARBShaderObjects.glShaderSourceARB(fragShader, source);
+        ARBShaderObjects.glCompileShaderARB(fragShader);
+        if(!printLogInfo(fragShader)){
+            fragShader=0;
+        }
+
+        return fragShader;
+    }
+
+    private static boolean printLogInfo(int obj){
+        IntBuffer iVal = BufferUtils.createIntBuffer(1);
+        ARBShaderObjects.glGetObjectParameterARB(obj,
+        ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
+
+        int length = iVal.get();
+        if (length > 1) {
+            // We have some info we need to output.
+            ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
+            iVal.flip();
+            ARBShaderObjects.glGetInfoLogARB(obj, iVal, infoLog);
+            byte[] infoBytes = new byte[length];
+            infoLog.get(infoBytes);
+            String out = new String(infoBytes);
+            System.out.println("Info log:\n"+out);
+        }
+        else return true;
+        return false;
+    }
+
+}

src/main/java/tutorials/wiki/shader/Box.java

-package tutorials.wiki.shader;
-
-import org.lwjgl.opengl.GL11;
-import java.io.BufferedReader;
-import java.io.FileReader;
-import java.nio.ByteBuffer;
-import java.nio.IntBuffer;
-import org.lwjgl.BufferUtils;
-import org.lwjgl.opengl.ARBShaderObjects;
-import org.lwjgl.opengl.ARBVertexShader;
-import org.lwjgl.opengl.ARBFragmentShader;
-import org.lwjgl.opengl.Util;
-
-/**
-* The vertex and fragment shaders are setup when the box object is
-* constructed. They are applied to the GL state prior to the box
-* being drawn, and released from that state after drawing.
-* @author Stephen Jones
-*/
-public class Box {
-    
-    /*
-    * if the shaders are setup ok we can use shaders, otherwise we just
-    * use default settings
-    */
-    public boolean useShader=true;
-    
-    /*
-    * program shader, to which is attached a vertex and fragment shaders.
-    * They are set to 0 as a check because GL will assign unique int
-    * values to each
-    */
-    private int shader=0;
-    private int vertShader=0;
-    private int fragShader=0;
-
-    public Box(){
-    
-        /*
-        * create the shader program. If OK, create vertex
-        * and fragment shaders
-        */
-        shader=ARBShaderObjects.glCreateProgramObjectARB();
-        
-        if(shader!=0){
-            vertShader=createVertShader("shaders/screen.vert");
-            fragShader=createFragShader("shaders/screen.frag");
-        }
-        else disableShader("Couldn't load shader files");
-
-        /*
-        * if the vertex and fragment shaders setup sucessfully,
-        * attach them to the shader program, link the sahder program
-        * (into the GL context I suppose), and validate
-        */
-        if (vertShader == 0)
-            disableShader("Error compiling vertex shader");
-        if (fragShader == 0)
-            disableShader("Error compiling fragment shader");
-
-        if(vertShader !=0 && fragShader !=0){
-            ARBShaderObjects.glAttachObjectARB(shader, vertShader);
-            ARBShaderObjects.glAttachObjectARB(shader, fragShader);
-            ARBShaderObjects.glLinkProgramARB(shader);
-            ARBShaderObjects.glValidateProgramARB(shader);
-            if (!printLogInfo(shader))
-                disableShader("Error linking program");
-            useShader=printLogInfo(shader);
-        }else disableShader("Error compiling one or more shaders");
-    }
-
-    /*
-    * If the shader was setup succesfully, we use the shader. Otherwise
-    * we run normal drawing code.
-    */
-    public void draw(){
-        if(useShader) {
-            ARBShaderObjects.glUseProgramObjectARB(shader);
-        }
-        GL11.glLoadIdentity();
-        GL11.glTranslatef(0.0f, 0.0f, -10.0f);
-        GL11.glColor3f(1.0f, 1.0f, 1.0f);//white
-
-        GL11.glBegin(GL11.GL_QUADS);
-        GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
-        GL11.glVertex3f(1.0f, 1.0f, 0.0f);
-        GL11.glVertex3f(1.0f, -1.0f, 0.0f);
-        GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
-        GL11.glEnd();
-
-        //release the shader
-        ARBShaderObjects.glUseProgramObjectARB(0);
-
-    }
-
-    /*
-    * With the exception of syntax, setting up vertex and fragment shaders
-    * is the same.
-    * @param the name and path to the vertex shader
-    */
-    private int createVertShader(String filename){
-        //vertShader will be non zero if succefully created
-
-        vertShader=ARBShaderObjects.glCreateShaderObjectARB(ARBVertexShader.GL_VERTEX_SHADER_ARB);
-        //if created, convert the vertex shader code to a String
-        if(vertShader==0){return 0;}
-        String vertexCode="";
-        String line;
-        try{
-            BufferedReader reader=new BufferedReader(new FileReader(filename));
-            while((line=reader.readLine())!=null){
-                vertexCode+=line + "\n";
-            }
-        }catch(Exception e){
-            System.out.println("Fail reading vertex shading code");
-            e.printStackTrace();
-            return 0;
-        }
-        /*
-        * associate the vertex code String with the created vertex shader
-        * and compile
-        */
-        ARBShaderObjects.glShaderSourceARB(vertShader, vertexCode);
-        ARBShaderObjects.glCompileShaderARB(vertShader);
-        //if there was a problem compiling, reset vertShader to zero
-        if(!printLogInfo(vertShader)){
-            vertShader=0;
-        }
-        //if zero we won't be using the shader
-        return vertShader;
-    }
-
-    //same as per the vertex shader except for method syntax
-    private int createFragShader(String filename){
-
-        fragShader=ARBShaderObjects.glCreateShaderObjectARB(ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
-        if(fragShader==0){return 0;}
-            String fragCode="";
-            String line;
-        try{
-            BufferedReader reader=new BufferedReader(new FileReader(filename));
-            while((line=reader.readLine())!=null){
-                fragCode+=line + "\n";
-            }
-        }catch(Exception e){
-            System.out.println("Fail reading fragment shading code");
-            e.printStackTrace();
-            return 0;
-        }
-        ARBShaderObjects.glShaderSourceARB(fragShader, fragCode);
-        ARBShaderObjects.glCompileShaderARB(fragShader);
-        if(!printLogInfo(fragShader)){
-            fragShader=0;
-        }
-
-        return fragShader;
-    }
-
-    // Unlike what the tutorial claims, this is NOT verbose on success.  I've therefore flipped
-    // the return values so things will actually work.
-    private static boolean printLogInfo(int obj){
-        IntBuffer iVal = BufferUtils.createIntBuffer(1);
-        ARBShaderObjects.glGetObjectParameterARB(obj,
-        ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
-
-        int length = iVal.get();
-        if (length > 1) {
-            // We have some info we need to output.
-            ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
-            iVal.flip();
-            ARBShaderObjects.glGetInfoLogARB(obj, iVal, infoLog);
-            byte[] infoBytes = new byte[length];
-            infoLog.get(infoBytes);
-            String out = new String(infoBytes);
-            System.out.println("Info log:\n"+out);
-        }
-        else return true;
-        return false;
-    }
-
-    private void disableShader(String reason) {
-        useShader = false;
-        System.err.println("Disabling shader: " + reason);
-    }
-
-}

src/main/java/tutorials/wiki/shader/Main.java

-package tutorials.wiki.shader;
-
-import org.lwjgl.input.Keyboard;
-import org.lwjgl.opengl.GL11;
-import org.lwjgl.opengl.Display;
-import org.lwjgl.opengl.DisplayMode;
-import org.lwjgl.util.glu.GLU;
-
-public class Main{
-
-    Box box;
-
-    private boolean done=false;
-
-    public Main(){
-        init();
-        
-        while(!done){
-            if ((Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) || Display.isCloseRequested())
-                done=true;
-            render();
-            Display.update();
-        }
-
-        Display.destroy();
-    }
-
-    private void render(){
-        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
-        GL11.glLoadIdentity();
-        box.draw();
-    }
-
-    private void init(){
-        int w=1024;
-        int h=768;
-    
-        try{
-            Display.setDisplayMode(new DisplayMode(w, h));
-            Display.setVSyncEnabled(true);
-            Display.setTitle("Shader Setup");
-            Display.create();
-        }catch(Exception e){
-            System.out.println("Error setting up display");
-            System.exit(0);
-        }
-
-        GL11.glViewport(0,0,w,h);
-        GL11.glMatrixMode(GL11.GL_PROJECTION);
-        GL11.glLoadIdentity();
-        GLU.gluPerspective(45.0f, ((float)w/(float)h),0.1f,100.0f);
-        GL11.glMatrixMode(GL11.GL_MODELVIEW);
-        GL11.glLoadIdentity();
-        GL11.glShadeModel(GL11.GL_SMOOTH);
-        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
-        GL11.glClearDepth(1.0f);
-        GL11.glEnable(GL11.GL_DEPTH_TEST);
-        GL11.glDepthFunc(GL11.GL_LEQUAL);
-        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
-
-        box = new Box();
-    }
-
-    public static void main(String[] args){
-        new Main();
-    }
-}

src/main/java/tutorials/wiki/shader/shaders/screen.frag

-varying vec4 vertColor;
-
-void main(){
-    gl_FragColor = vertColor;
-}

src/main/java/tutorials/wiki/shader/shaders/screen.vert

-varying vec4 vertColor;
-
-void main(){
-    gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
-    vertColor = vec4(0.6, 0.3, 0.4, 1.0);
-}
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.