aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-07-30 21:02:32 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-07-30 21:02:32 +0000
commit62fc6aad00fcfb09b85cce87a50b30e6422afe21 (patch)
tree5446730621b49b9beb12a56bea644b2158d371da
parentc8ffb4adf132ba6cf4ed3ad08db9f9bfbe1d7280 (diff)
Use simplified geometry for collision detection
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@196 84d2e878-0bd5-11dd-ad15-13eda11d74c5
-rw-r--r--data/Makefile.am2
-rw-r--r--data/models/lander-coll79
-rw-r--r--src/model.c64
-rw-r--r--src/physics.c10
-rw-r--r--src/types.h3
5 files changed, 140 insertions, 18 deletions
diff --git a/data/Makefile.am b/data/Makefile.am
index 4f5f4f7..1c3afd6 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -1,6 +1,6 @@
modelsdir = $(datadir)/thrust3d/models
models_DATA = models/floor models/lander models/randombox models/walle models/walln models/wallw models/walls \
- models/platform models/ceiling models/tiledfloor models/pipe
+ models/platform models/ceiling models/tiledfloor models/pipe models/lander-coll
shadersdir = $(datadir)/thrust3d/shaders
shaders_DATA = shaders/lighting.vert shaders/lighting.frag shaders/swirlytron.vert shaders/swirlytron.frag
diff --git a/data/models/lander-coll b/data/models/lander-coll
new file mode 100644
index 0000000..5523ad3
--- /dev/null
+++ b/data/models/lander-coll
@@ -0,0 +1,79 @@
+# source-material/models/lander-new-coll.obj
+
+QUADS
+ +0.000 +0.320 +0.284 0.000 0.000 +0.309 +0.951 +0.000
+ +0.000 +0.320 +0.384 0.000 0.000 +0.309 +0.951 +0.000
+ +0.188 +0.259 +0.384 0.000 0.000 +0.309 +0.951 +0.000
+ +0.188 +0.259 +0.284 0.000 0.000 +0.309 +0.951 +0.000
+ -0.304 +0.099 +0.284 0.000 0.000 -0.809 +0.588 +0.000
+ -0.304 +0.099 +0.384 0.000 0.000 -0.809 +0.588 +0.000
+ -0.188 +0.259 +0.384 0.000 0.000 -0.809 +0.588 +0.000
+ -0.188 +0.259 +0.284 0.000 0.000 -0.809 +0.588 +0.000
+ -0.188 -0.259 +0.284 0.000 0.000 -0.809 -0.588 +0.000
+ -0.188 -0.259 +0.384 0.000 0.000 -0.809 -0.588 +0.000
+ -0.304 -0.099 +0.384 0.000 0.000 -0.809 -0.588 +0.000
+ -0.304 -0.099 +0.284 0.000 0.000 -0.809 -0.588 +0.000
+ +0.188 -0.259 +0.284 0.000 0.000 +0.309 -0.951 +0.000
+ +0.188 -0.259 +0.384 0.000 0.000 +0.309 -0.951 +0.000
+ -0.000 -0.320 +0.384 0.000 0.000 +0.309 -0.951 +0.000
+ -0.000 -0.320 +0.284 0.000 0.000 +0.309 -0.951 +0.000
+ +0.304 +0.099 +0.284 0.000 0.000 +1.000 -0.000 +0.000
+ +0.304 +0.099 +0.384 0.000 0.000 +1.000 -0.000 +0.000
+ +0.304 -0.099 +0.384 0.000 0.000 +1.000 -0.000 +0.000
+ +0.304 -0.099 +0.284 0.000 0.000 +1.000 -0.000 +0.000
+
+TRIANGLES
+ +0.063 +0.194 +0.479 0.000 0.000 +0.052 +0.703 +0.709
+ +0.010 +0.233 +0.444 0.000 0.000 +0.052 +0.703 +0.709
+ -0.015 +0.183 +0.496 0.000 0.000 +0.052 +0.703 +0.709
+ -0.178 +0.042 +0.496 0.000 0.000 -0.508 +0.155 +0.847
+ -0.121 +0.000 +0.538 0.000 0.000 -0.508 +0.155 +0.847
+ -0.113 +0.082 +0.528 0.000 0.000 -0.508 +0.155 +0.847
+ +0.169 +0.071 +0.496 0.000 0.000 +0.602 +0.108 +0.791
+ +0.140 +0.000 +0.528 0.000 0.000 +0.602 +0.108 +0.791
+ +0.204 -0.000 +0.479 0.000 0.000 +0.602 +0.108 +0.791
+ -0.015 -0.183 +0.496 0.000 0.000 -0.183 -0.562 +0.807
+ -0.037 -0.115 +0.538 0.000 0.000 -0.183 -0.562 +0.807
+ -0.095 -0.157 +0.496 0.000 0.000 -0.183 -0.562 +0.807
+ +0.020 -0.063 +0.561 0.000 0.000 -0.067 -0.205 +0.976
+ -0.000 +0.000 +0.573 0.000 0.000 -0.067 -0.205 +0.976
+ -0.053 -0.039 +0.561 0.000 0.000 -0.067 -0.205 +0.976
+
+QUADS
+ +0.257 +0.339 +0.000 0.000 0.000 -0.000 +1.000 +0.000
+ +0.143 +0.339 +0.000 0.000 0.000 -0.000 +1.000 +0.000
+ +0.143 +0.339 +0.100 0.000 0.000 -0.000 +1.000 +0.000
+ +0.257 +0.339 +0.100 0.000 0.000 -0.000 +1.000 +0.000
+ +0.339 +0.143 +0.000 0.000 0.000 +1.000 +0.000 +0.000
+ +0.339 +0.257 +0.000 0.000 0.000 +1.000 +0.000 +0.000
+ +0.339 +0.257 +0.100 0.000 0.000 +1.000 +0.000 +0.000
+ +0.339 +0.143 +0.100 0.000 0.000 +1.000 +0.000 +0.000
+ -0.339 +0.257 +0.000 0.000 0.000 -1.000 +0.000 +0.000
+ -0.339 +0.143 +0.000 0.000 0.000 -1.000 +0.000 +0.000
+ -0.339 +0.143 +0.100 0.000 0.000 -1.000 +0.000 +0.000
+ -0.339 +0.257 +0.100 0.000 0.000 -1.000 +0.000 +0.000
+ -0.143 +0.339 +0.000 0.000 0.000 -0.000 +1.000 +0.000
+ -0.257 +0.339 +0.000 0.000 0.000 -0.000 +1.000 +0.000
+ -0.257 +0.339 +0.100 0.000 0.000 -0.000 +1.000 +0.000
+ -0.143 +0.339 +0.100 0.000 0.000 -0.000 +1.000 +0.000
+ -0.339 -0.143 +0.000 0.000 0.000 -1.000 +0.000 +0.000
+ -0.339 -0.257 +0.000 0.000 0.000 -1.000 +0.000 +0.000
+ -0.339 -0.257 +0.100 0.000 0.000 -1.000 +0.000 +0.000
+ -0.339 -0.143 +0.100 0.000 0.000 -1.000 +0.000 +0.000
+ -0.257 -0.339 +0.000 0.000 0.000 +0.000 -1.000 +0.000
+ -0.143 -0.339 +0.000 0.000 0.000 +0.000 -1.000 +0.000
+ -0.143 -0.339 +0.100 0.000 0.000 +0.000 -1.000 +0.000
+ -0.257 -0.339 +0.100 0.000 0.000 +0.000 -1.000 +0.000
+ +0.339 -0.257 +0.000 0.000 0.000 +1.000 +0.000 +0.000
+ +0.339 -0.143 +0.000 0.000 0.000 +1.000 +0.000 +0.000
+ +0.339 -0.143 +0.100 0.000 0.000 +1.000 +0.000 +0.000
+ +0.339 -0.257 +0.100 0.000 0.000 +1.000 +0.000 +0.000
+ +0.143 -0.339 +0.000 0.000 0.000 +0.000 -1.000 +0.000
+ +0.257 -0.339 +0.000 0.000 0.000 +0.000 -1.000 +0.000
+ +0.257 -0.339 +0.100 0.000 0.000 +0.000 -1.000 +0.000
+ +0.143 -0.339 +0.100 0.000 0.000 +0.000 -1.000 +0.000
+ +0.143 -0.339 +0.000 0.000 0.000 +0.000 -1.000 +0.000
+ +0.257 -0.339 +0.000 0.000 0.000 +0.000 -1.000 +0.000
+ +0.257 -0.339 +0.100 0.000 0.000 +0.000 -1.000 +0.000
+ +0.143 -0.339 +0.100 0.000 0.000 +0.000 -1.000 +0.000
+
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;