aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-10 19:14:40 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-10 19:14:40 +0000
commit9836d52cebef33002b8cea7d2e15d4e0da043e76 (patch)
treebe29c88526015ce27b8c480b4ce79c2b5a038ab2
parent98315c0a86e2d8157e3aec73d532bb4c87860ed2 (diff)
Process texture and Ks components from OBJ files
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@246 84d2e878-0bd5-11dd-ad15-13eda11d74c5
-rw-r--r--src/model.c10
-rw-r--r--src/obj2model.c22
-rw-r--r--src/render.c15
-rw-r--r--src/types.h2
4 files changed, 45 insertions, 4 deletions
diff --git a/src/model.c b/src/model.c
index 570a128..81958ec 100644
--- a/src/model.c
+++ b/src/model.c
@@ -78,7 +78,7 @@ 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, int coll) {
+ int vbos, int coll, GLfloat colspec) {
Primitive *p;
@@ -116,6 +116,7 @@ static Primitive *model_add_primitive(Model *model, GLenum type, GLfloat *vertic
p->col_r = r;
p->col_g = g;
p->col_b = b;
+ p->colspec = colspec;
p->texture = texture;
p->radius = radius;
p->shininess = shininess;
@@ -198,6 +199,7 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render
GLfloat col_r = 0.0;
GLfloat col_g = 0.0;
GLfloat col_b = 0.0;
+ GLfloat colspec = 0.0;
GLfloat radius = 0.0;
GLfloat shininess = 100.0;
PrimitiveAttrib attribs;
@@ -267,7 +269,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, coll);
+ render->vbos, coll, colspec);
num_vertices = 0;
type = PRIMITIVE_TRIANGLES;
attribs = ATTRIB_NONE;
@@ -452,6 +454,10 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render
attribs = attribs | ATTRIB_COLOUR;
col_r = r; col_g = g; col_b = b;
}
+ if ( sscanf(line, "colspec %f %f %f", &r, &g, &b) == 3 ) {
+ attribs = attribs | ATTRIB_COLSPEC;
+ colspec = r;
+ }
if ( sscanf(line, "radius %f", &radius) == 1 ) {
attribs = attribs | ATTRIB_RADIUS;
}
diff --git a/src/obj2model.c b/src/obj2model.c
index a720273..9b6f9ba 100644
--- a/src/obj2model.c
+++ b/src/obj2model.c
@@ -22,6 +22,19 @@
#define MAX_VERTICES 65536
+static char *texname(const char *filename) {
+ int i;
+ char *rval;
+ rval = strdup(filename);
+ for ( i=0; i<strlen(rval); i++ ) {
+ if ( rval[i] == '.' ) {
+ rval[i] = '\0';
+ return rval;
+ }
+ }
+ return rval;
+}
+
static void do_material(const char *mtllib, const char *mtlname, FILE *out, char *infilename) {
FILE *fh;
@@ -66,6 +79,15 @@ static void do_material(const char *mtllib, const char *mtlname, FILE *out, char
if ( strcmp(bits[0], "Kd") == 0 ) {
fprintf(out, "colour %s %s %s\n", bits[1], bits[2], bits[3]);
}
+ if ( strcmp(bits[0], "Ks") == 0 ) {
+ fprintf(out, "colspec %s %s %s\n", bits[1], bits[2], bits[3]);
+ }
+ if ( strcmp(bits[0], "map_Kd") == 0 ) {
+ char *tex;
+ tex = texname(bits[1]);
+ fprintf(out, "texture %s\n", tex);
+ free(tex);
+ }
if ( strcmp(bits[0], "Ns") == 0 ) {
fprintf(out, "shiny %s\n", bits[1]);
}
diff --git a/src/render.c b/src/render.c
index a51e093..2a81d8d 100644
--- a/src/render.c
+++ b/src/render.c
@@ -269,8 +269,19 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC
/* Set specular colour and shininess if required */
if ( p->attribs & ATTRIB_SHINY ) {
- GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
- glMaterialfv(GL_FRONT, GL_SPECULAR, white);
+ GLfloat spec[4];
+ if ( p->attribs & ATTRIB_COLSPEC ) {
+ spec[0] = p->colspec;
+ spec[1] = p->colspec;
+ spec[2] = p->colspec;
+ spec[3] = 1.0;
+ } else {
+ spec[0] = 1.0;
+ spec[1] = 1.0;
+ spec[2] = 1.0;
+ spec[3] = 1.0;
+ }
+ glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
glMaterialf(GL_FRONT, GL_SHININESS, p->shininess);
shaderutils_setunf(r->lighting_program, "shininess", p->shininess);
shaderutils_setunf(r->fill_program, "shininess", p->shininess);
diff --git a/src/types.h b/src/types.h
index 7f1119f..1304650 100644
--- a/src/types.h
+++ b/src/types.h
@@ -49,6 +49,7 @@ typedef enum {
ATTRIB_RADIUS = 1<<2, /* Radius is set */
ATTRIB_SHINY = 1<<3, /* Primitive is shiny */
ATTRIB_SWIRLY = 1<<4, /* Primitive is texture with swirlyness */
+ ATTRIB_COLSPEC = 1<<5, /* Specular colour specified? */
} PrimitiveAttrib;
typedef enum {
@@ -71,6 +72,7 @@ typedef struct {
GLfloat col_r;
GLfloat col_g;
GLfloat col_b;
+ GLfloat colspec;
char *texture;
GLfloat radius;
GLfloat shininess;