Roi Atalla avatar Roi Atalla committed 9a9dba3

Found a bug in the clear() method of my Matrix4 class. Apparently NIO Buffer's clear method doesn't actually clear the values.

Comments (0)

Files changed (2)

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

 	
 	private ShaderProgram program;
 	
+	private Matrix4 perspectiveMatrix;
+	private int perspectiveMatrixUniform;
+	
 	private Matrix4 modelViewMatrix;
 	private int modelViewMatrixUniform;
 	
 		glClearColor(0,0,0,0);
 		glClearDepth(1);
 		
+		glEnable(GL_LINE_SMOOTH);
+		glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
+		
 		program = new ShaderProgram(readFromFile("test.vert"),readFromFile("test.frag"));
 		
-		int perspectiveMatrix = glGetUniformLocation(program.getProgram(), "perspectiveMatrix");
+		perspectiveMatrixUniform = glGetUniformLocation(program.getProgram(), "perspectiveMatrix");
 		modelViewMatrixUniform = glGetUniformLocation(program.getProgram(), "modelViewMatrix");
 		
-		modelViewMatrix = new Matrix4().clearToIdentity();
-		modelViewMatrix.translate(0, -7, -20).rotate(20*(float)Math.PI/180, 1, 0, 0).rotate(-45*(float)Math.PI/180, 0, 1, 0);
+		perspectiveMatrix = new Matrix4().clearToPerspective((float)Math.PI/2, getWidth(), getHeight(), 1, 1000);
+		modelViewMatrix = new Matrix4();
 		
 		program.begin();
-		glUniformMatrix4(perspectiveMatrix, false, new Matrix4().clearToPerspective((float)Math.PI/2, getWidth(), getHeight(), 1, 1000).getBuffer());
-		glUniformMatrix4(modelViewMatrixUniform, false, modelViewMatrix.getBuffer());
+		glUniformMatrix4(perspectiveMatrixUniform, false, perspectiveMatrix.getBuffer());
 		program.end();
 		
 		float[] vertices = {
 	}
 	
 	@Override
+	public void resized() {
+		super.resized();
+		
+		program.begin();
+		glUniformMatrix4(perspectiveMatrixUniform, false, perspectiveMatrix.clearToPerspective((float)Math.PI/2, getWidth(), getHeight(), 1, 1000).getBuffer());
+		program.end();
+	}
+	
+	private long elapsedTime;
+	
+	@Override
+	public void update(long deltaTime) {
+		elapsedTime += deltaTime;
+		
+		float loopDuration = 3;
+		float angle = ((elapsedTime/(float)1e9)%loopDuration) * (2*(float)Math.PI/loopDuration);
+		modelViewMatrix.clearToIdentity().translate(0, -7, -20).rotate(20*(float)Math.PI/180, 1, 0, 0).rotate(angle, 0, 1, 0);
+	}
+	
+	@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);
 		

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

 	}
 	
 	public Matrix4 clear() {
-		matrix.clear();
+		for(int a = 0; a < 16; a++)
+			matrix.put(a,0);
 		return this;
 	}
 	
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.