1. Roi Atalla
  2. OpenGL Utils

Commits

Roi Atalla  committed 37e8156

Added lengthSquared method to Vector and its implementations. GLProgram's main loop now prints update and render time along with the FPS.

  • Participants
  • Parent commits ab6a81e
  • Branches default

Comments (0)

Files changed (6)

File src/com/ra4king/opengl/util/GLProgram.java

View file
 			lastTime = lastFPS = System.nanoTime();
 			int frames = 0;
 			
+			long updateTime = 0, renderTime = 0;
+			
 			while(!Display.isCloseRequested() && !shouldStop()) {
 				long deltaTime = System.nanoTime() - lastTime;
 				lastTime += deltaTime;
 						keyReleased(Keyboard.getEventKey(), Keyboard.getEventCharacter());
 				}
 				
+				long initial = System.nanoTime();
 				update(deltaTime);
+				updateTime += System.nanoTime() - initial;
 				
 				Utils.checkGLError("update");
 				
+				initial = System.nanoTime();
 				render();
 				
+				Display.update();
+				renderTime += System.nanoTime() - initial;
+
 				Utils.checkGLError("render");
 				
-				Display.update();
-				
 				frames++;
 				if(System.nanoTime() - lastFPS >= 1e9) {
-					System.out.println("FPS: ".concat(String.valueOf(frames)));
+					System.out.println("FPS: ".concat(String.valueOf(frames)) + "\tUpdate: " + (updateTime / frames) + "ns/" + String.format("%.2fms", updateTime / (frames * 1e6)) + "\tRender: " + (renderTime / frames) + "ns/" + String.format("%.2fms", renderTime / (frames * 1e6)));
 					lastFPS += 1e9;
+					updateTime = renderTime = 0;
+					
 					frames = 0;
 				}
 				

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

View file
 		stack = new Stack<>();
 	}
 	
+	public MatrixStack clear() {
+		stack.clear();
+		current = null;
+		return this;
+	}
+	
 	public Matrix4 getTop() {
 		return current;
 	}

File src/com/ra4king/opengl/util/math/Vector.java

View file
 	
 	float length();
 	
+	float lengthSquared();
+	
 	V copy();
 	
 	FloatBuffer toBuffer();

File src/com/ra4king/opengl/util/math/Vector2.java

View file
 		return (float)Math.sqrt(x * x + y * y);
 	}
 	
+	@Override
+	public float lengthSquared() {
+		return x * x + y * y;
+	}
+	
 	public Vector2 normalize() {
 		float length = length();
 		x /= length;

File src/com/ra4king/opengl/util/math/Vector3.java

View file
 		return false;
 	}
 	
+	@Override
+	public int hashCode() {
+		return (int)(x * (2 << 4) + y * (2 << 2) + z);
+	}
+	
 	public Vector3 set(float x, float y, float z) {
 		this.x = x;
 		this.y = y;
 		return (float)Math.sqrt(x * x + y * y + z * z);
 	}
 	
+	@Override
+	public float lengthSquared() {
+		return x * x + y * y + z * z;
+	}
+	
 	public Vector3 normalize() {
 		float length = length();
 		x /= length;

File src/com/ra4king/opengl/util/math/Vector4.java

View file
 		return (float)Math.sqrt(x * x + y * y + z * z + w * w);
 	}
 	
+	@Override
+	public float lengthSquared() {
+		return x * x + y * y + z * z + w * w;
+	}
+	
 	public Vector4 normalize() {
 		float length = length();
 		x /= length;