diff options
| author | Tim Redfern <tim@getdrop.com> | 2018-01-30 18:33:05 +0000 |
|---|---|---|
| committer | Tim Redfern <tim@getdrop.com> | 2018-01-30 18:33:05 +0000 |
| commit | b1744ad133b8543a3072897602ee33f6781655da (patch) | |
| tree | ed39a86086af69f8152aa443d972be3d590802a6 /polyTest/src/ofApp.cpp | |
| parent | be4ddf70e5cc22e3ff0ce2c45348231887946e13 (diff) | |
making a warp
Diffstat (limited to 'polyTest/src/ofApp.cpp')
| -rw-r--r-- | polyTest/src/ofApp.cpp | 110 |
1 files changed, 104 insertions, 6 deletions
diff --git a/polyTest/src/ofApp.cpp b/polyTest/src/ofApp.cpp index 1553666..7b87c9d 100644 --- a/polyTest/src/ofApp.cpp +++ b/polyTest/src/ofApp.cpp @@ -15,6 +15,86 @@ void ofApp::update(){ } +void gaussianElimination(float * input, int n) +{ + auto i = 0; + auto j = 0; + auto m = n - 1; + + while (i < m && j < n) + { + auto iMax = i; + for (auto k = i + 1; k < m; ++k) + { + if (fabs(input[k * n + j]) > fabs(input[iMax * n + j])) + { + iMax = k; + } + } + + if (input[iMax * n + j] != 0) + { + if (i != iMax) + { + for (auto k = 0; k < n; ++k) + { + auto ikIn = input[i * n + k]; + input[i * n + k] = input[iMax * n + k]; + input[iMax * n + k] = ikIn; + } + } + + float ijIn = input[i * n + j]; + for (auto k = 0; k < n; ++k) + { + input[i * n + k] /= ijIn; + } + + for (auto u = i + 1; u < m; ++u) + { + auto ujIn = input[u * n + j]; + for (auto k = 0; k < n; ++k) + { + input[u * n + k] -= ujIn * input[i * n + k]; + } + } + + ++i; + } + ++j; + } + + for (auto i = m - 2; i >= 0; --i) + { + for (auto j = i + 1; j < n - 1; ++j) + { + input[i * n + m] -= input[i * n + j] * input[j * n + m]; + } + } +} + +glm::mat4 getPerspectiveTransformMatrix(const glm::vec2 src[4], const glm::vec2 dst[4]) +{ + float p[8][9] = + { + { -src[0][0], -src[0][1], -1, 0, 0, 0, src[0][0] * dst[0][0], src[0][1] * dst[0][0], -dst[0][0] }, // h11 + { 0, 0, 0, -src[0][0], -src[0][1], -1, src[0][0] * dst[0][1], src[0][1] * dst[0][1], -dst[0][1] }, // h12 + { -src[1][0], -src[1][1], -1, 0, 0, 0, src[1][0] * dst[1][0], src[1][1] * dst[1][0], -dst[1][0] }, // h13 + { 0, 0, 0, -src[1][0], -src[1][1], -1, src[1][0] * dst[1][1], src[1][1] * dst[1][1], -dst[1][1] }, // h21 + { -src[2][0], -src[2][1], -1, 0, 0, 0, src[2][0] * dst[2][0], src[2][1] * dst[2][0], -dst[2][0] }, // h22 + { 0, 0, 0, -src[2][0], -src[2][1], -1, src[2][0] * dst[2][1], src[2][1] * dst[2][1], -dst[2][1] }, // h23 + { -src[3][0], -src[3][1], -1, 0, 0, 0, src[3][0] * dst[3][0], src[3][1] * dst[3][0], -dst[3][0] }, // h31 + { 0, 0, 0, -src[3][0], -src[3][1], -1, src[3][0] * dst[3][1], src[3][1] * dst[3][1], -dst[3][1] }, // h32 + }; + + gaussianElimination(&p[0][0], 9); + + return glm::mat4(p[0][8], p[3][8], 0, p[6][8], + p[1][8], p[4][8], 0, p[7][8], + 0, 0, 1, 0, + p[2][8], p[5][8], 0, 1); +} + ofPolyline ofApp::polyLineTransform(const ofPolyline& poly, ofMatrix4x4 xform){ ofPolyline tempPoly; for (auto& p:poly){ @@ -48,13 +128,31 @@ void ofApp::draw(){ ofBackground(0,0,0); ofSetColor(255,255,255); - ofTranslate(ofGetWidth()/2,ofGetHeight()/2); - ofRotate(0,0,1,ofGetElapsedTimef()*50); - drawPoly(makePolygon(4,200),200,200); - drawPoly(makePolygon(5,200),-200,200); - drawPoly(makePolygon(6,200),-200,-200); + ofMatrix4x4 m = ofMatrix4x4::newIdentityMatrix(); + m.rotateRad(ofGetElapsedTimef(),0,0,1); + m.translate(ofGetWidth()/2,ofGetHeight()/2,0); - + glm::vec2 src[]={ + glm::vec2(0,0), + glm::vec2(ofGetWidth(),0), + glm::vec2(ofGetWidth(),ofGetHeight()), + glm::vec2(0,ofGetHeight()) + }; + + glm::vec2 dst[]={ + glm::vec2(0,0), + glm::vec2(ofGetWidth(),0), + glm::vec2(ofGetWidth()-100,ofGetHeight()), + glm::vec2(100,ofGetHeight()) + }; + + ofMatrix4x4 warp =getPerspectiveTransformMatrix(src,dst); + + //drawPoly(polyLineTransform(makePolygon(4,200),m),200,200); + //drawPoly(polyLineTransform(makePolygon(5,200),m),-200,200); + //drawPoly(polyLineTransform(makePolygon(6,200),m),-200,-200); + + drawPoly(polyLineTransform(polyLineTransform(makePolygon(6,200),m),warp),0,0); } |
