forked from dicarlolab-mworks/MovingDots
/
DynamicRandomDots.cpp
220 lines (150 loc) · 6.14 KB
/
DynamicRandomDots.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
/*
* DynamicRandomDots.cpp
* DynamicRandomDots
*
* Created by Christopher Stawarz on 8/6/10.
* Copyright 2010 MIT. All rights reserved.
*
*/
#include "DynamicRandomDots.h"
DynamicRandomDots::DynamicRandomDots(const std::string &tag,
shared_ptr<Variable> framesPerSecond,
shared_ptr<Variable> fieldRadius,
shared_ptr<Variable> fieldCenterX,
shared_ptr<Variable> fieldCenterY,
shared_ptr<Variable> numDots,
shared_ptr<Variable> dotSize,
shared_ptr<Variable> colorR,
shared_ptr<Variable> colorG,
shared_ptr<Variable> colorB,
shared_ptr<Variable> alphaMultiplier,
shared_ptr<Variable> direction,
shared_ptr<Variable> speed) :
StandardDynamicStimulus(tag, framesPerSecond),
direction(direction),
speed(speed)
{
this->fieldRadius = fieldRadius->getValue().getFloat();
this->fieldCenterX = fieldCenterX->getValue().getFloat();
this->fieldCenterY = fieldCenterY->getValue().getFloat();
this->numDots = numDots->getValue().getInteger();
this->dotSize = dotSize->getValue().getFloat();
this->colorR = colorR->getValue().getFloat();
this->colorG = colorG->getValue().getFloat();
this->colorB = colorB->getValue().getFloat();
this->alpha = alphaMultiplier->getValue().getFloat();
}
DynamicRandomDots::~DynamicRandomDots() { }
void DynamicRandomDots::load(shared_ptr<StimulusDisplay> display) {
if (loaded)
return;
validateParameters();
computeDotSizeInPixels(display);
initializeDots();
loaded = true;
}
void DynamicRandomDots::validateParameters() {
if (fieldRadius <= 0.0f) {
throw SimpleException("field radius must be greater than 0");
}
if (numDots < 1) {
throw SimpleException("number of dots must be greater than or equal to 1");
}
if (dotSize <= 0.0f) {
throw SimpleException("dot size must be greater than 0");
}
}
void DynamicRandomDots::computeDotSizeInPixels(shared_ptr<StimulusDisplay> display) {
dotSizeInPixels.clear();
GLdouble xMin, xMax, yMin, yMax;
GLint width, height;
display->getDisplayBounds(xMin, xMax, yMin, yMax);
for (int i = 0; i < display->getNContexts(); i++) {
display->setCurrent(i);
display->getCurrentViewportSize(width, height);
dotSizeInPixels.push_back(dotSize / (xMax - xMin) * (GLfloat)width);
}
}
void DynamicRandomDots::initializeDots() {
dots.resize(numDots * verticesPerDot);
for (GLint i = 0; i < (numDots * verticesPerDot); i += verticesPerDot) {
GLfloat &x = dots[i];
GLfloat &y = dots[i+1];
do {
x = rand(-fieldRadius, fieldRadius);
y = rand(-fieldRadius, fieldRadius);
} while (x*x + y*y > fieldRadius*fieldRadius);
x += fieldCenterX;
y += fieldCenterY;
}
}
void DynamicRandomDots::updateDots() {
const GLfloat dt = (GLfloat)(currentTime - previousTime) / 1.0e6f;
const GLfloat dr = dt * speed->getValue().getFloat();
const GLfloat theta = direction->getValue().getFloat() / 180.0f * M_PI; // Degrees to radians
const GLfloat dx = dr * cos(theta);
const GLfloat dy = dr * sin(theta);
for (GLint i = 0; i < (numDots * verticesPerDot); i += verticesPerDot) {
GLfloat &x = dots[i];
GLfloat &y = dots[i+1];
x += dx;
y += dy;
GLfloat x1 = x - fieldCenterX;
GLfloat y1 = y - fieldCenterY;
if (x1*x1 + y1*y1 > fieldRadius*fieldRadius) {
y1 = rand(-fieldRadius, fieldRadius);
x1 = -sqrt(fieldRadius*fieldRadius - y1*y1) + rand(0.0f, dr);
x = x1*cos(theta) - y1*sin(theta);
y = x1*sin(theta) + y1*cos(theta);
x += fieldCenterX;
y += fieldCenterY;
}
}
}
void DynamicRandomDots::willPlay() {
StandardDynamicStimulus::willPlay();
previousTime = 0;
}
void DynamicRandomDots::didStop() {
StandardDynamicStimulus::didStop();
previousTime = -1;
}
void DynamicRandomDots::drawFrame(shared_ptr<StimulusDisplay> display, int frameNumber) {
// If we're drawing to the main display, update dot positions
if (display->getCurrentContextIndex() == 0) {
currentTime = getElapsedTime();
updateDots();
previousTime = currentTime;
}
// Enable antialiasing so dots are round, not square
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST);
glColor4f(colorR, colorG, colorB, alpha);
glEnableClientState(GL_VERTEX_ARRAY);
glPointSize(dotSizeInPixels[display->getCurrentContextIndex()]);
glVertexPointer(verticesPerDot, GL_FLOAT, 0, &(dots[0]));
glDrawArrays(GL_POINTS, 0, numDots);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_BLEND);
glDisable(GL_POINT_SMOOTH);
}
Datum DynamicRandomDots::getCurrentAnnounceDrawData() {
boost::mutex::scoped_lock locker(stim_lock);
Datum announceData = StandardDynamicStimulus::getCurrentAnnounceDrawData();
announceData.addElement(STIM_TYPE, "dynamic_random_dots");
announceData.addElement("field_radius", fieldRadius);
announceData.addElement("field_center_x", fieldCenterX);
announceData.addElement("field_center_y", fieldCenterY);
announceData.addElement("num_dots", (long)numDots);
announceData.addElement("dot_size", dotSize);
announceData.addElement(STIM_COLOR_R, colorR);
announceData.addElement(STIM_COLOR_G, colorG);
announceData.addElement(STIM_COLOR_B, colorB);
announceData.addElement("alpha_multiplier", alpha);
announceData.addElement("direction", direction->getValue().getFloat());
announceData.addElement("speed", speed->getValue().getFloat());
return announceData;
}