summaryrefslogtreecommitdiff
path: root/glitcher/src/ofApp.cpp
blob: 6055485407b9c289056e9a49801d471f01abfd7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "ofApp.h"


//--------------------------------------------------------------
void ofApp::setup() {
	ofSetLogLevel(OF_LOG_WARNING);
	
	ofSetFrameRate(60);
	
	buffer.allocate(ofGetWidth(),ofGetHeight());
	renderFBO.allocate(ofGetWidth(),ofGetHeight(),GL_RGB);
	//buffer.clear();
}

//--------------------------------------------------------------
void ofApp::update() {
	ofSetWindowTitle(ofToString(ofGetFrameRate()));
}

//--------------------------------------------------------------
void ofApp::draw() {

	//rough out how it could be possible to perform an avs style glitch transform
	//within a drawing
	//drawing- to an fbo
	//distorting- from an opencv texture
	//how to get from an fbo to an opencv texture
	ofSetColor(255, 255, 255);
	
	renderFBO.begin();
	buffer.draw(0,0);


	ofNoFill();
	ofTranslate(ofGetWidth()/2,ofGetHeight()/2);
    ofRect(-20,-20,40,40);
    renderFBO.end();
    renderFBO.readToPixels(buffer.getPixelsRef());
    buffer.flagImageChanged();

    cv::Mat buf = buffer.getCvImage();
    cv::Mat dstbuf;

    //create low res remap target

    cv::Mat dstX(32,24,CV_32FC1);
    cv::Mat dstY(32,24,CV_32FC1);
    cv::Mat srcX(32,24,CV_32FC1);
    cv::Mat srcY(32,24,CV_32FC1);

    float xFactor=ofGetWidth()/dstX.rows;
    float yFactor=ofGetHeight()/dstX.cols;
    for (int i=0;i<dstX.cols;i++){
    	for (int j=0;j<dstX.rows;j++){
    		dstX.at<double>(i,j)=i*xFactor;
    		dstY.at<double>(i,j)=j*yFactor;
    	}
    }

    //transform the low res matrix
    float tX=0; //fraction of image
    float tY=0; //fraction of image
    float oX=0; //fraction of image
    float oY=0; //fraction of image
    float s=0.99;
    float r=0.0;

    cv::Point2f srcTri[3], dstTri[3];
	cv::Mat rot_mat(2,3,CV_32FC1);
	cv::Mat trans_mat(2,3,CV_32FC1);
	cv::Mat out_mat(3,3,CV_32FC1);

	// Compute matrix by creating triangle and transforming
	srcTri[0].x=0;
	srcTri[0].y=0;
	srcTri[1].x=dstX.rows-1;
	srcTri[1].y=0;
	srcTri[2].x=0;
	srcTri[2].y=dstX.cols-1;
	for (int i=0;i<3;i++){
		dstTri[i].x=srcTri[i].x+(tX*dstX.cols);
		dstTri[i].y=srcTri[i].y+(tY*dstX.cols); //use cols for equiv coords
		//rotate and scale around centre
		//transform to centre
		dstTri[i].x-=(oX*dstX.cols);
		dstTri[i].y-=(oY*dstX.cols);

		dstTri[i].x*=s;
		dstTri[i].y*=s;

		double dx=(dstTri[i].x*cos(r))-(dstTri[i].y*sin(r));
		double dy=(dstTri[i].x*sin(r))+(dstTri[i].y*cos(r));

		dstTri[i].x=dx;
		dstTri[i].y=dy;

		//transform back
		dstTri[i].x+=(oX*dstX.cols);
		dstTri[i].y+=(oY*dstX.cols);
	}
	trans_mat=getAffineTransform( srcTri, dstTri );
	warpAffine( srcX, dstX, trans_mat, srcX.size(), cv::INTER_NEAREST, cv::BORDER_WRAP);
	warpAffine( srcY, dstY, trans_mat, srcX.size(), cv::INTER_NEAREST, cv::BORDER_WRAP);

	cv::Mat scaledstX;
	cv::Mat scaledstY;

	cv::resize(dstX,scaledstX, cv::Size(ofGetWidth(),ofGetHeight()), 0, 0, cv::INTER_NEAREST);
	cv::resize(dstY,scaledstY, cv::Size(ofGetWidth(),ofGetHeight()), 0, 0, cv::INTER_NEAREST);

	cv::remap(buf,dstbuf,scaledstX,scaledstY, cv::INTER_LINEAR, cv::BORDER_WRAP, cv::Scalar(0,0, 0) );
	IplImage *tmp = new IplImage(dstbuf);

	buffer=tmp;
	delete tmp;

    renderFBO.draw(0,0); //eventually textured into a viewport

}



//--------------------------------------------------------------
void ofApp::exit() {
	
}

//--------------------------------------------------------------
void ofApp::keyPressed (int key) {
	switch (key) {
			
		case OF_KEY_UP:
			break;
			
		case OF_KEY_DOWN:
			break;
	}
}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button)
{}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button)
{}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button)
{}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h)
{}