forked from linuxmao-org/Frontieres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudVis.cpp
380 lines (322 loc) · 8.63 KB
/
CloudVis.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//------------------------------------------------------------------------------
// FRONTIÈRES: An interactive granular sampler.
//------------------------------------------------------------------------------
// More information is available at
// http::/ccrma.stanford.edu/~carlsonc/256a/Borderlands/index.html
//
//
// Copyright (C) 2011 Christopher Carlson
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// CloudVis.cpp
// Frontières
//
// Created by Christopher Carlson on 11/15/11.
//
#include "visual/CloudVis.h"
#include "visual/GrainVis.h"
#include "visual/SampleVis.h"
#include "model/Scene.h"
#include "model/ParamCloud.h"
#include "utility/GTime.h"
// TODO avoid this
#include "interface/MyGLApplication.h"
#include "interface/MyGLWindow.h"
#include "model/Cloud.h"
// graphics includes
#include <qopengl.h>
#ifndef __APPLE__
#include <GL/glu.h>
#endif
#include <math.h>
extern CloudParams g_defaultCloudParams;
//-----------------------------------------------------------------------------------------------
// GRAPHICS
//-----------------------------------------------------------------------------------------------
CloudVis::~CloudVis()
{
for (GrainVis *vis : myGrainsV)
delete vis;
delete myTrajectory;
}
CloudVis::CloudVis(float x, float y, unsigned int numGrainsVis,
VecSceneSample *rects)
{
startTime = GTime::instance().sec;
updateCloudPosition(x, y);
xRandExtent = g_defaultCloudParams.xRandExtent;
yRandExtent = g_defaultCloudParams.yRandExtent;
// init add and remove flags to false
addFlag = false;
removeFlag = false;
// select on instantiation
isSelected = true;
// pulse frequency
freq = 1.0f;
// pointer to landscape visualization objects
theLandscape = rects;
for (int i = 0; i < numGrainsVis; i++) {
myGrainsV.push_back(new GrainVis(gcX, gcY));
}
numGrains = numGrainsVis;
// visualization stuff
minSelRad = 15.0f;
maxSelRad = 19.0f;
lambda = 0.997;
selRad = minSelRad;
targetRad = maxSelRad;
lastDrawTime=0;
//initialise without moves
isMoving=false;
myTrajectory=nullptr;
}
void CloudVis::setDuration(float dur)
{
freq = 1000.0 / dur;
}
// print information
void CloudVis::describe(std::ostream &out)
{
out << "- X : " << getX() << "\n";
out << "- Y : " << getY() << "\n";
out << "- X extent : " << getXRandExtent() << "\n";
out << "- Y extent : " << getYRandExtent() << "\n";
}
void CloudVis::registerCloud(Cloud *cloudToRegister)
{
myCloud = cloudToRegister;
}
void CloudVis::changesDone(bool done)
{
changed_gcX= done;
changed_gcY = done;
changed_xRandExtent = done;
changed_yRandExtent = done;
}
// return cloud x
float CloudVis::getX()
{
return gcX;
}
// return cloud y
float CloudVis::getY()
{
return gcY;
}
Trajectory* CloudVis::getTrajectory()
{
return this->myTrajectory;
}
bool CloudVis::getIsMoving()
{
return isMoving;
}
void CloudVis::setTrajectory(Trajectory *tr)
{
if(isMoving)
{
delete myTrajectory;
}
isMoving=true;
myTrajectory=tr;
}
void CloudVis::stopTrajectory()
{
isMoving=false;
}
void CloudVis::startTrajectory()
{
isMoving=true;
}
void CloudVis::draw()
{
double t_sec = 0;
double dt=0;
t_sec = GTime::instance().sec - startTime;
dt=t_sec-lastDrawTime;
lastDrawTime=t_sec;
//std::cout << "time between drawings: " <<dt<<std::endl;
// cout << t_sec << endl;
//computing trajectory
std::vector<double> pos = {0.,0.};
if (this->getIsMoving() && !this->isSelected){
pos=this->myTrajectory->computeTrajectory(dt);
updateCloudPosition(pos[0],pos[1] );
}
// if ((g_time -last_gtime) > 50){
glPushMatrix();
glTranslatef((GLfloat)gcX, (GLfloat)gcY, 0.0);
// Cloud representation
if (isSelected)
glColor4f(0.1, 0.7, 0.6, 0.35);
else
glColor4f(0.0, 0.4, 0.7, 0.3);
selRad = minSelRad + 0.5 * (maxSelRad - minSelRad) *
sin(2 * PI * (freq * t_sec + 0.125));
gluDisk(gluNewQuadric(), selRad, selRad + 5.0, 128, 2);
glPopMatrix();
glPushMatrix();
// update grain motion;
// Individual grains
for (int i = 0; i < numGrains; i++) {
myGrainsV[i]->draw();
}
glPopMatrix();
// end point version
}
// get trigger position/volume relative to sample visuals for single grain voice
void CloudVis::getTriggerPos(unsigned int idx, double *playPos,
double *playVol, float theDur)
{
bool trigger = false;
SampleVis *theRect = NULL;
if (idx < myGrainsV.size()) {
GrainVis *theGrain = myGrainsV[idx];
// TODO: motion models
// updateGrainPosition(idx,gcX + randf()*50.0 + randf()*(-50.0),gcY + randf()*50.0 + randf()*(-50.0));
updateGrainPosition(idx, gcX + (randf() * xRandExtent - randf() * xRandExtent),
gcY + (randf() * yRandExtent - randf() * yRandExtent));
VecSceneSample &landscape = *theLandscape;
for (int i = 0, n = landscape.size(); i < n; i++) {
theRect = landscape[i]->view.get();
bool tempTrig = false;
tempTrig = theRect->getNormedPosition(playPos, playVol, theGrain->getX(),
theGrain->getY(), i);
if (tempTrig == true)
trigger = true;
// cout << "playvol: " << *playPos << ", playpos: " << *playVol << endl;
}
if (trigger == true) {
theGrain->trigger(theDur);
}
}
}
// rand cloud size
void CloudVis::setFixedXRandExtent(float X)
{
xRandExtent = X;
changed_xRandExtent = true;
}
void CloudVis::setFixedYRandExtent(float Y)
{
yRandExtent = Y;
changed_yRandExtent = true;
}
void CloudVis::setFixedRandExtent(float X, float Y)
{
setFixedXRandExtent(X);
setFixedYRandExtent(Y);
}
bool CloudVis::changedXRandExtent()
{
return changed_xRandExtent;
}
bool CloudVis::changedYRandExtent()
{
return changed_yRandExtent;
}
void CloudVis::setXRandExtent(float mouseX)
{
xRandExtent = fabs(mouseX - gcX);
if (xRandExtent < 2.0f)
xRandExtent = 0.0f;
changed_xRandExtent = true;
}
void CloudVis::setYRandExtent(float mouseY)
{
yRandExtent = fabs(mouseY - gcY);
if (yRandExtent < 2.0f)
yRandExtent = 0.0f;
changed_yRandExtent = true;
}
void CloudVis::setRandExtent(float mouseX, float mouseY)
{
setXRandExtent(mouseX);
setYRandExtent(mouseY);
}
float CloudVis::getXRandExtent()
{
return xRandExtent;
}
float CloudVis::getYRandExtent()
{
return yRandExtent;
}
void CloudVis::setX(int newX)
{
updateCloudPosition(newX, gcY);
}
void CloudVis::setY(int newY)
{
updateCloudPosition(gcX, newY);
}
bool CloudVis::changedGcX()
{
return changed_gcX;
}
bool CloudVis::changedGcY()
{
return changed_gcY;
}
//
void CloudVis::updateCloudPosition(float x, float y)
{
float xDiff = x - gcX;
float yDiff = y - gcY;
gcX = x;
gcY = y;
for (int i = 0; i < myGrainsV.size(); i++) {
float newGrainX = myGrainsV[i]->getX() + xDiff;
float newGrainY = myGrainsV[i]->getY() + yDiff;
myGrainsV[i]->moveTo(newGrainX, newGrainY);
}
changed_gcX = true;
changed_gcY = true;
}
void CloudVis::updateGrainPosition(int idx, float x, float y)
{
if (idx < numGrains)
myGrainsV[idx]->moveTo(x, y);
}
// check mouse selection
bool CloudVis::select(float x, float y)
{
float xdiff = x - gcX;
float ydiff = y - gcY;
if (sqrt(xdiff * xdiff + ydiff * ydiff) < maxSelRad)
return true;
else
return false;
}
void CloudVis::setSelectState(bool selectState)
{
isSelected = selectState;
}
void CloudVis::addGrain()
{
// addFlag = true;
myGrainsV.push_back(new GrainVis(gcX, gcY));
numGrains = myGrainsV.size();
}
// remove a grain from the cloud (visualization only)
void CloudVis::removeGrain()
{
// removeFlag = true;
if (numGrains > 1) {
// delete object
delete myGrainsV[myGrainsV.size() - 1];
myGrainsV.pop_back();
numGrains = myGrainsV.size();
}
}