Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

eclipse structure to l3

  • Loading branch information...
commit 5e6381cfda20db70e51147e8a7ee2575f776bba6 1 parent aa8fc5f
@alihelmy authored
View
16 LessonThree/.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_util.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.jar">
+ <attributes>
+ <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="libs/native/windows"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
View
17 LessonThree/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>LessonThree</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
110 LessonThree/lwjglTutorial/lessonThree/Main.java
@@ -0,0 +1,110 @@
+package lwjglTutorial.lessonThree;
+
+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.Sys;
+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 Three");
+ Display.create();
+ } catch (LWJGLException exception) {
+ exception.printStackTrace();
+ System.exit(0);
+ }
+
+ this.init();
+
+ // main loop
+ while (!Display.isCloseRequested()) {
+ long timeElapsed=Sys.getTime();
+ triangle.update(timeElapsed);
+ this.render();
+ Display.update();
+ Display.sync(60);
+ }
+
+ // 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
168 LessonThree/lwjglTutorial/lessonThree/ShaderTriangle.java
@@ -0,0 +1,168 @@
+package lwjglTutorial.lessonThree;
+
+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.GL12.glDrawRangeElements;
+import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray;
+import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
+import static org.lwjgl.opengl.GL20.glGetAttribLocation;
+import static org.lwjgl.opengl.GL20.glGetUniformLocation;
+import static org.lwjgl.opengl.GL20.glUniform1f;
+import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
+
+import java.nio.ByteBuffer;
+
+public class ShaderTriangle {
+
+ private int shaderProgram, vertexShader, fragmentShader;
+ private boolean useShaders;
+ private int vertexAttributesBuffer, vertexIndicesBuffer;
+ private int vertexAttributesBufferSize, vertexIndicesBufferSize;
+ private final int numVertices=3, numAxisPerVertex=2, numColoursPerVertex=3;
+ private int vColorAttributeIndex, coord2DAttributeIndex,
+ fadeAttributeIndex;
+ private final int bytesPerFloat=Float.SIZE/8, bytesPerInt=Integer.SIZE/8;
+ private int stride;
+ private float currentFade;
+
+ public ShaderTriangle() {
+ shaderProgram=0;
+ vertexShader=0;
+ fragmentShader=0;
+ useShaders=false;
+ initShaders();
+ initShaderAttributes();
+ initBuffers();
+ }
+
+ public void update(long timeElapsed) {
+ currentFade=(float)(Math.sin(timeElapsed / 1000.0 * (2*3.14) / 5) / 2 + 0.5);
+ }
+
+ private void initShaderAttributes() {
+ String coord2dAttributeName="coord2D";
+ coord2DAttributeIndex=glGetAttribLocation(shaderProgram,
+ coord2dAttributeName);
+ String vColorAttributeName="vColor";
+ vColorAttributeIndex=glGetAttribLocation(shaderProgram,
+ vColorAttributeName);
+ String fadeAttributeName="fade";
+ fadeAttributeIndex=glGetUniformLocation(shaderProgram, fadeAttributeName);
+ }
+
+ private void initBuffers() {
+ setupVertexAttributesBuffer();
+ setupVertexIndecesBuffer();
+ }
+
+ private void setupVertexIndecesBuffer() {
+ vertexIndicesBuffer=glGenBuffersARB();
+ vertexIndicesBufferSize=bytesPerInt*numVertices;
+ // set size of vertex index buffer
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
+ glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBufferSize,
+ GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexIndeces=glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
+ GL_WRITE_ONLY_ARB, vertexIndicesBufferSize, null);
+ // put in vertex index data
+ int[] indecesData=new int[] {
+ 0, 1, 2
+ };
+ vertexIndeces.asIntBuffer().put(indecesData);
+ // flip buffer, unmap and unbind
+ vertexIndeces.flip();
+ glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+ }
+
+ private void setupVertexAttributesBuffer() {
+ vertexAttributesBuffer=glGenBuffersARB();
+ vertexAttributesBufferSize=bytesPerFloat*numVertices
+ *(numAxisPerVertex+numColoursPerVertex);
+ // set size of vertex attributes buffer
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBufferSize,
+ GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexPositionAttributes=glMapBufferARB(GL_ARRAY_BUFFER_ARB,
+ GL_WRITE_ONLY_ARB, vertexAttributesBufferSize, null);
+ // put int vertex position & color data
+ float[] vertexAttributesData=new float[] {
+ 0.0f, 0.8f, 1.0f, 1.0f, 0.0f, 0.8f, -0.8f, 0.0f, 0.0f, 1.0f,
+ -0.8f, -0.8f, 1.0f, 0.0f, 0.0f
+ };
+ vertexPositionAttributes.asFloatBuffer().put(vertexAttributesData);
+ // flip buffer, unmap and unbind
+ vertexPositionAttributes.flip();
+ glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+ // setup the stride and color offset
+ stride=(numAxisPerVertex+numColoursPerVertex)*bytesPerFloat;
+ }
+
+ private void initShaders() {
+ // create the main shader program
+ shaderProgram=glCreateProgramObjectARB();
+ if (shaderProgram!=0) { // if passed, create sub shaders
+ vertexShader=ShaderUtilities.createVertexShader("screen.vert");
+ fragmentShader=ShaderUtilities.createFragmentShader("screen.frag");
+ if (vertexShader!=0&&fragmentShader!=0) {
+ // add in shaders to main program
+ glAttachObjectARB(shaderProgram, vertexShader);
+ glAttachObjectARB(shaderProgram, fragmentShader);
+ glLinkProgramARB(shaderProgram);
+ glValidateProgramARB(shaderProgram);
+ useShaders=true;
+ }
+ }
+ }
+
+ public void draw() {
+ if (useShaders) { // use the shader linked
+ glUseProgramObjectARB(shaderProgram);
+ }
+ // bind vertex data array
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
+
+ //uniform shader attributes
+ glUniform1f(fadeAttributeIndex, currentFade);
+
+ // pass in info to vertex shader
+ int dataOffset=0;
+ glEnableVertexAttribArray(coord2DAttributeIndex);
+ glVertexAttribPointer(coord2DAttributeIndex, numAxisPerVertex,
+ GL_FLOAT, false, stride, 0);
+ dataOffset+=numAxisPerVertex*bytesPerFloat;
+ glEnableVertexAttribArray(vColorAttributeIndex);
+ glVertexAttribPointer(vColorAttributeIndex, numColoursPerVertex,
+ GL_FLOAT, false, stride, dataOffset);
+ dataOffset+=numColoursPerVertex*bytesPerFloat;
+
+ // draw the vertices using the indices
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
+ glDrawRangeElements(GL_TRIANGLES, 0, numVertices-1, numVertices,
+ GL_UNSIGNED_INT, 0);
+
+ // unbind the buffers
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+
+ glDisableVertexAttribArray(vColorAttributeIndex);
+
+ glUseProgramObjectARB(0);
+ }
+}
View
72 LessonThree/lwjglTutorial/lessonThree/ShaderUtilities.java
@@ -0,0 +1,72 @@
+package lwjglTutorial.lessonThree;
+
+import static org.lwjgl.opengl.ARBShaderObjects.glCompileShaderARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glCreateShaderObjectARB;
+import static org.lwjgl.opengl.ARBShaderObjects.glShaderSourceARB;
+import static org.lwjgl.opengl.GL11.GL_TRUE;
+import static org.lwjgl.opengl.GL20.GL_COMPILE_STATUS;
+import static org.lwjgl.opengl.GL20.glGetShader;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+import org.lwjgl.opengl.ARBFragmentShader;
+import org.lwjgl.opengl.ARBVertexShader;
+
+public class ShaderUtilities {
+
+ 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) {
+ int status=glGetShader(shaderToCheck, GL_COMPILE_STATUS);
+ return status == GL_TRUE;
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.