Skip to content

Commit 4364b38

Browse files
authored
fix pointCloudExample for emscripten
1 parent a21a7fd commit 4364b38

1 file changed

Lines changed: 61 additions & 20 deletions

File tree

examples/3d/pointCloudExample/src/ofApp.cpp

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,71 @@ void ofApp::setup() {
77
// load an image from disk
88
img.load("linzer.png");
99

10-
// we're going to load a ton of points into an ofMesh
11-
mesh.setMode(OF_PRIMITIVE_POINTS);
12-
13-
// loop through the image in the x and y axes
14-
int skip = 4; // load a subset of the points
15-
for(int y = 0; y < img.getHeight(); y += skip) {
16-
for(int x = 0; x < img.getWidth(); x += skip) {
17-
ofColor cur = img.getColor(x, y);
18-
if(cur.a > 0) {
19-
// the alpha value encodes depth, let's remap it to a good depth range
20-
float z = ofMap(cur.a, 0, 255, -300, 300);
21-
cur.a = 255;
22-
mesh.addColor(cur);
23-
glm::vec3 pos(x, y, z);
24-
mesh.addVertex(pos);
10+
//for everything but web
11+
#ifndef TARGET_EMSCRIPTEN
12+
mesh.setMode(OF_PRIMITIVE_POINTS); // we're going to load a ton of points into an ofMesh
13+
14+
glEnable(GL_POINT_SMOOTH); // use circular points instead of square points
15+
glPointSize(3); // make the points bigger
16+
17+
// loop through the image in the x and y axes
18+
int skip = 3; // load a subset of the points
19+
for(int y = 0; y < img.getHeight(); y += skip) {
20+
for(int x = 0; x < img.getWidth(); x += skip) {
21+
ofColor cur = img.getColor(x, y);
22+
if(cur.a > 0) {
23+
// the alpha value encodes depth, let's remap it to a good depth range
24+
float z = ofMap(cur.a, 0, 255, -300, 300);
25+
cur.a = 255;
26+
mesh.addColor(cur);
27+
glm::vec3 pos(x, y, z);
28+
mesh.addVertex(pos);
29+
}
2530
}
2631
}
27-
}
32+
33+
mesh.setupIndicesAuto();
34+
#else
35+
//for web we have to make tiny square meshes out of two triangles
36+
//OF should maybe do this on the backend for emscripten but for now we'll do it manually
37+
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
38+
39+
//play with this number to make the pixel/point size bigger
40+
float hPointSize = 1;
41+
42+
//these are the offsets for the four corners of the square
43+
std::vector<glm::vec3> offsets = {glm::vec3(-hPointSize, -hPointSize, 0.0), glm::vec3(hPointSize, -hPointSize, 0.0), glm::vec3(hPointSize, hPointSize, 0.0), glm::vec3(-hPointSize, hPointSize, 0.0) };
44+
45+
//we make the square out of two triangles so 0,1,2 is a triangle made out of the first 3 points above and 2,3,0 is a triangle made out of the last 3 points above
46+
std::vector<ofIndexType> indices = {0, 1, 2, 2, 3, 0};
47+
48+
// loop through the image in the x and y axes
49+
int skip = 3; // load a subset of the points
50+
int pixelCount = 0;
51+
for(int y = 0; y < img.getHeight(); y += skip) {
52+
for(int x = 0; x < img.getWidth(); x += skip) {
53+
ofColor cur = img.getColor(x, y);
54+
if(cur.a > 0) {
55+
// the alpha value encodes depth, let's remap it to a good depth range
56+
float z = ofMap(cur.a, 0, 255, -300, 300);
57+
cur.a = 255;
58+
glm::vec3 pos(x, y, z);
59+
//build our pixel/point as a small square from the offsets above
60+
for( auto & offset : offsets){
61+
mesh.addColor(cur);
62+
mesh.addVertex(offset + pos);
63+
}
64+
//add the index so the triangles can be made out of the vertex data
65+
for( auto & index : indices ){
66+
mesh.addIndex(pixelCount + index);
67+
}
68+
pixelCount+=4;
69+
}
70+
}
71+
}
72+
#endif
2873

2974
ofEnableDepthTest();
30-
#ifndef TARGET_EMSCRIPTEN
31-
glEnable(GL_POINT_SMOOTH); // use circular points instead of square points
32-
glPointSize(3); // make the points bigger
33-
#endif
3475
}
3576

3677
//--------------------------------------------------------------

0 commit comments

Comments
 (0)