Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FuzzyQuils opened this issue Feb 10, 2016 · 3 comments
Open

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

FuzzyQuils opened this issue Feb 10, 2016 · 3 comments

Comments

@FuzzyQuils
Copy link

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
Copy link
Owner

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
#21.

@FuzzyQuils
Copy link
Author

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
Copy link
Author

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;
}

``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants