Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

eclipse structure

  • Loading branch information...
commit e90193572060152c3fbe8431cf2cc5f245350b24 1 parent 5e6381c
Ali Helmy authored
16 LessonThree/bin/.classpath
... ... @@ -0,0 +1,16 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<classpath>
  3 + <classpathentry kind="src" path=""/>
  4 + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  5 + <classpathentry kind="lib" path="/libs/lwjgl_util.jar">
  6 + <attributes>
  7 + <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="libs/native/windows"/>
  8 + </attributes>
  9 + </classpathentry>
  10 + <classpathentry kind="lib" path="/libs/lwjgl.jar">
  11 + <attributes>
  12 + <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="libs/native/windows"/>
  13 + </attributes>
  14 + </classpathentry>
  15 + <classpathentry kind="output" path="bin"/>
  16 +</classpath>
17 LessonThree/bin/.project
... ... @@ -0,0 +1,17 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<projectDescription>
  3 + <name>LessonThree</name>
  4 + <comment></comment>
  5 + <projects>
  6 + </projects>
  7 + <buildSpec>
  8 + <buildCommand>
  9 + <name>org.eclipse.jdt.core.javabuilder</name>
  10 + <arguments>
  11 + </arguments>
  12 + </buildCommand>
  13 + </buildSpec>
  14 + <natures>
  15 + <nature>org.eclipse.jdt.core.javanature</nature>
  16 + </natures>
  17 +</projectDescription>
8 LessonThree/bin/shaders/screen.frag
... ... @@ -0,0 +1,8 @@
  1 +#version 120
  2 +
  3 +varying vec3 fColor;
  4 +uniform float fade;
  5 +
  6 +void main(void) {
  7 + gl_FragColor = vec4(fColor.x, fColor.y, fColor.z, fade);
  8 +}
11 LessonThree/bin/shaders/screen.vert
... ... @@ -0,0 +1,11 @@
  1 +#version 120
  2 +
  3 +attribute vec2 coord2D;
  4 +attribute vec3 vColor;
  5 +varying vec3 fColor;
  6 +uniform float fade;
  7 +
  8 +void main(void) {
  9 + gl_Position = vec4(coord2D, 0.0, 1.0);
  10 + fColor = vColor;
  11 +}
110 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/Main.java
... ... @@ -1,110 +0,0 @@
1   -package com.onehandstand.lwjgltutorial.lessonthree;
2   -
3   -import static org.lwjgl.opengl.GL11.GL_BLEND;
4   -import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
5   -import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
6   -import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
7   -import static org.lwjgl.opengl.GL11.GL_LEQUAL;
8   -import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
9   -import static org.lwjgl.opengl.GL11.GL_NICEST;
10   -import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
11   -import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT;
12   -import static org.lwjgl.opengl.GL11.GL_PROJECTION;
13   -import static org.lwjgl.opengl.GL11.GL_SMOOTH;
14   -import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA;
15   -import static org.lwjgl.opengl.GL11.glBlendFunc;
16   -import static org.lwjgl.opengl.GL11.glClear;
17   -import static org.lwjgl.opengl.GL11.glClearColor;
18   -import static org.lwjgl.opengl.GL11.glClearDepth;
19   -import static org.lwjgl.opengl.GL11.glDepthFunc;
20   -import static org.lwjgl.opengl.GL11.glEnable;
21   -import static org.lwjgl.opengl.GL11.glHint;
22   -import static org.lwjgl.opengl.GL11.glLoadIdentity;
23   -import static org.lwjgl.opengl.GL11.glMatrixMode;
24   -import static org.lwjgl.opengl.GL11.glShadeModel;
25   -import static org.lwjgl.opengl.GL11.glViewport;
26   -
27   -import org.lwjgl.LWJGLException;
28   -import org.lwjgl.Sys;
29   -import org.lwjgl.opengl.Display;
30   -import org.lwjgl.opengl.DisplayMode;
31   -import org.lwjgl.util.glu.GLU;
32   -
33   -public class Main {
34   -
35   - private ShaderTriangle triangle;
36   -
37   - public void start() {
38   - try {
39   - DisplayMode displayMode=new DisplayMode(640, 480);
40   - Display.setDisplayMode(displayMode);
41   - Display.setVSyncEnabled(true);
42   - Display.setTitle("Modern OpenGL LWJGL Port Lesson Three");
43   - Display.create();
44   - } catch (LWJGLException exception) {
45   - exception.printStackTrace();
46   - System.exit(0);
47   - }
48   -
49   - this.init();
50   -
51   - // main loop
52   - while (!Display.isCloseRequested()) {
53   - long timeElapsed=Sys.getTime();
54   - triangle.update(timeElapsed);
55   - this.render();
56   - Display.update();
57   - Display.sync(60);
58   - }
59   -
60   - // close down
61   - Display.destroy();
62   - System.exit(0);
63   - }
64   -
65   - private void init() {
66   - this.initOpenGL();
67   - this.initWorld();
68   - }
69   -
70   - private void initWorld() {
71   - this.triangle=new ShaderTriangle();
72   - }
73   -
74   - private void render() {
75   - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
76   - glLoadIdentity();
77   - // world rendering code
78   - this.triangle.draw();
79   - }
80   -
81   - private void initOpenGL() {
82   - // setup viewing area
83   - DisplayMode displayMode=Display.getDisplayMode();
84   - int width=displayMode.getWidth();
85   - int height=displayMode.getHeight();
86   - glViewport(0, 0, width, height);
87   - // setup the camera params
88   - glMatrixMode(GL_PROJECTION);
89   - glLoadIdentity();
90   - GLU.gluPerspective(45.0f, (float)width/(float)height, 1.0f, 100f);
91   - // setup the model (drawing) params
92   - glMatrixMode(GL_MODELVIEW);
93   - glLoadIdentity();
94   - glShadeModel(GL_SMOOTH);
95   - glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white
96   - glClearDepth(1.0f);
97   - glEnable(GL_DEPTH_TEST);
98   - glDepthFunc(GL_LEQUAL);
99   - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
100   - // enable blending
101   - glEnable(GL_BLEND);
102   - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
103   - }
104   -
105   - public static void main(String[] args) {
106   - Main lessonOne=new Main();
107   - lessonOne.start();
108   - }
109   -
110   -}
168 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/ShaderTriangle.java
... ... @@ -1,168 +0,0 @@
1   -package com.onehandstand.lwjgltutorial.lessonthree;
2   -
3   -import static org.lwjgl.opengl.ARBBufferObject.GL_STATIC_DRAW_ARB;
4   -import static org.lwjgl.opengl.ARBBufferObject.GL_WRITE_ONLY_ARB;
5   -import static org.lwjgl.opengl.ARBBufferObject.glBindBufferARB;
6   -import static org.lwjgl.opengl.ARBBufferObject.glBufferDataARB;
7   -import static org.lwjgl.opengl.ARBBufferObject.glGenBuffersARB;
8   -import static org.lwjgl.opengl.ARBBufferObject.glMapBufferARB;
9   -import static org.lwjgl.opengl.ARBBufferObject.glUnmapBufferARB;
10   -import static org.lwjgl.opengl.ARBShaderObjects.glAttachObjectARB;
11   -import static org.lwjgl.opengl.ARBShaderObjects.glCreateProgramObjectARB;
12   -import static org.lwjgl.opengl.ARBShaderObjects.glLinkProgramARB;
13   -import static org.lwjgl.opengl.ARBShaderObjects.glUseProgramObjectARB;
14   -import static org.lwjgl.opengl.ARBShaderObjects.glValidateProgramARB;
15   -import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB;
16   -import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB;
17   -import static org.lwjgl.opengl.GL11.GL_FLOAT;
18   -import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
19   -import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
20   -import static org.lwjgl.opengl.GL12.glDrawRangeElements;
21   -import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray;
22   -import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
23   -import static org.lwjgl.opengl.GL20.glGetAttribLocation;
24   -import static org.lwjgl.opengl.GL20.glGetUniformLocation;
25   -import static org.lwjgl.opengl.GL20.glUniform1f;
26   -import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
27   -
28   -import java.nio.ByteBuffer;
29   -
30   -public class ShaderTriangle {
31   -
32   - private int shaderProgram, vertexShader, fragmentShader;
33   - private boolean useShaders;
34   - private int vertexAttributesBuffer, vertexIndicesBuffer;
35   - private int vertexAttributesBufferSize, vertexIndicesBufferSize;
36   - private final int numVertices=3, numAxisPerVertex=2, numColoursPerVertex=3;
37   - private int vColorAttributeIndex, coord2DAttributeIndex,
38   - fadeAttributeIndex;
39   - private final int bytesPerFloat=Float.SIZE/8, bytesPerInt=Integer.SIZE/8;
40   - private int stride;
41   - private float currentFade;
42   -
43   - public ShaderTriangle() {
44   - shaderProgram=0;
45   - vertexShader=0;
46   - fragmentShader=0;
47   - useShaders=false;
48   - initShaders();
49   - initShaderAttributes();
50   - initBuffers();
51   - }
52   -
53   - public void update(long timeElapsed) {
54   - currentFade=(float)(Math.sin(timeElapsed / 1000.0 * (2*3.14) / 5) / 2 + 0.5);
55   - }
56   -
57   - private void initShaderAttributes() {
58   - String coord2dAttributeName="coord2D";
59   - coord2DAttributeIndex=glGetAttribLocation(shaderProgram,
60   - coord2dAttributeName);
61   - String vColorAttributeName="vColor";
62   - vColorAttributeIndex=glGetAttribLocation(shaderProgram,
63   - vColorAttributeName);
64   - String fadeAttributeName="fade";
65   - fadeAttributeIndex=glGetUniformLocation(shaderProgram, fadeAttributeName);
66   - }
67   -
68   - private void initBuffers() {
69   - setupVertexAttributesBuffer();
70   - setupVertexIndecesBuffer();
71   - }
72   -
73   - private void setupVertexIndecesBuffer() {
74   - vertexIndicesBuffer=glGenBuffersARB();
75   - vertexIndicesBufferSize=bytesPerInt*numVertices;
76   - // set size of vertex index buffer
77   - glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
78   - glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBufferSize,
79   - GL_STATIC_DRAW_ARB);
80   - ByteBuffer vertexIndeces=glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
81   - GL_WRITE_ONLY_ARB, vertexIndicesBufferSize, null);
82   - // put in vertex index data
83   - int[] indecesData=new int[] {
84   - 0, 1, 2
85   - };
86   - vertexIndeces.asIntBuffer().put(indecesData);
87   - // flip buffer, unmap and unbind
88   - vertexIndeces.flip();
89   - glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);
90   - glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
91   - }
92   -
93   - private void setupVertexAttributesBuffer() {
94   - vertexAttributesBuffer=glGenBuffersARB();
95   - vertexAttributesBufferSize=bytesPerFloat*numVertices
96   - *(numAxisPerVertex+numColoursPerVertex);
97   - // set size of vertex attributes buffer
98   - glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
99   - glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBufferSize,
100   - GL_STATIC_DRAW_ARB);
101   - ByteBuffer vertexPositionAttributes=glMapBufferARB(GL_ARRAY_BUFFER_ARB,
102   - GL_WRITE_ONLY_ARB, vertexAttributesBufferSize, null);
103   - // put int vertex position & color data
104   - float[] vertexAttributesData=new float[] {
105   - 0.0f, 0.8f, 1.0f, 1.0f, 0.0f, 0.8f, -0.8f, 0.0f, 0.0f, 1.0f,
106   - -0.8f, -0.8f, 1.0f, 0.0f, 0.0f
107   - };
108   - vertexPositionAttributes.asFloatBuffer().put(vertexAttributesData);
109   - // flip buffer, unmap and unbind
110   - vertexPositionAttributes.flip();
111   - glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
112   - glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
113   - // setup the stride and color offset
114   - stride=(numAxisPerVertex+numColoursPerVertex)*bytesPerFloat;
115   - }
116   -
117   - private void initShaders() {
118   - // create the main shader program
119   - shaderProgram=glCreateProgramObjectARB();
120   - if (shaderProgram!=0) { // if passed, create sub shaders
121   - vertexShader=ShaderUtilities.createVertexShader("screen.vert");
122   - fragmentShader=ShaderUtilities.createFragmentShader("screen.frag");
123   - if (vertexShader!=0&&fragmentShader!=0) {
124   - // add in shaders to main program
125   - glAttachObjectARB(shaderProgram, vertexShader);
126   - glAttachObjectARB(shaderProgram, fragmentShader);
127   - glLinkProgramARB(shaderProgram);
128   - glValidateProgramARB(shaderProgram);
129   - useShaders=true;
130   - }
131   - }
132   - }
133   -
134   - public void draw() {
135   - if (useShaders) { // use the shader linked
136   - glUseProgramObjectARB(shaderProgram);
137   - }
138   - // bind vertex data array
139   - glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
140   -
141   - //uniform shader attributes
142   - glUniform1f(fadeAttributeIndex, currentFade);
143   -
144   - // pass in info to vertex shader
145   - int dataOffset=0;
146   - glEnableVertexAttribArray(coord2DAttributeIndex);
147   - glVertexAttribPointer(coord2DAttributeIndex, numAxisPerVertex,
148   - GL_FLOAT, false, stride, 0);
149   - dataOffset+=numAxisPerVertex*bytesPerFloat;
150   - glEnableVertexAttribArray(vColorAttributeIndex);
151   - glVertexAttribPointer(vColorAttributeIndex, numColoursPerVertex,
152   - GL_FLOAT, false, stride, dataOffset);
153   - dataOffset+=numColoursPerVertex*bytesPerFloat;
154   -
155   - // draw the vertices using the indices
156   - glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
157   - glDrawRangeElements(GL_TRIANGLES, 0, numVertices-1, numVertices,
158   - GL_UNSIGNED_INT, 0);
159   -
160   - // unbind the buffers
161   - glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
162   - glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
163   -
164   - glDisableVertexAttribArray(vColorAttributeIndex);
165   -
166   - glUseProgramObjectARB(0);
167   - }
168   -}
98 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/ShaderUtilities.java
... ... @@ -1,98 +0,0 @@
1   -package com.onehandstand.lwjgltutorial.lessonthree;
2   -
3   -import static org.lwjgl.opengl.ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB;
4   -import static org.lwjgl.opengl.ARBShaderObjects.glCompileShaderARB;
5   -import static org.lwjgl.opengl.ARBShaderObjects.glCreateShaderObjectARB;
6   -import static org.lwjgl.opengl.ARBShaderObjects.glGetInfoLogARB;
7   -import static org.lwjgl.opengl.ARBShaderObjects.glGetObjectParameterARB;
8   -import static org.lwjgl.opengl.ARBShaderObjects.glShaderSourceARB;
9   -
10   -import java.io.BufferedReader;
11   -import java.io.File;
12   -import java.io.FileReader;
13   -import java.io.IOException;
14   -import java.nio.ByteBuffer;
15   -import java.nio.IntBuffer;
16   -
17   -import org.lwjgl.BufferUtils;
18   -import org.lwjgl.opengl.ARBFragmentShader;
19   -import org.lwjgl.opengl.ARBVertexShader;
20   -
21   -public class ShaderUtilities {
22   -
23   - private static boolean verboseValidityCheck=false;
24   - private static final String SHADERS_ROOT_PATH="shaders";
25   -
26   - public static int createFragmentShader(String pathToShaderFile) {
27   - int fragmentShader=createShader(pathToShaderFile,
28   - ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
29   - return fragmentShader;
30   - }
31   -
32   - public static int createVertexShader(String pathToShaderFile) {
33   - int vertexShader=createShader(pathToShaderFile,
34   - ARBVertexShader.GL_VERTEX_SHADER_ARB);
35   - return vertexShader;
36   - }
37   -
38   - public static int createShader(String shaderFileName, int shaderType) {
39   - String shaderFilePath=SHADERS_ROOT_PATH+File.separator+shaderFileName;
40   - int newShader=glCreateShaderObjectARB(shaderType);
41   - if (newShader!=0) {
42   - String shaderCode=readInShaderCodeFromShaderFile(shaderFilePath);
43   - if (shaderCode!=null&&!shaderCode.isEmpty()) {
44   - glShaderSourceARB(newShader, shaderCode);
45   - glCompileShaderARB(newShader);
46   - if (isShaderValid(newShader)) {
47   - return newShader;
48   - }
49   - }
50   - }
51   - // if anything fails, return 0 as error code
52   - return 0;
53   - }
54   -
55   - private static String readInShaderCodeFromShaderFile(String pathToShaderFile) {
56   - try {
57   - BufferedReader bufferedReader=new BufferedReader(new FileReader(
58   - pathToShaderFile));
59   - StringBuffer shaderCodeBuffer=new StringBuffer();
60   - String codeLine=bufferedReader.readLine();
61   - while (codeLine!=null) {
62   - shaderCodeBuffer.append(codeLine+"\n");
63   - codeLine=bufferedReader.readLine();
64   - }
65   - return shaderCodeBuffer.toString();
66   - } catch (IOException e) {
67   - // TODO Auto-generated catch block
68   - e.printStackTrace();
69   - return null;
70   - }
71   - }
72   -
73   - public static boolean isShaderValid(int shaderToCheck) {
74   - IntBuffer iVal=BufferUtils.createIntBuffer(1);
75   - glGetObjectParameterARB(shaderToCheck, GL_OBJECT_INFO_LOG_LENGTH_ARB,
76   - iVal);
77   - int length=iVal.get();
78   - boolean isValid=length>1;
79   -
80   - if (isValid&&verboseValidityCheck) {
81   - printShaderLogInfo(shaderToCheck, iVal, length);
82   - }
83   -
84   - return isValid;
85   - }
86   -
87   - private static void printShaderLogInfo(int shaderToCheck, IntBuffer iVal,
88   - int length) {
89   - ByteBuffer infoLog=BufferUtils.createByteBuffer(length);
90   - iVal.flip();
91   - glGetInfoLogARB(shaderToCheck, iVal, infoLog);
92   - byte[] infoBytes=new byte[length];
93   - infoLog.get(infoBytes);
94   - String out=new String(infoBytes);
95   - System.out.println("Info log:\n"+out);
96   - }
97   -
98   -}

0 comments on commit e901935

Please sign in to comment.
Something went wrong with that request. Please try again.