polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

model.c (3170B)


      1 
      2 #include "model.h"
      3 #include "ply.h"
      4 #include "resource.h"
      5 #include "debug.h"
      6 #include "hash.h"
      7 #include "util.h"
      8 #include <assert.h>
      9 
     10 #define MODELDEF(name) { .id = model_##name, .loaded = 0, .file = #name }
     11 
     12 static struct resource_manager dyn_modelman;
     13 
     14 struct model *init_model(struct model *model) {
     15 	init_id(&model->geom_id);
     16 	model->texture = 0;
     17 	model->nposes = 0;
     18 	for (u16 i = 0; i < ARRAY_SIZE(model->poses); i++) {
     19 		init_pose(model->poses);
     20 	}
     21 	return model;
     22 }
     23 
     24 struct model *get_all_models(u32 *count, struct model_id **ids) {
     25 	return get_all_resources(&dyn_modelman, count, (struct resource_id **)ids);
     26 }
     27 
     28 static inline struct model *new_uninitialized_model(struct model_id *id) {
     29 	return new_resource(&dyn_modelman, &id->id);
     30 }
     31 
     32 struct model *new_model(struct model_id *model_id, const char *name)
     33 {
     34 	struct model *model = new_uninitialized_model(model_id);
     35 	/* debug("new model %llu\n", model_id->dyn_model_id.uuid); */
     36 	init_model(model);
     37 	init_id(&model->geom_id);
     38 	new_geometry(&model->geom_id);
     39 	model->name = name;
     40 	return model;
     41 }
     42 
     43 void init_model_manager()
     44 {
     45 	init_resource_manager(&dyn_modelman, sizeof(struct model),
     46 		DEF_DYNAMIC_MODELS, MAX_DYNAMIC_MODELS, "model");
     47 }
     48 
     49 
     50 struct model *get_model(struct model_id *model_id)
     51 {
     52 	return get_resource(&dyn_modelman, &model_id->id);
     53 }
     54 
     55 static struct model *load_model(const char *name, struct model_id *mid)
     56 {
     57 	static char path[128] = {0};
     58 
     59 	struct model *model = new_model(mid, name);
     60 	struct geometry *geom = get_geometry(&model->geom_id);
     61 	struct mdl_geometry lgeom;
     62 
     63 	init_mdl_geometry(&lgeom);
     64 	init_geometry(geom);
     65 
     66 	model->name = name;
     67 	model->name_hash = hash_str(name);
     68 
     69 	assert(!geom->has_vbos);
     70 
     71 	/*
     72 	if (geom->has_vbos) {
     73 		debug("model %s already loaded\n", name);
     74 		return model;
     75 	}
     76 	*/
     77 
     78 	// Load mesh
     79 	debug("loading %s model uuid %" PRIu64 " with geom_id %" PRIu64 "\n", name,
     80 			mid->id.uuid, model->geom_id.id.uuid);
     81 
     82 	snprintf(path, 128, "data/models/%s.mdl", name);
     83 	load_mdl(path, model, &lgeom);
     84 
     85 	vec3_copy(lgeom.min, geom->min);
     86 	vec3_copy(lgeom.max, geom->max);
     87 
     88 	printf("%s min:%f,%f,%f max:%f,%f,%f\n", name,
     89 			geom->min[0], geom->min[1], geom->min[2],
     90 			geom->max[0], geom->max[1], geom->max[2]);
     91 
     92 	make_buffer_geometry(&lgeom.mkgeom, geom);
     93 	free_make_geometry(&lgeom.mkgeom);
     94 
     95 	return model;
     96 }
     97 
     98 struct model_id get_model_by_name(const char *name, struct model **found)
     99 {
    100 	struct model_id *ids;
    101 	struct model_id new_id;
    102 	struct model *model;
    103 	struct model *models;
    104 	u32 count, i, name_hash;
    105 
    106 	init_id(&new_id);
    107 
    108 	if (found) *found = NULL;
    109 
    110 	name_hash = hash_str(name);
    111 	models = get_all_models(&count, &ids);
    112 
    113 	for (i = 0; i < count; i++) {
    114 		model = &models[i];
    115 		if (model->name_hash == name_hash) {
    116 			if (found)
    117 				*found = model;
    118 			/*
    119 			printf("found %s:%s model_id:%"PRIu64" geom_id:%"PRIu64"\n",
    120 					name, model->name, ids[i].id.uuid, model->geom_id.uuid);
    121 					*/
    122 			return ids[i];
    123 		}
    124 	}
    125 
    126 	model = load_model(name, &new_id);
    127 
    128 	if (found) *found = model;
    129 
    130 	return new_id;
    131 }
    132 
    133 void destroy_model(struct model_id *model_id)
    134 {
    135 	struct model *model = get_model(model_id);
    136 
    137 	destroy_geometry(&model->geom_id);
    138 	destroy_resource(&dyn_modelman, &model_id->id);
    139 }
    140 
    141