Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moved shader creation code to their own util class

  • Loading branch information...
commit 4a0f42d5b690e3aa3dff8ed46007e49a85239b51 1 parent d93126b
@alihelmy authored
View
2  LessonTwo/com/onehandstand/lwjgltutorial/lessontwo/Main.java
@@ -34,7 +34,7 @@ public void start() {
DisplayMode displayMode = new DisplayMode(1280, 800);
Display.setDisplayMode(displayMode);
Display.setVSyncEnabled(true);
- Display.setTitle("Modern OpenGL LWJGL Port Lesson One");
+ Display.setTitle("Modern OpenGL LWJGL Port Lesson Two");
Display.create();
} catch (LWJGLException exception) {
exception.printStackTrace();
View
98 LessonTwo/com/onehandstand/lwjgltutorial/lessontwo/ShaderTriangle.java
@@ -1,27 +1,22 @@
package com.onehandstand.lwjgltutorial.lessontwo;
-import static org.lwjgl.opengl.ARBShaderObjects.*;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.IntBuffer;
-
-import org.lwjgl.BufferUtils;
-import org.lwjgl.opengl.ARBFragmentShader;
-import org.lwjgl.opengl.ARBVertexShader;
-import static org.lwjgl.opengl.GL11.*;
+import static org.lwjgl.opengl.ARBShaderObjects.glAttachObjectARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glCreateProgramObjectARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glLinkProgramARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glUseProgramObjectARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glValidateProgramARB;
+import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
+import static org.lwjgl.opengl.GL11.glBegin;
+import static org.lwjgl.opengl.GL11.glColor3f;
+import static org.lwjgl.opengl.GL11.glEnd;
+import static org.lwjgl.opengl.GL11.glLoadIdentity;
+import static org.lwjgl.opengl.GL11.glVertex2f;
public class ShaderTriangle {
private int shader, vertexShader, fragmentShader;
private boolean useShaders;
- public static boolean verboseValidityCheck=false;
- private static final String SHADERS_ROOT_PATH="shaders";
-
public ShaderTriangle() {
this.shader=0;
this.vertexShader=0;
@@ -34,8 +29,8 @@ private void initShaders() {
//create the main shader program
this.shader=glCreateProgramObjectARB();
if(this.shader!=0) { //if passed, create sub shaders
- this.vertexShader=createVertexShader(SHADERS_ROOT_PATH+File.separator+"screen.vert");
- this.fragmentShader=createFragmentShader(SHADERS_ROOT_PATH+File.separator+"screen.frag");
+ this.vertexShader=ShaderUtilities.createVertexShader("screen.vert");
+ this.fragmentShader=ShaderUtilities.createFragmentShader("screen.frag");
if(this.vertexShader!=0 && this.fragmentShader!=0) {
//add in shaders to main program
glAttachObjectARB(this.shader, this.vertexShader);
@@ -71,71 +66,4 @@ protected void drawVertices() {
glEnd();
}
- private int createFragmentShader(String pathToShaderFile) {
- int fragmentShader=createShader(pathToShaderFile, ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
- return fragmentShader;
- }
-
- private int createVertexShader(String pathToShaderFile) {
- int vertexShader=createShader(pathToShaderFile, ARBVertexShader.GL_VERTEX_SHADER_ARB);
- return vertexShader;
- }
-
- private int createShader(String pathToShaderFile, int shaderType) {
- int newShader=glCreateShaderObjectARB(shaderType);
- if(newShader!=0) {
- String shaderCode=readInShaderCodeFromShaderFile(pathToShaderFile);
- if(shaderCode!=null && !shaderCode.isEmpty()) {
- glShaderSourceARB(newShader, shaderCode);
- glCompileShaderARB(newShader);
- if(isShaderValid(newShader)) {
- return newShader;
- }
- }
- }
- //if anything fails, return 0 as error code
- return 0;
- }
-
- private String readInShaderCodeFromShaderFile(String pathToShaderFile) {
- try {
- BufferedReader bufferedReader=new BufferedReader(new FileReader(pathToShaderFile));
- StringBuffer shaderCodeBuffer=new StringBuffer();
- String codeLine=bufferedReader.readLine();
- while(codeLine!=null) {
- shaderCodeBuffer.append(codeLine+"\n");
- codeLine=bufferedReader.readLine();
- }
- return shaderCodeBuffer.toString();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- }
- }
-
- private boolean isShaderValid(int shaderToCheck) {
- IntBuffer iVal = BufferUtils.createIntBuffer(1);
- glGetObjectParameterARB(shaderToCheck, GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
- int length = iVal.get();
- boolean isValid = length>1;
-
- if (isValid && verboseValidityCheck) {
- printShaderLogInfo(shaderToCheck, iVal, length);
- }
-
- return isValid;
- }
-
- private void printShaderLogInfo(int shaderToCheck, IntBuffer iVal,
- int length) {
- ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
- iVal.flip();
- glGetInfoLogARB(shaderToCheck, iVal, infoLog);
- byte[] infoBytes = new byte[length];
- infoLog.get(infoBytes);
- String out = new String(infoBytes);
- System.out.println("Info log:\n"+out);
- }
-
}
View
94 LessonTwo/com/onehandstand/lwjgltutorial/lessontwo/ShaderUtilities.java
@@ -0,0 +1,94 @@
+package com.onehandstand.lwjgltutorial.lessontwo;
+
+import static org.lwjgl.opengl.ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glCompileShaderARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glCreateShaderObjectARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glGetInfoLogARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glGetObjectParameterARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glShaderSourceARB;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
+
+import org.lwjgl.BufferUtils;
+import org.lwjgl.opengl.ARBFragmentShader;
+import org.lwjgl.opengl.ARBVertexShader;
+
+public class ShaderUtilities {
+
+ private static boolean verboseValidityCheck=false;
+ private static final String SHADERS_ROOT_PATH="shaders";
+
+ public static int createFragmentShader(String pathToShaderFile) {
+ int fragmentShader=createShader(pathToShaderFile, ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
+ return fragmentShader;
+ }
+
+ public static int createVertexShader(String pathToShaderFile) {
+ int vertexShader=createShader(pathToShaderFile, ARBVertexShader.GL_VERTEX_SHADER_ARB);
+ return vertexShader;
+ }
+
+ public static int createShader(String shaderFileName, int shaderType) {
+ String shaderFilePath=SHADERS_ROOT_PATH+File.separator+shaderFileName;
+ int newShader=glCreateShaderObjectARB(shaderType);
+ if(newShader!=0) {
+ String shaderCode=readInShaderCodeFromShaderFile(shaderFileName);
+ if(shaderCode!=null && !shaderCode.isEmpty()) {
+ glShaderSourceARB(newShader, shaderCode);
+ glCompileShaderARB(newShader);
+ if(isShaderValid(newShader)) {
+ return newShader;
+ }
+ }
+ }
+ //if anything fails, return 0 as error code
+ return 0;
+ }
+
+ private static String readInShaderCodeFromShaderFile(String pathToShaderFile) {
+ try {
+ BufferedReader bufferedReader=new BufferedReader(new FileReader(pathToShaderFile));
+ StringBuffer shaderCodeBuffer=new StringBuffer();
+ String codeLine=bufferedReader.readLine();
+ while(codeLine!=null) {
+ shaderCodeBuffer.append(codeLine+"\n");
+ codeLine=bufferedReader.readLine();
+ }
+ return shaderCodeBuffer.toString();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public static boolean isShaderValid(int shaderToCheck) {
+ IntBuffer iVal = BufferUtils.createIntBuffer(1);
+ glGetObjectParameterARB(shaderToCheck, GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
+ int length = iVal.get();
+ boolean isValid = length>1;
+
+ if (isValid && verboseValidityCheck) {
+ printShaderLogInfo(shaderToCheck, iVal, length);
+ }
+
+ return isValid;
+ }
+
+ private static void printShaderLogInfo(int shaderToCheck, IntBuffer iVal,
+ int length) {
+ ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
+ iVal.flip();
+ glGetInfoLogARB(shaderToCheck, iVal, infoLog);
+ byte[] infoBytes = new byte[length];
+ infoLog.get(infoBytes);
+ String out = new String(infoBytes);
+ System.out.println("Info log:\n"+out);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.