From 772631d07bced1963b54978c7eb86ebfd69f633c Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 26 Nov 2010 19:02:27 +0000 Subject: Brute-force patch subdivision --- src/math/functions.h | 9 +++++++++ src/model/mapfile.cc | 51 +++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/math/functions.h b/src/math/functions.h index 8135392..27a068e 100644 --- a/src/math/functions.h +++ b/src/math/functions.h @@ -107,6 +107,15 @@ inline void swap(float &x, float &y) y = tmp; } +/// float to the n-th power +inline float pow(const float x, const size_t pow) { + float r = 1; + for (size_t i = 1; i <= pow; i++) { + r *= x; + } + return r; +} + } // namespace math #endif // __INCLUDED_MATH_FUNCTIONS_H__ diff --git a/src/model/mapfile.cc b/src/model/mapfile.cc index f57106d..76a75a9 100644 --- a/src/model/mapfile.cc +++ b/src/model/mapfile.cc @@ -388,18 +388,45 @@ bool MapFile::read_patchdef() } // load patch data into the model geometry - // TODO triangulate, assign texture coordinates - for (size_t r = 0; r < rows-1; r++) { - for (size_t c = 0; c < columns -1; c++) { - // unwind face counter-clockwise - Quad *quad = new Quad( - patchvertices[r+1][c+1], - patchvertices[r][c+1], - patchvertices[r][c], - patchvertices[r+1][c], - math::normal(patchvertices[r][c+1], patchvertices[r][c], patchvertices[r+1][c+1]) - ); - primitives->add_quad(quad); + // the patch is a set of quadratic B-splines with m = n =2 + // TODO texture coordinates, normals + + // TODO variable sibdivision + const size_t subdivide = 4; + const float binom[3] = {1, 2, 1}; + + for (size_t r = 0; r < rows-2; r +=2 ) { + for (size_t c = 0; c < columns -2; c += 2) { + math::Vector3f mesh[subdivide + 1][subdivide +1]; + + 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; + + for (size_t mi = 0; mi < 3; mi++) { + for (size_t mj = 0; mj < 3; mj++) { + mesh[i][j] += + patchvertices[r+mi][c+mj] * + binom[mi] * pow (u,mi) * pow(1.0f - u, 2-mi) * + binom[mj] * pow (v,mj) * pow(1.0f - v, 2-mj); + } + } + } + } + + for (size_t i = 0; i < subdivide; i++) { + for (size_t j = 0; j < subdivide; j++) { + Quad *quad = new Quad( + mesh[i+1][j+1], + mesh[i][j+1], + mesh[i][j], + mesh[i+1][j], + math::normal(mesh[i][j+1], mesh[i][j], mesh[i+1][j+1]) + ); + primitives->add_quad(quad); + } + } } } -- cgit v1.2.3