Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2010-11-26 19:02:27 +0000
committerStijn Buys <ingar@osirion.org>2010-11-26 19:02:27 +0000
commit772631d07bced1963b54978c7eb86ebfd69f633c (patch)
tree294dd732a9378d604831da6ea73d60c4f8c236d3
parenta96da42a7c1232dee986e1f658ee34440f78ac6c (diff)
Brute-force patch subdivision
-rw-r--r--src/math/functions.h9
-rw-r--r--src/model/mapfile.cc51
2 files changed, 48 insertions, 12 deletions
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);
+ }
+ }
}
}