aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-05-04 16:20:03 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-05-04 16:20:03 +0000
commit14e45c579c33d482e822b781061711f578ee3b2d (patch)
treef99260947e9f5301fb4c4bd52b767ba3f4f1226e /src
parent557dd30015bc696a087feee122f3c112fef21459 (diff)
More modelling work
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@6 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src')
-rw-r--r--src/model.c127
-rw-r--r--src/render.c23
2 files changed, 105 insertions, 45 deletions
diff --git a/src/model.c b/src/model.c
index 8a8fd50..afbb5bb 100644
--- a/src/model.c
+++ b/src/model.c
@@ -26,7 +26,7 @@
#include "render.h"
/* Maximum number of vertices per primitive */
-#define MAX_VERTICES 256
+#define MAX_VERTICES 4096
ModelContext *model_init() {
@@ -169,14 +169,9 @@ static int model_load_obj(ModelContext *ctx, const char *name, RenderContext *re
return -1;
}
- 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));
- num_vertices = 0;
+ /* Zip through and find all the vertices */
n_vtmp = 0;
n_vntmp = 0;
- texture = NULL;
while ( !feof(fh) ) {
char line[1024];
@@ -212,48 +207,100 @@ static int model_load_obj(ModelContext *ctx, const char *name, RenderContext *re
n_vntmp++;
continue;
}
+
+ }
+
+ /* Go through again and look for faces */
+ 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));
+ texture = NULL;
+ num_vertices = 0;
+ rewind(fh);
+ while ( !feof(fh) ) {
+
+ char line[1024];
+ char **bits;
+ int n, i;
+
+ fgets(line, 1023, fh);
+ n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
- if ( line[s] == 'f' ) {
- char **bits;
- int i, n;
- n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
- for ( i=0; i<n; i++ ) {
+ /* Read in a face */
+ if ( strcmp(bits[0], "f") == 0 ) {
+
+ for ( i=1; i<n; i++ ) {
+
char **sp;
- int np, ip, vnum, nnum;
- np = assplode(bits[i], "/", &sp, ASSPLODE_DUPS);
- if ( np != 3 ) {
- continue;
- }
- vnum = atoi(sp[0]);
- nnum = atoi(sp[2]);
- if ( vnum >= n_vtmp ) {
- fprintf(stderr, "Vertex index is too high\n");
- continue;
+ int np, ip, vnum, nnum, j, nslash;
+
+ nslash = 0;
+ for ( j=0; j<strlen(bits[i]); j++ ) {
+ if ( bits[i][j] == '/' ) nslash++;
}
- if ( nnum >= n_vntmp ) {
- fprintf(stderr, "Normal index is too high\n");
- continue;
+ if ( nslash == 2 ) {
+
+ np = assplode(bits[i], "/", &sp, ASSPLODE_DUPS);
+ if ( np != 3 ) {
+ continue;
+ }
+ vnum = atoi(sp[0])-1;
+ nnum = atoi(sp[2])-1;
+ if ( vnum >= n_vtmp ) {
+ fprintf(stderr, "Vertex index is too high (%i/%i)\n", vnum, n_vtmp);
+ continue;
+ }
+ if ( nnum >= n_vntmp ) {
+ fprintf(stderr, "Normal index is too high (%i/%i)\n", nnum, n_vntmp);
+ continue;
+ }
+ if ( num_vertices < MAX_VERTICES ) {
+ vertices[3*num_vertices+0] = vtmp[3*vnum+0];
+ vertices[3*num_vertices+1] = vtmp[3*vnum+1];
+ vertices[3*num_vertices+2] = vtmp[3*vnum+2];
+ normals[3*num_vertices+0] = vntmp[3*nnum+0];
+ normals[3*num_vertices+1] = vntmp[3*nnum+1];
+ normals[3*num_vertices+2] = vntmp[3*nnum+2];
+ texcoords[2*num_vertices+0] = 0.0;
+ texcoords[2*num_vertices+1] = 0.0;
+ num_vertices++;
+ } else {
+ fprintf(stderr, "Too many vertices\n");
+ }
+ free(sp[0]);
+ free(sp[1]);
+ free(sp[2]);
+ free(sp);
+
+ } else if ( nslash == 0 ) {
+
+ vnum = atoi(bits[i]);
+ if ( num_vertices < MAX_VERTICES ) {
+ vertices[3*num_vertices+0] = vtmp[3*vnum+0];
+ vertices[3*num_vertices+1] = vtmp[3*vnum+1];
+ vertices[3*num_vertices+2] = vtmp[3*vnum+2];
+ normals[3*num_vertices+0] = 1.0;
+ normals[3*num_vertices+1] = 0.0;
+ normals[3*num_vertices+2] = 0.0;
+ num_vertices++;
+ } else {
+ fprintf(stderr, "Too many vertices\n");
+ }
+
}
- vertices[3*num_vertices+0] = vtmp[3*vnum+0];
- vertices[3*num_vertices+1] = vtmp[3*vnum+1];
- vertices[3*num_vertices+2] = vtmp[3*vnum+2];
- normals[3*num_vertices+0] = vntmp[3*nnum+0];
- normals[3*num_vertices+1] = vntmp[3*nnum+1];
- normals[3*num_vertices+2] = vntmp[3*nnum+2];
- texcoords[2*num_vertices+0] = 0.0;
- texcoords[2*num_vertices+1] = 0.0;
- num_vertices++;
- free(sp[0]);
- free(sp[1]);
- free(sp[2]);
- free(sp);
- free(bits[i]);
+
}
- free(bits);
+
model_add_primitive(model, GL_POLYGON, vertices, normals, texcoords, num_vertices,
ATTRIB_NONE, 1.0, 1.0, 1.0, texture);
+ num_vertices = 0;
+
}
+ for ( i=0; i<n; i++ ) free(bits[i]);
+ free(bits);
+
}
fclose(fh);
diff --git a/src/render.c b/src/render.c
index df3862b..b45634d 100644
--- a/src/render.c
+++ b/src/render.c
@@ -115,7 +115,7 @@ RenderContext *render_setup() {
ctx->frames = 0;
ctx->t_fps = SDL_GetTicks();
ctx->fps = 0;
-
+
return ctx;
}
@@ -192,6 +192,7 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC
}
+#if 0
static void render_configure_light(int *ilightp, Light light, Room *room, Game *game) {
GLfloat x, y, z;
@@ -219,7 +220,7 @@ static void render_configure_light(int *ilightp, Light light, Room *room, Game *
glLightfv(GL_LIGHT0+ilight, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0+ilight, GL_SPECULAR, specular);
glLightf(GL_LIGHT0+ilight, GL_LINEAR_ATTENUATION, 0.01);
- //glEnable(GL_LIGHT0+ilight);
+ glEnable(GL_LIGHT0+ilight);
ilight++;
}
@@ -260,11 +261,12 @@ static void render_setup_lighting(Game *game, Room *room_current) {
}
- //ambient[0] = 0.3; ambient[1] = 0.3; ambient[2] = 0.3; ambient[3] = 1.0;
- ambient[0] = 1.0; ambient[1] = 1.0; ambient[2] = 1.0; ambient[3] = 1.0;
+ ambient[0] = 0.3; ambient[1] = 0.3; ambient[2] = 0.3; ambient[3] = 1.0;
+ //ambient[0] = 1.0; ambient[1] = 1.0; ambient[2] = 1.0; ambient[3] = 1.0;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
}
+#endif
void render_draw(Game *game) {
@@ -290,6 +292,17 @@ void render_draw(Game *game) {
// 0.0, 0.0, -495.0,
// 0.0, sqrtf(2.0), sqrtf(2.0));
+ GLfloat pos[] = {-1.0, -1.0, 1.0, 0.0};
+ GLfloat ambient[4];
+ GLfloat diffuse[] = {0.8, 0.8, 0.8, 1.0};
+ GLfloat specular[] = {0.8, 0.8, 0.8, 1.0};
+ ambient[0] = 0.3; ambient[1] = 0.3; ambient[2] = 0.3; ambient[3] = 1.0;
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
+ glLightfv(GL_LIGHT0, GL_POSITION, pos);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
+ glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
+ glEnable(GL_LIGHT0);
+
/* Draw the objects */
//glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "texture"), 0);
//glUseProgramObjectARB(game->render->lighting_program);
@@ -301,7 +314,7 @@ void render_draw(Game *game) {
room = game->rooms[i];
if ( room == NULL ) return;
- render_setup_lighting(game, room);
+ //render_setup_lighting(game, room);
for ( j=0; j<room->num_objects; j++ ) {
GLfloat x, y, z;