Эх сурвалжийг харах

Models.txt is now models.json

Changed the format of the models-file to JSON.
frans 8 жил өмнө
parent
commit
eee4760b28

+ 1 - 2
TODO.txt

@@ -1,6 +1,5 @@
 Particles and post-processing is broken.
-I am suspecting that it would be laughably easy to merge the normalmapped and non-normalmapped entity shaders.
-Texture atlases  and transparency vars with the new model spec system.
+Texture atlases with the new model spec system.
 Multiplayer
 Modular GUIs
  - Make text variables uniform/ change text rendering method

+ 15 - 0
formats.md

@@ -0,0 +1,15 @@
+# Tankernn Game Engine format specification
+
+## models.json
+
+Available options:
+
+- id (int) <required>
+- model (filename) <required>
+- texture (filename)
+- normal (filename)
+- specular (filename)
+- shinedamper (float)
+- reflectivity (float)
+- refractivity (float)
+- transparency (boolean)

+ 13 - 0
pom.xml

@@ -39,6 +39,19 @@
 			<artifactId>commons-lang3</artifactId>
 			<version>3.5</version>
 		</dependency>
+		<!-- https://mvnrepository.com/artifact/org.json/json -->
+		<dependency>
+			<groupId>org.json</groupId>
+			<artifactId>json</artifactId>
+			<version>20160810</version>
+		</dependency>
+		<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
+		<dependency>
+			<groupId>com.google.guava</groupId>
+			<artifactId>guava</artifactId>
+			<version>19.0</version>
+		</dependency>
+
 	</dependencies>
 
 	<build>

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/MainLoop.java

@@ -66,7 +66,7 @@ public class MainLoop {
 	public static void main(String[] args) throws IOException {
 		DisplayManager.createDisplay("Tankernn Game Engine tester");
 		
-		Loader loader = new Loader(new InternalFile("models.txt"));
+		Loader loader = new Loader(new InternalFile("models.json"));
 
 		// ### Terrain textures ###
 

+ 23 - 25
src/main/java/eu/tankernn/gameEngine/loader/Loader.java

@@ -1,8 +1,8 @@
 package eu.tankernn.gameEngine.loader;
 
-import java.io.BufferedReader;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.nio.FloatBuffer;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -11,6 +11,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.json.JSONArray;
+import org.json.JSONObject;
 import org.lwjgl.BufferUtils;
 import org.lwjgl.opengl.GL11;
 import org.lwjgl.opengl.GL15;
@@ -110,7 +112,8 @@ public class Loader {
 	}
 
 	public RawModel loadToVAO(ModelData data) {
-		return (RawModel) loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getTangents(), data.getIndices());
+		return (RawModel) loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getTangents(),
+				data.getIndices());
 	}
 
 	/**
@@ -120,7 +123,7 @@ public class Loader {
 	 *            The path, relative to the root of the jar file, of the file to
 	 *            load.
 	 * @return The texture ID
-	 * @throws FileNotFoundException 
+	 * @throws FileNotFoundException
 	 */
 	public Texture loadTexture(String filename) throws FileNotFoundException {
 		Texture texture = Texture.newTexture(new InternalFile(filename)).create();
@@ -191,30 +194,24 @@ public class Loader {
 	private void readModelSpecification(InternalFile file) throws IOException {
 		Map<InternalFile, RawModel> cachedRawModels = new HashMap<InternalFile, RawModel>();
 		Map<InternalFile, Texture> cachedTextures = new HashMap<InternalFile, Texture>();
+		JSONObject spec;
 
-		BufferedReader in = file.getReader();
-		String line;
-		String[] spec;
+		JSONArray jsonArr = new JSONArray(file.readFile());
 
-		while (in.ready()) {
-			line = in.readLine();
-
-			if (line.startsWith("#") || line.isEmpty())
-				continue;
-
-			spec = line.split("\\s+");
+		for (int j = 0; j < jsonArr.length(); j++) {
+			spec = jsonArr.getJSONObject(j);
 
 			int id;
 			RawModel model;
 			ModelTexture modelTexture;
 			Texture[] textures = new Texture[3];
 
-			id = Integer.parseInt(spec[0]);
+			id = spec.getInt("id");
 
-			InternalFile objFile = new InternalFile(spec[1]);
-			InternalFile textureFile = new InternalFile(spec[2]),
-					specularFile = spec[3].equals("null") ? null : new InternalFile(spec[3]),
-					normalFile = spec[4].equals("null") ? null : new InternalFile(spec[4]);
+			InternalFile objFile = new InternalFile(spec.getString("model"));
+			InternalFile textureFile = new InternalFile(spec.getString("texture")),
+					specularFile = spec.has("specular") ? new InternalFile(spec.getString("specular")) : null,
+					normalFile = spec.has("normal") ? new InternalFile(spec.getString("normal")) : null;
 
 			InternalFile[] textureFiles = { textureFile, specularFile, normalFile };
 
@@ -226,6 +223,7 @@ public class Loader {
 			}
 
 			for (int i = 0; i < textureFiles.length; i++) {
+				System.out.println(textureFiles[i]);
 				if (textureFiles[i] == null)
 					textures[i] = null;
 				else if (cachedTextures.containsKey(textureFiles[i]))
@@ -238,18 +236,18 @@ public class Loader {
 
 			modelTexture = new ModelTexture(textures[0]);
 			if (textures[1] != null)
-				modelTexture.setNormalMap(textures[1]);
+				modelTexture.setNormalMap(textures[2]);
 			if (textures[2] != null)
-				modelTexture.setSpecularMap(textures[2]);
+				modelTexture.setSpecularMap(textures[1]);
 
-			modelTexture.setShineDamper(Float.parseFloat(spec[5]));
-			modelTexture.setReflectivity(Float.parseFloat(spec[6]));
-			modelTexture.setRefractivity(Float.parseFloat(spec[7]));
+			modelTexture.setShineDamper(BigDecimal.valueOf(spec.optDouble("shinedamper", 10.0d)).floatValue());
+			modelTexture.setReflectivity(BigDecimal.valueOf(spec.optDouble("reflectivity", 0d)).floatValue());
+			modelTexture.setRefractivity(BigDecimal.valueOf(spec.optDouble("refractivity", 0d)).floatValue());
+			
+			modelTexture.setHasTransparency(spec.optBoolean("transparency"));
 
 			models.put(id, new TexturedModel(model, modelTexture));
 		}
-
-		in.close();
 	}
 
 	public int registerModel(int id, TexturedModel model) throws Exception {

+ 3 - 3
src/main/java/eu/tankernn/gameEngine/loader/textures/ModelTexture.java

@@ -5,9 +5,9 @@ public class ModelTexture {
 	private Texture normalMap;
 	private Texture specularMap;
 
-	private float shineDamper = 1;
-	private float reflectivity = 0;
-	private float refractivity = 0;
+	private float shineDamper = 10.0f;
+	private float reflectivity = 0.0f;
+	private float refractivity = 0.0f;
 
 	private boolean hasTransparency = false;
 	private boolean useFakeLighting = false;

+ 24 - 18
src/main/java/eu/tankernn/gameEngine/renderEngine/entities/fragmentShader.glsl

@@ -28,29 +28,31 @@ uniform vec3 skyColor;
 
 //const float levels = 3.0;
 
-void main(void){
-	
+void main(void) {
+
 	float objectNearestLight = texture(shadowMap, shadowCoords.xy).r;
 	float lightFactor = 1.0;
 	if (shadowCoords.z > objectNearestLight + 0.002) {
 		lightFactor = 1.0 - 0.4;
 	}
-	
+
 	vec3 unitNormal = normalize(surfaceNormal);
 
 	if (usesNormalMap > 0.5) {
-		vec4 normalMapValue = 2.0 * texture(normalMap, pass_textureCoords) - 1.0;
+		vec4 normalMapValue = 2.0 * texture(normalMap, pass_textureCoords)
+				- 1.0;
 		unitNormal = normalize(normalMapValue.rgb);
 	}
 
 	vec3 unitVectorToCamera = normalize(toCameraVector);
-	
+
 	vec3 totalDiffuse = vec3(0.0);
 	vec3 totalSpecular = vec3(0.0);
-	
+
 	for (int i = 0; i < 4; i++) {
 		float distance = length(toLightVector[i]);
-		float attFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);
+		float attFactor = attenuation[i].x + (attenuation[i].y * distance)
+				+ (attenuation[i].z * distance * distance);
 		vec3 unitLightVector = normalize(toLightVector[i]);
 		float nDotl = dot(unitNormal, unitLightVector);
 		float brightness = max(nDotl, 0.0);
@@ -63,17 +65,18 @@ void main(void){
 		float dampedFactor = pow(specularFactor, shineDamper);
 		//level = floor(dampedFactor * levels);
 		//dampedFactor = level / levels;
-		totalDiffuse = totalDiffuse + (brightness * lightColor[i])/attFactor;
-		totalSpecular = totalSpecular + (dampedFactor * reflectivity * lightColor[i])/attFactor;
+		totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attFactor;
+		totalSpecular = totalSpecular
+				+ (dampedFactor * reflectivity * lightColor[i]) / attFactor;
 	}
-	
+
 	totalDiffuse = max(totalDiffuse * lightFactor, 0.2); //Ambient lighting 2.0
-	
+
 	vec4 textureColor = texture(modelTexture, pass_textureCoords);
 	if (textureColor.a < 0.5) {
 		discard;
 	}
-	
+
 	out_BrightColor = vec4(0.0);
 	if (usesSpecularMap > 0.5) {
 		vec4 mapInfo = texture(specularMap, pass_textureCoords);
@@ -83,16 +86,19 @@ void main(void){
 			totalDiffuse = vec3(1.0);
 		}
 	}
-	
-	out_Color = vec4(totalDiffuse, 1.0) * textureColor + vec4(totalSpecular, 1.0);
+
+	out_Color = vec4(totalDiffuse, 1.0) * textureColor
+			+ vec4(totalSpecular, 1.0);
 	out_Color = mix(vec4(skyColor, 1.0), out_Color, visibility);
-	
+
 	vec4 reflectedColor = texture(enviroMap, reflectedVector);
 	vec4 refractedColor = texture(enviroMap, refractedVector);
 	vec4 enviroColor = reflectedColor;
 	if (refractivity > 0) {
-		enviroColor = mix(reflectedColor, refractedColor, (reflectivity / 10) / refractivity);
+		enviroColor = mix(reflectedColor, refractedColor,
+				(reflectivity / 10) / refractivity);
 	}
-	
-	out_Color = mix(out_Color, enviroColor, (reflectivity / 10 + refractivity) / 2);
+
+	out_Color = mix(out_Color, enviroColor,
+			(reflectivity / 10 + refractivity) / 2);
 }

+ 17 - 18
src/main/java/eu/tankernn/gameEngine/renderEngine/entities/vertexShader.glsl

@@ -37,47 +37,46 @@ uniform vec4 plane;
 
 void main(void) {
 	vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
-	shadowCoords = toShadowMapSpace * worldPosition;
 	gl_ClipDistance[0] = dot(worldPosition, plane);
+	shadowCoords = toShadowMapSpace * worldPosition;
 	mat4 modelViewMatrix = viewMatrix * transformationMatrix;
-	vec4 positionRelativeToCam = viewMatrix * worldPosition;
+	vec4 positionRelativeToCam = modelViewMatrix * vec4(position, 1.0);
 	gl_Position = projectionMatrix * positionRelativeToCam;
-	pass_textureCoords = (textureCoords/numberOfRows) + offset;
-	
+	pass_textureCoords = (textureCoords / numberOfRows) + offset;
+
 	vec3 actualNormal = normal;
 	if (useFakeLighting > 0.5) {
 		actualNormal = vec3(0.0, 1.0, 0.0);
 	}
-	
+
 	surfaceNormal = (modelViewMatrix * vec4(actualNormal, 0.0)).xyz;
 
 	if (usesNormalMap > 0.5) {
 		vec3 norm = normalize(surfaceNormal);
 		vec3 tang = normalize((modelViewMatrix * vec4(tangent, 0.0)).xyz);
 		vec3 bitang = normalize(cross(norm, tang));
-		
-		mat3 toTangentSpace = mat3(
-			tang.x, bitang.x, norm.x,
-			tang.y, bitang.y, norm.y,
-			tang.z, bitang.z, norm.z
-		);
-
-		for(int i = 0; i < 4; i++) {
-			toLightVector[i] = toTangentSpace * (lightPositionEyeSpace[i] - positionRelativeToCam.xyz);
+
+		mat3 toTangentSpace = mat3(tang.x, bitang.x, norm.x, tang.y, bitang.y,
+				norm.y, tang.z, bitang.z, norm.z);
+
+		for (int i = 0; i < 4; i++) {
+			toLightVector[i] = toTangentSpace
+					* (lightPositionEyeSpace[i] - positionRelativeToCam.xyz);
 		}
 		toCameraVector = toTangentSpace * (-positionRelativeToCam.xyz);
 	} else {
 		for (int i = 0; i < 4; i++) {
 			toLightVector[i] = lightPosition[i] - worldPosition.xyz;
 		}
-		toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - worldPosition.xyz;
+		toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz
+				- worldPosition.xyz;
 	}
-	
+
 	vec3 unitNormal = normalize(normal);
 	vec3 viewVector = normalize(worldPosition.xyz - cameraPosition);
 	reflectedVector = reflect(viewVector, unitNormal);
-	refractedVector = refract(viewVector, unitNormal, 1.0/1.33);
-	
+	refractedVector = refract(viewVector, unitNormal, 1.0 / 1.33);
+
 	float distance = length(positionRelativeToCam.xyz);
 	visibility = exp(-pow((distance * density), gradient));
 	visibility = clamp(visibility, 0.0, 1.0);

+ 12 - 0
src/main/java/eu/tankernn/gameEngine/util/InternalFile.java

@@ -7,8 +7,12 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.URL;
 import java.util.Arrays;
 
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+
 public class InternalFile {
 
 	private static final String FILE_SEPARATOR = "/";
@@ -75,6 +79,14 @@ public class InternalFile {
 			throw e;
 		}
 	}
+	
+	public URL getURL() {
+		return InternalFile.class.getResource(path);
+	}
+	
+	public String readFile() throws IOException {
+		return Resources.toString(getURL(), Charsets.UTF_8);
+	}
 
 	public String getName() {
 		return name;