diff options
author | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-05-22 00:41:28 +0000 |
---|---|---|
committer | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-05-22 00:41:28 +0000 |
commit | a5f2f4662ae54c6e073934e64805e3f837cfc278 (patch) | |
tree | e52d3e9b148e70dc4f1b611203c69d2b13a47c41 /src | |
parent | 3370ccead4394a270ae548db51e8bf064297c30f (diff) |
Polygon subdivision, spotlight testing
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@34 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src')
-rw-r--r-- | src/model.c | 124 | ||||
-rw-r--r-- | src/render.c | 8 |
2 files changed, 130 insertions, 2 deletions
diff --git a/src/model.c b/src/model.c index 5607e95..bd5ea5f 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 4096 +#define MAX_VERTICES 8192*5 ModelContext *model_init() { @@ -253,6 +253,128 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render } } + /* Subdivide the previous face if requested */ + if ( sscanf(line, "subdivide %f %f", &x, &y) == 2 ) { + if ( type == PRIMITIVE_QUADS ) { + if ( (num_vertices % 4)==0 ) { + GLfloat u, v; + GLfloat v1x, v1y, v1z; + GLfloat v2x, v2y, v2z; + GLfloat v3x, v3y, v3z; + GLfloat v4x, v4y, v4z; + GLfloat nx, ny, nz; + GLfloat t1x, t1y; + GLfloat t2x, t2y; + GLfloat t3x, t3y; + GLfloat t4x, t4y; + nx = normals[3*(num_vertices-1) + 0]; + ny = normals[3*(num_vertices-1) + 1]; + nz = normals[3*(num_vertices-1) + 2]; + v4x = vertices[3*(num_vertices-1) + 0]; + v4y = vertices[3*(num_vertices-1) + 1]; + v4z = vertices[3*(num_vertices-1) + 2]; + v3x = vertices[3*(num_vertices-2) + 0]; + v3y = vertices[3*(num_vertices-2) + 1]; + v3z = vertices[3*(num_vertices-2) + 2]; + v2x = vertices[3*(num_vertices-3) + 0]; + v2y = vertices[3*(num_vertices-3) + 1]; + v2z = vertices[3*(num_vertices-3) + 2]; + v1x = vertices[3*(num_vertices-4) + 0]; + v1y = vertices[3*(num_vertices-4) + 1]; + v1z = vertices[3*(num_vertices-4) + 2]; + t4x = texcoords[2*(num_vertices-1) + 0]; + t4y = texcoords[2*(num_vertices-1) + 1]; + t3x = texcoords[2*(num_vertices-2) + 0]; + t3y = texcoords[2*(num_vertices-2) + 1]; + t2x = texcoords[2*(num_vertices-3) + 0]; + t2y = texcoords[2*(num_vertices-3) + 1]; + t1x = texcoords[2*(num_vertices-4) + 0]; + t1y = texcoords[2*(num_vertices-4) + 1]; + if ( num_vertices - 4 + x*y*4 > MAX_VERTICES ) { + fprintf(stderr, "Not enough free vertex space to subdivide face\n"); + } + num_vertices -= 4; + for ( u=0; u<x; u++ ) { + for ( v=0; v<y; v++ ) { + GLfloat l1x, l1y, l1z; + GLfloat l2x, l2y, l2z; + GLfloat l3x, l3y, l3z; + GLfloat l4x, l4y, l4z; + GLfloat tl1x, tl1y; + GLfloat tl2x, tl2y; + GLfloat tl3x, tl3y; + GLfloat tl4x, tl4y; + /* Interpolate vertices */ + l1x = v1x + u*(v2x-v1x)/x; + l1y = v1y + u*(v2y-v1y)/x; + l1z = v1z + u*(v2z-v1z)/x; + l2x = v4x + u*(v3x-v4x)/x; + l2y = v4y + u*(v3y-v4y)/x; + l2z = v4z + u*(v3z-v4z)/x; + l3x = v1x + (u+1)*(v2x-v1x)/x; + l3y = v1y + (u+1)*(v2y-v1y)/x; + l3z = v1z + (u+1)*(v2z-v1z)/x; + l4x = v4x + (u+1)*(v3x-v4x)/x; + l4y = v4y + (u+1)*(v3y-v4y)/x; + l4z = v4z + (u+1)*(v3z-v4z)/x; + /* Interpolate texture coords */ + tl1x = t1x + u*(t2x-t1x)/x; + tl1y = t1y + u*(t2y-t1y)/x; + tl2x = t4x + u*(t3x-t4x)/x; + tl2y = t4y + u*(t3y-t4y)/x; + tl3x = t1x + (u+1)*(t2x-t1x)/x; + tl3y = t1y + (u+1)*(t2y-t1y)/x; + tl4x = t4x + (u+1)*(t3x-t4x)/x; + tl4y = t4y + (u+1)*(t3y-t4y)/x; + /* Calculate vertices */ + vertices[3*num_vertices+0] = l1x + v*(l2x-l1x)/y; + vertices[3*num_vertices+1] = l1y + v*(l2y-l1y)/y; + vertices[3*num_vertices+2] = l1z + v*(l2z-l1z)/y; + texcoords[2*num_vertices+0] = tl1x + v*(tl2x-tl1x)/y; + texcoords[2*num_vertices+1] = tl1y + v*(tl2y-tl1y)/y; + normals[3*num_vertices+0] = nx; + normals[3*num_vertices+1] = ny; + normals[3*num_vertices+2] = nz; + num_vertices++; + vertices[3*num_vertices+0] = l3x + v*(l4x-l3x)/y; + vertices[3*num_vertices+1] = l3y + v*(l4y-l3y)/y; + vertices[3*num_vertices+2] = l3z + v*(l4z-l3z)/y; + texcoords[2*num_vertices+0] = tl3x + v*(tl4x-tl3x)/y; + texcoords[2*num_vertices+1] = tl3y + v*(tl4y-tl3y)/y; + normals[3*num_vertices+0] = nx; + normals[3*num_vertices+1] = ny; + normals[3*num_vertices+2] = nz; + num_vertices++; + vertices[3*num_vertices+0] = l3x + (v+1)*(l4x-l3x)/y; + vertices[3*num_vertices+1] = l3y + (v+1)*(l4y-l3y)/y; + vertices[3*num_vertices+2] = l3z + (v+1)*(l4z-l3z)/y; + texcoords[2*num_vertices+0] = tl3x + v*(tl4x-tl3x)/y; + texcoords[2*num_vertices+1] = tl3y + v*(tl4y-tl3y)/y; + normals[3*num_vertices+0] = nx; + normals[3*num_vertices+1] = ny; + normals[3*num_vertices+2] = nz; + num_vertices++; + vertices[3*num_vertices+0] = l1x + (v+1)*(l2x-l1x)/y; + vertices[3*num_vertices+1] = l1y + (v+1)*(l2y-l1y)/y; + vertices[3*num_vertices+2] = l1z + (v+1)*(l2z-l1z)/y; + texcoords[2*num_vertices+0] = tl1x + v*(tl2x-tl1x)/y; + texcoords[2*num_vertices+1] = tl1y + v*(tl2y-tl1y)/y; + normals[3*num_vertices+0] = nx; + normals[3*num_vertices+1] = ny; + normals[3*num_vertices+2] = nz; + num_vertices++; + } + } + } else { + fprintf(stderr, "'subdivide' directive must come at a quad boundary\n"); + return 1; + } + } else { + fprintf(stderr, "Can't subdivide this type of primitive\n"); + return 1; + } + } + if ( sscanf(line, "pulse %f %f %f", &r, &g, &b) == 3 ) { attribs = attribs | ATTRIB_COLOUR; attribs = attribs | ATTRIB_PULSE; diff --git a/src/render.c b/src/render.c index 07bdbd1..4f2ba97 100644 --- a/src/render.c +++ b/src/render.c @@ -347,7 +347,8 @@ static void render_draw_stuff(Game *game, Uint32 t) { static void render_setup_lighting(Game *game) { - GLfloat pos[] = {-1.0, -0.8, 1.3, 0.0}; + GLfloat pos[] = {0.0, 0.0, 0.0, 1.0}; + GLfloat dir[] = {0.0, 1.0, 0.0}; GLfloat ambient[4]; GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat specular[] = {1.0, 1.0, 1.0, 1.0}; @@ -356,9 +357,12 @@ static void render_setup_lighting(Game *game) { 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); + glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir); + glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30.0); glEnable(GL_LIGHT0); } @@ -449,6 +453,8 @@ void render_draw(Game *game, Uint32 t) { r = game->render; + //glPolygonMode(GL_FRONT, GL_LINE); + /* First pass: Looking upwards */ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->fbo); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |