summaryrefslogtreecommitdiff
path: root/tshirtmapping/src/testApp.cpp
blob: dd00b8f9462f899aaab656695a3d91728ea150e9 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "testApp.h"
//texture binding with normalised coords
void bindTexture(ofBaseHasTexture &t) {
    ofTexture &tex = t.getTextureReference();
    tex.bind();

    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    glLoadIdentity();

    ofTextureData texData = tex.getTextureData();
    if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) {
        glScalef(tex.getWidth(), tex.getHeight(), 1.0f);
    } else {
        glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f);
    }

    glMatrixMode(GL_MODELVIEW);
}
void unbindTexture(ofBaseHasTexture &t) {
    t.getTextureReference().unbind();

    glMatrixMode(GL_TEXTURE);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}
void bindTex(ofTexture &tex) {
    tex.bind();

    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    glLoadIdentity();

    ofTextureData texData = tex.getTextureData();
    if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) {
        glScalef(tex.getWidth(), tex.getHeight(), 1.0f);
    } else {
        glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f);
    }

    glMatrixMode(GL_MODELVIEW);
}
void unbindTex(ofTexture &tex) {
    tex.unbind();

    glMatrixMode(GL_TEXTURE);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}

//--------------------------------------------------------------
void testApp::setup(){
    draw3D=true;

	isLive			= true;
	isTracking		= false;
	isFiltering		= false;
	isCloud		    = false;
	isCPBkgnd		= true;
	isMasking		= true;

	nearThreshold = 500;
	farThreshold  = 1000;

	filterFactor = 10.0f;

	recordContext.setup();
	recordDepth.setup(&recordContext);
	recordImage.setup(&recordContext);
	recordUser.setup(&recordContext);
	recordUser.setSmoothing(filterFactor);				// built in openni skeleton smoothing...
	recordUser.setUseMaskPixels(isMasking);
	recordUser.setUseCloudPoints(draw3D);
	recordUser.setMaxNumberOfUsers(2);

	texture.setUseTexture(true);
	texture.loadImage("target.png");

	camRotationY=0;

	ofBackground(0,0,0);
}

//--------------------------------------------------------------
void testApp::update(){
	// update all nodes
		recordContext.update();
		recordDepth.update();
		recordImage.update();
		recordUser.update();
}

void testApp::setCam(){

	ofNode c=ofNode();
	ofNode t=ofNode();
	t.setParent(c);
	t.setPosition(vars[1].getVal(),vars[2].getVal(),vars[3].getVal());
	//make target controls relative to rotation
	c.rotate(vars[5].getVal(), ofVec3f(0, 1, 0));

	target.setPosition(t.getGlobalPosition());
	//camera.orbit(vars[5].getVal(), vars[4].getVal(), vars[7].getVal(), target);
	camera.setFov(vars[0].getVal());
	if (vertigo) {
		//distance=width/(2 tan (fov *0.5)
		//multiply distance by ratio of tans of fov before and after
		vars[7].setVal((tan((vars[0].readVal()-vars[0].inc)*PI*0.0027777)*vars[7].readVal())/tan(vars[0].readVal()*PI*0.0027777));
	}



	ofVec3f p(0, 0, vars[7].getVal());
	//p.rotate(ofClamp(vars[2].getVal(), -89, 89), ofVec3f(1, 0, 0));
	p.rotate(vars[4].getVal(), ofVec3f(1, 0, 0));
	p.rotate(vars[5].getVal(), ofVec3f(0, 1, 0));
	p += target.getPosition();
	camera.setPosition(p);

    camera.lookAt(target,ofVec3f(0,1,0).rotate(vars[6].getVal(),ofVec3f(0,0,1)).rotate(vars[5].getVal(),ofVec3f(0,1,0)));
	light.setPosition(camera.getGlobalPosition());

}

//--------------------------------------------------------------
void testApp::draw(){
	ofSetColor(255, 255, 255);

	if(!draw3D) {
        //recordDepth.draw(0,0,ofGetWidth(),ofGetHeight());
        recordUser.draw(ofGetWidth(),ofGetHeight());
        ofSetColor(255,255,255);
        ofSetLineWidth(5);
        bindTexture(texture);
        ofEnableAlphaBlending();
        glPushMatrix();
            glScalef(ofGetWidth()/640.0f,ofGetHeight()/480.0f, 1);
            for (int i=0;i<recordUser.getNumberOfTrackedUsers();i++){
                ofxTrackedUser* u=(recordUser.getTrackedUser(i+1));

                if (u->neck.found) {

                    float ratio=16.0f/9.0f;
                    float heightratio=0.8f;
                    float widthratio=0.8f;

                    float ax=((float)(u->hip.position[1].X-u->hip.position[0].X));  //hip width x
                    float ay=((float)(u->hip.position[1].Y-u->hip.position[0].Y)); //hip width y

                    float cx=(float)(u->hip.position[0].X)+(ax*0.5f); //centre of hips
                    float cy=(float)(u->hip.position[0].Y)+(ay*0.5f);

                    float hx=(((float)(u->left_shoulder.position[0].X))-cx); //height of torso
                    float hy=(((float)(u->left_shoulder.position[0].Y))-cy);

                    float p1x=cx+(hx*(1.0f-heightratio)*0.5f); //centre of frame bottom
                    float p1y=cy+(hy*(1.0f-heightratio)*0.5f);

                    float p2x=cx+(hx*(1.0f-((1.0f-heightratio)*0.5f))); //centre of frame top
                    float p2y=cy+(hy*(1.0f-((1.0f-heightratio)*0.5f)));

                    ofVec3f c1=ofVec3f(p2x-(ax*0.5*widthratio), p2y-(ay*0.5*widthratio),0);
                    ofVec3f c2=ofVec3f(p2x+(ax*0.5*widthratio), p2y+(ay*0.5*widthratio),0);
                    ofVec3f c3=ofVec3f(p1x-(ax*0.5*widthratio), p1y-(ay*0.5*widthratio),0);
                    ofVec3f c4=ofVec3f(p1x+(ax*0.5*widthratio), p1y+(ay*0.5*widthratio),0);

                    ofMesh targ;
                    targ.addVertex(c1);
                    targ.addTexCoord(ofVec2f(0,0));
                    targ.addVertex(c2);
                    targ.addTexCoord(ofVec2f(1,0));
                    targ.addVertex(c3);
                    targ.addTexCoord(ofVec2f(0,1));
                    targ.addVertex(c3);
                    targ.addTexCoord(ofVec2f(0,1));
                    targ.addVertex(c2);
                    targ.addTexCoord(ofVec2f(1,0));
                    targ.addVertex(c4);
                    targ.addTexCoord(ofVec2f(1,1));
                    targ.draw();

                    }
            }
        glPopMatrix();
        unbindTexture(texture);
	}
	else { //3D -- works!
	glPushMatrix();

        int w = recordUser.getWidth();
        int h = recordUser.getHeight();

        glTranslatef(w, h/2, -500);
        ofRotateY(camRotationY);
	//camera.begin();

        glBegin(GL_POINTS);

        int step = 1;

        for(int y = 0; y < h; y += step) {
            for(int x = 0; x < w; x += step) {
                ofPoint pos = recordUser.getWorldCoordinateAt(x, y, 0); //userID);
                if (pos.z == 0 ) continue;	// gets rid of background -> still a bit weird if userID > 0... //&& isCPBkgnd
                ofColor color = recordUser.getWorldColorAt(x,y, 0); //userID);
                glColor4ub((unsigned char)color.r, (unsigned char)color.g, (unsigned char)color.b, (unsigned char)color.a);
                glVertex3f(pos.x, pos.y, pos.z);
            }
        }

        glEnd();

        glColor3f(1.0f, 1.0f, 1.0f);


        bindTexture(texture);
        ofEnableAlphaBlending();
        glPushMatrix();
        //glScalef(ofGetWidth()/640.0f,ofGetHeight()/480.0f, 1);
        for (int i=0;i<recordUser.getNumberOfTrackedUsers();i++){
            ofxTrackedUser* u=(recordUser.getTrackedUser(i+1));

            if (u->neck.found) {

                float ratio=16.0f/9.0f;
                float heightratio=0.8f;
                float widthratio=0.8f;

                float ax=((float)(u->hip.position[1].X-u->hip.position[0].X));  //hip vector
                float ay=((float)(u->hip.position[1].Y-u->hip.position[0].Y));
                float az=((float)(u->hip.position[1].Z-u->hip.position[0].Z));

                float cx=(float)(u->hip.position[0].X)+(ax*0.5f); //centre of hips
                float cy=(float)(u->hip.position[0].Y)+(ay*0.5f);
                float cz=(float)(u->hip.position[0].Z)+(az*0.5f);

                float hx=(((float)(u->left_shoulder.position[0].X))-cx); //torso vector
                float hy=(((float)(u->left_shoulder.position[0].Y))-cy);
                float hz=(((float)(u->left_shoulder.position[0].Z))-cz);

                float p1x=cx+(hx*(1.0f-heightratio)*0.5f); //centre of frame bottom
                float p1y=cy+(hy*(1.0f-heightratio)*0.5f);
                float p1z=cz+(hz*(1.0f-heightratio)*0.5f);

                float p2x=cx+(hx*(1.0f-((1.0f-heightratio)*0.5f))); //centre of frame top
                float p2y=cy+(hy*(1.0f-((1.0f-heightratio)*0.5f)));
                float p2z=cz+(hz*(1.0f-((1.0f-heightratio)*0.5f)));

                ofVec3f c1=ofVec3f(p2x-(ax*0.5*widthratio), p2y-(ay*0.5*widthratio), p2z-(az*0.5*widthratio));
                ofVec3f c2=ofVec3f(p2x+(ax*0.5*widthratio), p2y+(ay*0.5*widthratio), p2z+(az*0.5*widthratio));
                ofVec3f c3=ofVec3f(p1x-(ax*0.5*widthratio), p1y-(ay*0.5*widthratio), p1z-(az*0.5*widthratio));
                ofVec3f c4=ofVec3f(p1x+(ax*0.5*widthratio), p1y+(ay*0.5*widthratio), p1z+(az*0.5*widthratio));

                ofMesh targ;
                targ.addVertex(c1);
                targ.addTexCoord(ofVec2f(0,0));
                targ.addVertex(c2);
                targ.addTexCoord(ofVec2f(1,0));
                targ.addVertex(c3);
                targ.addTexCoord(ofVec2f(0,1));
                targ.addVertex(c3);
                targ.addTexCoord(ofVec2f(0,1));
                targ.addVertex(c2);
                targ.addTexCoord(ofVec2f(1,0));
                targ.addVertex(c4);
                targ.addTexCoord(ofVec2f(1,1));
                targ.draw();

                }
        }
        glPopMatrix();
        unbindTexture(texture);

	//camera.end();

        glPopMatrix();

	}

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
    camRotationY = x;
}

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

}

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

}

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

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}