Permalink
Browse files

added in cube data

  • Loading branch information...
alihelmy committed Oct 18, 2011
1 parent 90b677f commit 14669ad674c1eacc63566f5eae9998bc15ca82ea
@@ -1,35 +1,18 @@
package com.onehandstand.lwjgltutorial.lessonfive;
-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.ARBBufferObject.*;
+import static org.lwjgl.opengl.ARBShaderObjects.*;
+import static org.lwjgl.opengl.ARBVertexBufferObject.*;
+import static org.lwjgl.opengl.GL11.*;
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 static org.lwjgl.opengl.GL20.*;
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;
@@ -39,7 +22,7 @@
private boolean useShaders;
private int vertexAttributesBuffer, vertexIndicesBuffer;
private int vertexAttributesBufferSize, vertexIndicesBufferSize;
- private final int numVertices=3, numAxisPerVertex=3, numColoursPerVertex=3;
+ private final int numVertices=8, numAxisPerVertex=3, numColoursPerVertex=3;
private int vColorAttributeIndex, coord3DAttributeIndex,
fadeAttributeIndex, transformMatrixAttributeIndex;
private FloatBuffer transformationValues;
@@ -56,10 +39,10 @@ public Cube() {
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);
+ 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();
@@ -68,7 +51,8 @@ public void update(long timeElapsed) {
Matrix4f rotationMatrix=new Matrix4f();
rotationMatrix.setIdentity();
rotationMatrix.rotate(angle, zAxis);
- Matrix4f transformationMatrix=Matrix4f.mul(translationMatrix, rotationMatrix, null);
+ Matrix4f transformationMatrix=Matrix4f.mul(translationMatrix,
+ rotationMatrix, null);
transformationValues.clear();
transformationMatrix.store(transformationValues);
transformationValues.flip();
@@ -97,7 +81,9 @@ private void initBuffers() {
private void setupVertexIndecesBuffer() {
vertexIndicesBuffer=glGenBuffersARB();
- vertexIndicesBufferSize=bytesPerInt*numVertices;
+ 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,
@@ -106,7 +92,24 @@ private void setupVertexIndecesBuffer() {
GL_WRITE_ONLY_ARB, vertexIndicesBufferSize, null);
// put in vertex index data
int[] indecesData=new int[] {
- 0, 1, 2
+ // 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,
};
vertexIndeces.asIntBuffer().put(indecesData);
// flip buffer, unmap and unbind
@@ -127,9 +130,16 @@ private void setupVertexAttributesBuffer() {
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
+ // 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,
+ // 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,
};
vertexPositionAttributes.asFloatBuffer().put(vertexAttributesData);
// flip buffer, unmap and unbind
@@ -163,11 +173,12 @@ public void draw() {
}
// bind vertex data array
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
-
- //uniform shader attributes
+
+ // uniform shader attributes
glUniform1f(fadeAttributeIndex, currentFade);
- glUniformMatrix4(transformMatrixAttributeIndex, false, transformationValues);
-
+ glUniformMatrix4(transformMatrixAttributeIndex, false,
+ transformationValues);
+
// pass in info to vertex shader
int dataOffset=0;
glEnableVertexAttribArray(coord3DAttributeIndex);
@@ -181,7 +192,11 @@ public void draw() {
// draw the vertices using the indices
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
- glDrawRangeElements(GL_TRIANGLES, 0, numVertices-1, numVertices,
+ // 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);
// unbind the buffers
@@ -28,11 +28,12 @@
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 triangle;
+ private Cube cube;
public void start() {
try {
@@ -51,7 +52,7 @@ public void start() {
// main loop
while (!Display.isCloseRequested()) {
long timeElapsed=Sys.getTime();
- triangle.update(timeElapsed);
+ cube.update(timeElapsed);
this.render();
Display.update();
Display.sync(60);
@@ -68,14 +69,14 @@ private void init() {
}
private void initWorld() {
- this.triangle=new Cube();
+ this.cube=new Cube();
}
private void render() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// world rendering code
- this.triangle.draw();
+ this.cube.draw();
}
private void initOpenGL() {
@@ -95,7 +96,7 @@ private void initOpenGL() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
+// glDepthFunc(GL11.GL_LESS);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// enable blending
glEnable(GL_BLEND);

0 comments on commit 14669ad

Please sign in to comment.