goat3dgfx

diff src/scene.cc @ 13:25bf39105c82

lalal
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Nov 2013 08:08:59 +0200
parents 1868c5975f31
children 7d6b667821cf
line diff
     1.1 --- a/src/scene.cc	Sun Nov 24 18:18:38 2013 +0200
     1.2 +++ b/src/scene.cc	Wed Nov 27 08:08:59 2013 +0200
     1.3 @@ -2,6 +2,8 @@
     1.4  #include "opengl.h"
     1.5  #include "goat3d.h"
     1.6  
     1.7 +static Mesh *load_mesh(goat3d_mesh *gmesh);
     1.8 +
     1.9  Scene::~Scene()
    1.10  {
    1.11  	destroy();
    1.12 @@ -38,7 +40,10 @@
    1.13  	int num_meshes = goat3d_get_mesh_count(goat);
    1.14  	for(int i=0; i<num_meshes; i++) {
    1.15  		goat3d_mesh *gmesh = goat3d_get_mesh(goat, i);
    1.16 -		// TODO cont...
    1.17 +		Mesh *mesh = load_mesh(gmesh);
    1.18 +		if(mesh) {
    1.19 +			meshes.push_back(mesh);
    1.20 +		}
    1.21  	}
    1.22  
    1.23  	goat3d_free(goat);
    1.24 @@ -63,3 +68,50 @@
    1.25  		}
    1.26  	}
    1.27  }
    1.28 +
    1.29 +static Mesh *load_mesh(goat3d_mesh *gmesh)
    1.30 +{
    1.31 +	int nverts = goat3d_get_mesh_attrib_count(gmesh, GOAT3D_MESH_ATTR_VERTEX);
    1.32 +	float *data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_VERTEX);
    1.33 +
    1.34 +	if(!nverts || !data) {
    1.35 +		return 0;
    1.36 +	}
    1.37 +	int nfaces = goat3d_get_mesh_face_count(gmesh);
    1.38 +
    1.39 +	Mesh *mesh = new Mesh;
    1.40 +	const char *name = goat3d_get_mesh_name(gmesh);
    1.41 +	if(name) {
    1.42 +		mesh->set_name(name);
    1.43 +	}
    1.44 +
    1.45 +	mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, data);
    1.46 +
    1.47 +	if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_NORMAL))) {
    1.48 +		mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, data);
    1.49 +	}
    1.50 +	if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TANGENT))) {
    1.51 +		mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, nverts, data);
    1.52 +	}
    1.53 +	if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
    1.54 +		mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, data);
    1.55 +	}
    1.56 +	if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_COLOR))) {
    1.57 +		mesh->set_attrib_data(MESH_ATTR_COLOR, 4, nverts, data);
    1.58 +	}
    1.59 +	if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_WEIGHT))) {
    1.60 +		mesh->set_attrib_data(MESH_ATTR_BONEWEIGHTS, 4, nverts, data);
    1.61 +	}
    1.62 +	int *idata = (int*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_MATRIX);
    1.63 +	if(idata) {
    1.64 +		for(int i=0; i<nverts; i++) {
    1.65 +			mesh->set_attrib(MESH_ATTR_BONEIDX, i, Vector4(idata[0], idata[1], idata[2], idata[3]));
    1.66 +			idata += 4;
    1.67 +		}
    1.68 +	}
    1.69 +
    1.70 +	if(nfaces && !(idata = goat3d_get_mesh_faces(gmesh))) {
    1.71 +		mesh->set_index_data(nfaces * 3, (unsigned int*)idata);
    1.72 +	}
    1.73 +	return mesh;
    1.74 +}