Skip to content

Commit 521ae0a

Browse files
committed
Updated sprite rendering example with and without custom shader
1 parent d68f415 commit 521ae0a

File tree

10 files changed

+167
-78
lines changed

10 files changed

+167
-78
lines changed
Loading

examples/gl/pointsAsTexturesExample/bin/data/shader.frag

-8
This file was deleted.

examples/gl/pointsAsTexturesExample/bin/data/shader.vert

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
OF_GLSL_SHADER_HEADER
2+
uniform sampler2D tex;
3+
4+
in vec4 in_Color;
5+
6+
out vec4 out_Color;
7+
8+
void main (void) {
9+
10+
out_Color = texture(tex, gl_PointCoord) * in_Color;
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
OF_GLSL_SHADER_HEADER
2+
3+
uniform mat4 modelViewProjectionMatrix;
4+
uniform vec4 globalColor;
5+
in vec4 position;
6+
in vec3 normal;
7+
in vec4 color;
8+
9+
out vec4 in_Color;
10+
11+
uniform float pointSize;
12+
13+
void main() {
14+
gl_Position = modelViewProjectionMatrix * position;
15+
// we are passing in size variations via the normal.x
16+
gl_PointSize = normal.x * pointSize;
17+
in_Color = color;
18+
19+
}

examples/gl/pointsAsTexturesExample/bin/data/shaders_gles/shader.frag

-14
This file was deleted.

examples/gl/pointsAsTexturesExample/bin/data/shaders_gles/shader.vert

-15
This file was deleted.

examples/gl/pointsAsTexturesExample/src/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ int main( ){
1313

1414
//Use ofGLFWWindowSettings for more options like multi-monitor fullscreen
1515
ofGLWindowSettings settings;
16+
settings.setGLVersion(3, 2);
1617
settings.setSize(1024, 768);
1718
settings.windowMode = OF_WINDOW; //can also be OF_FULLSCREEN
1819

examples/gl/pointsAsTexturesExample/src/ofApp.cpp

+124-31
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
//--------------------------------------------------------------
44
void ofApp::setup() {
5-
5+
66
ofBackgroundHex(0x000000);
77
ofSetFrameRate(60);
88
ofSetVerticalSync(true);
9-
9+
1010
// load the texure
1111
ofDisableArbTex();
1212
ofLoadImage(texture, "dot.png");
13-
13+
ofLoadImage(logoTexture, "ofLogo.png");
14+
1415
// set the camera distance
15-
camDist = 1605;
16+
camDist = 1605;
1617
camera.setDistance(camDist);
1718

1819
// randomly add a point on a sphere
@@ -27,29 +28,46 @@ void ofApp::setup() {
2728
p.x = std::cos( theta1 ) * std::cos( theta2 );
2829
p.y = std::sin( theta1 );
2930
p.z = std::cos( theta1 ) * std::sin( theta2 );
31+
3032
p *= radius;
3133

3234
addPoint(p.x, p.y, p.z);
33-
3435
}
3536

3637
// upload the data to the vbo
3738
int total = (int)points.size();
3839
vbo.setVertexData(&points[0], total, GL_STATIC_DRAW);
3940
vbo.setNormalData(&sizes[0], total, GL_STATIC_DRAW);
41+
vbo.setColorData(&colors[0], total, GL_STATIC_DRAW);
4042

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+
}
4851

4952
}
5053

5154
//--------------------------------------------------------------
5255
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+
}
5371
}
5472

5573
//--------------------------------------------------------------
@@ -58,30 +76,74 @@ void ofApp::draw() {
5876
glDepthMask(GL_FALSE);
5977

6078
ofSetColor(255, 100, 90);
79+
80+
ofEnableAlphaBlending();
6181

6282
// this makes everything look glowy :)
6383
ofEnableBlendMode(OF_BLENDMODE_ADD);
6484
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+
}
65104

66105
// bind the shader and camera
67106
// everything inside this function
68107
// will be effected by the shader/camera
69-
shader.begin();
70108
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+
}
71120

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();
77121

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+
}
80129

130+
if (tex != nullptr) {
131+
tex->unbind();
132+
}
133+
134+
if (bUseShader){
135+
shader.end();
136+
} else {
137+
138+
}
139+
81140
ofDisablePointSprites();
141+
142+
camera.end();
143+
82144
ofDisableBlendMode();
83145

84-
// check to see if the points are
146+
// check to see if the points are
85147
// sizing to the right size
86148
ofEnableAlphaBlending();
87149
camera.begin();
@@ -91,31 +153,44 @@ void ofApp::draw() {
91153
mid = glm::normalize(mid);
92154
mid *= 300;
93155
ofDrawLine(points[i], mid);
94-
}
156+
}
95157
camera.end();
96-
158+
97159
glDepthMask(GL_TRUE);
98-
160+
99161
ofSetColor(255, 100);
100-
ofDrawRectangle(0, 0, 250, 90);
162+
ofDrawRectangle(0, 0, 250, bUseShader ? 90 : 120);
101163
ofSetColor(0);
102164
string info = "FPS "+ofToString(ofGetFrameRate(), 0) + "\n";
103165
info += "Total Points "+ofToString((int)points.size())+"\n";
104166
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+
107175
ofDrawBitmapString(info, 20, 20);
108-
109176
}
110177

111178
//--------------------------------------------------------------
112179
void ofApp::addPoint(float x, float y, float z) {
113180
glm::vec3 p(x, y, z);
114181
points.push_back(p);
115182

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);
118185
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));
119194
}
120195

121196
//--------------------------------------------------------------
@@ -133,6 +208,9 @@ void ofApp::keyPressed(int key) {
133208
// clear all the points
134209
if(key == 'c') {
135210
points.clear();
211+
sizes.clear();
212+
colors.clear();
213+
mesh.clear();
136214
}
137215

138216
// add crazy amount
@@ -148,9 +226,24 @@ void ofApp::keyPressed(int key) {
148226
int total = (int)points.size();
149227
vbo.setVertexData(&points[0], total, GL_STATIC_DRAW);
150228
vbo.setNormalData(&sizes[0], total, GL_STATIC_DRAW);
151-
229+
vbo.setColorData(&colors[0], total, GL_STATIC_DRAW);
152230
}
153231

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+
}
154247
}
155248

156249
//--------------------------------------------------------------

examples/gl/pointsAsTexturesExample/src/ofApp.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,21 @@ class ofApp : public ofBaseApp {
2525
// vector to store all values
2626
vector <glm::vec3> points;
2727
vector <glm::vec3> sizes;
28-
28+
vector<ofFloatColor> colors;
29+
2930
ofVbo vbo;
31+
ofMesh mesh;
32+
bool bUseShader = true;
3033
ofShader shader;
3134
ofEasyCam camera;
3235

3336
float camDist;
37+
bool bUseColors = false;
38+
39+
float pointSize = 10.0f;
40+
bool bSmoothing = false;
3441

35-
ofTexture texture;
42+
ofTexture texture, logoTexture;
43+
int texIndex = 1;
44+
3645
};

0 commit comments

Comments
 (0)