Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added in lesson2 eclipse structure

  • Loading branch information...
commit 0e3a07f4ecbe8a679659c7e902bfe272085d09ba 1 parent e2bccf0
@alihelmy authored
Showing with 940 additions and 11 deletions.
  1. +17 −0 .metadata/.log
  2. +95 −0 .metadata/.plugins/org.eclipse.core.resources/.history/17/700c2efc898c00111f1581251e4956b9
  3. +5 −0 .metadata/.plugins/org.eclipse.core.resources/.history/1c/809bca70898c00111f1581251e4956b9
  4. +106 −0 .metadata/.plugins/org.eclipse.core.resources/.history/25/305948cb898c00111f1581251e4956b9
  5. +94 −0 .metadata/.plugins/org.eclipse.core.resources/.history/33/503646cb898c00111f1581251e4956b9
  6. 0  ...ties.java → .metadata/.plugins/org.eclipse.core.resources/.history/55/0088cebe898c00111f1581251e4956b9
  7. 0  ...Main.java → .metadata/.plugins/org.eclipse.core.resources/.history/63/c03110b9898c00111f1581251e4956b9
  8. +7 −0 .metadata/.plugins/org.eclipse.core.resources/.history/88/009f3073898c00111f1581251e4956b9
  9. +133 −0 .metadata/.plugins/org.eclipse.core.resources/.history/9d/30ca4acb898c00111f1581251e4956b9
  10. +94 −0 .metadata/.plugins/org.eclipse.core.resources/.history/cd/7042baf8898c00111f1581251e4956b9
  11. 0  ...ngle.java → .metadata/.plugins/org.eclipse.core.resources/.history/d1/b029debc898c00111f1581251e4956b9
  12. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonOne/.indexes/2e/history.index
  13. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonOne/.markers.snap
  14. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonOne/.syncinfo.snap
  15. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonTwo/.indexes/3e/f4/history.index
  16. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonTwo/.indexes/81/df/3e/14/history.index
  17. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonTwo/.markers.snap
  18. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonTwo/.syncinfo.snap
  19. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/libs/.markers.snap
  20. BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/libs/.syncinfo.snap
  21. BIN  .metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap
  22. BIN  .metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources
  23. BIN  .metadata/.plugins/org.eclipse.core.resources/.snap
  24. +2 −0  .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs
  25. +2 −0  .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.editors.prefs
  26. +0 −1  .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.prefs
  27. +2 −0  .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs
  28. +11 −0 .metadata/.plugins/org.eclipse.debug.core/.launches/LessonTwo.launch
  29. BIN  .metadata/.plugins/org.eclipse.jdt.core/2791608477.index
  30. +1 −10 .metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt
  31. BIN  .metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat
  32. BIN  .metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png
  33. BIN  .metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png
  34. BIN  .metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png
  35. +4 −0 .metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/LessonTwo/2012/4/16/refactorings.history
  36. +1 −0  .metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/LessonTwo/2012/4/16/refactorings.index
  37. +16 −0 LessonTwo/.classpath
  38. +17 −0 LessonTwo/.project
  39. +106 −0 LessonTwo/lwjgltutorial/lessonTwo/Main.java
  40. +133 −0 LessonTwo/lwjgltutorial/lessonTwo/ShaderTriangle.java
  41. +94 −0 LessonTwo/lwjgltutorial/lessonTwo/ShaderUtilities.java
View
17 .metadata/.log
@@ -8,3 +8,20 @@ Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.e
!ENTRY org.eclipse.core.resources 2 10035 2012-04-22 16:28:51.949
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.
+
+!ENTRY org.eclipse.jdt.ui 4 10001 2012-04-22 16:46:57.956
+!MESSAGE Internal Error
+!STACK 1
+Java Model Exception: Java Model Status [Timed out while retrieving the attached javadoc for BufferedReader [in BufferedReader.class [in java.io [in C:\Program Files\Java\jre6\lib\rt.jar]]] ]
+ at org.eclipse.jdt.internal.core.JavaElement.getURLContents(JavaElement.java:808)
+ at org.eclipse.jdt.internal.core.BinaryType.getJavadocContents(BinaryType.java:1037)
+ at org.eclipse.jdt.internal.core.BinaryMethod.getAttachedJavadoc(BinaryMethod.java:643)
+ at org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2.getHTMLContent(JavadocContentAccess2.java:465)
+ at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.extractJavadoc(ProposalInfo.java:95)
+ at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.computeInfo(ProposalInfo.java:75)
+ at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.getInfo(ProposalInfo.java:58)
+ at org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal.getAdditionalProposalInfo(AbstractJavaCompletionProposal.java:555)
+ at org.eclipse.jface.text.contentassist.AdditionalInfoController$3.run(AdditionalInfoController.java:106)
+ at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
+!SUBENTRY 1 org.eclipse.jdt.core 4 1012 2012-04-22 16:46:57.957
+!MESSAGE Timed out while retrieving the attached javadoc for BufferedReader [in BufferedReader.class [in java.io [in C:\Program Files\Java\jre6\lib\rt.jar]]]
View
95 .metadata/.plugins/org.eclipse.core.resources/.history/17/700c2efc898c00111f1581251e4956b9
@@ -0,0 +1,95 @@
+package 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(shaderFilePath);
+ 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();
+ }
+ bufferedReader.close();
+ 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);
+ }
+
+}
View
5 .metadata/.plugins/org.eclipse.core.resources/.history/1c/809bca70898c00111f1581251e4956b9
@@ -0,0 +1,5 @@
+#version 120
+attribute vec2 coord2d;
+void main(void) {
+ gl_Position = vec4(coord2d, 0.0, 1.0);
+}
View
106 .metadata/.plugins/org.eclipse.core.resources/.history/25/305948cb898c00111f1581251e4956b9
@@ -0,0 +1,106 @@
+package lwjgltutorial.lessonTwo;
+
+import static org.lwjgl.opengl.GL11.GL_BLEND;
+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_ONE_MINUS_SRC_ALPHA;
+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.GL_SRC_ALPHA;
+import static org.lwjgl.opengl.GL11.glBlendFunc;
+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 Main {
+
+ private ShaderTriangle triangle;
+
+ public void start() {
+ try {
+ DisplayMode displayMode = new DisplayMode(640, 480);
+ Display.setDisplayMode(displayMode);
+ Display.setVSyncEnabled(true);
+ Display.setTitle("Modern OpenGL LWJGL Port Lesson Two");
+ 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();
+ System.exit(0);
+ }
+
+ 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, 1.0f, 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);
+ //enable blending
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ }
+
+ public static void main(String[] args) {
+ Main lessonOne = new Main();
+ lessonOne.start();
+ }
+
+}
View
94 .metadata/.plugins/org.eclipse.core.resources/.history/33/503646cb898c00111f1581251e4956b9
@@ -0,0 +1,94 @@
+package 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(shaderFilePath);
+ 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);
+ }
+
+}
View
0  ...gltutorial/lessontwo/ShaderUtilities.java → ...story/55/0088cebe898c00111f1581251e4956b9
File renamed without changes
View
0  ...ndstand/lwjgltutorial/lessontwo/Main.java → ...story/63/c03110b9898c00111f1581251e4956b9
File renamed without changes
View
7 .metadata/.plugins/org.eclipse.core.resources/.history/88/009f3073898c00111f1581251e4956b9
@@ -0,0 +1,7 @@
+#version 120
+
+attribute vec2 coord2d;
+
+void main(void) {
+ gl_Position = vec4(coord2d, 0.0, 1.0);
+}
View
133 .metadata/.plugins/org.eclipse.core.resources/.history/9d/30ca4acb898c00111f1581251e4956b9
@@ -0,0 +1,133 @@
+package lwjgltutorial.lessonTwo;
+
+import static org.lwjgl.opengl.ARBBufferObject.GL_STATIC_DRAW_ARB;
+import static org.lwjgl.opengl.ARBBufferObject.GL_WRITE_ONLY_ARB;
+import static org.lwjgl.opengl.ARBBufferObject.glBindBufferARB;
+import static org.lwjgl.opengl.ARBBufferObject.glBufferDataARB;
+import static org.lwjgl.opengl.ARBBufferObject.glGenBuffersARB;
+import static org.lwjgl.opengl.ARBBufferObject.glMapBufferARB;
+import static org.lwjgl.opengl.ARBBufferObject.glUnmapBufferARB;
+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.ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB;
+import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB;
+import static org.lwjgl.opengl.GL11.GL_FLOAT;
+import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
+import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
+import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
+import static org.lwjgl.opengl.GL11.glDisableClientState;
+import static org.lwjgl.opengl.GL11.glEnableClientState;
+import static org.lwjgl.opengl.GL11.glLoadIdentity;
+import static org.lwjgl.opengl.GL11.glVertexPointer;
+import static org.lwjgl.opengl.GL12.glDrawRangeElements;
+import java.nio.ByteBuffer;
+
+import org.lwjgl.opengl.GL11;
+import org.lwjgl.opengl.GL20;
+
+public class ShaderTriangle {
+
+ private int shader, vertexShader, fragmentShader;
+ private boolean useShaders;
+ private int vboVertexAttributes, vboVertexIndices;
+ private int vboVertexAttributesSize, vboVertexIndicesSize;
+ private final int numVertices=3, numAxisPerVertex=2;
+ private int coord2dAttributeIndex;
+
+ public ShaderTriangle() {
+ this.shader = 0;
+ this.vertexShader = 0;
+ this.fragmentShader = 0;
+ this.useShaders = false;
+ this.initShaders();
+ this.initBuffers();
+
+ String coord2dAttributeName="coord2d";
+ this.coord2dAttributeIndex=GL20.glGetAttribLocation(this.shader, coord2dAttributeName);
+ }
+
+ private void initBuffers() {
+ this.vboVertexAttributes=glGenBuffersARB();
+ this.vboVertexIndices=glGenBuffersARB();
+ int bytesPerFloat=Float.SIZE/8;
+ int bytesPerInt=Integer.SIZE/8;
+ vboVertexAttributesSize=bytesPerFloat*numVertices*numAxisPerVertex;
+ vboVertexIndicesSize=bytesPerInt*numVertices;
+
+ // set size of vertex attributes buffer
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vboVertexAttributesSize, GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexPositionAttributes = glMapBufferARB(
+ GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
+ vboVertexAttributesSize, null);
+ //put int vertex position data
+ float[] vertexPositions=new float[] {
+ 0.0f, 0.8f, //v1
+ 0.8f, -0.8f, //v2
+ -0.8f, -0.8f //v3
+ };
+ vertexPositionAttributes.asFloatBuffer().put(vertexPositions);
+ //flip buffer, unmap and unbind
+ vertexPositionAttributes.flip();
+ glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+
+ // set size of vertex index buffer
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this.vboVertexIndices);
+ glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboVertexIndicesSize, GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexIndeces = glMapBufferARB(
+ GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
+ vboVertexIndicesSize, null);
+ //put in vertex index data
+ int[] indeces=new int[] {0, 1, 2};
+ vertexIndeces.asIntBuffer().put(indeces);
+ //flip buffer, unmap and unbind
+ vertexIndeces.flip();
+ glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+ }
+
+ private void initShaders() {
+ // create the main shader program
+ this.shader = glCreateProgramObjectARB();
+ if (this.shader != 0) { // if passed, create sub shaders
+ 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);
+ glAttachObjectARB(this.shader, this.fragmentShader);
+ glLinkProgramARB(this.shader);
+ glValidateProgramARB(this.shader);
+ this.useShaders = true;
+ }
+ }
+ }
+
+ public void draw() {
+ if(this.useShaders) {
+ glUseProgramObjectARB(this.shader);
+ }
+
+ //bind vertex array
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
+ //pass in coords info to vertex shader
+ GL20.glEnableVertexAttribArray(coord2dAttributeIndex);
+ GL20.glVertexAttribPointer(coord2dAttributeIndex, 2, GL_FLOAT, false, 0, 0);
+
+ //draw the vertices using the indices
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this.vboVertexIndices);
+ glDrawRangeElements(GL_TRIANGLES, 0, this.numVertices-1, this.numVertices, GL_UNSIGNED_INT, 0);
+
+ //unbind the buffers
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+
+ glUseProgramObjectARB(0);
+ }
+}
View
94 .metadata/.plugins/org.eclipse.core.resources/.history/cd/7042baf8898c00111f1581251e4956b9
@@ -0,0 +1,94 @@
+package 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(shaderFilePath);
+ 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);
+ }
+
+}
View
0  ...jgltutorial/lessontwo/ShaderTriangle.java → ...story/d1/b029debc898c00111f1581251e4956b9
File renamed without changes
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonOne/.indexes/2e/history.index
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonOne/.markers.snap
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonOne/.syncinfo.snap
Binary file not shown
View
BIN  ...data/.plugins/org.eclipse.core.resources/.projects/LessonTwo/.indexes/3e/f4/history.index
Binary file not shown
View
BIN  ...plugins/org.eclipse.core.resources/.projects/LessonTwo/.indexes/81/df/3e/14/history.index
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonTwo/.markers.snap
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/LessonTwo/.syncinfo.snap
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/libs/.markers.snap
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.projects/libs/.syncinfo.snap
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.core.resources/.snap
Binary file not shown
View
2  .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs
@@ -6,6 +6,8 @@ fontPropagated=true
org.eclipse.jdt.ui.editor.tab.width=
org.eclipse.jdt.ui.formatterprofiles.version=12
org.eclipse.jdt.ui.javadoclocations.migrated=true
+org.eclipse.jdt.ui.text.custom_templates=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?><templates/>
+org.eclipse.jdt.ui.text.templates_migrated=true
org.eclipse.jface.textfont=1|Consolas|10.0|0|WINDOWS|1|0|0|0|0|0|0|0|0|1|0|0|0|0|Consolas;
proposalOrderMigrated=true
spelling_locale=en_GB
View
2  .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.editors.prefs
@@ -1,2 +1,4 @@
eclipse.preferences.version=1
+lineNumberRuler=true
overviewRuler_migration=migrated_3.1
+spacesForTabs=true
View
1  .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.prefs
@@ -1,3 +1,2 @@
-ENABLE_ANIMATIONS=false
eclipse.preferences.version=1
showIntro=false
View
2  .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs
@@ -1,3 +1,5 @@
ENABLED_DECORATORS=org.eclipse.m2e.core.mavenVersionDecorator\:false,org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator\:true,org.eclipse.jdt.ui.override.decorator\:true,org.eclipse.jdt.ui.interface.decorator\:false,org.eclipse.jdt.ui.buildpath.decorator\:true,org.eclipse.m2e.core.maven2decorator\:true,org.eclipse.mylyn.context.ui.decorator.interest\:true,org.eclipse.mylyn.tasks.ui.decorators.task\:true,org.eclipse.mylyn.team.ui.changeset.decorator\:true,org.eclipse.team.cvs.ui.decorator\:true,org.eclipse.ui.LinkedResourceDecorator\:true,org.eclipse.ui.VirtualResourceDecorator\:true,org.eclipse.ui.ContentTypeDecorator\:true,org.eclipse.ui.ResourceFilterDecorator\:false,org.tigris.subversion.subclipse.ui.decorator\:true,
PLUGINS_NOT_ACTIVATED_ON_STARTUP=org.eclipse.m2e.discovery;
eclipse.preferences.version=1
+editors=<?xml version\="1.0" encoding\="UTF-8"?>\r\n<editors>\r\n<descriptor class\="org.eclipse.jdt.internal.ui.javaeditor.ClassFileEditor" id\="org.eclipse.jdt.ui.ClassFileEditorNoSource" image\="$nl$/icons/full/obj16/classf_obj.gif" internal\="true" label\="Class File Viewer" openMode\="1" open_in_place\="false" plugin\="org.eclipse.jdt.ui"/>\r\n<descriptor id\="org.eclipse.ui.browser.editorSupport" image\="$nl$/icons/obj16/internal_browser.gif" internal\="false" label\="Web Browser" launcher\="org.eclipse.ui.internal.browser.BrowserLauncher" openMode\="4" open_in_place\="false" plugin\="org.eclipse.ui.browser"/>\r\n<descriptor id\="org.eclipse.jdt.ui.JARDescEditor" image\="$nl$/icons/full/obj16/jar_desc_obj.gif" internal\="false" label\="JAR Export Wizard" launcher\="org.eclipse.jdt.internal.ui.jarpackager.OpenJarExportWizardEditorLauncher" openMode\="4" open_in_place\="false" plugin\="org.eclipse.jdt.ui"/>\r\n<descriptor class\="org.eclipse.jdt.internal.debug.ui.snippeteditor.JavaSnippetEditor" id\="org.eclipse.jdt.debug.ui.SnippetEditor" image\="$nl$/icons/full/obj16/jsbook_obj.gif" internal\="true" label\="Scrapbook" openMode\="1" open_in_place\="false" plugin\="org.eclipse.jdt.debug.ui"/>\r\n</editors>
+resourcetypes=<?xml version\="1.0" encoding\="UTF-8"?>\r\n<editors version\="3.1">\r\n<info extension\="class without source" name\="*">\r\n<editor id\="org.eclipse.jdt.ui.ClassFileEditorNoSource"/>\r\n</info>\r\n<info extension\="html" name\="*">\r\n<editor id\="org.eclipse.ui.browser.editorSupport"/>\r\n</info>\r\n<info extension\="htm" name\="*">\r\n<editor id\="org.eclipse.ui.browser.editorSupport"/>\r\n</info>\r\n<info extension\="jardesc" name\="*">\r\n<editor id\="org.eclipse.jdt.ui.JARDescEditor"/>\r\n<defaultEditor id\="org.eclipse.jdt.ui.JARDescEditor"/>\r\n</info>\r\n<info extension\="jpage" name\="*">\r\n<editor id\="org.eclipse.jdt.debug.ui.SnippetEditor"/>\r\n</info>\r\n<info extension\="shtml" name\="*">\r\n<editor id\="org.eclipse.ui.browser.editorSupport"/>\r\n</info>\r\n</editors>
View
11 .metadata/.plugins/org.eclipse.debug.core/.launches/LessonTwo.launch
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
+<listEntry value="/LessonTwo/lwjgltutorial/lessonTwo/Main.java"/>
+</listAttribute>
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
+<listEntry value="1"/>
+</listAttribute>
+<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="lwjgltutorial.lessonTwo.Main"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="LessonTwo"/>
+</launchConfiguration>
View
BIN  .metadata/.plugins/org.eclipse.jdt.core/2791608477.index
Binary file not shown
View
11 .metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt
@@ -1,13 +1,4 @@
INDEX VERSION 1.126+C:\Users\ali\Dropbox\work\lwjglTutorial\.metadata\.plugins\org.eclipse.jdt.core
-1195944079.index
-1789169078.index
-3730526690.index
3452083210.index
-4251467834.index
-3585733931.index
-3277441100.index
-2877109961.index
-3270002986.index
-3000053649.index
-4088397835.index
2481996896.index
+2791608477.index
View
BIN  .metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat
Binary file not shown
View
BIN  .metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  .metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  .metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
4 ...s/org.eclipse.ltk.core.refactoring/.refactorings/LessonTwo/2012/4/16/refactorings.history
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<session version="1.0">
+<refactoring comment="Rename package &apos;com.onehandstand.lwjgltutorial.lessontwo&apos; to &apos;lwjgltutorial.lessonTwo&apos;&#x0D;&#x0A;- Original project: &apos;LessonTwo&apos;&#x0D;&#x0A;- Original element: &apos;LessonTwo/com.onehandstand.lwjgltutorial.lessontwo&apos;&#x0D;&#x0A;- Renamed element: &apos;LessonTwo/lwjgltutorial.lessonTwo&apos;&#x0D;&#x0A;- Update references to refactored element&#x0D;&#x0A;- Update textual occurrences in comments and strings" description="Rename package &apos;com.onehandstand.lwjgltutorial.lessontwo&apos;" flags="589830" hierarchical="false" id="org.eclipse.jdt.ui.rename.package" input="/&lt;com.onehandstand.lwjgltutorial.lessontwo" name="lwjgltutorial.lessonTwo" qualified="false" references="true" stamp="1335105924722" textual="false" version="1.0"/>
+</session>
View
1  ...ins/org.eclipse.ltk.core.refactoring/.refactorings/LessonTwo/2012/4/16/refactorings.index
@@ -0,0 +1 @@
+1335105924722 Rename package 'com.onehandstand.lwjgltutorial.lessontwo'
View
16 LessonTwo/.classpath
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path=""/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="lib" path="/libs/lwjgl.jar">
+ <attributes>
+ <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="libs/native/windows"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="lib" path="/libs/lwjgl_util.jar">
+ <attributes>
+ <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="libs/native/windows"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="output" path=""/>
+</classpath>
View
17 LessonTwo/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>LessonTwo</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
View
106 LessonTwo/lwjgltutorial/lessonTwo/Main.java
@@ -0,0 +1,106 @@
+package lwjgltutorial.lessonTwo;
+
+import static org.lwjgl.opengl.GL11.GL_BLEND;
+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_ONE_MINUS_SRC_ALPHA;
+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.GL_SRC_ALPHA;
+import static org.lwjgl.opengl.GL11.glBlendFunc;
+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 Main {
+
+ private ShaderTriangle triangle;
+
+ public void start() {
+ try {
+ DisplayMode displayMode = new DisplayMode(640, 480);
+ Display.setDisplayMode(displayMode);
+ Display.setVSyncEnabled(true);
+ Display.setTitle("Modern OpenGL LWJGL Port Lesson Two");
+ 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();
+ System.exit(0);
+ }
+
+ 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, 1.0f, 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);
+ //enable blending
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ }
+
+ public static void main(String[] args) {
+ Main lessonOne = new Main();
+ lessonOne.start();
+ }
+
+}
View
133 LessonTwo/lwjgltutorial/lessonTwo/ShaderTriangle.java
@@ -0,0 +1,133 @@
+package lwjgltutorial.lessonTwo;
+
+import static org.lwjgl.opengl.ARBBufferObject.GL_STATIC_DRAW_ARB;
+import static org.lwjgl.opengl.ARBBufferObject.GL_WRITE_ONLY_ARB;
+import static org.lwjgl.opengl.ARBBufferObject.glBindBufferARB;
+import static org.lwjgl.opengl.ARBBufferObject.glBufferDataARB;
+import static org.lwjgl.opengl.ARBBufferObject.glGenBuffersARB;
+import static org.lwjgl.opengl.ARBBufferObject.glMapBufferARB;
+import static org.lwjgl.opengl.ARBBufferObject.glUnmapBufferARB;
+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.ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB;
+import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB;
+import static org.lwjgl.opengl.GL11.GL_FLOAT;
+import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
+import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
+import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
+import static org.lwjgl.opengl.GL11.glDisableClientState;
+import static org.lwjgl.opengl.GL11.glEnableClientState;
+import static org.lwjgl.opengl.GL11.glLoadIdentity;
+import static org.lwjgl.opengl.GL11.glVertexPointer;
+import static org.lwjgl.opengl.GL12.glDrawRangeElements;
+import java.nio.ByteBuffer;
+
+import org.lwjgl.opengl.GL11;
+import org.lwjgl.opengl.GL20;
+
+public class ShaderTriangle {
+
+ private int shader, vertexShader, fragmentShader;
+ private boolean useShaders;
+ private int vboVertexAttributes, vboVertexIndices;
+ private int vboVertexAttributesSize, vboVertexIndicesSize;
+ private final int numVertices=3, numAxisPerVertex=2;
+ private int coord2dAttributeIndex;
+
+ public ShaderTriangle() {
+ this.shader = 0;
+ this.vertexShader = 0;
+ this.fragmentShader = 0;
+ this.useShaders = false;
+ this.initShaders();
+ this.initBuffers();
+
+ String coord2dAttributeName="coord2d";
+ this.coord2dAttributeIndex=GL20.glGetAttribLocation(this.shader, coord2dAttributeName);
+ }
+
+ private void initBuffers() {
+ this.vboVertexAttributes=glGenBuffersARB();
+ this.vboVertexIndices=glGenBuffersARB();
+ int bytesPerFloat=Float.SIZE/8;
+ int bytesPerInt=Integer.SIZE/8;
+ vboVertexAttributesSize=bytesPerFloat*numVertices*numAxisPerVertex;
+ vboVertexIndicesSize=bytesPerInt*numVertices;
+
+ // set size of vertex attributes buffer
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vboVertexAttributesSize, GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexPositionAttributes = glMapBufferARB(
+ GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
+ vboVertexAttributesSize, null);
+ //put int vertex position data
+ float[] vertexPositions=new float[] {
+ 0.0f, 0.8f, //v1
+ 0.8f, -0.8f, //v2
+ -0.8f, -0.8f //v3
+ };
+ vertexPositionAttributes.asFloatBuffer().put(vertexPositions);
+ //flip buffer, unmap and unbind
+ vertexPositionAttributes.flip();
+ glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+
+ // set size of vertex index buffer
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this.vboVertexIndices);
+ glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboVertexIndicesSize, GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexIndeces = glMapBufferARB(
+ GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
+ vboVertexIndicesSize, null);
+ //put in vertex index data
+ int[] indeces=new int[] {0, 1, 2};
+ vertexIndeces.asIntBuffer().put(indeces);
+ //flip buffer, unmap and unbind
+ vertexIndeces.flip();
+ glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+ }
+
+ private void initShaders() {
+ // create the main shader program
+ this.shader = glCreateProgramObjectARB();
+ if (this.shader != 0) { // if passed, create sub shaders
+ 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);
+ glAttachObjectARB(this.shader, this.fragmentShader);
+ glLinkProgramARB(this.shader);
+ glValidateProgramARB(this.shader);
+ this.useShaders = true;
+ }
+ }
+ }
+
+ public void draw() {
+ if(this.useShaders) {
+ glUseProgramObjectARB(this.shader);
+ }
+
+ //bind vertex array
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
+ //pass in coords info to vertex shader
+ GL20.glEnableVertexAttribArray(coord2dAttributeIndex);
+ GL20.glVertexAttribPointer(coord2dAttributeIndex, 2, GL_FLOAT, false, 0, 0);
+
+ //draw the vertices using the indices
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this.vboVertexIndices);
+ glDrawRangeElements(GL_TRIANGLES, 0, this.numVertices-1, this.numVertices, GL_UNSIGNED_INT, 0);
+
+ //unbind the buffers
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+
+ glUseProgramObjectARB(0);
+ }
+}
View
94 LessonTwo/lwjgltutorial/lessonTwo/ShaderUtilities.java
@@ -0,0 +1,94 @@
+package 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(shaderFilePath);
+ 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.