goat3d

changeset 57:76d0f55f9d5f

mesh and animation saving looks done...
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Jan 2014 03:57:15 +0200
parents ca549434dc95
children d317eb4f83da
files converters/ass2goat/src/main.c src/goat3d.cc src/goat3d.h
diffstat 3 files changed, 85 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/converters/ass2goat/src/main.c	Thu Jan 23 02:50:54 2014 +0200
     1.2 +++ b/converters/ass2goat/src/main.c	Thu Jan 23 03:57:15 2014 +0200
     1.3 @@ -13,6 +13,7 @@
     1.4  int convert(const char *infname);
     1.5  int convert_anim(const char *infname);
     1.6  void process_material(struct goat3d_material *mtl, struct aiMaterial *aimtl);
     1.7 +void process_mesh(struct goat3d *goat, struct goat3d_mesh *mesh, struct aiMesh *aimesh);
     1.8  void process_node(struct goat3d *goat, struct goat3d_node *parent, struct aiNode *ainode);
     1.9  int process_anim(struct goat3d *goat, struct aiAnimation *aianim);
    1.10  static int output_filename(char *buf, int bufsz, const char *fname, const char *suffix);
    1.11 @@ -101,6 +102,14 @@
    1.12  		goat3d_add_mtl(goat, mat);
    1.13  	}
    1.14  
    1.15 +	for(i=0; i<(int)aiscn->mNumMeshes; i++) {
    1.16 +		struct aiMesh *aimesh = aiscn->mMeshes[i];
    1.17 +		struct goat3d_mesh *mesh = goat3d_create_mesh();
    1.18 +
    1.19 +		process_mesh(goat, mesh, aimesh);
    1.20 +		goat3d_add_mesh(goat, mesh);
    1.21 +	}
    1.22 +
    1.23  	for(i=0; i<(int)aiscn->mRootNode->mNumChildren; i++) {
    1.24  		process_node(goat, 0, aiscn->mRootNode->mChildren[i]);
    1.25  	}
    1.26 @@ -195,11 +204,60 @@
    1.27  		goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, aistr.data);
    1.28  	}
    1.29  	if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_OPACITY(0), &aistr) == aiReturn_SUCCESS) {
    1.30 -		// TODO this is semantically inverted... maybe add an alpha attribute?
    1.31 +		/* TODO this is semantically inverted... maybe add an alpha attribute? */
    1.32  		goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, aistr.data);
    1.33  	}
    1.34  }
    1.35  
    1.36 +void process_mesh(struct goat3d *goat, struct goat3d_mesh *mesh, struct aiMesh *aimesh)
    1.37 +{
    1.38 +	int i, num_verts, num_faces;
    1.39 +	struct goat3d_material *mtl;
    1.40 +
    1.41 +	if(aimesh->mName.length > 0) {
    1.42 +		goat3d_set_mesh_name(mesh, aimesh->mName.data);
    1.43 +	}
    1.44 +
    1.45 +	if((mtl = goat3d_get_mtl(goat, aimesh->mMaterialIndex))) {
    1.46 +		goat3d_set_mesh_mtl(mesh, mtl);
    1.47 +	}
    1.48 +
    1.49 +	num_verts = aimesh->mNumVertices;
    1.50 +	num_faces = aimesh->mNumFaces;
    1.51 +
    1.52 +	for(i=0; i<num_verts; i++) {
    1.53 +		struct aiVector3D *v;
    1.54 +		struct aiColor4D *col;
    1.55 +
    1.56 +		v = aimesh->mVertices + i;
    1.57 +		goat3d_add_mesh_attrib3f(mesh, GOAT3D_MESH_ATTR_VERTEX, v->x, v->y, v->z);
    1.58 +
    1.59 +		if(aimesh->mNormals) {
    1.60 +			v = aimesh->mNormals + i;
    1.61 +			goat3d_add_mesh_attrib3f(mesh, GOAT3D_MESH_ATTR_NORMAL, v->x, v->y, v->z);
    1.62 +		}
    1.63 +		if(aimesh->mTangents) {
    1.64 +			v = aimesh->mTangents + i;
    1.65 +			goat3d_add_mesh_attrib3f(mesh, GOAT3D_MESH_ATTR_TANGENT, v->x, v->y, v->z);
    1.66 +		}
    1.67 +		if(aimesh->mTextureCoords[0]) {
    1.68 +			v = aimesh->mTextureCoords[0] + i;
    1.69 +			goat3d_add_mesh_attrib2f(mesh, GOAT3D_MESH_ATTR_TEXCOORD, v->x, v->y);
    1.70 +		}
    1.71 +		if(aimesh->mColors[0]) {
    1.72 +			col = aimesh->mColors[0] + i;
    1.73 +			goat3d_add_mesh_attrib4f(mesh, GOAT3D_MESH_ATTR_COLOR, col->r, col->g, col->b, col->a);
    1.74 +		}
    1.75 +		/* TODO: add bones */
    1.76 +	}
    1.77 +
    1.78 +	for(i=0; i<num_faces; i++) {
    1.79 +		struct aiFace *face = aimesh->mFaces + i;
    1.80 +
    1.81 +		goat3d_add_mesh_face(mesh, face->mIndices[0], face->mIndices[1], face->mIndices[2]);
    1.82 +	}
    1.83 +}
    1.84 +
    1.85  void process_node(struct goat3d *goat, struct goat3d_node *parent, struct aiNode *ainode)
    1.86  {
    1.87  	int i;
     2.1 --- a/src/goat3d.cc	Thu Jan 23 02:50:54 2014 +0200
     2.2 +++ b/src/goat3d.cc	Thu Jan 23 03:57:15 2014 +0200
     2.3 @@ -247,6 +247,21 @@
     2.4  	g->scn->add_material(mtl);
     2.5  }
     2.6  
     2.7 +GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g)
     2.8 +{
     2.9 +	return g->scn->get_material_count();
    2.10 +}
    2.11 +
    2.12 +GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx)
    2.13 +{
    2.14 +	return (goat3d_material*)g->scn->get_material(idx);
    2.15 +}
    2.16 +
    2.17 +GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name)
    2.18 +{
    2.19 +	return (goat3d_material*)g->scn->get_material(name);
    2.20 +}
    2.21 +
    2.22  GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
    2.23  {
    2.24  	return new goat3d_material;
    2.25 @@ -405,6 +420,12 @@
    2.26  	goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
    2.27  }
    2.28  
    2.29 +GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
    2.30 +		float x, float y)
    2.31 +{
    2.32 +	goat3d_add_mesh_attrib4f(mesh, attrib, x, y, 0, 1);
    2.33 +}
    2.34 +
    2.35  GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
    2.36  		float x, float y, float z)
    2.37  {
     3.1 --- a/src/goat3d.h	Thu Jan 23 02:50:54 2014 +0200
     3.2 +++ b/src/goat3d.h	Thu Jan 23 03:57:15 2014 +0200
     3.3 @@ -108,6 +108,9 @@
     3.4  
     3.5  /* materials */
     3.6  GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl);
     3.7 +GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g);
     3.8 +GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx);
     3.9 +GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name);
    3.10  
    3.11  GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void);
    3.12  GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl);
    3.13 @@ -156,6 +159,8 @@
    3.14  GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
    3.15  		const void *data, int vnum);
    3.16  GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, float val);
    3.17 +GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
    3.18 +		float x, float y);
    3.19  GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
    3.20  		float x, float y, float z);
    3.21  GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,