aboutsummaryrefslogtreecommitdiff
path: root/src/model.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/model.c')
-rw-r--r--src/model.c64
1 files changed, 52 insertions, 12 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);
}