public static AnimGameItem loadAnimGameItem(String resourcePath, String texturesDir) throws Exception { return loadAnimGameItem(resourcePath, texturesDir, aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices | aiProcess_Triangulate | aiProcess_FixInfacingNormals | aiProcess_LimitBoneWeights); }
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; }
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 Map<String, Animation> processAnimations(AIScene aiScene, List<Bone> boneList, Node rootNode, Matrix4f rootTransformation) { Map<String, Animation> animations = new HashMap<>(); // Process all animations int numAnimations = aiScene.mNumAnimations(); PointerBuffer aiAnimations = aiScene.mAnimations(); for (int i = 0; i < numAnimations; i++) { AIAnimation aiAnimation = AIAnimation.create(aiAnimations.get(i)); // Calculate transformation matrices for each node int numChanels = aiAnimation.mNumChannels(); PointerBuffer aiChannels = aiAnimation.mChannels(); for (int j = 0; j < numChanels; j++) { AINodeAnim aiNodeAnim = AINodeAnim.create(aiChannels.get(j)); String nodeName = aiNodeAnim.mNodeName().dataString(); Node node = rootNode.findByName(nodeName); buildTransFormationMatrices(aiNodeAnim, node); } List<AnimatedFrame> frames = buildAnimationFrames(boneList, rootNode, rootTransformation); Animation animation = new Animation(aiAnimation.mName().dataString(), frames, aiAnimation.mDuration()); animations.put(animation.getName(), animation); } return animations; }
private static Node processNodesHierarchy(AINode aiNode, Node parentNode) { String nodeName = aiNode.mName().dataString(); Node node = new Node(nodeName, parentNode); int numChildren = aiNode.mNumChildren(); PointerBuffer aiChildren = aiNode.mChildren(); for (int i = 0; i < numChildren; i++) { AINode aiChildNode = AINode.create(aiChildren.get(i)); Node childNode = processNodesHierarchy(aiChildNode, node); node.addChild(childNode); } return node; }
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; }
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 Map<String, Animation> processAnimations(AIScene aiScene, List<Bone> boneList, Node rootNode, Matrix4f rootTransformation) { Map<String, Animation> animations = new HashMap<>(); // Process all animations int numAnimations = aiScene.mNumAnimations(); PointerBuffer aiAnimations = aiScene.mAnimations(); for (int i = 0; i < numAnimations; i++) { AIAnimation aiAnimation = AIAnimation.create(aiAnimations.get(i)); // Calculate transformation matrices for each node int numChanels = aiAnimation.mNumChannels(); PointerBuffer aiChannels = aiAnimation.mChannels(); for (int j = 0; j < numChanels; j++) { AINodeAnim aiNodeAnim = AINodeAnim.create(aiChannels.get(j)); String nodeName = aiNodeAnim.mNodeName().dataString(); Node node = rootNode.findByName(nodeName); buildTransFormationMatrices(aiNodeAnim, node); } List<AnimatedFrame> frames = buildAnimationFrames(boneList, rootNode, rootTransformation); Animation animation = new Animation(aiAnimation.mName().dataString(), frames, aiAnimation.mDuration()); animations.put(animation.getName(), animation); } return animations; }
private static Node processNodesHierarchy(AINode aiNode, Node parentNode) { String nodeName = aiNode.mName().dataString(); Node node = new Node(nodeName, parentNode); int numChildren = aiNode.mNumChildren(); PointerBuffer aiChildren = aiNode.mChildren(); for (int i = 0; i < numChildren; i++) { AINode aiChildNode = AINode.create(aiChildren.get(i)); Node childNode = processNodesHierarchy(aiChildNode, node); node.addChild(childNode); } return node; }
public static AnimGameItem loadAnimGameItem(String resourcePath, String texturesDir) throws Exception { return loadAnimGameItem(resourcePath, texturesDir, aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices | aiProcess_Triangulate | aiProcess_FixInfacingNormals | aiProcess_LimitBoneWeights); }
@Override public void init(Window window) throws Exception { renderer.init(window); scene = new Scene(); Mesh[] terrainMesh = StaticMeshesLoader.load("src/main/resources/models/terrain/terrain.obj", "src/main/resources/models/terrain"); GameItem terrain = new GameItem(terrainMesh); terrain.setScale(100.0f); animItem = AnimMeshesLoader.loadAnimGameItem("src/main/resources/models/bob/boblamp.md5mesh", ""); animItem.setScale(0.05f); animation = animItem.getCurrentAnimation(); scene.setGameItems(new GameItem[]{animItem, terrain}); // Shadows scene.setRenderShadows(true); // Fog Vector3f fogColour = new Vector3f(0.5f, 0.5f, 0.5f); scene.setFog(new Fog(true, fogColour, 0.02f)); // Setup SkyBox float skyBoxScale = 100.0f; SkyBox skyBox = new SkyBox("src/main/resources/models/skybox.obj", new Vector4f(0.65f, 0.65f, 0.65f, 1.0f)); skyBox.setScale(skyBoxScale); scene.setSkyBox(skyBox); // Setup Lights setupLights(); camera.getPosition().x = -1.5f; camera.getPosition().y = 3.0f; camera.getPosition().z = 4.5f; camera.getRotation().x = 15.0f; camera.getRotation().y = 390.0f; }