Permalink
Browse files

latest

  • Loading branch information...
alihelmy committed Oct 31, 2011
1 parent 14669ad commit 7ffda27da92751b8f1d950db943e88a88313640b
@@ -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;
}
}
Oops, something went wrong.

0 comments on commit 7ffda27

Please sign in to comment.