www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Derelict Assimp not loading mesh properly? (Maybe index buffer)

reply "Bennet" <bennetleff gmail.com> writes:
I wrote a custom OBJ file importer which worked fairly well 
however was not robust enough to support everything. I've decided 
to give AssImp a shot. I followed some tutorials and have set up 
my code to read in the vertices, tex coords, normals, and indices 
of an OBJ cube model that I have had success loading with my 
custom importer. The cube model's faces do not render properly, 
instead only rendering a few tri's of the cube. The way AssImp 
handles the data is obviously a bit different than my solution 
because the normals, positions/vertices, tex coords, and indices 
do not match my custom solution. I would very much appreciate 
some insight into why I'm having issues as all of the tutorials 
I've found have matched my approach. If a picture of the faulty 
rendered cube would be helpful I can work on getting a screenshot 
up. Thank you.
My Asset importing class is at:
http://codebin.org/view/4d2ec4d3
The rest of my code is at (Mesh Class is in source/graphics):
https://github.com/BennetLeff/PhySim
Mar 05 2015
next sibling parent reply "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
On Friday, 6 March 2015 at 02:41:19 UTC, Bennet wrote:
 I wrote a custom OBJ file importer which worked fairly well 
 however was not robust enough to support everything. I've 
 decided to give AssImp a shot. I followed some tutorials and 
 have set up my code to read in the vertices, tex coords, 
 normals, and indices of an OBJ cube model that I have had 
 success loading with my custom importer. The cube model's faces 
 do not render properly, instead only rendering a few tri's of 
 the cube. The way AssImp handles the data is obviously a bit 
 different than my solution because the normals, 
 positions/vertices, tex coords, and indices do not match my 
 custom solution. I would very much appreciate some insight into 
 why I'm having issues as all of the tutorials I've found have 
 matched my approach. If a picture of the faulty rendered cube 
 would be helpful I can work on getting a screenshot up. Thank 
 you.
 My Asset importing class is at:
 http://codebin.org/view/4d2ec4d3
 The rest of my code is at (Mesh Class is in source/graphics):
 https://github.com/BennetLeff/PhySim
There are some issues with your loadMesh code: numVerts = mesh.mNumFaces * 3; The number of vertices is independent if the number of faces. What you're calculating here is the number of indices, which is indeed guaranteed to be three times the number of faces since you're asking for triangulation when loading the scene. The number of vertices is simply: mesh.mNumVertices And this value should be used as size for the vertex, normal, and texture coordinate arrays. Then you'll need two separate loops to load the vertices and indices. Loading the vertices is simply looping through all vertex arrays of the source mesh and copying them to your vertex arrays. To load the indices you need to loop through all faces in the source mesh, and then add the face indices to your index buffer. Something like this: foreach(faceIndex; 0 .. mash.mNumFaces) { auto face = mesh.mFaces[faceIndex]; foreach(i; 0 .. 3) indices[faceIndex*3 + i] = face.mIndices[i]; }
Mar 07 2015
parent reply "Bennet" <bennetleff gmail.com> writes:
On Saturday, 7 March 2015 at 13:11:22 UTC, Rene Zwanenburg wrote:
 On Friday, 6 March 2015 at 02:41:19 UTC, Bennet wrote:
 I wrote a custom OBJ file importer which worked fairly well 
 however was not robust enough to support everything. I've 
 decided to give AssImp a shot. I followed some tutorials and 
 have set up my code to read in the vertices, tex coords, 
 normals, and indices of an OBJ cube model that I have had 
 success loading with my custom importer. The cube model's 
 faces do not render properly, instead only rendering a few 
 tri's of the cube. The way AssImp handles the data is 
 obviously a bit different than my solution because the 
 normals, positions/vertices, tex coords, and indices do not 
 match my custom solution. I would very much appreciate some 
 insight into why I'm having issues as all of the tutorials 
 I've found have matched my approach. If a picture of the 
 faulty rendered cube would be helpful I can work on getting a 
 screenshot up. Thank you.
 My Asset importing class is at:
 http://codebin.org/view/4d2ec4d3
 The rest of my code is at (Mesh Class is in source/graphics):
 https://github.com/BennetLeff/PhySim
There are some issues with your loadMesh code: numVerts = mesh.mNumFaces * 3; The number of vertices is independent if the number of faces. What you're calculating here is the number of indices, which is indeed guaranteed to be three times the number of faces since you're asking for triangulation when loading the scene. The number of vertices is simply: mesh.mNumVertices And this value should be used as size for the vertex, normal, and texture coordinate arrays. Then you'll need two separate loops to load the vertices and indices. Loading the vertices is simply looping through all vertex arrays of the source mesh and copying them to your vertex arrays. To load the indices you need to loop through all faces in the source mesh, and then add the face indices to your index buffer. Something like this: foreach(faceIndex; 0 .. mash.mNumFaces) { auto face = mesh.mFaces[faceIndex]; foreach(i; 0 .. 3) indices[faceIndex*3 + i] = face.mIndices[i]; }
I've made that edit but I'm getting lots of "nan" in my array of vertices, textures coordinates and normals. Here's a screenshot: http://imgur.com/lXDxFJM
Mar 07 2015
parent reply "Bennet" <bennetleff gmail.com> writes:
On Saturday, 7 March 2015 at 21:19:47 UTC, Bennet wrote:
 On Saturday, 7 March 2015 at 13:11:22 UTC, Rene Zwanenburg 
 wrote:
 On Friday, 6 March 2015 at 02:41:19 UTC, Bennet wrote:
 I wrote a custom OBJ file importer which worked fairly well 
 however was not robust enough to support everything. I've 
 decided to give AssImp a shot. I followed some tutorials and 
 have set up my code to read in the vertices, tex coords, 
 normals, and indices of an OBJ cube model that I have had 
 success loading with my custom importer. The cube model's 
 faces do not render properly, instead only rendering a few 
 tri's of the cube. The way AssImp handles the data is 
 obviously a bit different than my solution because the 
 normals, positions/vertices, tex coords, and indices do not 
 match my custom solution. I would very much appreciate some 
 insight into why I'm having issues as all of the tutorials 
 I've found have matched my approach. If a picture of the 
 faulty rendered cube would be helpful I can work on getting a 
 screenshot up. Thank you.
 My Asset importing class is at:
 http://codebin.org/view/4d2ec4d3
 The rest of my code is at (Mesh Class is in source/graphics):
 https://github.com/BennetLeff/PhySim
There are some issues with your loadMesh code: numVerts = mesh.mNumFaces * 3; The number of vertices is independent if the number of faces. What you're calculating here is the number of indices, which is indeed guaranteed to be three times the number of faces since you're asking for triangulation when loading the scene. The number of vertices is simply: mesh.mNumVertices And this value should be used as size for the vertex, normal, and texture coordinate arrays. Then you'll need two separate loops to load the vertices and indices. Loading the vertices is simply looping through all vertex arrays of the source mesh and copying them to your vertex arrays. To load the indices you need to loop through all faces in the source mesh, and then add the face indices to your index buffer. Something like this: foreach(faceIndex; 0 .. mash.mNumFaces) { auto face = mesh.mFaces[faceIndex]; foreach(i; 0 .. 3) indices[faceIndex*3 + i] = face.mIndices[i]; }
I've made that edit but I'm getting lots of "nan" in my array of vertices, textures coordinates and normals. Here's a screenshot: http://imgur.com/lXDxFJM
Update: I've gotten the vertices, normals an uvs to load into arrays properly, now I just can't get the indices loaded. I'm loading them in the manner you suggested but they only load up to index 22 when there are 33 vertices. I imagine there should be indices ranging from 0-33 loaded into the array not 0-22. However there are 33 indices loaded.
Mar 07 2015
parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
On Saturday, 7 March 2015 at 22:01:26 UTC, Bennet wrote:
 Update: I've gotten the vertices, normals an uvs to load into 
 arrays properly, now I just can't get the indices loaded. I'm 
 loading them in the manner you suggested but they only load up 
 to index 22 when there are 33 vertices. I imagine there should 
 be indices ranging from 0-33 loaded into the array not 0-22. 
 However there are 33 indices loaded.
Are you sure about that? Your earlier screenshot shows only 23 vertices..
Mar 08 2015
prev sibling parent reply "Michael Robertson" <mlizard32 yahoo.com> writes:
On Friday, 6 March 2015 at 02:41:19 UTC, Bennet wrote:
 I wrote a custom OBJ file importer which worked fairly well 
 however was not robust enough to support everything. I've 
 decided to give AssImp a shot. I followed some tutorials and 
 have set up my code to read in the vertices, tex coords, 
 normals, and indices of an OBJ cube model that I have had 
 success loading with my custom importer. The cube model's faces 
 do not render properly, instead only rendering a few tri's of 
 the cube. The way AssImp handles the data is obviously a bit 
 different than my solution because the normals, 
 positions/vertices, tex coords, and indices do not match my 
 custom solution. I would very much appreciate some insight into 
 why I'm having issues as all of the tutorials I've found have 
 matched my approach. If a picture of the faulty rendered cube 
 would be helpful I can work on getting a screenshot up. Thank 
 you.
 My Asset importing class is at:
 http://codebin.org/view/4d2ec4d3
 The rest of my code is at (Mesh Class is in source/graphics):
 https://github.com/BennetLeff/PhySim
Imagine a square mesh made up of two triangles, the un-indexed version has 6 vertices, but the indexed version that you would be using has 4 vertices because a side is shared by the two triangles (which is managed by the index array). This is why you can't assume each face has 3 vertices. I have been trying to learn opengl and D recently together and this is the simple code I have been using to load models from assimp. for(int i = 0; i < mesh.mNumVertices; i++) { aiVector3D vert = mesh.mVertices[i]; verts ~= vec3(vert.x, vert.y, vert.z); aiVector3D uvw = mesh.mTextureCoords[0][i]; uvs ~= vec2(uvw.x, uvw.y); aiVector3D n = mesh.mNormals[i]; normals ~= vec3(n.x, n.y, n.z); } for(int i = 0; i < mesh.mNumFaces; i++) { const aiFace face = mesh.mFaces[i]; indices ~= face.mIndices[0]; indices ~= face.mIndices[1]; indices ~= face.mIndices[2]; }
Mar 09 2015
parent "BennetD" <bennetleff gmail.com> writes:
On Monday, 9 March 2015 at 14:25:54 UTC, Michael Robertson wrote:
 On Friday, 6 March 2015 at 02:41:19 UTC, Bennet wrote:
 I wrote a custom OBJ file importer which worked fairly well 
 however was not robust enough to support everything. I've 
 decided to give AssImp a shot. I followed some tutorials and 
 have set up my code to read in the vertices, tex coords, 
 normals, and indices of an OBJ cube model that I have had 
 success loading with my custom importer. The cube model's 
 faces do not render properly, instead only rendering a few 
 tri's of the cube. The way AssImp handles the data is 
 obviously a bit different than my solution because the 
 normals, positions/vertices, tex coords, and indices do not 
 match my custom solution. I would very much appreciate some 
 insight into why I'm having issues as all of the tutorials 
 I've found have matched my approach. If a picture of the 
 faulty rendered cube would be helpful I can work on getting a 
 screenshot up. Thank you.
 My Asset importing class is at:
 http://codebin.org/view/4d2ec4d3
 The rest of my code is at (Mesh Class is in source/graphics):
 https://github.com/BennetLeff/PhySim
Imagine a square mesh made up of two triangles, the un-indexed version has 6 vertices, but the indexed version that you would be using has 4 vertices because a side is shared by the two triangles (which is managed by the index array). This is why you can't assume each face has 3 vertices. I have been trying to learn opengl and D recently together and this is the simple code I have been using to load models from assimp. for(int i = 0; i < mesh.mNumVertices; i++) { aiVector3D vert = mesh.mVertices[i]; verts ~= vec3(vert.x, vert.y, vert.z); aiVector3D uvw = mesh.mTextureCoords[0][i]; uvs ~= vec2(uvw.x, uvw.y); aiVector3D n = mesh.mNormals[i]; normals ~= vec3(n.x, n.y, n.z); } for(int i = 0; i < mesh.mNumFaces; i++) { const aiFace face = mesh.mFaces[i]; indices ~= face.mIndices[0]; indices ~= face.mIndices[1]; indices ~= face.mIndices[2]; }
My code roughly matches yours at this point however I get a weird bug with only my cube model where the top face and one of the side tri's are not rendered. However it seems to work fine with a model of a pitcher and a model of a monkey.
Mar 10 2015