Permalink
Browse files

copy of lesson six

  • Loading branch information...
1 parent a8d0e97 commit 15eff278b43bd6c19743882d80cd3e5135de15b0 @alihelmy committed Nov 8, 2011
@@ -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();
@@ -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);
}
@@ -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;
}
@@ -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;
+ }
+}
Oops, something went wrong.

0 comments on commit 15eff27

Please sign in to comment.