Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

latest

  • Loading branch information...
commit 7ffda27da92751b8f1d950db943e88a88313640b 1 parent 14669ad
@alihelmy authored
View
198 LessonFive/com/onehandstand/lwjgltutorial/lessonfive/Cube.java
@@ -1,116 +1,116 @@
package com.onehandstand.lwjgltutorial.lessonfive;
-import static org.lwjgl.opengl.ARBBufferObject.*;
-import static org.lwjgl.opengl.ARBShaderObjects.*;
-import static org.lwjgl.opengl.ARBVertexBufferObject.*;
-import static org.lwjgl.opengl.GL11.*;
+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.GL20.*;
+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.glGetUniformLocation;
+import static org.lwjgl.opengl.GL20.glUniform3;
+import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
-import org.lwjgl.opengl.ARBBufferObject;
import org.lwjgl.opengl.GL11;
-import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
public class Cube {
- private int shaderProgram, vertexShader, fragmentShader;
- private boolean useShaders;
private int vertexAttributesBuffer, vertexIndicesBuffer;
private int vertexAttributesBufferSize, vertexIndicesBufferSize;
private final int numVertices=8, numAxisPerVertex=3, numColoursPerVertex=3;
- private int vColorAttributeIndex, coord3DAttributeIndex,
- fadeAttributeIndex, transformMatrixAttributeIndex;
- private FloatBuffer transformationValues;
+ private int numIndeces;
+ private int colorAttributeIndex, coordsAttributeIndex,
+ positionAttributeIndex, rotationAttributeIndex;
private final int bytesPerFloat=Float.SIZE/8, bytesPerInt=Integer.SIZE/8;
private int stride;
- private float currentFade;
+ private Vector3f position, rotation;
+ private FloatBuffer positionValues, rotationValues;
+ private static int shader;
public Cube() {
- shaderProgram=0;
- vertexShader=0;
- fragmentShader=0;
- useShaders=false;
- initShaders();
+ position=new Vector3f(0f, 0f, -4f);
+ rotation=new Vector3f(0f, 0f, 0f);
initShaderAttributes();
initBuffers();
}
public void update(long timeElapsed) {
- currentFade=1.0f;// (float)(Math.sin(timeElapsed/1000.0*(2*3.14)/5)/2+0.5);
- float movement=0.0f;// (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();
+ // TODO update rotation
+
+ // load up position buffer
+ positionValues.clear();
+ position.store(positionValues);
+ positionValues.flip();
+
+ // load up rotatio buffer
+ rotationValues.clear();
+ rotation.store(rotationValues);
+ rotationValues.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);
+ String coordsAttributeName="coords";
+ coordsAttributeIndex=glGetAttribLocation(shader, coordsAttributeName);
+ String colorAttributeName="color";
+ colorAttributeIndex=glGetAttribLocation(shader, colorAttributeName);
+ String positionAttributeName="modelPosition";
+ positionAttributeIndex=glGetUniformLocation(shader,
+ positionAttributeName);
+ String rotationAttributeName="modelRotation";
+ rotationAttributeIndex=glGetUniformLocation(shader,
+ rotationAttributeName);
}
private void initBuffers() {
setupVertexAttributesBuffer();
setupVertexIndecesBuffer();
+ setupTransformationBuffers();
+ }
+
+ private void setupTransformationBuffers() {
+ positionValues=BufferUtils.createFloatBuffer(3);
+ rotationValues=BufferUtils.createFloatBuffer(3); // x,y,z rotations
}
private void setupVertexIndecesBuffer() {
vertexIndicesBuffer=glGenBuffersARB();
- vertexIndicesBufferSize=bytesPerInt*3*2*6; // 3 vertices per triangle,
- // 2 triangles per face,
- // 6 faces
- // 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[] {
// front
- 0, 1, 2,
- 2, 3, 0,
+ 0, 1, 2, 2, 3, 0,
// top
- 1, 5, 6,
- 6, 2, 1,
+ 1, 5, 6, 6, 2, 1,
// back
- 7, 6, 5,
- 5, 4, 7,
+ 7, 6, 5, 5, 4, 7,
// bottom
- 4, 0, 3,
- 3, 7, 4,
+ 4, 0, 3, 3, 7, 4,
// left
- 4, 5, 1,
- 1, 0, 4,
+ 4, 5, 1, 1, 0, 4,
// right
- 3, 2, 6,
- 6, 7, 3,
+ 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();
@@ -131,15 +131,15 @@ private void setupVertexAttributesBuffer() {
// put int vertex position & color data
float[] vertexAttributesData=new float[] {
// front
- -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
- 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
- 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
- -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
+ -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
- -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
- 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
- 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
- -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
+ -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
@@ -150,61 +150,37 @@ private void setupVertexAttributesBuffer() {
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);
+ // bind shader attributes
+ glUniform3(positionAttributeIndex, positionValues);
+ glUniform3(rotationAttributeIndex, rotationValues);
// pass in info to vertex shader
int dataOffset=0;
- glEnableVertexAttribArray(coord3DAttributeIndex);
- glVertexAttribPointer(coord3DAttributeIndex, numAxisPerVertex,
+ glEnableVertexAttribArray(coordsAttributeIndex);
+ glVertexAttribPointer(coordsAttributeIndex, numAxisPerVertex,
GL_FLOAT, false, stride, 0);
dataOffset+=numAxisPerVertex*bytesPerFloat;
- glEnableVertexAttribArray(vColorAttributeIndex);
- glVertexAttribPointer(vColorAttributeIndex, numColoursPerVertex,
+ 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);
- // get index buffer size
- int indecesBufferSize=ARBBufferObject
- .glGetBufferParameterARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
- ARBBufferObject.GL_BUFFER_SIZE_ARB);
- GL11.glDrawElements(GL_TRIANGLES, indecesBufferSize/bytesPerInt,
- GL_UNSIGNED_INT, 0);
+ 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(vColorAttributeIndex);
+ glDisableVertexAttribArray(colorAttributeIndex);
+ }
- glUseProgramObjectARB(0);
+ public static void setShaderProgram(int shaderProgram) {
+ shader=shaderProgram;
}
}
View
50 LessonFive/com/onehandstand/lwjgltutorial/lessonfive/Main.java
@@ -1,10 +1,14 @@
package com.onehandstand.lwjgltutorial.lessonfive;
+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_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;
@@ -16,7 +20,6 @@
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;
@@ -28,12 +31,13 @@
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
-import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
public class Main {
private Cube cube;
+ private int shaderProgram, vertexShader, fragmentShader;
+ private boolean useShaders;
public void start() {
try {
@@ -47,13 +51,13 @@ public void start() {
System.exit(0);
}
- this.init();
+ init();
// main loop
while (!Display.isCloseRequested()) {
long timeElapsed=Sys.getTime();
cube.update(timeElapsed);
- this.render();
+ render();
Display.update();
Display.sync(60);
}
@@ -64,19 +68,43 @@ public void start() {
}
private void init() {
- this.initOpenGL();
- this.initWorld();
+ 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() {
- this.cube=new Cube();
+ if(useShaders) {
+ Cube.setShaderProgram(shaderProgram);
+ }
+ cube=new Cube();
}
private void render() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// world rendering code
- this.cube.draw();
+ cube.draw();
}
private void initOpenGL() {
@@ -88,7 +116,8 @@ private void initOpenGL() {
// setup the camera params
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
- GLU.gluPerspective(45.0f, (float)width/(float)height, 1.0f, 100f);
+ GLU.gluLookAt(0f, 2f, 0f, 0f, 0f, -4f, 0f, 1f, 0f);
+ GLU.gluPerspective(45.0f, (float)width/(float)height, 0.1f, 10f);
// setup the model (drawing) params
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
@@ -96,7 +125,6 @@ private void initOpenGL() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
-// glDepthFunc(GL11.GL_LESS);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// enable blending
glEnable(GL_BLEND);
View
8 LessonFive/shaders/screen.frag
@@ -1,9 +1,9 @@
#version 120
-varying vec3 fColor;
-uniform float fade;
-uniform mat4 transformMatrix;
+uniform vec3 modelPosition;
+uniform vec3 modelRotation;
+varying vec3 fragmentColor;
void main(void) {
- gl_FragColor = vec4(fColor.x, fColor.y, fColor.z, fade);
+ gl_FragColor=vec4(fragmentColor.x,fragmentColor.y,fragmentColor.z,1.0);
}
View
38 LessonFive/shaders/screen.vert
@@ -1,12 +1,36 @@
#version 120
-attribute vec3 coord3D;
-attribute vec3 vColor;
-varying vec3 fColor;
-uniform float fade;
-uniform mat4 transformMatrix;
+attribute vec3 coords;
+attribute vec3 color;
+uniform vec3 modelPosition;
+uniform vec3 modelRotation;
+varying vec3 fragmentColor;
void main(void) {
- gl_Position = transformMatrix * vec4(coord3D, 1.0);
- fColor = vColor;
+ 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.