/** * Loads one or more meshes at the specified resource path with * the textures at the specified texture path with the specified flags. * * @param resourcePath - Path for the mesh resource to load. * @param texturesDir - Path for the textures to use for the mesh. * @param flags - Flags for the Assimp importer to use. * * @return Mesh array with the loaded meshes. * * @throws Exception */ public static Mesh[] load(String resourcePath, String texturesDir, int flags) throws Exception { AIScene aiScene = Assimp.aiImportFile(resourcePath, flags); if (aiScene == null) { throw new Exception("Error loading model"); } int numMaterials = aiScene.mNumMaterials(); PointerBuffer aiMaterials = aiScene.mMaterials(); List<Material> materials = new ArrayList<>(); for (int i = 0; i < numMaterials; i++) { AIMaterial aiMaterial = AIMaterial.create(aiMaterials.get(i)); processMaterial(aiMaterial, materials, texturesDir); } int numMeshes = aiScene.mNumMeshes(); PointerBuffer aiMeshes = aiScene.mMeshes(); Mesh[] meshes = new Mesh[numMeshes]; for (int i = 0; i < numMeshes; i++) { AIMesh aiMesh = AIMesh.create(aiMeshes.get(i)); Mesh mesh = processMesh(aiMesh, materials); meshes[i] = mesh; } return meshes; }
public static Mesh[] load(String resourcePath, String texturesDir, int flags) throws Exception { AIScene aiScene = aiImportFile(resourcePath, flags); if (aiScene == null) { throw new Exception("Error loading model"); } int numMaterials = aiScene.mNumMaterials(); PointerBuffer aiMaterials = aiScene.mMaterials(); List<Material> materials = new ArrayList<>(); for (int i = 0; i < numMaterials; i++) { AIMaterial aiMaterial = AIMaterial.create(aiMaterials.get(i)); processMaterial(aiMaterial, materials, texturesDir); } int numMeshes = aiScene.mNumMeshes(); PointerBuffer aiMeshes = aiScene.mMeshes(); Mesh[] meshes = new Mesh[numMeshes]; for (int i = 0; i < numMeshes; i++) { AIMesh aiMesh = AIMesh.create(aiMeshes.get(i)); Mesh mesh = processMesh(aiMesh, materials); meshes[i] = mesh; } return meshes; }
private static Mesh processMesh(AIMesh aiMesh, List<Material> materials) { List<Float> vertices = new ArrayList<>(); List<Float> textures = new ArrayList<>(); List<Float> normals = new ArrayList<>(); List<Integer> indices = new ArrayList(); processVertices(aiMesh, vertices); processNormals(aiMesh, normals); processTextCoords(aiMesh, textures); processIndices(aiMesh, indices); Mesh mesh = new Mesh(Utils.listToArray(vertices), Utils.listToArray(textures), Utils.listToArray(normals), Utils.listIntToArray(indices)); Material material; int materialIdx = aiMesh.mMaterialIndex(); if (materialIdx >= 0 && materialIdx < materials.size()) { material = materials.get(materialIdx); } else { material = new Material(); } mesh.setMaterial(material); return mesh; }
/** * Process one mesh. * * @param aiMesh - AIMesh to process. * @param materials - List of materials. * * @return A fully processed mesh. */ private static Mesh processMesh(AIMesh aiMesh, List<Material> materials) { List<Float> vertices = new ArrayList<>(); List<Float> textures = new ArrayList<>(); List<Float> normals = new ArrayList<>(); List<Integer> indices = new ArrayList<>(); processVertices(aiMesh, vertices); processTextureCoordinates(aiMesh, textures); processNormals(aiMesh, normals); processIndices(aiMesh, indices); Mesh mesh = new Mesh(ArrayHelper.listToArrayFloat(vertices), ArrayHelper.listToArrayFloat(textures), ArrayHelper.listToArrayFloat(normals), ArrayHelper.listToArrayInt(indices)); Material material; int materialIdx = aiMesh.mMaterialIndex(); if (materialIdx >= 0 && materialIdx < materials.size()) { material = materials.get(materialIdx); } else { material = new Material(); } mesh.setMaterial(material); return mesh; }
/** * Proceses the vertices of a mesh. * * @param aiMesh - AIMesh to process vertices for. * @param vertices - List of vertices to add processed vertices to. */ private static void processVertices(AIMesh aiMesh, List<Float> vertices) { AIVector3D.Buffer aiVertices = aiMesh.mVertices(); while(aiVertices.remaining() > 0) { AIVector3D aiVertex = aiVertices.get(); vertices.add(aiVertex.x()); vertices.add(aiVertex.y()); vertices.add(aiVertex.z()); } }
/** * Proceses the texture coordinates of a mesh. * * @param aiMesh - AIMesh to process texture coordinates for. * @param textures - List of texture coordinates to add * processed coordinates to. */ private static void processTextureCoordinates(AIMesh aiMesh, List<Float> textures) { AIVector3D.Buffer aiTextures = aiMesh.mTextureCoords(0); int numTextCoords = aiTextures != null ? aiTextures.remaining() : 0; for (int i = 0; i < numTextCoords; i++) { AIVector3D textCoord = aiTextures.get(); textures.add(textCoord.x()); textures.add(1 - textCoord.y()); } }
/** * Proceses the normals of a mesh. * * @param aiMesh - AIMesh to process normals for. * @param normals - List of normals to add processed normals to. */ private static void processNormals(AIMesh aiMesh, List<Float> normals) { AIVector3D.Buffer aiNormals = aiMesh.mNormals(); while(aiNormals != null && aiNormals.remaining() > 0) { AIVector3D aiVertex = aiNormals.get(); normals.add(aiVertex.x()); normals.add(aiVertex.y()); normals.add(aiVertex.z()); } }
/** * Proceses the indices of a mesh. * * @param aiMesh - AIMesh to process indices for. * @param indices - List of indices to add processed indices to. */ private static void processIndices(AIMesh aiMesh, List<Integer> indices) { int numFaces = aiMesh.mNumFaces(); AIFace.Buffer aiFaces = aiMesh.mFaces(); for (int i = 0; i < numFaces; i++) { AIFace aiFace = aiFaces.get(); IntBuffer buffer = aiFace.mIndices(); while (buffer.remaining() > 0) { indices.add(buffer.get()); } } }
/** * Loads a model file and a texture to a {@link TexturedModel}. This method requires flags that will determine how * how the mesh is handled. * * @param modelPath Path to the model * @param texturePath Path to the texture. If the model contains a material library, * the material library should point to the texture's folder. * @param flags Flags separated by the Binary OR operator, "|" * @return Array of {@link TexturedModel}s that were contained by the * model file. */ public static TexturedModel[] load(String modelPath, String texturePath, int flags) { AIScene aiScene = aiImportFile(modelPath, flags); if (aiScene == null) throw new RuntimeException("Could not load model: " + modelPath + "\nWith flags " + flags); int numMaterials = aiScene.mNumMaterials(); PointerBuffer aiMaterials = aiScene.mMaterials(); List<Material> materials = new ArrayList<Material>(); for (int i = 0; i < numMaterials; i++) { AIMaterial aiMaterial = AIMaterial.create(aiMaterials.get(i)); processMat(aiMaterial, materials, texturePath); } int numMeshes = aiScene.mNumMeshes(); PointerBuffer aiMeshes = aiScene.mMeshes(); TexturedModel[] meshes = new TexturedModel[numMeshes]; for (int i = 0; i < numMeshes; i++) { AIMesh aiMesh = AIMesh.create(aiMeshes.get(i)); TexturedModel mesh = processStaticMesh(aiMesh, materials); String name = modelPath.split("/")[modelPath.split("/").length - 1]; String tex = texturePath.split("/")[texturePath.split("/").length - 1]; DBObject obj = new DBObject(name, DBObjectType.MODEL); obj.addArray(DBArray.createFloatArray("vertices", mesh.getModelData().getVertices())); obj.addArray(DBArray.createFloatArray("textureCoords", mesh.getModelData().getTextureCoords())); obj.addArray(DBArray.createFloatArray("normals", mesh.getModelData().getNormals())); obj.addArray(DBArray.createIntegerArray("indices", mesh.getModelData().getIndices())); obj.addString(DBString.create("texture", tex)); AssetCache.db.addObject(obj); meshes[i] = mesh; } AssetCache.db.serialize("assets.lum"); return meshes; }
private static void processVertices(AIMesh aiMesh, List<Float> vertices) { AIVector3D.Buffer aiVertices = aiMesh.mVertices(); while (aiVertices.remaining() > 0) { AIVector3D aiVertex = aiVertices.get(); vertices.add(aiVertex.x()); vertices.add(aiVertex.y()); vertices.add(aiVertex.z()); } }
private static void processNormals(AIMesh aiMesh, List<Float> normals) { AIVector3D.Buffer aiVertices = aiMesh.mVertices(); while (aiVertices.remaining() > 0) { AIVector3D aiVertex = aiVertices.get(); normals.add(aiVertex.x()); normals.add(aiVertex.y()); normals.add(aiVertex.z()); } }
private static void processTextureCoordinates(AIMesh aiMesh, List<Float> textures) { AIVector3D.Buffer textCoords = aiMesh.mTextureCoords(0); int numTextCoords = textCoords != null ? textCoords.remaining() : 0; for (int i = 0; i < numTextCoords; i++) { AIVector3D textCoord = textCoords.get(); textures.add(textCoord.x()); textures.add(1 - textCoord.y()); } }
private static void processIndices(AIMesh aiMesh, List<Integer> indices) { int numFaces = aiMesh.mNumFaces(); AIFace.Buffer aiFaces = aiMesh.mFaces(); for (int i = 0; i < numFaces; i++) { AIFace aiFace = aiFaces.get(i); IntBuffer buffer = aiFace.mIndices(); while (buffer.remaining() > 0) { indices.add(buffer.get()); } } }
protected static void processIndices(AIMesh aiMesh, List<Integer> indices) { int numFaces = aiMesh.mNumFaces(); AIFace.Buffer aiFaces = aiMesh.mFaces(); for (int i = 0; i < numFaces; i++) { AIFace aiFace = aiFaces.get(i); IntBuffer buffer = aiFace.mIndices(); while (buffer.remaining() > 0) { indices.add(buffer.get()); } } }
protected static void processNormals(AIMesh aiMesh, List<Float> normals) { AIVector3D.Buffer aiNormals = aiMesh.mNormals(); while (aiNormals != null && aiNormals.remaining() > 0) { AIVector3D aiNormal = aiNormals.get(); normals.add(aiNormal.x()); normals.add(aiNormal.y()); normals.add(aiNormal.z()); } }
protected static void processTextCoords(AIMesh aiMesh, List<Float> textures) { AIVector3D.Buffer textCoords = aiMesh.mTextureCoords(0); int numTextCoords = textCoords != null ? textCoords.remaining() : 0; for (int i = 0; i < numTextCoords; i++) { AIVector3D textCoord = textCoords.get(); textures.add(textCoord.x()); textures.add(1 - textCoord.y()); } }
protected static void processVertices(AIMesh aiMesh, List<Float> vertices) { AIVector3D.Buffer aiVertices = aiMesh.mVertices(); while (aiVertices.remaining() > 0) { AIVector3D aiVertex = aiVertices.get(); vertices.add(aiVertex.x()); vertices.add(aiVertex.y()); vertices.add(aiVertex.z()); } }
public static AnimGameItem loadAnimGameItem(String resourcePath, String texturesDir, int flags) throws Exception { AIScene aiScene = aiImportFile(resourcePath, flags); if (aiScene == null) { throw new Exception("Error loading model"); } int numMaterials = aiScene.mNumMaterials(); PointerBuffer aiMaterials = aiScene.mMaterials(); List<Material> materials = new ArrayList<>(); for (int i = 0; i < numMaterials; i++) { AIMaterial aiMaterial = AIMaterial.create(aiMaterials.get(i)); processMaterial(aiMaterial, materials, texturesDir); } List<Bone> boneList = new ArrayList<>(); int numMeshes = aiScene.mNumMeshes(); PointerBuffer aiMeshes = aiScene.mMeshes(); Mesh[] meshes = new Mesh[numMeshes]; for (int i = 0; i < numMeshes; i++) { AIMesh aiMesh = AIMesh.create(aiMeshes.get(i)); Mesh mesh = processMesh(aiMesh, materials, boneList); meshes[i] = mesh; } AINode aiRootNode = aiScene.mRootNode(); Matrix4f rootTransfromation = AnimMeshesLoader.toMatrix(aiRootNode.mTransformation()); Node rootNode = processNodesHierarchy(aiRootNode, null); Map<String, Animation> animations = processAnimations(aiScene, boneList, rootNode, rootTransfromation); AnimGameItem item = new AnimGameItem(meshes, animations); return item; }
private static Mesh processMesh(AIMesh aiMesh, List<Material> materials, List<Bone> boneList) { List<Float> vertices = new ArrayList<>(); List<Float> textures = new ArrayList<>(); List<Float> normals = new ArrayList<>(); List<Integer> indices = new ArrayList<>(); List<Integer> boneIds = new ArrayList<>(); List<Float> weights = new ArrayList<>(); processVertices(aiMesh, vertices); processNormals(aiMesh, normals); processTextCoords(aiMesh, textures); processIndices(aiMesh, indices); processBones(aiMesh, boneList, boneIds, weights); Mesh mesh = new Mesh(Utils.listToArray(vertices), Utils.listToArray(textures), Utils.listToArray(normals), Utils.listIntToArray(indices), Utils.listIntToArray(boneIds), Utils.listToArray(weights)); Material material; int materialIdx = aiMesh.mMaterialIndex(); if (materialIdx >= 0 && materialIdx < materials.size()) { material = materials.get(materialIdx); } else { material = new Material(); } mesh.setMaterial(material); return mesh; }
private static Mesh processMesh(AIMesh aiMesh, List<Material> materials) { List<Float> vertices = new ArrayList<>(); List<Float> textures = new ArrayList<>(); List<Float> normals = new ArrayList<>(); List<Integer> indices = new ArrayList(); processVertices(aiMesh, vertices); processNormals(aiMesh, normals); processTextCoords(aiMesh, textures); processIndices(aiMesh, indices); Mesh mesh = new Mesh(Utils.listToArray(vertices), Utils.listToArray(textures), Utils.listToArray(normals), Utils.listIntToArray(indices) ); Material material; int materialIdx = aiMesh.mMaterialIndex(); if (materialIdx >= 0 && materialIdx < materials.size()) { material = materials.get(materialIdx); } else { material = new Material(); } mesh.setMaterial(material); return mesh; }
private static void processNormals(AIMesh aiMesh, List<Float> normals) { AIVector3D.Buffer aiNormals = aiMesh.mNormals(); while (aiNormals != null && aiNormals.remaining() > 0) { AIVector3D aiNormal = aiNormals.get(); normals.add(aiNormal.x()); normals.add(aiNormal.y()); normals.add(aiNormal.z()); } }
private static void processTextCoords(AIMesh aiMesh, List<Float> textures) { AIVector3D.Buffer textCoords = aiMesh.mTextureCoords(0); int numTextCoords = textCoords != null ? textCoords.remaining() : 0; for (int i = 0; i < numTextCoords; i++) { AIVector3D textCoord = textCoords.get(); textures.add(textCoord.x()); textures.add(1 - textCoord.y()); } }
public static List<Model> loadModel(String path, String file) { List<Model> models = new ArrayList<>(); List<Material> materials = new ArrayList<>(); path = AssimpStaticModelLoader.class.getClassLoader().getResource(path).getPath().toString(); if (path.startsWith("/")) path = path.substring(1); AIScene aiScene = Assimp.aiImportFile(path + "/" + file, 0); if (aiScene.mMaterials() != null){ for (int i=0; i<aiScene.mNumMaterials(); i++){ AIMaterial aiMaterial = AIMaterial.create(aiScene.mMaterials().get(i)); Material material = processMaterial(aiMaterial, path); materials.add(material); } } for (int i=0; i<aiScene.mNumMeshes(); i++){ AIMesh aiMesh = AIMesh.create(aiScene.mMeshes().get(i)); Mesh mesh = processMesh(aiMesh); Model model = new Model(); model.setMesh(mesh); int materialIndex = aiMesh.mMaterialIndex(); model.setMaterial(materials.get(materialIndex)); models.add(model); } return models; }
private static TexturedModel processStaticMesh(AIMesh aiMesh, List<Material> materials) { List<Float> vertices = new ArrayList<Float>(); List<Float> textures = new ArrayList<Float>(); List<Float> normals = new ArrayList<Float>(); List<Integer> indices = new ArrayList<Integer>(); processVertices(aiMesh, vertices); processNormals(aiMesh, normals); processTextureCoordinates(aiMesh, textures); processIndices(aiMesh, indices); float[] vertArray = new float[vertices.size()]; float[] normArray = new float[normals.size()]; for (int i = 0; i < vertices.size(); i++) { vertArray[i] = vertices.get(i); normArray[i] = normals.get(i); } float[] textArray = new float[textures.size()]; for (int i = 0; i < textures.size(); i++) textArray[i] = textures.get(i); int[] indArray = new int[indices.size()]; for (int i = 0; i < indices.size(); i++) indArray[i] = indices.get(i); VertexArray vao = null; if (aiMesh.mNumAnimMeshes() == 0) vao = Loader.getInstance().load(vertArray, textArray, normArray, indArray); ModelData md = new ModelData(vertArray, textArray, normArray, indArray); assert (vao != null); Material material; int materialIdx = aiMesh.mMaterialIndex(); if (materialIdx >= 0 && materialIdx < materials.size()) { material = materials.get(materialIdx); } else { material = new Material(); } TexturedModel model = new TexturedModel(vao, material, md); return model; }