package de.nameless.graphicEngine.model;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import static de.nameless.gameEngine.util.Logger.debug;
import static de.nameless.gameEngine.util.Logger.log;
import javax.media.opengl.GL;
import de.nameless.gameEngine.gui.LoadingScreen;
public class NEModelManager {
private static String modelDirectory = "resources/Models/";
private Hashtable<String,MS3DModel> PathMap;
/**
* singelton
*/
private static NEModelManager instance;
public static GL gl;
private static int displayListCounter = 2;
public static NEModelManager getInstance(){
if (instance == null) instance = new NEModelManager();
return instance;
}
private NEModelManager(){
PathMap = new Hashtable<String,MS3DModel>();
}
public MS3DModel load(String path){
try {
if(PathMap.get(path)==null){
InputStream stream = ResourceRetriever.getResourceAsStream(modelDirectory + path);
MS3DModel neu = MS3DModel.decodeMS3DModel(stream);
neu.Path = path;
PathMap.put(path, neu);
debug(neu + " wurde geladen.",false);
return neu;
} else {
return PathMap.get(path);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void preLoad(String[] pathes){
LoadingScreen l = new LoadingScreen("lade: ");
for (int i = 0; i < pathes.length; i++) {
l.DoNextStep(pathes.length+1, i, pathes[i]);
load(pathes[i]);
}
l.dispose();
}
public void preCompileAllLoaded(GL gl){
LoadingScreen l = new LoadingScreen("compiling: ");
int i = 0;
for (MS3DModel m : this.PathMap.values()) {
i++;
l.DoNextStep(PathMap.size(), i, m.Path);
compileModel(m, gl);
}
l.dispose();
}
public void compileModel(MS3DModel model, GL gl){
model.displayListID = displayListCounter;
int vor = displayListCounter;
for (int groupC = 0; groupC < model.groups.length; groupC++) {
MS3DGroup group = model.groups[groupC];
gl.glNewList(++displayListCounter, gl.GL_COMPILE);
model.doGroupOGL(gl,groupC);
gl.glEndList();
group.displayListID = displayListCounter;
}
debug(model + " wurde f�r GL kompeliert! ID:" + vor + " bis " + displayListCounter, false);
}
public MS3DModel getModel(String path){
MS3DModel m = PathMap.get(path);
if (m == null){
load(path);
}
return PathMap.get(path);
}
}