Browse Source

Textures have dimensions

Tankernn 8 years ago
parent
commit
2c68faaa95

+ 1 - 0
TODO.txt

@@ -1,3 +1,4 @@
+Text particles (render text to textures)
 Texture atlases with the new model spec system.
 Multiplayer
 Modular GUIs

+ 4 - 4
src/main/java/eu/tankernn/gameEngine/entities/Entity2D.java

@@ -9,7 +9,7 @@ public class Entity2D extends GuiTexture {
 	protected Vector2f velocity = new Vector2f(0, 0);
 	protected boolean alive = true;
 	
-	public Entity2D(Texture texture, Vector2f position, Vector2f scale) {
+	public Entity2D(Texture texture, Vector2f position, float scale) {
 		super(texture, position, scale);
 	}
 	
@@ -22,8 +22,8 @@ public class Entity2D extends GuiTexture {
 	}
 
 	public boolean collides(Entity2D b) {
-		if (Math.abs(position.x - b.getPosition().x) < scale.x + b.getSize().x) {
-			if (Math.abs(position.y - b.getPosition().y) < scale.y + b.getSize().y) {
+		if (Math.abs(position.x - b.getPosition().x) < getSize().x + b.getSize().x) {
+			if (Math.abs(position.y - b.getPosition().y) < getSize().y + b.getSize().y) {
 				return true;
 			}
 		}
@@ -35,7 +35,7 @@ public class Entity2D extends GuiTexture {
 	}
 	
 	public Vector2f getSize() {
-		return super.getScale();
+		return super.getSize();
 	}
 	
 	public boolean isAlive() {

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/environmentMap/EnvironmentMapRenderer.java

@@ -21,7 +21,7 @@ public class EnvironmentMapRenderer {
 		
 		int depthBuffer = GL30.glGenRenderbuffers();
 		GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
-		GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL14.GL_DEPTH_COMPONENT24, cubeMap.size, cubeMap.size);
+		GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL14.GL_DEPTH_COMPONENT24, cubeMap.getWidth(), cubeMap.getHeight());
 		GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, depthBuffer);
 		GL11.glViewport(0, 0, cubeMap.size, cubeMap.size);
 		

+ 15 - 6
src/main/java/eu/tankernn/gameEngine/loader/textures/Texture.java

@@ -2,6 +2,7 @@ package eu.tankernn.gameEngine.loader.textures;
 
 import org.lwjgl.opengl.GL11;
 import org.lwjgl.opengl.GL13;
+import org.lwjgl.util.vector.Vector2f;
 
 import eu.tankernn.gameEngine.util.InternalFile;
 
@@ -9,17 +10,17 @@ public class Texture {
 
 	public final int textureId;
 	public final int size;
+	public final Vector2f ratio;
 	private final int type;
 
-	protected Texture(int textureId, int size) {
-		this.textureId = textureId;
-		this.size = size;
-		this.type = GL11.GL_TEXTURE_2D;
+	protected Texture(int textureId, int width, int height) {
+		this(textureId, GL11.GL_TEXTURE_2D, width, height);
 	}
 
-	protected Texture(int textureId, int type, int size) {
+	protected Texture(int textureId, int type, int width, int height) {
 		this.textureId = textureId;
-		this.size = size;
+		this.ratio = (Vector2f) new Vector2f(width, height).normalise();
+		this.size = width * height;
 		this.type = type;
 	}
 
@@ -46,4 +47,12 @@ public class Texture {
 		return new Texture(cubeMapId, GL13.GL_TEXTURE_CUBE_MAP, size);
 	}
 
+	public int getWidth() {
+		return (int) (size * ratio.x);
+	}
+	
+	public int getHeight() {
+		return (int) (size * ratio.y);
+	}
+
 }

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/loader/textures/TextureBuilder.java

@@ -18,7 +18,7 @@ public class TextureBuilder {
 	public Texture create(){
 		TextureData textureData = TextureUtils.decodeTextureFile(file);
 		int textureId = TextureUtils.loadTextureToOpenGL(textureData, this);
-		return new Texture(textureId, textureData.getWidth());
+		return new Texture(textureId, textureData.getWidth(), textureData.getHeight());
 	}
 	
 	public TextureBuilder clampEdges(){

+ 2 - 2
src/main/java/eu/tankernn/gameEngine/loader/textures/TextureUtils.java

@@ -115,7 +115,7 @@ public class TextureUtils {
 		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
 		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
 		GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, texture, 0);
-		return new Texture(texture, width * height);
+		return new Texture(texture, width, height);
 	}
 
 	/**
@@ -137,7 +137,7 @@ public class TextureUtils {
 		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
 		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
 		GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, texture, 0);
-		return new Texture(texture, width * height);
+		return new Texture(texture, width, height);
 	}
 
 	public static int createDepthBufferAttachment(int width, int height) {

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/renderEngine/font/FontRenderer.java

@@ -23,7 +23,7 @@ public class FontRenderer {
 		endRendering();
 	}
 	
-	public void finalilze() {
+	public void finalize() {
 		shader.finalize();
 	}
 	

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/renderEngine/font/TextMaster.java

@@ -53,6 +53,6 @@ public class TextMaster {
 	}
 
 	public void finalize() {
-		renderer.finalilze();
+		renderer.finalize();
 	}
 }

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/renderEngine/gui/GuiRenderer.java

@@ -28,7 +28,7 @@ public class GuiRenderer {
 		GL11.glDisable(GL11.GL_DEPTH_TEST);
 		for (GuiTexture gui : guis) {
 			gui.getTexture().bindToUnit(0);
-			Matrix4f matrix = Maths.createTransformationMatrix(gui.getPosition(), gui.getScale());
+			Matrix4f matrix = Maths.createTransformationMatrix(gui.getPosition(), gui.getSize());
 			shader.transformationMatrix.loadMatrix(matrix);
 			GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, quad.getIndexCount());
 		}

+ 5 - 5
src/main/java/eu/tankernn/gameEngine/renderEngine/gui/GuiTexture.java

@@ -8,9 +8,9 @@ public class GuiTexture {
 	
 	private Texture texture;
 	protected Vector2f position;
-	protected Vector2f scale;
+	protected float scale;
 	
-	public GuiTexture(Texture texture, Vector2f position, Vector2f scale) {
+	public GuiTexture(Texture texture, Vector2f position, float scale) {
 		this.texture = texture;
 		this.position = position;
 		this.scale = scale;
@@ -24,15 +24,15 @@ public class GuiTexture {
 		return position;
 	}
 
-	public Vector2f getScale() {
-		return scale;
+	public Vector2f getSize() {
+		return (Vector2f) new Vector2f(texture.ratio).scale(scale);
 	}
 
 	public void setPosition(Vector2f position) {
 		this.position = position;
 	}
 
-	public void setScale(Vector2f scale) {
+	public void setScale(float scale) {
 		this.scale = scale;
 	}
 	

+ 2 - 2
src/main/java/eu/tankernn/gameEngine/util/MousePicker.java

@@ -106,8 +106,8 @@ public class MousePicker {
 		for (GuiTexture gui: guis) {
 			float posX = gui.getPosition().x;
 			float posY = gui.getPosition().y;
-			float scaleX = gui.getScale().x;
-			float scaleY = gui.getScale().y;
+			float scaleX = gui.getSize().x;
+			float scaleY = gui.getSize().y;
 			
 			if (mouseCoords.x > posX - scaleX && mouseCoords.x < posX + scaleX && mouseCoords.y > posY - scaleY && mouseCoords.y < posY + scaleY) {
 				return gui;