Example #1
0
void _lao_load_model(LaoModel *m, const char *filename) {
	_LaoObjFileData data;
	unsigned int i;
	
	LaoVertex *verts;
	
	memset(m, 0, sizeof(LaoModel));	
	m->name = _lao_get_res_name(LAO_MODEL_PATH, filename);
		
	parse_obj(filename, &data);
	
	m->count = data.icount;
	
	verts = calloc(data.icount, sizeof(LaoVertex));
	memset(verts, 0, data.icount*sizeof(LaoVertex));
	for(i = 0; i < data.icount; i++) {
		int xi, ni, ti;
		
		xi = data.indices[i][0]-1;
		if(xi < 0 || xi >= data.xcount)
			bad_reference_error(filename, "vertex", i);
		
		memcpy(verts[i].x, data.xs[xi], sizeof(Vec3));
				
		if(data.tcount) {
			ti = data.indices[i][1]-1;
			if(ti < 0 || ti >= data.tcount)
				bad_reference_error(filename, "texcoord", i);
			
			verts[i].s = data.texcoords[ti][0];
			verts[i].t = data.texcoords[ti][1];
		}
		
		if(data.ncount) {
			ni = data.indices[i][2]-1;
			if(ni < 0 || ni >= data.ncount)
				bad_reference_error(filename, "normal", ni);
			
			memcpy(verts[i].n, data.normals[ni], sizeof(Vec3));
		}
	}
	
	_lao_vbo_bind_model(&lao_bos.vbo, m, verts);
	
	lao_log_msg("%s -> %s", filename, m->name);
	
	free(verts);
	free(data.xs);
	free(data.normals);
	free(data.texcoords);
	free(data.indices);
}
Example #2
0
File: model.c Project: miton/taisei
Model *load_model(char *filename) {
	Model *m = create_element((void **)&resources.models, sizeof(Model));
	
	ObjFileData data;
	unsigned int i;
	
	Vertex *verts;
	unsigned int ioffset = _vbo.offset;
	
	char *beg = strstr(filename, "models/") + 7;
	char *end = strrchr(filename, '.');
	
	m->name = malloc(end - beg + 1);
	memset(m->name, 0, end-beg + 1);
	strncpy(m->name, beg, end-beg);
		
	
	parse_obj(filename, &data);
		
	m->fverts = data.fverts;
	m->indices = calloc(data.icount, sizeof(int));
	m->icount = data.icount;
	
	verts = calloc(data.icount, sizeof(Vertex));
	
	
	memset(verts, 0, data.icount*sizeof(Vertex));
	for(i = 0; i < data.icount; i++) {
		int xi, ni, ti;
		
		xi = data.indices[i][0]-1;
		if(xi < 0 || xi >= data.xcount)
			bad_reference_error(filename, "vertex", i);
		
		memcpy(verts[i].x, data.xs[xi], sizeof(Vector));
				
		if(data.tcount) {
			ti = data.indices[i][1]-1;
			if(ti < 0 || ti >= data.tcount)
				bad_reference_error(filename, "texcoord", i);
			
			verts[i].s = data.texcoords[ti][0];
			verts[i].t = data.texcoords[ti][1];
		}
		
		if(data.ncount) {
			ni = data.indices[i][2]-1;
			if(ni < 0 || ni >= data.ncount)
				bad_reference_error(filename, "normal", ni);
			
			memcpy(verts[i].n, data.normals[ni], sizeof(Vector));
		}
		
		m->indices[i] = i+ioffset;
	}
	
	vbo_add_verts(&_vbo, verts, data.icount);
	
	printf("-- loaded '%s' as '%s'\n", filename, m->name);
	
	free(verts);
	free(data.xs);
	free(data.normals);
	free(data.texcoords);
	free(data.indices);
	return m;
}