package.d 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Written in the D programming language.
  2. /**
  3. Copyright: Copyright Felix 'Zoadian' Hufnagel 2014-.
  4. License: $(WEB http://www.gnu.org/licenses/lgpl.html, LGPLv3).
  5. Authors: $(WEB zoadian.de, Felix 'Zoadian' Hufnagel)
  6. */
  7. module three.assimp;
  8. import std.traits;
  9. version(none){
  10. Model loadModel(string filePath, Vec3f position = Vec3f(0,0,0), Vec3f scale = Vec3f(1,1,1), Quatf orientation = Quatf(0,0,0,1)) {
  11. if(!exists(filePath)) throw new Exception("File does not exist");
  12. try{
  13. "loading model: ".writeln(filePath);
  14. auto scene = aiImportFile(filePath.toStringz(), 0
  15. //| aiPostProcessSteps.CalcTangentSpace
  16. //| aiPostProcessSteps.Triangulate
  17. //| aiPostProcessSteps.JoinIdenticalVertices
  18. //| aiPostProcessSteps.GenNormals
  19. //| aiPostProcessSteps.FlipWindingOrder
  20. //| aiPostProcessSteps.SortByPType
  21. );
  22. Mesh[] meshes;
  23. for(uint k = 0; k < scene.mNumMeshes; ++k) {
  24. "mesh".writeln();
  25. const(aiMesh*) mesh = scene.mMeshes[k];
  26. assert(mesh !is null);
  27. float[] vertexData;
  28. for (uint t = 0; t < mesh.mNumFaces; ++t) {
  29. //"face".writeln();
  30. const(aiFace*) face = &mesh.mFaces[t];
  31. assert(face !is null);
  32. for(uint v = 0; v < 3; ++v) {
  33. //"a".writeln();
  34. aiVector3D p, n, uv;
  35. assert(face.mNumIndices > v);
  36. uint vertex = face.mIndices[v];
  37. assert(mesh.mNumVertices > vertex);
  38. p = mesh.mVertices[vertex];
  39. n = mesh.mNormals[vertex];
  40. // check if the mesh has texture coordinates
  41. if(mesh.mTextureCoords[0] !is null) {
  42. uv = mesh.mTextureCoords[0][vertex];
  43. }
  44. //TODO: speed this up!
  45. vertexData ~= [p.x, p.y, p.z, n.x, n.y, n.z, uv.x, uv.y, 1.0f, 1.0f, 1.0f];
  46. }
  47. }
  48. "genvdata".writeln();
  49. //alias VertexData!(VertexBufferObject!(BufferTarget.Array, VTX_POSITION, VTX_NORMAL, VTX_TEXCOORD, VTX_COLOR)) AssimpVertexData;
  50. //meshes ~= new VertexMesh!(AssimpVertexData)(vertexData, mesh.mNumFaces);
  51. }
  52. "release".writeln();
  53. aiReleaseImport(scene);
  54. "loaded".writeln();
  55. return new Model(position, scale, orientation, meshes);
  56. }catch(Exception e)
  57. {
  58. assert(0);
  59. }
  60. }
  61. }