137 lines
5.5 KiB
Java
137 lines
5.5 KiB
Java
package electrosphere.renderer.loading;
|
|
|
|
import electrosphere.engine.Globals;
|
|
import electrosphere.logger.LoggerInterface;
|
|
import electrosphere.renderer.model.Material;
|
|
import electrosphere.renderer.model.Mesh;
|
|
import electrosphere.renderer.model.Model;
|
|
import electrosphere.renderer.texture.TextureMap;
|
|
import electrosphere.renderer.texture.TextureMap.MeshTextureData;
|
|
import electrosphere.util.FileUtils;
|
|
|
|
import java.io.File;
|
|
import org.lwjgl.assimp.AIScene;
|
|
import static org.lwjgl.assimp.Assimp.*;
|
|
|
|
/**
|
|
* Main model loading class
|
|
*/
|
|
public class ModelLoader {
|
|
|
|
/**
|
|
* Loads a model via assimp
|
|
* @param path The path to the model
|
|
* @return The model if it exists, null otherwise
|
|
*/
|
|
public static AIScene loadAIScene(String path){
|
|
AIScene rVal;
|
|
// File file = new File(Thread.currentThread().getContextClassLoader().getResource(fileName).getFile());
|
|
// Main.class.getResourceAsStream(fileName).readAllBytes();
|
|
File toRead = FileUtils.getAssetFile(path);
|
|
rVal = aiImportFile(toRead.getAbsolutePath(),
|
|
aiProcess_GenSmoothNormals |
|
|
aiProcess_JoinIdenticalVertices |
|
|
aiProcess_Triangulate |
|
|
aiProcess_FixInfacingNormals |
|
|
aiProcess_LimitBoneWeights |
|
|
aiProcess_GlobalScale
|
|
);
|
|
if(rVal == null){
|
|
LoggerInterface.loggerRenderer.ERROR(new IllegalStateException(aiGetErrorString()));
|
|
}
|
|
return rVal;
|
|
}
|
|
|
|
public static Model createModelFromAiScene(AIScene scene, TextureMap localTextureMap, String path){
|
|
Model rVal = null;
|
|
if(scene != null){
|
|
rVal = Model.createModelFromAiscene(path, scene);
|
|
attemptAddTexturesFromPathname(path, localTextureMap, rVal);
|
|
}
|
|
return rVal;
|
|
}
|
|
|
|
|
|
/**
|
|
* Attempt to assign textures to meshes on this model based on texture map entries
|
|
* @param path The path to the model
|
|
* @param localTextureMap The local texture map
|
|
* @param m The model
|
|
*/
|
|
static void attemptAddTexturesFromPathname(String path, TextureMap localTextureMap, Model m){
|
|
|
|
LoggerInterface.loggerRenderer.DEBUG("Load textures for " + path);
|
|
|
|
if(Globals.textureMapDefault.containsModel(path)){
|
|
//
|
|
//load from global map
|
|
//
|
|
MeshTextureData defaultMeshData = Globals.textureMapDefault.getDefaultMeshTextures(path);
|
|
for(Mesh mesh : m.getMeshes()){
|
|
MeshTextureData meshTextureData = Globals.textureMapDefault.getMeshTextures(path, mesh.getMeshName());
|
|
if(meshTextureData != null){
|
|
setMaterial(mesh,meshTextureData);
|
|
} else if(defaultMeshData != null){
|
|
setMaterial(mesh,defaultMeshData);
|
|
} else {
|
|
LoggerInterface.loggerRenderer.WARNING("Model " + path + " does not have texture data defined for \"" + mesh.getMeshName() + "\"");
|
|
}
|
|
}
|
|
} else if(localTextureMap != null && localTextureMap.containsModel(path)) {
|
|
//
|
|
//load from local folder for model
|
|
//
|
|
MeshTextureData defaultMeshData = localTextureMap.getDefaultMeshTextures(path);
|
|
for(Mesh mesh : m.getMeshes()){
|
|
MeshTextureData meshTextureData = localTextureMap.getMeshTextures(path, mesh.getMeshName());
|
|
if(meshTextureData != null){
|
|
setMaterial(mesh,meshTextureData);
|
|
} else if(defaultMeshData != null){
|
|
setMaterial(mesh,defaultMeshData);
|
|
} else {
|
|
LoggerInterface.loggerRenderer.WARNING("Model " + path + " does not have texture data defined for \"" + mesh.getMeshName() + "\"");
|
|
}
|
|
}
|
|
} else {
|
|
LoggerInterface.loggerRenderer.WARNING("Trying to get textures for model that doesn't have local or global texture map entries! " + path);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets the material for the mesh
|
|
* @param mesh The mesh
|
|
* @param meshTextureData The texture data for the mesh
|
|
*/
|
|
private static void setMaterial(Mesh mesh, MeshTextureData meshTextureData){
|
|
Material finalMat = new Material();
|
|
|
|
//set diffuse
|
|
String diffusePath = meshTextureData.getDiffuse();
|
|
LoggerInterface.loggerRenderer.DEBUG(mesh.getMeshName() + "->" + diffusePath);
|
|
if(diffusePath != null){
|
|
LoggerInterface.loggerRenderer.DEBUG(diffusePath);
|
|
// Texture diffuse = new Texture(diffuse_path);
|
|
Globals.assetManager.addTexturePathtoQueue(diffusePath);
|
|
finalMat.set_diffuse(diffusePath);
|
|
LoggerInterface.loggerRenderer.DEBUG(diffusePath);
|
|
} else {
|
|
finalMat.set_diffuse(Globals.textureDiffuseDefault);
|
|
}
|
|
|
|
//set specular
|
|
String specularPath = meshTextureData.getSpecular();
|
|
if(specularPath != null){
|
|
// Texture specular = new Texture(specular_path);
|
|
Globals.assetManager.addTexturePathtoQueue(specularPath);
|
|
finalMat.set_specular(specularPath);
|
|
LoggerInterface.loggerRenderer.DEBUG(specularPath);
|
|
} else {
|
|
finalMat.set_specular(Globals.textureSpecularDefault);
|
|
}
|
|
//once we've either added default textures or actual textures,
|
|
//set the current mesh's material to this new one
|
|
mesh.setMaterial(finalMat);
|
|
}
|
|
|
|
}
|