2
2
3
3
// --------------------------------------------------------------
4
4
void ofApp::setup () {
5
-
5
+
6
6
ofBackgroundHex (0x000000 );
7
7
ofSetFrameRate (60 );
8
8
ofSetVerticalSync (true );
9
-
9
+
10
10
// load the texure
11
11
ofDisableArbTex ();
12
12
ofLoadImage (texture, " dot.png" );
13
-
13
+ ofLoadImage (logoTexture, " ofLogo.png" );
14
+
14
15
// set the camera distance
15
- camDist = 1605 ;
16
+ camDist = 1605 ;
16
17
camera.setDistance (camDist);
17
18
18
19
// randomly add a point on a sphere
@@ -27,29 +28,46 @@ void ofApp::setup() {
27
28
p.x = std::cos ( theta1 ) * std::cos ( theta2 );
28
29
p.y = std::sin ( theta1 );
29
30
p.z = std::cos ( theta1 ) * std::sin ( theta2 );
31
+
30
32
p *= radius;
31
33
32
34
addPoint (p.x , p.y , p.z );
33
-
34
35
}
35
36
36
37
// upload the data to the vbo
37
38
int total = (int )points.size ();
38
39
vbo.setVertexData (&points[0 ], total, GL_STATIC_DRAW);
39
40
vbo.setNormalData (&sizes[0 ], total, GL_STATIC_DRAW);
41
+ vbo.setColorData (&colors[0 ], total, GL_STATIC_DRAW);
40
42
41
-
42
- // load the shader
43
- #ifdef TARGET_OPENGLES
44
- shader.load (" shaders_gles/shader" );
45
- #else
46
- shader.load (" shaders/shader" );
47
- #endif
43
+ // we will be drawing point sprites, so set mode on mesh to POINTS
44
+ mesh.setMode (OF_PRIMITIVE_POINTS);
45
+
46
+ if ( ofIsGLProgrammableRenderer () ) {
47
+ shader.load (" shaders_gl3/shader" );
48
+ } else {
49
+ shader.load (" shaders/shader" );
50
+ }
48
51
49
52
}
50
53
51
54
// --------------------------------------------------------------
52
55
void ofApp::update () {
56
+ pointSize = ofMap (ofGetMouseX (), 100 .f , ofGetWidth () - 50 .0f , 1 .0f , 60 .f , true );
57
+ ofSetPointSize (pointSize);
58
+
59
+ if ( bUseShader ) {
60
+ mesh.enableColors ();
61
+ vbo.enableColors ();
62
+ } else {
63
+ if ( bUseColors ) {
64
+ mesh.enableColors ();
65
+ vbo.enableColors ();
66
+ } else {
67
+ mesh.disableColors ();
68
+ vbo.disableColors ();
69
+ }
70
+ }
53
71
}
54
72
55
73
// --------------------------------------------------------------
@@ -58,30 +76,74 @@ void ofApp::draw() {
58
76
glDepthMask (GL_FALSE);
59
77
60
78
ofSetColor (255 , 100 , 90 );
79
+
80
+ ofEnableAlphaBlending ();
61
81
62
82
// this makes everything look glowy :)
63
83
ofEnableBlendMode (OF_BLENDMODE_ADD);
64
84
ofEnablePointSprites ();
85
+
86
+ if (bSmoothing) {
87
+ ofEnableSmoothing ();
88
+ } else {
89
+ ofDisableSmoothing ();
90
+ }
91
+
92
+ // determine if we should bind a texture for our points to draw
93
+ ofTexture* tex = nullptr ;
94
+ if (bUseShader) {
95
+ // we are using a shader and it expects a texture
96
+ tex = &texture;
97
+ } else {
98
+ if ( texIndex == 1 ) {
99
+ tex = &texture;
100
+ } else if ( texIndex == 2 ) {
101
+ tex = &logoTexture;
102
+ }
103
+ }
65
104
66
105
// bind the shader and camera
67
106
// everything inside this function
68
107
// will be effected by the shader/camera
69
- shader.begin ();
70
108
camera.begin ();
109
+ if (bUseShader) {
110
+ shader.begin ();
111
+ // pass the point size to the shader
112
+ shader.setUniform1f (" pointSize" , pointSize);
113
+ }
114
+
115
+ if ( tex != nullptr ) {
116
+ // bind the texture so that when all the points
117
+ // are drawn they are replace with our dot image
118
+ tex->bind ();
119
+ }
71
120
72
- // bind the texture so that when all the points
73
- // are drawn they are replace with our dot image
74
- texture.bind ();
75
- vbo.draw (GL_POINTS, 0 , (int )points.size ());
76
- texture.unbind ();
77
121
78
- camera.end ();
79
- shader.end ();
122
+ if (bUseShader) {
123
+ // draw via the vbo for the custom shader
124
+ vbo.draw (GL_POINTS, 0 , (int )points.size ());
125
+ } else {
126
+ // lets draw the mesh
127
+ mesh.drawVertices ();
128
+ }
80
129
130
+ if (tex != nullptr ) {
131
+ tex->unbind ();
132
+ }
133
+
134
+ if (bUseShader){
135
+ shader.end ();
136
+ } else {
137
+
138
+ }
139
+
81
140
ofDisablePointSprites ();
141
+
142
+ camera.end ();
143
+
82
144
ofDisableBlendMode ();
83
145
84
- // check to see if the points are
146
+ // check to see if the points are
85
147
// sizing to the right size
86
148
ofEnableAlphaBlending ();
87
149
camera.begin ();
@@ -91,31 +153,44 @@ void ofApp::draw() {
91
153
mid = glm::normalize (mid);
92
154
mid *= 300 ;
93
155
ofDrawLine (points[i], mid);
94
- }
156
+ }
95
157
camera.end ();
96
-
158
+
97
159
glDepthMask (GL_TRUE);
98
-
160
+
99
161
ofSetColor (255 , 100 );
100
- ofDrawRectangle (0 , 0 , 250 , 90 );
162
+ ofDrawRectangle (0 , 0 , 250 , bUseShader ? 90 : 120 );
101
163
ofSetColor (0 );
102
164
string info = " FPS " +ofToString (ofGetFrameRate (), 0 ) + " \n " ;
103
165
info += " Total Points " +ofToString ((int )points.size ())+" \n " ;
104
166
info += " Press 'a' to add more\n " ;
105
- info += " Press 'c' to remove all" ;
106
-
167
+ info += " Press 'c' to remove all\n " ;
168
+ info += " Press 's' for shader: " + string (bUseShader ? " yes" :" no" )+" \n " ;
169
+ if ( !bUseShader) {
170
+ info += " Press 'x' for colors: " + string (bUseColors ? " yes" : " no" ) + " \n " ;
171
+ info += " Press 't' for texture: " + string (texIndex == 0 ? " no" : " yes" ) + " \n " ;
172
+ info += " Press 'z' for smoothing: " + string (bSmoothing ? " yes" : " no" ) + " \n " ;
173
+ }
174
+
107
175
ofDrawBitmapString (info, 20 , 20 );
108
-
109
176
}
110
177
111
178
// --------------------------------------------------------------
112
179
void ofApp::addPoint (float x, float y, float z) {
113
180
glm::vec3 p (x, y, z);
114
181
points.push_back (p);
115
182
116
- // we are passing the size in as a normal x position
117
- float size = ofRandom (5 , 50 );
183
+ // we are passing the size varation per point via the normal x
184
+ float size = ofRandom (1 . f , 2 . f );
118
185
sizes.push_back (glm::vec3 (size));
186
+
187
+ ofFloatColor tc;
188
+ tc.setHsb (ofRandom (0 .5f , 1 .0f ), 0 .7f , 0 .8f , 1 .0f );
189
+
190
+ colors.push_back (tc);
191
+ mesh.addColor (tc);
192
+ mesh.addVertex (p);
193
+ mesh.addNormal (glm::vec3 (size));
119
194
}
120
195
121
196
// --------------------------------------------------------------
@@ -133,6 +208,9 @@ void ofApp::keyPressed(int key) {
133
208
// clear all the points
134
209
if (key == ' c' ) {
135
210
points.clear ();
211
+ sizes.clear ();
212
+ colors.clear ();
213
+ mesh.clear ();
136
214
}
137
215
138
216
// add crazy amount
@@ -148,9 +226,24 @@ void ofApp::keyPressed(int key) {
148
226
int total = (int )points.size ();
149
227
vbo.setVertexData (&points[0 ], total, GL_STATIC_DRAW);
150
228
vbo.setNormalData (&sizes[0 ], total, GL_STATIC_DRAW);
151
-
229
+ vbo. setColorData (&colors[ 0 ], total, GL_STATIC_DRAW);
152
230
}
153
231
232
+ if ( key == ' s' ) {
233
+ bUseShader = !bUseShader;
234
+ }
235
+ if ( key == ' t' ) {
236
+ texIndex ++;
237
+ if ( texIndex > 2 ) {
238
+ texIndex = 0 ;
239
+ }
240
+ }
241
+ if ( key == ' z' ) {
242
+ bSmoothing = !bSmoothing;
243
+ }
244
+ if ( key == ' x' ) {
245
+ bUseColors = !bUseColors;
246
+ }
154
247
}
155
248
156
249
// --------------------------------------------------------------
0 commit comments