Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

corrected structure to make sure shader files are accessible

  • Loading branch information...
commit b7e21463518876e16b9e9af395944e0840f9872a 1 parent 9364361
@alihelmy authored
View
98 LessonOne/com/onehandstand/lwjgltutorial/lessonone/LessonOne.java
@@ -0,0 +1,98 @@
+package com.onehandstand.lwjgltutorial.lessonone;
+
+import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
+import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
+import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
+import static org.lwjgl.opengl.GL11.GL_LEQUAL;
+import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
+import static org.lwjgl.opengl.GL11.GL_NICEST;
+import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT;
+import static org.lwjgl.opengl.GL11.GL_PROJECTION;
+import static org.lwjgl.opengl.GL11.GL_SMOOTH;
+import static org.lwjgl.opengl.GL11.glClear;
+import static org.lwjgl.opengl.GL11.glClearColor;
+import static org.lwjgl.opengl.GL11.glClearDepth;
+import static org.lwjgl.opengl.GL11.glDepthFunc;
+import static org.lwjgl.opengl.GL11.glEnable;
+import static org.lwjgl.opengl.GL11.glHint;
+import static org.lwjgl.opengl.GL11.glLoadIdentity;
+import static org.lwjgl.opengl.GL11.glMatrixMode;
+import static org.lwjgl.opengl.GL11.glShadeModel;
+import static org.lwjgl.opengl.GL11.glViewport;
+
+import org.lwjgl.LWJGLException;
+import org.lwjgl.opengl.Display;
+import org.lwjgl.opengl.DisplayMode;
+import org.lwjgl.util.glu.GLU;
+
+public class LessonOne {
+
+ private ShaderTriangle triangle;
+
+ public void start() {
+ try {
+ DisplayMode displayMode = new DisplayMode(1280, 800);
+ Display.setDisplayMode(displayMode);
+ Display.setVSyncEnabled(true);
+ Display.setTitle("Modern OpenGL LWJGL Port Lesson One");
+ Display.create();
+ } catch (LWJGLException exception) {
+ exception.printStackTrace();
+ System.exit(0);
+ }
+
+ this.init();
+
+ // main loop
+ while (!Display.isCloseRequested()) {
+ this.render();
+ Display.update();
+ }
+
+ // close down
+ Display.destroy();
+ }
+
+ private void init() {
+ this.initOpenGL();
+ this.initWorld();
+ }
+
+ private void initWorld() {
+ this.triangle=new ShaderTriangle();
+ }
+
+ private void render() {
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glLoadIdentity();
+ //world rendering code
+ this.triangle.draw();
+ }
+
+ private void initOpenGL() {
+ // setup viewing area
+ DisplayMode displayMode = Display.getDisplayMode();
+ int width = displayMode.getWidth();
+ int height = displayMode.getHeight();
+ glViewport(0, 0, width, height);
+ // setup the camera params
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ GLU.gluPerspective(45.0f, (float)width / (float)height, 0.1f, 100f);
+ // setup the model (drawing) params
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glShadeModel(GL_SMOOTH);
+ glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white
+ glClearDepth(1.0f);
+ glEnable(GL_DEPTH_TEST);
+ glDepthFunc(GL_LEQUAL);
+ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
+ }
+
+ public static void main(String[] args) {
+ LessonOne lessonOne = new LessonOne();
+ lessonOne.start();
+ }
+
+}
View
140 LessonOne/com/onehandstand/lwjgltutorial/lessonone/ShaderTriangle.java
@@ -0,0 +1,140 @@
+package com.onehandstand.lwjgltutorial.lessonone;
+
+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.*;
+
+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;
+ this.fragmentShader=0;
+ this.useShaders=false;
+ this.initShaders();
+ }
+
+ 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");
+ if(this.vertexShader!=0 && this.fragmentShader!=0) {
+ //add in shaders to main program
+ glAttachObjectARB(this.shader, this.vertexShader);
+ glAttachObjectARB(this.shader, this.vertexShader);
+ glLinkProgramARB(this.shader);
+ glValidateProgramARB(this.shader);
+ //check if shader is valid
+ if(this.isShaderValid(this.shader)) {
+ this.useShaders=true;
+ }
+ }
+ }
+ }
+
+ public void draw() {
+ if(useShaders) { //use the shader linked
+ glUseProgramObjectARB(this.shader);
+ }
+
+ // normal rendering code
+ glLoadIdentity();
+ //white, which will show if our fragment shader doesn't work
+ glColor3f(1.0f, 1.0f, 1.0f);
+
+ glBegin(GL_TRIANGLES);
+ glVertex2f(0.0f, 0.8f);
+ glVertex2f(-0.8f, -0.8f);
+ glVertex2f(0.8f, -0.8f);
+ glEnd();
+
+ //free up use of shader program
+ glUseProgramObjectARB(0);
+ }
+
+ 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
6 LessonOne/shaders/screen.frag
@@ -0,0 +1,6 @@
+#version 120
+void main(void) {
+ gl_FragColor[0] = 0.0;
+ gl_FragColor[1] = 0.0;
+ gl_FragColor[2] = 1.0;
+}
View
5 LessonOne/shaders/screen.vert
@@ -0,0 +1,5 @@
+#version 120
+attribute vec2 coord2d;
+void main(void) {
+ gl_Position = vec4(coord2d, 0.0, 1.0);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.