Skip to content

Instantly share code, notes, and snippets.

@zturtleman
Last active January 12, 2020 09:46
Show Gist options
  • Save zturtleman/276ba59ab6e9e6b72b9df2490f95428c to your computer and use it in GitHub Desktop.
Save zturtleman/276ba59ab6e9e6b72b9df2490f95428c to your computer and use it in GitHub Desktop.
some info about loading IQM models

IQM info: http://sauerbraten.org/iqm/

IQM format description (structs referenced below): http://sauerbraten.org/iqm/iqm.txt

Load the whole IQM file into C unsigned char array

unsigned char *fileData;
example_read_file_function( &fileData );
struct iqmheader *header = (struct iqmheader *)fileData;

iqmheader uint32_t num_triangles, ofs_triangles

There are 3 × num_triangles uint32_t vertex indexes at IQM file + ofs_triangles. Upload as an OpenGL GL_ELEMENTS_BUFFER buffer object.

uint32_t *indexes = (uint32_t*)( fileData + header->ofs_triangles );
// number of indexes: header->num_triangles*3;

iqmheader uint32_t num_vertexarrays, num_vertexes, ofs_vertexarrays

iqmvertexarray are at IQM file + ofs_vertexarrays. Look through and find ones you want (IQM_POSITION, etc) then interleave the arrays (position, normal, texcoords, ... repeating for each vertex) for uploading to OpenGL as an GL_ARRAY_BUFFER buffer object.

[See IQM gpu-demo link below.]

iqmheader uint32_t num_meshes, ofs_meshes

Meshes have material name at IQM file + header->ofs_text + mesh->material and triangle & vertex range for rendering.

struct iqmmesh *mesh = (struct iqmmesh*)( fileData + header->ofs_meshes );
for ( uint32_t i = 0; i < header->num_meshes, mesh++ ) {
	// loop though all meshes
	char *meshName = (char *)( fileData + header->ofs_text + mesh->name );
	char *meshMaterial = (char *)( fileData + header->ofs_text + mesh->material );
}

Triangle and vertex upload:

After the Upload

After uploading indexes and vertex data you have to bind vertex attributes so the shader knows where the position, etc are in the vertex buffer object [see Mesh tutorial]. You also have to bind the vertex attribute name/index for the GLSL shader [See Shaders tutorial]. Though all of that is general OpenGL stuff.

Skeletal drawing

The above should give you unanimated IQM support. To apply skeleton transform you need vertex attributes for bone joint blend indexes and weights [vertex buffer and shader], upload bone joint transform matrices [relative change from IQM::joints aka the bind pose] to OpenGL shader uniform before drawing mesh, and calculate vertex transform matrix in GLSL shader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment