Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added in lesson 5 as copy of lesson 4

  • Loading branch information...
commit 82a945cd6e761014e8de20c6e4aecdc5d45e87cc 1 parent 5da3164
@alihelmy authored
View
110 LessonFive/com/onehandstand/lwjgltutorial/lessonfour/Main.java
@@ -0,0 +1,110 @@
+package com.onehandstand.lwjgltutorial.lessonfour;
+
+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 Four");
+ 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
195 LessonFive/com/onehandstand/lwjgltutorial/lessonfour/ShaderTriangle.java
@@ -0,0 +1,195 @@
+package com.onehandstand.lwjgltutorial.lessonfour;
+
+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.glUniformMatrix4;
+import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
+
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
+
+import org.lwjgl.BufferUtils;
+import org.lwjgl.util.vector.Matrix4f;
+import org.lwjgl.util.vector.Vector3f;
+
+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=3, numColoursPerVertex=3;
+ private int vColorAttributeIndex, coord3DAttributeIndex,
+ fadeAttributeIndex, transformMatrixAttributeIndex;
+ private FloatBuffer transformationValues;
+ 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);
+ float movement=(float)Math.sin(timeElapsed/1000.0 * (2.314)/5);
+ float angle=(float)(timeElapsed/100000.0*45)%360;
+ Vector3f zAxis=new Vector3f(0f, 0f, 1f);
+ Matrix4f translationMatrix=new Matrix4f();
+ translationMatrix.setIdentity();
+ translationMatrix.translate(new Vector3f(movement, 0f, 0f));
+ Matrix4f rotationMatrix=new Matrix4f();
+ rotationMatrix.setIdentity();
+ rotationMatrix.rotate(angle, zAxis);
+ Matrix4f transformationMatrix=Matrix4f.mul(translationMatrix, rotationMatrix, null);
+ transformationValues.clear();
+ transformationMatrix.store(transformationValues);
+ transformationValues.flip();
+ }
+
+ private void initShaderAttributes() {
+ String coord3DAttributeName="coord3D";
+ coord3DAttributeIndex=glGetAttribLocation(shaderProgram,
+ coord3DAttributeName);
+ String vColorAttributeName="vColor";
+ vColorAttributeIndex=glGetAttribLocation(shaderProgram,
+ vColorAttributeName);
+ String fadeAttributeName="fade";
+ fadeAttributeIndex=glGetUniformLocation(shaderProgram,
+ fadeAttributeName);
+ String transformMatrixAttributeName="transformMatrix";
+ transformMatrixAttributeIndex=glGetUniformLocation(shaderProgram,
+ transformMatrixAttributeName);
+ transformationValues=BufferUtils.createFloatBuffer(16);
+ }
+
+ 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, 0.0f, 1.0f, 1.0f, 0.0f,
+ 0.8f, -0.8f, 0.0f, 0.0f, 0.0f, 1.0f,
+ -0.8f, -0.8f, 0.0f, 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);
+ glUniformMatrix4(transformMatrixAttributeIndex, false, transformationValues);
+
+ // pass in info to vertex shader
+ int dataOffset=0;
+ glEnableVertexAttribArray(coord3DAttributeIndex);
+ glVertexAttribPointer(coord3DAttributeIndex, 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
98 LessonFive/com/onehandstand/lwjgltutorial/lessonfour/ShaderUtilities.java
@@ -0,0 +1,98 @@
+package com.onehandstand.lwjgltutorial.lessonfour;
+
+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
9 LessonFive/shaders/screen.frag
@@ -0,0 +1,9 @@
+#version 120
+
+varying vec3 fColor;
+uniform float fade;
+uniform mat4 transformMatrix;
+
+void main(void) {
+ gl_FragColor = vec4(fColor.x, fColor.y, fColor.z, fade);
+}
View
12 LessonFive/shaders/screen.vert
@@ -0,0 +1,12 @@
+#version 120
+
+attribute vec3 coord3D;
+attribute vec3 vColor;
+varying vec3 fColor;
+uniform float fade;
+uniform mat4 transformMatrix;
+
+void main(void) {
+ gl_Position = transformMatrix * vec4(coord3D, 1.0);
+ fColor = vColor;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.