diff options
author | Stijn Buys <ingar@osirion.org> | 2010-12-01 13:07:50 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2010-12-01 13:07:50 +0000 |
commit | 8586343a28bdce6251037644fe667de8a68fe887 (patch) | |
tree | cbcd17a73596cfa7c82c7ef3728c7ec3835c385c /src/model | |
parent | 2cc28deb2c51f2269116895e7282299d18578813 (diff) |
Corrected patch normal calculation.
Diffstat (limited to 'src/model')
-rw-r--r-- | src/model/mapfile.cc | 63 |
1 files changed, 20 insertions, 43 deletions
diff --git a/src/model/mapfile.cc b/src/model/mapfile.cc index bf41cb4..94a8eab 100644 --- a/src/model/mapfile.cc +++ b/src/model/mapfile.cc @@ -405,39 +405,22 @@ bool MapFile::read_patchdef() math::Vector3f mesh[subdivide + 1][subdivide +1]; math::Vector3f meshnormals[subdivide + 1][subdivide +1]; - math::Vector3f patchnormals[3][3]; - - patchnormals[0][0].assign(math::normal(patchvertices[r+0][c+0], patchvertices[r+1][c+0], patchvertices[r+0][c+1])); - patchnormals[0][2].assign(math::normal(patchvertices[r+0][c+2], patchvertices[r+0][c+1], patchvertices[r+1][c+2])); - patchnormals[2][2].assign(math::normal(patchvertices[r+2][c+2], patchvertices[r+1][c+2], patchvertices[r+2][c+1])); - patchnormals[2][0].assign(math::normal(patchvertices[r+2][c+0], patchvertices[r+2][c+1], patchvertices[r+1][c+0])); - - patchnormals[0][1].assign(patchnormals[0][0] + patchnormals[0][2]); - patchnormals[0][1] *= 0.5f; - patchnormals[0][1].normalize(); - - patchnormals[2][1].assign(patchnormals[2][0] + patchnormals[2][2]); - patchnormals[2][1] *= 0.5f; - patchnormals[2][1].normalize(); - - patchnormals[1][0].assign(patchnormals[0][0] + patchnormals[2][0]); - patchnormals[1][0] *= 0.5f; - patchnormals[1][0].normalize(); - - patchnormals[1][2].assign(patchnormals[0][2] + patchnormals[2][2]); - patchnormals[1][2] *= 0.5f; - patchnormals[1][2].normalize(); - - patchnormals[1][1].assign(patchnormals[0][1] + patchnormals[2][1] + patchnormals[1][0] + patchnormals[1][2]); - patchnormals[1][1] *= 0.25f; - patchnormals[1][1].normalize(); - + for (size_t i = 0; i <= subdivide; i++) { const float u = (float) i / (float) subdivide; for (size_t j = 0; j <= subdivide; j++) { const float v = (float) j / (float) subdivide; - //math::Vector3f tan_u; - //math::Vector3f tan_v; + math::Vector3f tan_u; + math::Vector3f tan_v; + + // the surface normals aren't defined at the edges, + // we cheat and pull the edge points towards the middle + float un = u; + float vn = v; + if ((i ==0) || (j==0) || (i == subdivide) || (j == subdivide)) { + un = (u - 0.5f) * 0.95f + 0.5f; + vn = (v - 0.5f) * 0.95f + 0.5f; + } for (size_t mi = 0; mi < 3; mi++) { for (size_t mj = 0; mj < 3; mj++) { @@ -445,32 +428,26 @@ bool MapFile::read_patchdef() patchvertices[r+mi][c+mj] * binom[mi] * powf (u, (float) mi) * powf(1.0f - u, 2.0f - (float) mi) * binom[mj] * powf (v, (float) mj) * powf(1.0f - v, 2.0f - (float) mj); - - meshnormals[i][j] += - patchnormals[mi][mj] * - binom[mi] * powf (u, (float) mi) * powf(1.0f - u, 2.0f - (float) mi) * - binom[mj] * powf (v, (float) mj) * powf(1.0f - v, 2.0f - (float) mj); - /* + tan_u += patchvertices[r+mi][c+mj] * binom[mi] * ( - ((float) mi) * powf(u, (float) mi - 1.0f) * powf(1.0f - u, 2.0f - (float) mi) + - ((float) mi - 2.0f) * powf(u, (float) mi) * powf(1.0f - u, 1.0f - (float) mi) + ((float) mi) * powf(un, (float) mi - 1.0f) * powf(1.0f - un, 2.0f - (float) mi) + + ((float) mi - 2.0f) * powf(un, (float) mi) * powf(1.0f - un, 1.0f - (float) mi) ) * - binom[mj] * powf (v, (float) mj) * powf(1.0f - v, 2.0f - (float) mj); + binom[mj] * powf (vn, (float) mj) * powf(1.0f - vn, 2.0f - (float) mj); tan_v += patchvertices[r+mi][c+mj] * - binom[mi] * powf (u, (float) mi) * powf(1.0f - u, 2.0f - (float) mi) * + binom[mi] * powf (un, (float) mi) * powf(1.0f - un, 2.0f - (float) mi) * binom[mj] * ( - ((float) mj) * powf(v, (float) mj - 1.0f) * powf(1.0f - v, 2.0f - (float) mj) + - ((float) mj - 2.0f) * powf(v, (float) mj) * powf(1.0f - v, 1.0f - (float) mj) + ((float) mj) * powf(vn, (float) mj - 1.0f) * powf(1.0f - vn, 2.0f - (float) mj) + + ((float) mj - 2.0f) * powf(vn, (float) mj) * powf(1.0f - vn, 1.0f - (float) mj) ); - */ } } - //meshnormals[i][j].assign(math::crossproduct(tan_u, tan_v)); + meshnormals[i][j].assign(math::crossproduct(tan_u, tan_v)); meshnormals[i][j].normalize(); class_box.expand(mesh[i][j] * SCALE); } |