diff --git a/hw04/oglwidget.cpp b/hw04/oglwidget.cpp index 3be1f28..f96660f 100644 --- a/hw04/oglwidget.cpp +++ b/hw04/oglwidget.cpp @@ -24,6 +24,8 @@ void Print() { cout << p[0] << " " << p[1] << " " << p[2] << endl; } + void operator*=( float a); + void operator+=( Vertex a); // print coordinates on screen }; @@ -64,6 +66,18 @@ { return (a.p[0] == b.p[0]) && (a.p[1] == b.p[1]) && (a.p[2] == b.p[2]); } +void Vertex::operator*=( float a) +{ + this->p[0] *= a; + this->p[1] *= a; + this->p[2] *= a; +} +void Vertex::operator+=( Vertex a) +{ + this->p[0] += a.p[0]; + this->p[1] += a.p[1]; + this->p[2] += a.p[2]; +} class Tri { @@ -77,14 +91,20 @@ void Print(); }; +float beta_n( int n); void subDivEdgeMidpoint(); void saveData(); +void subDivLoop(); + Tri::Tri() { it[0] = -1; it[1] = -1; it[2] = -1; + ie[0] = -1; + ie[1] = -1; + ie[2] = -1; } Tri::Tri(int *i) { @@ -116,6 +136,13 @@ cout << endl; } +float beta_n( int n) +{ + float an = (3.0f/8.0f) + pow((3.0f/8.0f) + (1.0f/4.0f)*cos(2.0f*PI/n), 2); + + return (8.0f/5.0f)*an - (3.0f/5.0f); +} + void loadData() { @@ -146,8 +173,8 @@ tris.pop_back(); - subDivEdgeMidpoint(); - subDivEdgeMidpoint(); +// subDivEdgeMidpoint(); +// subDivEdgeMidpoint(); // Connectivity Algorithm for (int i = 0; i < tris.size(); ++i) { @@ -177,12 +204,134 @@ tris[i] = triag; } + for (auto triag : tris) { triag.Print(); } + subDivLoop(); -// saveData(); + saveData(); +} + +void subDivLoop() { + + for( int i=0; i< pts.size(); i++){ // multiply every vertex with beta + break; + int n = valences[i]; // n = valence of v_i + float beta = beta_n( n); + pts[i] *= beta; // v_i *= beta(n) + } + + const unsigned long long int amountToSubDiv = tris.size(); + for (int i = 0; i < amountToSubDiv; ++i) { + Tri triag = tris[i]; + + int ai = triag.iv[0]; + Vertex a = pts[ai]; + int bi = triag.iv[1]; + Vertex b = pts[bi]; + int ci = triag.iv[2]; + Vertex c = pts[ci]; + + Vertex d; + for (int ti : triag.it) { + int di = -1; + Tri t = tris[ti]; + int count = 0; + for (int y : t.iv) { + if (ci == y || bi == y) + count++; + else + di = y; + } + if (count == 2) { + cout << " 1: " << di; + d = pts[di]; + break; + } + } + + Vertex e0 = (1.0f/8.0f)*(a + (3.0f*b) + (3.0f*c) + d); + triag.ie[0] = pts.size(); + pts.push_back(e0); + valences.push_back(0); + + + + for (auto ti: triag.it) { + int di = -1; + Tri t = tris[ti]; + int count = 0; + for (int y : t.iv) { + if (ai == y || ci == y) + count++; + else + di = y; + } + if (count == 2) { + cout << " 2: " << di; + d = pts[di]; + break; + } + } + + Vertex e1 = (1.0f/8.0f)*((3.0f*a) + b + (3.0f*c) + d); + triag.ie[1] = pts.size(); + pts.push_back(e1); + valences.push_back(0); + + + for (auto ti: triag.it) { + int di = -1; + Tri t = tris[ti]; + int count = 0; + for (int y : t.iv) { + if (ai == y || bi == y) + count++; + else + di = y; + } + if (count == 2) { + cout << " 3: " << di << endl; + d = pts[di]; + break; + } + } + + Vertex e2 = (1.0f/8.0f)*((3.0f*a) + (3.0f*b) + c + d); + triag.ie[2] = pts.size(); + pts.push_back(e2); + valences.push_back(0); + + + cout << e0.p[0] << " " << e0.p[1] << " " << e0.p[2] << endl; + cout << e1.p[0] << " " << e1.p[1] << " " << e1.p[2] << endl; + cout << e2.p[0] << " " << e2.p[1] << " " << e2.p[2] << endl << endl; + + +// a += ((1-beta_n(valences[ai])) / (valences[ai])) * (0.5f * (e1 + e2)); +// b += ((1-beta_n(valences[bi])) / (valences[bi])) * (0.5f * (e0 + e2)); +// c += ((1-beta_n(valences[ci])) / (valences[ci])) * (0.5f * (e1 + e0)); + + +// pts[ai] = a; +// pts[bi] = b; +// pts[ci] = c; + + + triag.iv[0] = triag.ie[1]; + triag.iv[1] = triag.ie[0]; + triag.iv[2] = ci; + + tris[i] = triag; +// continue; + + tris.push_back(*new Tri(triag.ie[1], triag.ie[2], triag.ie[0])); + tris.push_back(*new Tri(ai, triag.ie[2], triag.ie[1])); + tris.push_back(*new Tri(triag.ie[2], bi, triag.ie[0])); + } + } void saveData() { @@ -274,6 +423,12 @@ } glEnd(); + + glBegin(GL_POINTS); + for (auto vert : pts) { + glVertex3fv(vert.p); + } + glEnd(); } // initialize Open GL lighting and projection matrix