Hello Guest

Troubles with load obj file via Assimp

  • 0 Replies
  • 1002 Views
Troubles with load obj file via Assimp
« on: May 01, 2023, 13:44:29 »
I am trying to load obj file with Assimp but I am stumbling on problem. For some reasons file reading fail and i don't understand why.

public RawModel loadToVAO(String resourcePath) {
         AIScene scene = Assimp.aiImportFileFromMemory(
                  ByteBuffer.wrap(resourcePath.getBytes()) ,
                  Assimp.aiProcess_JoinIdenticalVertices | Assimp.aiProcess_Triangulate | Assimp.aiProcess_FixInfacingNormals,
                  "obj"
          );
          if (scene == null) {
                 throw new RuntimeException(String.format("Could not load %s", resourcePath));
             }
         int vaoID = createVAO();
         System.out.println(Assimp.aiGetErrorString());
           PointerBuffer buffer = scene.mMeshes();
           IntBuffer indices;
           /*ArrayList<Float> positions=new ArrayList<Float>(),texCoords=new ArrayList<Float>(), normals=new ArrayList<Float>();
           ArrayList<Integer>indices=new ArrayList<Integer>();*/
          
               AIMesh mesh = AIMesh.create(buffer.get(0));
              
               AIVector3D.Buffer vectors = mesh.mVertices();
               FloatBuffer positions = BufferUtils.createFloatBuffer(vectors.limit());
             

              for(int i = 0; i < vectors.limit(); i++)
              {
                  AIVector3D vector = vectors.get(i);

                  positions.put(vector.x());
                  positions.put(vector.y());
                  positions.put(vector.z());
              }

              AIVector3D.Buffer coords = mesh.mTextureCoords(0);
              FloatBuffer texCoords = BufferUtils.createFloatBuffer(coords.limit());

              for(int i= 0; i < coords.limit(); i++)
              {
                  AIVector3D coord = coords.get(i);

                  texCoords.put(coords.x());
                  texCoords.put(coords.y());
              }

              AIVector3D.Buffer norms = mesh.mNormals();
              FloatBuffer normals = BufferUtils.createFloatBuffer(norms.limit());
              for(int i = 0; i < norms.limit(); i++)
              {
                  AIVector3D norm = norms.get(i);

                  normals.put(norm.x());
                  normals.put(norm.y());
                  normals.put(norm.z());
              }
             
              AIFace.Buffer inds = mesh.mFaces();
              indices = inds.mIndices();
              bindIndicesBuffer(indices);
              storeDataInAttributeList(0,3,positions);
            storeDataInAttributeList(1,2,texCoords);
            storeDataInAttributeList(2,3,normals);
            unbindVAO();