Roi Atalla avatar Roi Atalla committed 0737cb5

I'm awesome.

Comments (0)

Files changed (6)

 	</classpathentry>
 	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
 		<attributes>
+			<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="OpenGL/target/natives"/>
 			<attribute name="maven.pomderived" value="true"/>
 		</attributes>
 	</classpathentry>

src/main/java/com/ra4king/opengl/GLProgram.java

 	}
 	
 	private void gameLoop() {
-		init();
-		
-		resized();
-		
-		long lastTime, lastFPS;
-		lastTime = lastFPS = System.nanoTime();
-		int frames = 0;
-		
-		while(!Display.isCloseRequested() && !shouldStop()) {
-			long deltaTime = System.nanoTime() - lastTime;
-			lastTime += deltaTime;
+		try {
+			init();
 			
-			if(Display.wasResized())
-				resized();
+			resized();
 			
-			update(deltaTime);
-			render();
+			long lastTime, lastFPS;
+			lastTime = lastFPS = System.nanoTime();
+			int frames = 0;
 			
-			Display.update();
-			
-			int error;
-			while((error = glGetError()) != GL_NO_ERROR)
-				System.out.println(gluErrorString(error));
-			
-			frames++;
-			if(System.nanoTime() - lastFPS >= 1e9) {
-				System.out.println("FPS: ".concat(String.valueOf(frames)));
-				lastFPS += 1e9;
-				frames = 0;
+			while(!Display.isCloseRequested() && !shouldStop()) {
+				long deltaTime = System.nanoTime() - lastTime;
+				lastTime += deltaTime;
+				
+				if(Display.wasResized())
+					resized();
+				
+				update(deltaTime);
+				render();
+				
+				Display.update();
+				
+				int error;
+				while((error = glGetError()) != GL_NO_ERROR)
+					System.out.println(gluErrorString(error));
+				
+				frames++;
+				if(System.nanoTime() - lastFPS >= 1e9) {
+					System.out.println("FPS: ".concat(String.valueOf(frames)));
+					lastFPS += 1e9;
+					frames = 0;
+				}
+				
+				Display.sync(fps);
 			}
-			
-			Display.sync(fps);
 		}
-		
-		destroy();
+		finally {
+			destroy();
+		}
 	}
 	
 	public int getWidth() {

src/main/java/com/ra4king/opengl/test/Test.java

+package com.ra4king.opengl.test;
+
+import static org.lwjgl.opengl.GL11.*;
+import static org.lwjgl.opengl.GL15.*;
+import static org.lwjgl.opengl.GL20.*;
+import static org.lwjgl.opengl.GL30.*;
+
+import java.nio.FloatBuffer;
+import java.nio.ShortBuffer;
+
+import org.lwjgl.BufferUtils;
+
+import com.ra4king.opengl.GLProgram;
+import com.ra4king.opengl.util.ShaderProgram;
+import com.ra4king.opengl.util.math.Matrix4;
+
+public class Test extends GLProgram {
+	public static void main(String[] args) {
+		new Test().run(true);
+	}
+	
+	public Test() {
+		super(true);
+	}
+	
+	private ShaderProgram program;
+	
+	private Matrix4 modelViewMatrix;
+	private int modelViewMatrixUniform;
+	
+	private int vao;
+	
+	@Override
+	public void init() {
+		glClearColor(0,0,0,0);
+		glClearDepth(1);
+		
+		program = new ShaderProgram(readFromFile("test.vert"),readFromFile("test.frag"));
+		
+		int perspectiveMatrix = glGetUniformLocation(program.getProgram(), "perspectiveMatrix");
+		modelViewMatrixUniform = glGetUniformLocation(program.getProgram(), "modelViewMatrix");
+		
+		modelViewMatrix = new Matrix4().clearToIdentity();
+		modelViewMatrix.translate(0, -8, -20).rotate((float)Math.PI, 0, 1, 0);
+		
+		program.begin();
+		glUniformMatrix4(perspectiveMatrix, false, new Matrix4().clearToPerspective((float)Math.PI/2, getWidth(), getHeight(), 1, 1000).getBuffer());
+		program.end();
+		
+		float[] vertices = {
+			 5,  5,  5,
+			 5, -5,  5,
+			-5, -5,  5,
+			-5,  5,  5,
+			
+			 5,  5, -5,
+			 5, -5, -5,
+			-5, -5, -5,
+			-5,  5, -5,
+			
+			0, 0, 0,
+			0, 1, 0,
+			1, 1, 0,
+			1, 0, 0,
+			
+			0, 0, 1,
+			0, 1, 1,
+			1, 1, 1,
+			1, 0, 1,
+		};
+		
+		short[] indicies = {
+				0, 1, 2,
+				3, 0, 2,
+				
+				7, 6, 5,
+				4, 7, 5,
+				
+				4, 5, 1,
+				0, 4, 1,
+				
+				3, 2, 6,
+				7, 3, 6,
+				
+				4, 0, 3,
+				7, 4, 3,
+				
+				1, 5, 6,
+				2, 1, 6
+		};
+		
+		int vbo1 = glGenBuffers();
+		glBindBuffer(GL_ARRAY_BUFFER, vbo1);
+		glBufferData(GL_ARRAY_BUFFER, (FloatBuffer)BufferUtils.createFloatBuffer(vertices.length).put(vertices).flip(), GL_STATIC_DRAW);
+		glBindBuffer(GL_ARRAY_BUFFER, 0);
+		
+		int vbo2 = glGenBuffers();
+		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2);
+		glBufferData(GL_ELEMENT_ARRAY_BUFFER, (ShortBuffer)BufferUtils.createShortBuffer(indicies.length).put(indicies).flip(), GL_STATIC_DRAW);
+		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+		
+		vao = glGenVertexArrays();
+		glBindVertexArray(vao);
+		glBindBuffer(GL_ARRAY_BUFFER, vbo1);
+		glEnableVertexAttribArray(0);
+		glEnableVertexAttribArray(1);
+		glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
+		glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 8*3*4);
+		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2);
+		glBindVertexArray(0);
+		
+		glBindBuffer(GL_ARRAY_BUFFER, 0);
+		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+		
+		glEnable(GL_DEPTH_TEST);
+		glDepthRange(0,1);
+		glDepthFunc(GL_LEQUAL);
+		
+		glEnable(GL_CULL_FACE);
+		glCullFace(GL_BACK);
+		glFrontFace(GL_CW);
+	}
+	
+	@Override
+	public void render() {
+		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+		
+		program.begin();
+		
+		glBindVertexArray(vao);
+		glUniformMatrix4(modelViewMatrixUniform, false, modelViewMatrix.getBuffer());
+		glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
+		glBindVertexArray(0);
+		
+		program.end();
+	}
+}

src/main/java/com/ra4king/opengl/test/test.frag

+#version 330
+
+smooth in vec4 theColor;
+
+out vec4 outputColor;
+
+void main()
+{
+	outputColor = theColor;
+}

src/main/java/com/ra4king/opengl/test/test.vert

+#version 330
+
+layout(location = 0) in vec4 position;
+layout(location = 1) in vec4 color;
+
+smooth out vec4 theColor;
+
+uniform mat4 perspectiveMatrix;
+uniform mat4 modelViewMatrix;
+
+void main()
+{
+	gl_Position = perspectiveMatrix * (modelViewMatrix * position);
+	theColor = color;
+}

src/main/java/com/ra4king/opengl/util/math/Matrix4.java

 			throw new IllegalArgumentException("float array must have at least 16 values.");
 		
 		matrix.position(0);
-		matrix.put(m,0,16).flip();
+		matrix.put(m,0,16);
 		
 		return this;
 	}
 		
 		m[15] = 1;
 		
-		float[] newm = new float[16];
-		for(int a = 0; a < 16; a += 4) {
-			newm[a+0] = get(0)*m[a] + get(4)*m[a+1] + get(8)*m[a+2] + get(12)*m[a+3];
-			newm[a+1] = get(1)*m[a] + get(5)*m[a+1] + get(9)*m[a+2] + get(13)*m[a+3];
-			newm[a+2] = get(2)*m[a] + get(6)*m[a+1] + get(10)*m[a+2] + get(14)*m[a+3];
-			newm[a+3] = get(3)*m[a] + get(7)*m[a+1] + get(11)*m[a+2] + get(15)*m[a+3];
-		}
+		for(int a = 0; a < 4; a++)
+			System.out.println(m[a] + " " + m[a+4] + " " + m[a+8] + " " + m[a+12]);
+		System.out.println();
 		
-		put(newm);
-		
-		return this;
+		return mult(m);
 	}
 	
 	public Matrix4 rotate(float angle, Vector3 vec) {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.