diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/model.c | 64 | ||||
-rw-r--r-- | src/physics.c | 10 | ||||
-rw-r--r-- | src/types.h | 3 |
3 files changed, 60 insertions, 17 deletions
diff --git a/src/model.c b/src/model.c index c159aa1..570a128 100644 --- a/src/model.c +++ b/src/model.c @@ -28,6 +28,9 @@ /* Maximum number of vertices per primitive */ #define MAX_VERTICES 100000 +/* Prototype */ +static Model *model_lookup(ModelContext *ctx, const char *name); + ModelContext *model_init() { ModelContext *ctx; @@ -74,7 +77,8 @@ static Model *model_new(const char *name) { static Primitive *model_add_primitive(Model *model, GLenum type, GLfloat *vertices, GLfloat *normals, GLfloat *texcoords, int n, PrimitiveAttrib attribs, GLfloat r, GLfloat g, - GLfloat b, char *texture, GLfloat radius, GLfloat shininess, int vbos) { + GLfloat b, char *texture, GLfloat radius, GLfloat shininess, + int vbos, int coll) { Primitive *p; @@ -116,7 +120,7 @@ static Primitive *model_add_primitive(Model *model, GLenum type, GLfloat *vertic p->radius = radius; p->shininess = shininess; - if ( vbos ) { + if ( !coll && vbos ) { /* FIXME: Move this snippet to render.c in some tidy way */ @@ -137,9 +141,17 @@ static Primitive *model_add_primitive(Model *model, GLenum type, GLfloat *vertic } model->attrib_total = model->attrib_total | attribs; - model->primitives = realloc(model->primitives, sizeof(Primitive *) * (model->num_primitives+1)); - model->primitives[model->num_primitives] = p; - model->num_primitives++; + + if ( !coll ) { + model->primitives = realloc(model->primitives, sizeof(Primitive *) * (model->num_primitives+1)); + model->primitives[model->num_primitives] = p; + model->num_primitives++; + } else { + model->coll_primitives = realloc(model->coll_primitives, + sizeof(Primitive *) * (model->num_coll_primitives+1)); + model->coll_primitives[model->num_coll_primitives] = p; + model->num_coll_primitives++; + } return 0; @@ -173,7 +185,7 @@ static void model_calculate_normals(GLfloat *vertices, GLfloat *normals, int fir } -static int model_load(ModelContext *ctx, const char *name, RenderContext *render) { +static int model_load(ModelContext *ctx, const char *name, RenderContext *render, int coll) { FILE *fh; char tmp[64]; @@ -192,6 +204,19 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render char *texture; int subdiv; + /* Attempt to open the file */ + if ( !coll ) { + model = model_new(name); + } else { + model = model_lookup(ctx, name); + if ( model == NULL ) { + fprintf(stderr, "Collision geometry must be loaded AFTER rendering geometry (%s)\n", name); + return -1; + } + model->num_coll_primitives = model->num_primitives; + model->coll_primitives = model->primitives; + } + /* Don't subdivide the geometry if we're doing per-fragment lighting anyway */ if ( render->shaders ) { subdiv = 0; @@ -199,13 +224,20 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render subdiv = 1; } - snprintf(tmp, 63, "%s/models/%s", DATADIR, name); + if ( !coll ) { + snprintf(tmp, 63, "%s/models/%s", DATADIR, name); + } else { + snprintf(tmp, 63, "%s/models/%s-coll", DATADIR, name); + } fh = fopen(tmp, "r"); if ( fh == NULL ) { return -1; } + if ( coll ) { + model->num_coll_primitives = 0; + model->coll_primitives = NULL; + } - model = model_new(name); vertices = malloc(MAX_VERTICES*3*sizeof(GLfloat)); normals = malloc(MAX_VERTICES*3*sizeof(GLfloat)); texcoords = malloc(MAX_VERTICES*2*sizeof(GLfloat)); @@ -235,7 +267,7 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render if ( num_vertices > 0 ) { model_add_primitive(model, type, vertices, normals, texcoords, num_vertices, attribs, col_r, col_g, col_b, texture, radius, shininess, - render->vbos); + render->vbos, coll); num_vertices = 0; type = PRIMITIVE_TRIANGLES; attribs = ATTRIB_NONE; @@ -290,7 +322,7 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render } /* Subdivide the previous face if requested */ - if ( subdiv && (sscanf(line, "subdivide %f %f", &x, &y) == 2) ) { + if ( !coll && subdiv && (sscanf(line, "subdivide %f %f", &x, &y) == 2) ) { if ( type == PRIMITIVE_QUADS ) { if ( (num_vertices % 4)==0 ) { GLfloat u, v; @@ -448,7 +480,11 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render fclose(fh); - return model_add(ctx, model); + if ( !coll ) { + return model_add(ctx, model); + } else { + return 0; + } } @@ -480,7 +516,11 @@ ModelInstance *model_instance_new(ModelContext *ctx, const char *name, RenderCon if ( instance->model == NULL ) { /* Couldn't find model, so try to load it */ - model_load(ctx, name, render); + if ( model_load(ctx, name, render, 0) != 0 ) { + fprintf(stderr, "Couldn't load model '%s'\n", name); + return NULL; + } + model_load(ctx, name, render, 1); instance->model = model_lookup(ctx, name); } diff --git a/src/physics.c b/src/physics.c index 1e64bda..00879e7 100644 --- a/src/physics.c +++ b/src/physics.c @@ -200,16 +200,16 @@ static int physics_check_collide(ModelInstance *obj, ModelInstance *other, doubl int found = 0; /* Check all the vertices in the moving object... */ - for ( i=0; i<obj->model->num_primitives; i++ ) { + for ( i=0; i<obj->model->num_coll_primitives; i++ ) { int j; - for ( j=0; j<obj->model->primitives[i]->num_vertices; j++ ) { + for ( j=0; j<obj->model->coll_primitives[i]->num_vertices; j++ ) { int a; - const double stx = obj->model->primitives[i]->vertices[3*j+0]; - const double sty = obj->model->primitives[i]->vertices[3*j+1]; + const double stx = obj->model->coll_primitives[i]->vertices[3*j+0]; + const double sty = obj->model->coll_primitives[i]->vertices[3*j+1]; const double sx = stx*cos(obj->yaw) + sty*sin(obj->yaw); const double sy = -stx*sin(obj->yaw) + sty*cos(obj->yaw); - const double sz = obj->model->primitives[i]->vertices[3*j+2]; + const double sz = obj->model->coll_primitives[i]->vertices[3*j+2]; /* ...against all primitives in the static object */ for ( a=0; a<other->model->num_primitives; a++ ) { diff --git a/src/types.h b/src/types.h index a759cd4..f601d95 100644 --- a/src/types.h +++ b/src/types.h @@ -84,6 +84,9 @@ typedef struct { int num_primitives; Primitive **primitives; /* Geometry */ + int num_coll_primitives; + Primitive **coll_primitives; + PrimitiveAttrib attrib_total; } Model; |