Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

copy of lesson six

  • Loading branch information...
commit 15eff278b43bd6c19743882d80cd3e5135de15b0 1 parent a8d0e97
Ali Helmy authored
26 LessonFive/com/onehandstand/lwjgltutorial/lessonfive/Main.java
View
@@ -22,28 +22,34 @@
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glViewport;
-import static org.lwjgl.util.glu.GLU.gluPerspective;
import static org.lwjgl.util.glu.GLU.gluLookAt;
+import static org.lwjgl.util.glu.GLU.gluPerspective;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
+import org.lwjgl.input.Keyboard;
+import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
-import org.lwjgl.util.glu.GLU;
+import org.lwjgl.util.vector.Vector2f;
+import org.lwjgl.util.vector.Vector3f;
public class Main {
private Cube cube;
private int shaderProgram, vertexShader, fragmentShader;
private boolean useShaders;
+ private Vector3f cameraPosition, cameraDirection;
+ private Vector2f pointerPosition;
public void start() {
try {
- DisplayMode displayMode=new DisplayMode(640, 480);
+ DisplayMode displayMode=new DisplayMode(800, 600);
Display.setDisplayMode(displayMode);
Display.setVSyncEnabled(true);
Display.setTitle("Modern OpenGL LWJGL Port Lesson Five");
Display.create();
+ pointerPosition=new Vector2f(0,0);
} catch (LWJGLException exception) {
exception.printStackTrace();
System.exit(0);
@@ -55,6 +61,9 @@ public void start() {
while (!Display.isCloseRequested()) {
long timeElapsed=Sys.getTime();
cube.update(timeElapsed);
+
+ readInUserInput();
+
render();
Display.update();
Display.sync(60);
@@ -65,6 +74,10 @@ public void start() {
System.exit(0);
}
+ private void readInUserInput() {
+ // TODO
+ }
+
private void init() {
initOpenGL();
initShaders();
@@ -92,6 +105,9 @@ private void initShaders() {
}
private void initWorld() {
+ cameraPosition=new Vector3f(0,2,0);
+ cameraDirection=new Vector3f(0, 0, -4);
+
if(useShaders) {
Cube.setShaderProgram(shaderProgram);
}
@@ -104,7 +120,9 @@ private void render() {
//camera adjustment
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
- gluLookAt(0, 2, 0, 0, 0, -4, 0, 1, 0);
+ gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
+ cameraDirection.x, cameraDirection.y, cameraDirection.z,
+ 0, 1, 0);
// world rendering code
cube.draw();
2  LessonFive/shaders/screen.frag
View
@@ -5,5 +5,5 @@ uniform vec3 modelRotation;
varying vec3 fragmentColor;
void main(void) {
- gl_FragColor=vec4(fragmentColor,1.0);
+ gl_FragColor=vec4(fragmentColor.x,fragmentColor.y,fragmentColor.z,1.0);
}
2  LessonFive/shaders/screen.vert
View
@@ -30,7 +30,7 @@ void main(void) {
mat4x4 rotationMatrix=mat4x4(1.0);
rotationMatrix=heading*pitch*roll;
- gl_Position=gl_ModelViewProjectionMatrix*positionMatrix*rotationMatrix*vec4(coords,1.0);
+ gl_Position=gl_ModelViewProjectionMatrix*positionMatrix*rotationMatrix*vec4(coords, 1.0);
fragmentColor=color;
}
164 LessonSix/com/onehandstand/lwjgltutorial/lessonsix/Cube.java
View
@@ -0,0 +1,164 @@
+package com.onehandstand.lwjgltutorial.lessonsix;
+
+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.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.GL20.glDisableVertexAttribArray;
+import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
+import static org.lwjgl.opengl.GL20.glGetAttribLocation;
+import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
+
+import java.nio.ByteBuffer;
+
+import org.lwjgl.opengl.ARBShaderObjects;
+import org.lwjgl.opengl.GL11;
+import org.lwjgl.util.vector.Vector3f;
+
+public class Cube {
+
+ private int vertexAttributesBuffer, vertexIndicesBuffer;
+ private int vertexAttributesBufferSize, vertexIndicesBufferSize;
+ private final int numVertices=8, numAxisPerVertex=3, numColoursPerVertex=3;
+ private int numIndeces;
+ private int colorAttributeIndex, coordsAttributeIndex,
+ modelPositionAttributeIndex, modelRotationAttributeIndex;
+ private final int bytesPerFloat=Float.SIZE/8, bytesPerInt=Integer.SIZE/8;
+ private int stride;
+ private Vector3f position, rotation;
+ private static int shader;
+
+ public Cube() {
+ position=new Vector3f(0f, 0f, -4f);
+ rotation=new Vector3f(0f, 0f, 0f);
+ initShaderAttributes();
+ initBuffers();
+ }
+
+ public void update(long timeElapsed) {
+ rotation.y-=(45.0/6000.0);
+ }
+
+ private void initShaderAttributes() {
+ String coordsAttributeName="coords";
+ coordsAttributeIndex=glGetAttribLocation(shader, coordsAttributeName);
+ String colorAttributeName="color";
+ colorAttributeIndex=glGetAttribLocation(shader, colorAttributeName);
+ String modelPositionAttributeName="modelPosition";
+ modelPositionAttributeIndex=ARBShaderObjects.glGetUniformLocationARB(shader, modelPositionAttributeName);
+ String modelRotationAttributeName="modelRotation";
+ modelRotationAttributeIndex=ARBShaderObjects.glGetUniformLocationARB(shader, modelRotationAttributeName);
+ }
+
+ private void initBuffers() {
+ setupVertexAttributesBuffer();
+ setupVertexIndecesBuffer();
+ }
+
+ private void setupVertexIndecesBuffer() {
+ vertexIndicesBuffer=glGenBuffersARB();
+ // put in vertex index data
+ int[] indecesData=new int[] {
+ // front
+ 0, 1, 2, 2, 3, 0,
+ // top
+ 1, 5, 6, 6, 2, 1,
+ // back
+ 7, 6, 5, 5, 4, 7,
+ // bottom
+ 4, 0, 3, 3, 7, 4,
+ // left
+ 4, 5, 1, 1, 0, 4,
+ // right
+ 3, 2, 6, 6, 7, 3,
+ };
+ // set index count for later rendering
+ numIndeces=indecesData.length;
+ vertexIndicesBufferSize=bytesPerInt*numIndeces;
+ // 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);
+ 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[] {
+ // front
+ -1f, -1f, 1f, 1f, 0f, 0f,
+ 1f, -1f, 1f, 0f, 1f, 0f,
+ 1f, 1f, 1f, 0f, 0f, 1f,
+ -1f, 1f, 1f, 1f, 1f, 1f,
+ // back
+ -1f, -1f, -1f, 1f, 0f, 0f,
+ 1f, -1f, -1f, 0f, 1f, 0f,
+ 1f, 1f, -1f, 0f, 0f, 1f,
+ -1f, 1f, -1f, 1f, 1f, 1f,
+ };
+ 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;
+ }
+
+ public void draw() {
+ // bind vertex data array
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
+
+ //bind the uniform attributes
+ ARBShaderObjects.glUniform3fARB(modelPositionAttributeIndex, position.x, position.y, position.z);
+ ARBShaderObjects.glUniform3fARB(modelRotationAttributeIndex, rotation.x, rotation.y, rotation.z);
+
+ // pass in info to vertex shader
+ int dataOffset=0;
+ glEnableVertexAttribArray(coordsAttributeIndex);
+ glVertexAttribPointer(coordsAttributeIndex, numAxisPerVertex,
+ GL_FLOAT, false, stride, 0);
+ dataOffset+=numAxisPerVertex*bytesPerFloat;
+ glEnableVertexAttribArray(colorAttributeIndex);
+ glVertexAttribPointer(colorAttributeIndex, numColoursPerVertex,
+ GL_FLOAT, false, stride, dataOffset);
+ dataOffset+=numColoursPerVertex*bytesPerFloat;
+
+ // draw the vertices using the indices
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
+ GL11.glDrawElements(GL_TRIANGLES, numIndeces, GL_UNSIGNED_INT, 0);
+
+ // unbind the buffers
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+
+ glDisableVertexAttribArray(colorAttributeIndex);
+ }
+
+ public static void setShaderProgram(int shaderProgram) {
+ shader=shaderProgram;
+ }
+}
155 LessonSix/com/onehandstand/lwjgltutorial/lessonsix/Main.java
View
@@ -0,0 +1,155 @@
+package com.onehandstand.lwjgltutorial.lessonsix;
+
+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.GL11.GL_COLOR_BUFFER_BIT;
+import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
+import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
+import static org.lwjgl.opengl.GL11.GL_LEQUAL;
+import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
+import static org.lwjgl.opengl.GL11.GL_NICEST;
+import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT;
+import static org.lwjgl.opengl.GL11.GL_PROJECTION;
+import static org.lwjgl.opengl.GL11.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.glViewport;
+import static org.lwjgl.util.glu.GLU.gluLookAt;
+import static org.lwjgl.util.glu.GLU.gluPerspective;
+
+import org.lwjgl.LWJGLException;
+import org.lwjgl.Sys;
+import org.lwjgl.input.Keyboard;
+import org.lwjgl.input.Mouse;
+import org.lwjgl.opengl.Display;
+import org.lwjgl.opengl.DisplayMode;
+import org.lwjgl.util.vector.Vector2f;
+import org.lwjgl.util.vector.Vector3f;
+
+public class Main {
+
+ private Cube cube;
+ private int shaderProgram, vertexShader, fragmentShader;
+ private boolean useShaders;
+ private Vector3f cameraPosition, cameraDirection;
+ private Vector2f pointerPosition;
+
+ public void start() {
+ try {
+ DisplayMode displayMode=new DisplayMode(800, 600);
+ Display.setDisplayMode(displayMode);
+ Display.setVSyncEnabled(true);
+ Display.setTitle("Modern OpenGL LWJGL Port Lesson Six");
+ Display.create();
+ pointerPosition=new Vector2f(0,0);
+ } catch (LWJGLException exception) {
+ exception.printStackTrace();
+ System.exit(0);
+ }
+
+ init();
+
+ // main loop
+ while (!Display.isCloseRequested()) {
+ long timeElapsed=Sys.getTime();
+ cube.update(timeElapsed);
+
+ readInUserInput();
+
+ render();
+ Display.update();
+ Display.sync(60);
+ }
+
+ // close down
+ Display.destroy();
+ System.exit(0);
+ }
+
+ private void readInUserInput() {
+ // TODO
+ }
+
+ private void init() {
+ initOpenGL();
+ initShaders();
+ initWorld();
+ }
+
+ 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;
+ }
+ }
+ if (useShaders) { // use the shader linked
+ glUseProgramObjectARB(shaderProgram);
+ }
+ }
+
+ private void initWorld() {
+ cameraPosition=new Vector3f(0, 2, 2);
+ cameraDirection=new Vector3f(0, 0, -4);
+
+ if(useShaders) {
+ Cube.setShaderProgram(shaderProgram);
+ }
+ cube=new Cube();
+ }
+
+ private void render() {
+ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+
+ //camera adjustment
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
+ cameraDirection.x, cameraDirection.y, cameraDirection.z,
+ 0, 1, 0);
+
+ // world rendering code
+ cube.draw();
+ }
+
+ private void initOpenGL() {
+ // setup viewing area
+ DisplayMode displayMode=Display.getDisplayMode();
+ int width=displayMode.getWidth();
+ int height=displayMode.getHeight();
+ glViewport(0, 0, width, height);
+
+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // white
+ glClearDepth(1.0f);
+ glEnable(GL_DEPTH_TEST);
+ glDepthFunc(GL_LEQUAL);
+ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
+
+ // setup the lens params [projection matrix]
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(45.0f, (float)width/(float)height, 0.1f, 10f);
+ }
+
+ public static void main(String[] args) {
+ Main main=new Main();
+ main.start();
+ }
+
+}
98 LessonSix/com/onehandstand/lwjgltutorial/lessonsix/ShaderUtilities.java
View
@@ -0,0 +1,98 @@
+package com.onehandstand.lwjgltutorial.lessonsix;
+
+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);
+ }
+
+}
9 LessonSix/shaders/screen.frag
View
@@ -0,0 +1,9 @@
+#version 120
+
+uniform vec3 modelPosition;
+uniform vec3 modelRotation;
+varying vec3 fragmentColor;
+
+void main(void) {
+ gl_FragColor=vec4(fragmentColor.x,fragmentColor.y,fragmentColor.z,1.0);
+}
36 LessonSix/shaders/screen.vert
View
@@ -0,0 +1,36 @@
+#version 120
+
+attribute vec3 coords;
+attribute vec3 color;
+uniform vec3 modelPosition;
+uniform vec3 modelRotation;
+varying vec3 fragmentColor;
+
+void main(void) {
+ mat4x4 positionMatrix=mat4x4(1.0);
+ positionMatrix[3].x=modelPosition.x;
+ positionMatrix[3].y=modelPosition.y;
+ positionMatrix[3].z=modelPosition.z;
+
+ mat4x4 heading=mat4x4(1.0);
+ heading[0][0]=cos(modelRotation.y);
+ heading[0][2]=-(sin(modelRotation.y));
+ heading[2][0]=sin(modelRotation.y);
+ heading[2][2]=cos(modelRotation.y);
+ mat4x4 pitch=mat4x4(1.0);
+ pitch[1][1]=cos(modelRotation.x);
+ pitch[1][2]=sin(modelRotation.x);
+ pitch[2][1]=-(sin(modelRotation.x));
+ pitch[2][2]=cos(modelRotation.x);
+ mat4x4 roll=mat4x4(1.0);
+ roll[0][0]=cos(modelRotation.z);
+ roll[0][1]=sin(modelRotation.z);
+ roll[1][0]=-(sin(modelRotation.z));
+ roll[1][1]=cos(modelRotation.z);
+ mat4x4 rotationMatrix=mat4x4(1.0);
+ rotationMatrix=heading*pitch*roll;
+
+ gl_Position=gl_ModelViewProjectionMatrix*positionMatrix*rotationMatrix*vec4(coords, 1.0);
+
+ fragmentColor=color;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.