public void load() { vertices = BufferUtils.createFloatBuffer(18); vertices.put(new float[] { -0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f }); vertices.rewind(); vbo = GL15.glGenBuffers(); vao = GL30.glGenVertexArrays(); GL15.glBindBuffer (GL15.GL_ARRAY_BUFFER, vbo); GL15.glBufferData (GL15.GL_ARRAY_BUFFER, vertices, GL15.GL_STATIC_DRAW); GL30.glBindVertexArray(vao); GL20.glEnableVertexAttribArray (0); GL15.glBindBuffer (GL15.GL_ARRAY_BUFFER, vbo); GL20.glVertexAttribPointer (0, 3, GL_FLOAT, false, 0, 0); // Used to just to get an identity matrix for the model. modelTransform = new Transform(); // Compiles the default shaders. shader = new ShaderProgram(); shader.CompileShaders(); glClearColor(0.1f, 0.2f, 0.3f, 0.0f); glViewport(0,0,window.getWidth(), window.getHeight()); // Sets up a camera class that holds the matrices. cam = new Camera(window.getWidth(), window.getHeight()); cam.setOrthographic(); // Set projection matrix cam.setPosition(0, 0, -1f); // Set view matrix // Create a buffer to pass the info to the shader uniforms matrixBuffer = BufferUtils.createFloatBuffer(16); System.out.println("Projection Matrix: \n"+cam.projection.toString()); System.out.println("View Matrix: \n"+cam.matrix.toString()); System.out.println("Model Matrix: \n"+modelTransform.matrix.toString());}public void render() { glClear(GL11.GL_COLOR_BUFFER_BIT); shader.Use(); // Uploads the projection (cam.projection) and view (cam.matrix) shader.setCameraMatrices(matrixBuffer, cam); // Uploads the model (model.matrix) shader.setModelMatrix(matrixBuffer, modelTransform.matrix); GL30.glBindVertexArray (vao); glDrawArrays (GL_TRIANGLES, 0, 3); ShaderProgram.Release(); //shortcut for glUseProgram(0)}
public void setOrthographic() { this.isPerspective = false; float left = -(width/2); float right = (width/2); float top = -(height/2); float bottom = (height/2); this.projection.setIdentity(); this.projection = MatrixMath.ortho(left, right, bottom, top, near, far); }
public static Matrix4f ortho(float left, float right, float bottom, float top, float zNear, float zFar) { Matrix4f m = new Matrix4f(); m.m00 = 2 / (right - left); m.m11 = 2 / (top - bottom); m.m22 = -2 / (zFar - zNear); m.m30 = -(right + left) / (right - left); m.m31 = -(top + bottom) / (top - bottom); m.m32 = -(zFar + zNear) / (zFar - zNear); return m;}
public void setCameraMatrices(FloatBuffer buf, Matrix4f projection, Matrix4f view) { if(ProgramInUse != program) { System.err.println("Attempting to upload matrices to the wrong ShaderProgram!"); return; } projection.store(buf); buf.flip(); GL20.glUniformMatrix4(projMatrix, false, buf); view.store(buf); buf.flip(); GL20.glUniformMatrix4(viewMatrix, false, buf);}
"#version 150\n" +"uniform mat4 projectionMatrix;\n" +"uniform mat4 viewMatrix;\n" +"uniform mat4 modelMatrix;\n" +"in vec4 in_Position;\n" +"void main () {\n" +" gl_Position = in_Position;\n" +" gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;" +"}";
Initiating game object...OpenGL Version: 3.2.9752 Core Profile Forward-Compatible ContextProjection Matrix: 0.003125 0.0 0.0 -0.00.0 -0.004166667 0.0 0.00.0 0.0 -0.0020001999 -1.00019990.0 0.0 0.0 1.0View Matrix: 1.0 0.0 0.0 0.00.0 1.0 0.0 0.00.0 0.0 1.0 -1.00.0 0.0 0.0 1.0Model Matrix: 1.0 0.0 0.0 0.00.0 1.0 0.0 0.00.0 0.0 1.0 0.00.0 0.0 0.0 1.0Closing down...
GL20.glUniformMatrix4(projMatrix, false, buf);
GL20.glUniformMatrix4(projMatrix, true, buf);
m.m30 = -(right + left) / (right - left); m.m31 = -(top + bottom) / (top - bottom); m.m32 = -(zFar + zNear) / (zFar - zNear);
m.m03 = -(right + left) / (right - left); m.m13 = -(top + bottom) / (top - bottom); m.m23 = -(zFar + zNear) / (zFar - zNear);
float left = -(width/2f);float right = (width/2f);float top = (height/2f);float bottom = -(height/2f);float near = 0.1f;float far = 10f; this.projection.setIdentity();this.projection = MatrixMath.ortho(left, right, bottom, top, near, far);