lsalzman / iqm

Inter-Quake Model format development kit
MIT License
239 stars 73 forks source link

How would one extract the triangles from the IQM file? #21

Open FuzzyQuils opened 8 years ago

FuzzyQuils commented 8 years ago

Hello. I ask if this is possible: Essentially, I am using IQM format in my game engine, but for my physics engine to work, I feed in an array of this struct: struct Triangle { Vec3 p1; Vec3 p2; Vec3 p3; }

For loading the IQM files, I'm using a modified version of the demo implementation, wrapped into a special class I wrote myself.

Now, rendering's fine, but how would one actually extract the vertex coordinates from the stream, using the indices, and put that into an array of that struct above? my attempt was this:

trisSoup IQM_Mesh::exporttris () {
    iqmmesh &m = meshes[0];
    trisSoup ps;
    ps.tris.resize(m.num_triangles);

    for (int i = 0; i < m.num_triangles; i += 1) {
        ps.tris[i].p1 = Vec3 { inposition[tris[i].vertex[0]], inposition[tris[i].vertex[0]+1], inposition[tris[i].vertex[0]+2] };
        ps.tris[i].p2 = Vec3 { inposition[tris[i].vertex[1]], inposition[tris[i].vertex[1]+1], inposition[tris[i].vertex[1]+2] };
        ps.tris[i].p3 = Vec3 { inposition[tris[i].vertex[2]], inposition[tris[i].vertex[2]+1], inposition[tris[i].vertex[2]+2] };
    }

    ps.length = m.num_triangles;
    return ps;
}

note that trisSoup is a class using an std::vector of the triangle struct. With that code, i would assume it's correct to address with the tris array, but I end up with a big mess instead of my platform when I do a test rendering of the mesh. I assume this is because I messed up something in the exporting macro...

So, my question is: what is the right way to get a list of triangles from the IQM's vertex stream?

Regards

FuzzyQuills

lsalzman commented 8 years ago

The positions array is just a big flat array of vertex coordinates, and the index list for the triangles is just indexes, so your code is mostly okay.

The only problem is you need to remove the vertex index is a vertex index, not a component index, so you need to treat outposition like a Vec3, not a float (assuming your Vec3 has 3 floats in it). So either multiply the vertex index by 3 if you still want to keep outposition as a float, or make outposition a Vec3 so the compiler will automatically do that for you. It will also get rid of the need to construct those Vec3s in your loop, since the compiler will handle loading all 3 components at a time for you that way.

On Wed, Feb 10, 2016 at 4:04 AM, FuzzyQuills notifications@github.com wrote:

Hello. I ask if this is possible: Essentially, I am using IQM format in my game engine, but for my physics engine to work, I feed in an array of this struct: struct Triangle { Vec3 p1; Vec3 p2; Vec3 p3; }

For loading the IQM files, I'm using a modified version of the demo implementation, wrapped into a special class I wrote myself.

Now, rendering's fine, but how would one actually extract the vertex coordinates from the stream, using the indices, and put that into an array of that struct above? my attempt was this: `trisSoup IQM_Mesh::exporttris () { iqmmesh &m = meshes[0]; trisSoup ps; ps.tris.resize(m.num_triangles);

for (int i = 0; i < m.num_triangles; i += 1) { ps.tris[i].p1 = Vec3 { outposition[tris[i].vertex[0]], outposition[tris[i].vertex[0]+1], outposition[tris[i].vertex[0]+2] }; ps.tris[i].p2 = Vec3 { outposition[tris[i].vertex[1]], outposition[tris[i].vertex[1]+1], outposition[tris[i].vertex[1]+2] }; ps.tris[i].p3 = Vec3 { outposition[tris[i].vertex[2]], outposition[tris[i].vertex[2]+1], outposition[tris[i].vertex[2]+2] }; }

ps.length = m.num_triangles; return ps;

}`

note that trisSoup is a class using an std::vector of the triangle struct. With that code, i would assume it's correct to address with the tris array, but I end up with a big mess instead of my platform when I do a test rendering of the mesh. I assume this is because I messed up something in the exporting macro...

So, my question is: what is the right way to get a list of triangles from the IQM's vertex stream?

Regards

FuzzyQuills

— Reply to this email directly or view it on GitHub https://github.com/lsalzman/iqm/issues/21.

FuzzyQuils commented 8 years ago

Oh wait, so the triangles array points to 3 vec3's not three floats? I'll fiddle around and see if that works. :)

EDIT: That kind of makes sense actually; I assume that's how OpenGL handles it then when calling glDrawElements()... (Simply jumping to tris[index]*3)

FuzzyQuils commented 8 years ago

Yep, that was it. :) I'm still lazily constructing vec3's, but at least I got it to work. My code now: ``

trisSoup IQM_Mesh::exporttris () {
    iqmmesh &m = meshes[0];
    trisSoup ps;
    ps.tris.resize(m.num_triangles);

    for (int i = 0; i < m.num_triangles; i += 1) {
        ps.tris[i].p1 = Vec3 { inposition[tris[i].vertex[0]*3], inposition[tris[i].vertex[0]*3+1], inposition[tris[i].vertex[0]*3+2] };
        ps.tris[i].p2 = Vec3 { inposition[tris[i].vertex[1]*3], inposition[tris[i].vertex[1]*3+1], inposition[tris[i].vertex[1]*3+2] };
        ps.tris[i].p3 = Vec3 { inposition[tris[i].vertex[2]*3], inposition[tris[i].vertex[2]*3+1], inposition[tris[i].vertex[2]*3+2] };
    }

    ps.length = m.num_triangles;
    return ps;
}

``