forked from openframeworks/openFrameworks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testApp.cpp
168 lines (123 loc) · 4.52 KB
/
testApp.cpp
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
/**
*
* OFDevCon Example Code Sprint
*
* This example shows building a mesh, texturing it with a webcam, and extruding the vertices based on the pixel brightness
* Moving the mouse also rotates the mesh to see it at different angles
*
* Created by Tim Gfrerer and James George for openFrameworks workshop at Waves Festival Vienna sponsored by Lichterloh and Pratersauna
* Adapted during ofDevCon on 2/23/2012
*/
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofSetVerticalSync(true);
ofSetFrameRate(60);
ofBackground(66,66,66);
//initialize the video grabber
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(320,240);
//store the width and height for convenience
int width = vidGrabber.getWidth();
int height = vidGrabber.getHeight();
//add one vertex to the mesh for each pixel
for (int y = 0; y < height; y++){
for (int x = 0; x<width; x++){
mainMesh.addVertex(ofPoint(x,y,0)); // mesh index = x + y*width
// this replicates the pixel array within the camera bitmap...
mainMesh.addColor(ofFloatColor(0,0,0)); // placeholder for colour data, we'll get this from the camera
}
}
for (int y = 0; y<height-1; y++){
for (int x=0; x<width-1; x++){
mainMesh.addIndex(x+y*width); // 0
mainMesh.addIndex((x+1)+y*width); // 1
mainMesh.addIndex(x+(y+1)*width); // 10
mainMesh.addIndex((x+1)+y*width); // 1
mainMesh.addIndex((x+1)+(y+1)*width); // 11
mainMesh.addIndex(x+(y+1)*width); // 10
}
}
//this is an annoying thing that is used to flip the camera
cam.setScale(1,-1,1);
//this determines how much we push the meshes out
extrusionAmount = 300.0;
}
//--------------------------------------------------------------
void testApp::update(){
//grab a new frame
vidGrabber.grabFrame();
//update the mesh if we have a new frame
if (vidGrabber.isFrameNew()){
//this determines how far we extrude the mesh
for (int i=0; i<vidGrabber.getWidth()*vidGrabber.getHeight(); i++){
ofFloatColor sampleColor(vidGrabber.getPixels()[i*3]/255.f, // r
vidGrabber.getPixels()[i*3+1]/255.f, // g
vidGrabber.getPixels()[i*3+2]/255.f); // b
//now we get the vertex aat this position
//we extrude the mesh based on it's brightness
ofVec3f tmpVec = mainMesh.getVertex(i);
tmpVec.z = sampleColor.getBrightness() * extrusionAmount;
mainMesh.setVertex(i, tmpVec);
mainMesh.setColor(i, sampleColor);
}
}
//let's move the camera when you move the mouse
float rotateAmount = ofMap(ofGetMouseY(), 0, ofGetHeight(), 0, 360);
//move the camera around the mesh
ofVec3f camDirection(0,0,1);
ofVec3f centre(vidGrabber.getWidth()/2.f,vidGrabber.getHeight()/2.f, 255/2.f);
ofVec3f camDirectionRotated = camDirection.rotated(rotateAmount, ofVec3f(1,0,0));
ofVec3f camPosition = centre + camDirectionRotated * extrusionAmount;
cam.setPosition(camPosition);
cam.lookAt(centre);
}
//--------------------------------------------------------------
void testApp::draw(){
//we have to disable depth testing to draw the video frame
glDisable(GL_DEPTH_TEST);
// vidGrabber.draw(20,20);
//but we want to enable it to show the mesh
glEnable(GL_DEPTH_TEST);
cam.begin();
//You can either draw the mesh or the wireframe
// mainMesh.drawWireframe();
mainMesh.drawFaces();
cam.end();
//draw framerate for fun
ofSetColor(255);
string msg = "fps: " + ofToString(ofGetFrameRate(), 2);
ofDrawBitmapString(msg, 10, 20);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
switch(key) {
case 'f':
ofToggleFullscreen();
break;
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
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){
}