Roi Atalla avatar Roi Atalla committed 230c453

Moved MatrixStack to its own class in util.math

Comments (0)

Files changed (2)

src/main/java/com/ra4king/opengl/arcsynthesis/gl33/chapter6/example4/Example6_4.java

 
 import java.nio.FloatBuffer;
 import java.nio.ShortBuffer;
-import java.util.Stack;
 
 import org.lwjgl.BufferUtils;
 import org.lwjgl.input.Keyboard;
 import com.ra4king.opengl.GLProgram;
 import com.ra4king.opengl.util.ShaderProgram;
 import com.ra4king.opengl.util.math.Matrix4;
+import com.ra4king.opengl.util.math.MatrixStack;
 import com.ra4king.opengl.util.math.Vector3;
 
 public class Example6_4 extends GLProgram {
 	private final float[] CYAN_COLOR = { 0, 1, 1, 1 };
 	private final float[] MAGENTA_COLOR = { 1, 0, 1, 1 };
 	
-	private final float[] vertices = {
+	private final float[] data = {
 			+1.0f, +1.0f, +1.0f,
 			+1.0f, -1.0f, +1.0f,
 			-1.0f, -1.0f, +1.0f,
 		hierarchy = new Hierarchy();
 		cameraToClipMatrix = new Matrix4().clearToPerspective(45*(float)Math.PI/180, getWidth(), getHeight(), 1, 100);
 		
-		program.begin();
-		glUniformMatrix4(cameraToClipMatrixUniform, false, cameraToClipMatrix.getBuffer());
-		program.end();
-		
 		int vbo = glGenBuffers();
 		glBindBuffer(GL_ARRAY_BUFFER, vbo);
-		glBufferData(GL_ARRAY_BUFFER, (FloatBuffer)BufferUtils.createFloatBuffer(vertices.length).put(vertices).flip(), GL_STATIC_DRAW);
+		glBufferData(GL_ARRAY_BUFFER, (FloatBuffer)BufferUtils.createFloatBuffer(data.length).put(data).flip(), GL_STATIC_DRAW);
 		glBindBuffer(GL_ARRAY_BUFFER, 0);
 		
 		int vbo2 = glGenBuffers();
 	}
 	
 	@Override
+	public void resized() {
+		super.resized();
+		
+		program.begin();
+		glUniformMatrix4(cameraToClipMatrixUniform, false, cameraToClipMatrix.clearToPerspective(45*(float)Math.PI/180, getWidth(), getHeight(), 1, 100).getBuffer());
+		program.end();
+	}
+	
+	@Override
 	public void keyPressed(int key, char c, long nanos) {
 		if(key == Keyboard.KEY_SPACE)
 			hierarchy.writePose();
 		hierarchy.draw();
 	}
 	
-	private static class MatrixStack {
-		private Stack<Matrix4> stack;
-		private Matrix4 current;
-		
-		public MatrixStack() {
-			current = new Matrix4().clearToIdentity();
-			stack = new Stack<Matrix4>();
-		}
-		
-		public Matrix4 getTop() {
-			return current;
-		}
-		
-		public void rotateX(float angle) {
-			current.rotate(angle * (float)Math.PI/180, 1, 0, 0);
-		}
-		
-		public void rotateY(float angle) {
-			current.rotate(angle * (float)Math.PI/180, 0, 1, 0);
-		}
-		
-		public void rotateZ(float angle) {
-			current.rotate(angle * (float)Math.PI/180, 0, 0, 1);
-		}
-		
-		public void scale(Vector3 vec) {
-			current.scale(vec);
-		}
-		
-		public void translate(Vector3 vec) {
-			current.translate(vec);
-		}
-		
-		public void pushMatrix() {
-			stack.push(current);
-			current = new Matrix4(current);
-		}
-		
-		public void popMatrix() {
-			current = stack.pop();
-		}
-	}
-	
 	private class Hierarchy {
-		private final float STANDARD_ANGLE_INCREMENT = 11.25f;
+		private final float STANDARD_ANGLE_INCREMENT = 5f;
 		
 		private Vector3 posBase;
 		private float angleBase;
 				modelToCameraStack.translate(new Vector3(0,0,lenFinger/2));
 				modelToCameraStack.scale(new Vector3(widthFinger/2,widthFinger/2,lenFinger/2));
 				glUniformMatrix4(modelToCameraMatrixUniform,false,modelToCameraStack.getTop().getBuffer());
+				glDrawElements(GL_TRIANGLES, indicies.length, GL_UNSIGNED_SHORT, 0);
 				modelToCameraStack.popMatrix();
 				
 				modelToCameraStack.popMatrix();

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

+package com.ra4king.opengl.util.math;
+
+import java.util.Stack;
+
+public class MatrixStack {
+	private Stack<Matrix4> stack;
+	private Matrix4 current;
+	
+	public MatrixStack() {
+		current = new Matrix4().clearToIdentity();
+		stack = new Stack<Matrix4>();
+	}
+	
+	public Matrix4 getTop() {
+		return current;
+	}
+	
+	public void rotateX(float angle) {
+		current.rotate(angle * (float)Math.PI/180, 1, 0, 0);
+	}
+	
+	public void rotateY(float angle) {
+		current.rotate(angle * (float)Math.PI/180, 0, 1, 0);
+	}
+	
+	public void rotateZ(float angle) {
+		current.rotate(angle * (float)Math.PI/180, 0, 0, 1);
+	}
+	
+	public void scale(Vector3 vec) {
+		current.scale(vec);
+	}
+	
+	public void translate(Vector3 vec) {
+		current.translate(vec);
+	}
+	
+	public void pushMatrix() {
+		stack.push(current);
+		current = new Matrix4(current);
+	}
+	
+	public void popMatrix() {
+		current = stack.pop();
+	}
+}
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.