Skip to content

Instantly share code, notes, and snippets.

@companje
Created August 4, 2012 16:21
Show Gist options
  • Save companje/3258654 to your computer and use it in GitHub Desktop.
Save companje/3258654 to your computer and use it in GitHub Desktop.
Subdivide a tetrahedron to a sphere with ofMesh in openFrameworks
#include "ofMain.h"
#include "ofAppGlutWindow.h"
class ofApp : public ofBaseApp {
public:
ofMesh mesh;
ofEasyCam cam;
void setup() {
ofSetFrameRate(20);
ofBackground(0);
mesh.addVertex(ofVec3f(0,0,1));
mesh.addVertex(ofVec3f(0,2*sqrt(2),-1)/3);
mesh.addVertex(ofVec3f(-sqrt(6),-sqrt(2),-1)/3);
mesh.addVertex(ofVec3f(sqrt(6),-sqrt(2),-1)/3);
int numSubDivisions = 4;
subdivideTriangles(mesh,0,1,2,numSubDivisions);
subdivideTriangles(mesh,0,2,3,numSubDivisions);
subdivideTriangles(mesh,0,3,1,numSubDivisions);
subdivideTriangles(mesh,1,3,2,numSubDivisions);
}
void subdivideTriangles(ofMesh &mesh, int i0, int i1, int i2, int level) {
if (level-->0) {
mesh.addVertex(((mesh.getVertex(i0)+mesh.getVertex(i1))*.5).normalized());
mesh.addVertex(((mesh.getVertex(i1)+mesh.getVertex(i2))*.5).normalized());
mesh.addVertex(((mesh.getVertex(i2)+mesh.getVertex(i0))*.5).normalized());
int i01=mesh.getNumVertices()-3;
int i12=mesh.getNumVertices()-2;
int i20=mesh.getNumVertices()-1;
subdivideTriangles(mesh,i0,i01,i20,level);
subdivideTriangles(mesh,i01,i1,i12,level);
subdivideTriangles(mesh,i01,i12,i20,level);
subdivideTriangles(mesh,i20,i12,i2,level);
} else {
mesh.addTriangle(i0,i1,i2);
}
}
void draw() {
cam.begin();
ofScale(100,100,100);
mesh.drawWireframe();
cam.end();
}
};
int main() {
ofAppGlutWindow window;
window.setGlutDisplayString("rgba double depth alpha samples>=4");
ofSetupOpenGL(&window, 500, 500, OF_WINDOW);
ofRunApp(new ofApp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment