package electrosphere.util; import electrosphere.renderer.Material; import electrosphere.renderer.Mesh; import electrosphere.renderer.Model; import electrosphere.renderer.ShaderProgram; import electrosphere.renderer.texture.Texture; import electrosphere.renderer.texture.TextureMap; import com.google.gson.Gson; import electrosphere.cfg.MainConfig; import electrosphere.main.Globals; import electrosphere.main.Main; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URISyntaxException; import java.net.URL; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import org.joml.Matrix4f; import org.joml.Vector3f; import org.lwjgl.BufferUtils; import org.lwjgl.assimp.AIMatrix4x4; import static org.lwjgl.opengl.GL30.glBindVertexArray; import static org.lwjgl.opengl.GL30.glGenVertexArrays; /** * * @author awhoove */ public class Utilities { public static Matrix4f convertAIMatrix(AIMatrix4x4 mat){ Matrix4f rVal = new Matrix4f(); //Old, wrong approach: // mat.set( // mat.a1(), // mat.b1(), // mat.c1(), // mat.d1(), // mat.a2(), // mat.b2(), // mat.c2(), // mat.d2(), // mat.a3(), // mat.b3(), // mat.c3(), // mat.d3(), // mat.a4(), // mat.b4(), // mat.c4(), // mat.d4() // ); //as demo'd in https://github.com/lwjglgamedev/lwjglbook/blob/master/chapter27/c27-p2/src/main/java/org/lwjglb/engine/loaders/assimp/AnimMeshesLoader.java rVal.m00(mat.a1()); rVal.m10(mat.a2()); rVal.m20(mat.a3()); rVal.m30(mat.a4()); rVal.m01(mat.b1()); rVal.m11(mat.b2()); rVal.m21(mat.b3()); rVal.m31(mat.b4()); rVal.m02(mat.c1()); rVal.m12(mat.c2()); rVal.m22(mat.c3()); rVal.m32(mat.c4()); rVal.m03(mat.d1()); rVal.m13(mat.d2()); rVal.m23(mat.d3()); rVal.m33(mat.d4()); return rVal; } public static void save_test_texture_map_to_location(String s){ TextureMap t = new TextureMap(); t.add_model("model1"); t.add_mesh_to_model("model1", "mesh1"); t.add_mesh_to_model("model1", "mesh2"); t.add_mesh_to_model("model1", "mesh3"); t.add_model("model2"); t.add_mesh_to_model("model2", "mesh1"); t.add_mesh_to_model("model2", "mesh2"); t.add_model("model3"); t.add_mesh_to_model("model3", "mesh1"); t.add_mesh_to_model("model3", "mesh2"); t.add_mesh_to_model("model3", "mesh3"); t.add_mesh_to_model("model3", "mesh4"); Gson gson = new Gson(); try { Files.write(new File(s).toPath(), gson.toJson(t).getBytes()); } catch (IOException ex) { ex.printStackTrace(); // just for testing :thinking: } } static final int maxReadFails = 3; static final int READ_TIMEOUT_DURATION = 5; public static String readFileToString(File f){ String rVal = ""; BufferedReader reader; try { reader = Files.newBufferedReader(f.toPath()); int failCounter = 0; boolean reading = true; StringBuilder builder = new StringBuilder(""); while(reading){ if(reader.ready()){ failCounter = 0; int nextValue = reader.read(); if(nextValue == -1){ reading = false; } else { builder.append((char)nextValue); } } else { failCounter++; if(failCounter > maxReadFails){ reading = false; } else { try { TimeUnit.MILLISECONDS.sleep(READ_TIMEOUT_DURATION); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } rVal = builder.toString(); } catch (IOException ex) { ex.printStackTrace(); } return rVal; } public static String readBakedResourceToString(InputStream resourceInputStream){ String rVal = ""; BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader(resourceInputStream)); int failCounter = 0; boolean reading = true; StringBuilder builder = new StringBuilder(""); while(reading){ if(reader.ready()){ failCounter = 0; int nextValue = reader.read(); if(nextValue == -1){ reading = false; } else { builder.append((char)nextValue); } } else { failCounter++; if(failCounter > maxReadFails){ reading = false; } else { try { TimeUnit.MILLISECONDS.sleep(READ_TIMEOUT_DURATION); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } rVal = builder.toString(); } catch (IOException ex) { ex.printStackTrace(); } return rVal; } public static void loadMainConfig(){ if(Main.class.getResource("/Config/localconfig.json") != null){ Globals.mainConfig = loadObjectFromBakedJsonFile("/Config/localconfig.json", MainConfig.class); if(Globals.mainConfig.version != MainConfig.CONFIG_FILE_VERSION){ //dynamically generate config and save it MainConfig.generateMainConfig(); } } else { MainConfig.generateMainConfig(); } } public static T loadObjectFromBakedJsonFile(String fileName, Class className){ T rVal = null; String rawJSON = Utilities.readBakedResourceToString(Main.class.getResourceAsStream(fileName)); Gson gson = new Gson(); rVal = gson.fromJson(rawJSON, className); return rVal; } public static void saveObjectToBakedJsonFile(String fileName, Object object){ URL resourceUrl = Main.class.getResource(fileName); File file = new File(""); try { file = new File(resourceUrl.toURI()); } catch (URISyntaxException ex) { ex.printStackTrace(); } Gson gson = new Gson(); try { Files.write(file.toPath(), gson.toJson(object).getBytes()); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } public static void saveObjectToJsonFile(String fileName, Object object){ Path path = new File(Main.class.getResource(fileName).getPath()).toPath(); Gson gson = new Gson(); try { Files.write(path, gson.toJson(object).getBytes()); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } }