From 927f39b74d86d329bde1ded5eeaa9b235b3d9c86 Mon Sep 17 00:00:00 2001 From: Tom White Date: Thu, 14 Aug 2008 22:56:58 +0100 Subject: Interpolate normals when subdividing --- src/model.c | 61 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/model.c b/src/model.c index 5c12f0e..3dc6d46 100644 --- a/src/model.c +++ b/src/model.c @@ -343,14 +343,26 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render GLfloat v2x, v2y, v2z; GLfloat v3x, v3y, v3z; GLfloat v4x, v4y, v4z; - GLfloat nx, ny, nz; + GLfloat n1x, n1y, n1z; + GLfloat n2x, n2y, n2z; + GLfloat n3x, n3y, n3z; + GLfloat n4x, n4y, n4z; 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]; + n4x = normals[3*(num_vertices-1) + 0]; + n4y = normals[3*(num_vertices-1) + 1]; + n4z = normals[3*(num_vertices-1) + 2]; + n3x = normals[3*(num_vertices-2) + 0]; + n3y = normals[3*(num_vertices-2) + 1]; + n3z = normals[3*(num_vertices-2) + 2]; + n2x = normals[3*(num_vertices-3) + 0]; + n2y = normals[3*(num_vertices-3) + 1]; + n2z = normals[3*(num_vertices-3) + 2]; + n1x = normals[3*(num_vertices-4) + 0]; + n1y = normals[3*(num_vertices-4) + 1]; + n1z = normals[3*(num_vertices-4) + 2]; v4x = vertices[3*(num_vertices-1) + 0]; v4y = vertices[3*(num_vertices-1) + 1]; v4z = vertices[3*(num_vertices-1) + 2]; @@ -385,6 +397,10 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render GLfloat tl2x, tl2y; GLfloat tl3x, tl3y; GLfloat tl4x, tl4y; + GLfloat nl1x, nl1y, nl1z; + GLfloat nl2x, nl2y, nl2z; + GLfloat nl3x, nl3y, nl3z; + GLfloat nl4x, nl4y, nl4z; /* Interpolate vertices */ l1x = v1x + u*(v2x-v1x)/x; l1y = v1y + u*(v2y-v1y)/x; @@ -407,42 +423,55 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render tl3y = t1y + (u+1)*(t2y-t1y)/x; tl4x = t4x + (u+1)*(t3x-t4x)/x; tl4y = t4y + (u+1)*(t3y-t4y)/x; + /* Interpolate normals */ + nl1x = n1x + u*(n2x-n1x)/x; + nl1y = n1y + u*(n2y-n1y)/x; + nl1z = n1z + u*(n2z-n1z)/x; + nl2x = n4x + u*(n3x-n4x)/x; + nl2y = n4y + u*(n3y-n4y)/x; + nl2z = n4z + u*(n3z-n4z)/x; + nl3x = n1x + (u+1)*(n2x-n1x)/x; + nl3y = n1y + (u+1)*(n2y-n1y)/x; + nl3z = n1z + (u+1)*(n2z-n1z)/x; + nl4x = n4x + (u+1)*(n3x-n4x)/x; + nl4y = n4y + (u+1)*(n3y-n4y)/x; + nl4z = n4z + (u+1)*(n3z-n4z)/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; + normals[3*num_vertices+0] = nl1x + v*(nl2x-nl1x)/y; + normals[3*num_vertices+1] = nl1y + v*(nl2y-nl1y)/y; + normals[3*num_vertices+2] = nl1z + v*(nl2z-nl1z)/y; 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; + normals[3*num_vertices+0] = nl3x + v*(nl4x-nl3x)/y; + normals[3*num_vertices+1] = nl3y + v*(nl4y-nl3y)/y; + normals[3*num_vertices+2] = nl3z + v*(nl4z-nl3z)/y; 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+1)*(tl4x-tl3x)/y; texcoords[2*num_vertices+1] = tl3y + (v+1)*(tl4y-tl3y)/y; - normals[3*num_vertices+0] = nx; - normals[3*num_vertices+1] = ny; - normals[3*num_vertices+2] = nz; + normals[3*num_vertices+0] = nl3x + (v+1)*(nl4x-nl3x)/y; + normals[3*num_vertices+1] = nl3y + (v+1)*(nl4y-nl3y)/y; + normals[3*num_vertices+2] = nl3z + (v+1)*(nl4z-nl3z)/y; 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+1)*(tl2x-tl1x)/y; texcoords[2*num_vertices+1] = tl1y + (v+1)*(tl2y-tl1y)/y; - normals[3*num_vertices+0] = nx; - normals[3*num_vertices+1] = ny; - normals[3*num_vertices+2] = nz; + normals[3*num_vertices+0] = n1x + (v+1)*(nl2x-nl1x)/y; + normals[3*num_vertices+1] = n1y + (v+1)*(nl2y-nl1y)/y; + normals[3*num_vertices+2] = n1z + (v+1)*(nl2z-nl1z)/y; num_vertices++; } } -- cgit v1.2.3