void updateMesh(ofMesh& mesh) { float t = ofGetElapsedTimef(); ofVec3f axis; axis.x = ofSignedNoise(1, 0, 0, t); axis.y = ofSignedNoise(0, 1, 0, t); axis.z = ofSignedNoise(0, 0, 1, t); axis.normalize(); vector<ofVec3f>& verts = mesh.getVertices(); vector<ofVec3f>& norms = mesh.getNormals(); for (int i = 0; i < verts.size(); i++) { ofVec3f& v = verts[i]; ofVec3f& n = norms[i]; ofVec3f vv = v; float r = vv.y * fmodf(t, 10) * 0.1; vv.rotate(r, axis); n.rotate(r, axis); v = vv; } }
//-------------------------------------------------------------- void ofxBulletTriMeshShape::updateMesh( btDiscreteDynamicsWorld* a_world, ofMesh& aMesh ) { if( aMesh.getNumVertices() != totalVerts || aMesh.getNumIndices() != totalIndices ) { ofLogWarning() << "updateMesh :: the verts or the indices are not the correct size, not updating"; return; } auto& tverts = aMesh.getVertices(); btVector3 aabbMin(BT_LARGE_FLOAT,BT_LARGE_FLOAT,BT_LARGE_FLOAT); btVector3 aabbMax(-BT_LARGE_FLOAT,-BT_LARGE_FLOAT,-BT_LARGE_FLOAT); for( int i = 0; i < totalVerts; i++ ) { auto& v = tverts[i]; bullet_vertices[i].setValue( v.x, v.y, v.z ); aabbMin.setMin( bullet_vertices[i] ); aabbMax.setMax( bullet_vertices[i] ); } btBvhTriangleMeshShape* triShape = (btBvhTriangleMeshShape*)_shape; // triShape->partialRefitTree( aabbMin, aabbMax ); triShape->refitTree( aabbMin, aabbMax ); //clear all contact points involving mesh proxy. Note: this is a slow/unoptimized operation. a_world->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs( getRigidBody()->getBroadphaseHandle(), a_world->getDispatcher()); }
void BGGraphics::pushCirclePart(ofMesh& mesh, ofVec2f position, float nodeRadius, float minAngle, float deltaAngle) { mesh.setMode(OF_PRIMITIVE_TRIANGLES); pushVertex(mesh, position.x, position.y, 1, 0, 0, 1, 0, 0); float innerRadius = nodeRadius - NETWORK_OFFSET; float internalOffsetY = .5;// innerRadius / nodeRadius; int samples = (int)(15 * deltaAngle / M_PI); samples = max(1, samples); for(int i=0; i<samples; ++i) { float angle = minAngle + deltaAngle * i / (float)(samples - 1); ofVec2f to(cosf(angle), sinf(angle)); ofVec2f p1 = position + to * innerRadius; ofVec2f p2 = position + to * nodeRadius; //pushVertex(ofMesh & mesh, float x, float y, float z, float nx, float ny, float nz, float offsetX, float offsetY) pushVertex(mesh, p1.x, p1.y, 1, 0, 0, 1, 0, internalOffsetY); pushVertex(mesh, p2.x, p2.y, 0, to.x, to.y, 0, 0, 1); if(i > 0) { int offset = 1 + 2 * i; mesh.addTriangle(0, offset - 2, offset); mesh.addTriangle(offset - 2, offset - 1, offset); mesh.addTriangle(offset - 1, offset, offset + 1); } } }
//-------------------------------------------------------------- void ofxBulletSoftTriMesh::updateMesh( ofMesh& aMesh ) { int totalNodes = getNumNodes(); auto& tverts = aMesh.getVertices(); if( _cachedMesh.getMode() == OF_PRIMITIVE_TRIANGLES ) { if( tverts.size() != totalNodes ) { tverts.resize( totalNodes ); } auto& tnormals = aMesh.getNormals(); if( aMesh.getNumNormals() != totalNodes ) { tnormals.resize( totalNodes ); } for( int i = 0; i < totalNodes; i++ ) { tverts[i].x = _softBody->m_nodes[i].m_x.x(); tverts[i].y = _softBody->m_nodes[i].m_x.y(); tverts[i].z = _softBody->m_nodes[i].m_x.z(); tnormals[i].x = _softBody->m_nodes[i].m_n.x(); tnormals[i].y = _softBody->m_nodes[i].m_n.y(); tnormals[i].z = _softBody->m_nodes[i].m_n.z(); } } _lastMeshUpdateFrame = ofGetFrameNum(); }
//--------------------------------------------------------------------- void ofDrawBitmapCharacterEnd(){ if( vC > 0 ){ charMesh.getVertices().resize(vC); charMesh.getTexCoords().resize(vC); bitmappedFontTexture.bind(); ofPtr<ofGLProgrammableRenderer> programmableRenderer = ofGetGLProgrammableRenderer(); if (!programmableRenderer){ #ifndef TARGET_OPENGLES // this temporarily enables alpha testing, // which discards pixels unless their alpha is 1.0f glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0); #endif }else{ // glPush/PopAttrib is deprecated + we are doing the alpha test through a shader programmableRenderer->setAlphaBitmapText(true); } charMesh.draw(); if (!programmableRenderer){ #ifndef TARGET_OPENGLES glPopAttrib(); #endif }else{ programmableRenderer->setAlphaBitmapText(false); } bitmappedFontTexture.unbind(); } }
void ofxPolyline::createMesh(ofMesh &mesh, int begin, int end) { if (end - begin == 1) { // If only two points, just make a line. mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP); pair<ofVec2f, ofVec2f> joint = jointBegin((*this)[begin], (*this)[end], width); mesh.addVertex(joint.first); mesh.addVertex(joint.second); joint = jointEnd((*this)[begin], (*this)[end], width); mesh.addVertex(joint.first); mesh.addVertex(joint.second); return; } else if (end - begin == 0) { // Return no mesh return; } // More than two points if (strokeLinejoin == OFXSVG_STROKE_LINEJOIN_MITER) { createMeshMiterJoint(mesh, begin, end); } else if (strokeLinejoin == OFXSVG_STROKE_LINEJOIN_BEVEL) { createMeshBevelJoint(mesh, begin, end); } else if (strokeLinejoin == OFXSVG_STROKE_LINEJOIN_ROUND) { createMeshRoundJoint(mesh, begin, end); } }
void setup() { useSharedData(); data.setup(ofGetWidth(), ofGetHeight(), 8); historyMesh.setMode(OF_PRIMITIVE_POINTS); dataMesh.setMode(OF_PRIMITIVE_POINTS); neighborsMesh.setMode(OF_PRIMITIVE_POINTS); }
void setup() { vid.initGrabber(w, h); mesh.setMode(OF_PRIMITIVE_POINTS); mesh.addVertices(vector<ofVec3f>(n)); mesh.addColors(vector<ofFloatColor>(n)); }
ofMesh & ofBitmapStringGetMesh(const string & text, int x, int y){ int len = (int)text.length(); //float yOffset = 0; float fontSize = 8.0f; bool bOrigin = false; float sx = x; float sy = y-fontSize; ofDrawBitmapCharacterStart(text.size()); for(int c = 0; c < len; c++){ if(text[c] == '\n'){ sy += bOrigin ? -1 : 1 * (fontSize*1.7); sx = x; //glRasterPos2f(x,y + (int)yOffset); } else if (text[c] >= 32){ // < 32 = control characters - don't draw // solves a bug with control characters // getting drawn when they ought to not be ofDrawBitmapCharacter(text[c], (int)sx, (int)sy); sx += fontSize; } } //We do this because its way faster charMesh.getVertices().resize(vC); charMesh.getTexCoords().resize(vC); return charMesh; }
int getClosestTripletOnMesh(const ofMesh& objectMesh,const ofMesh& imageMesh, float x, float y, float* distance) { float bestDistance = numeric_limits<float>::infinity(); int bestChoice = 0; for(int i = 0; i < objectMesh.getNumIndices()/3; i++) { ofVec3f cur = ofVec3f(0,0,0); for (int j=0;j<3;j++) { cur+=imageMesh.getVerticesPointer()[objectMesh.getIndexPointer()[3*i+j]]; } cur/=3.0; float dx = x - cur.x; float dy = y - cur.y; float dz = 0 - cur.z; float curDistance = dx * dx + dy * dy + dz * dz; if(curDistance < bestDistance) { bestDistance = curDistance; bestChoice = i; } } return bestChoice; }
void ofxScale(ofMesh &mesh, float x, float y, float z) { for (int i=0; i<mesh.getNumVertices(); i++) { mesh.getVertices()[i].x *= x; mesh.getVertices()[i].y *= y; mesh.getVertices()[i].z *= z; } }
ofMesh convertFromIndices(const ofMesh& mesh) { ofMesh result; // have to do a const_cast because ofMesh::get*() is not const correct ofMesh& cmesh = const_cast<ofMesh&>(mesh); int vertices = mesh.getNumVertices(); int colors = mesh.getNumColors(); int normals = mesh.getNumNormals(); int texcoords = mesh.getNumTexCoords(); int indices = mesh.getNumIndices(); for(int i = 0; i < indices; i++) { int cur = cmesh.getIndex(i); if(vertices > 0) { result.addVertex(cmesh.getVertex(cur)); } if(colors > 0) { result.addColor(cmesh.getColor(cur)); } if(normals > 0) { result.addNormal(cmesh.getNormal(cur)); } if(texcoords > 0) { result.addTexCoord(cmesh.getTexCoord(cur)); } } return result; }
//-------------------------------------------------------------- void ofVboByteColor::setMesh(const ofMesh & mesh, int usage){ setVertexData(mesh.getVerticesPointer(),mesh.getNumVertices(),usage); setColorData(mesh.getColorsPointer(),mesh.getNumColors(),usage); setNormalData(mesh.getNormalsPointer(),mesh.getNumNormals(),usage); setTexCoordData(mesh.getTexCoordsPointer(),mesh.getNumTexCoords(),usage); setIndexData(mesh.getIndexPointer(), mesh.getNumIndices(), usage); }
//http://www.packtpub.com/sites/default/files/9781849518048_Chapter_07.pdf //knotExample void ofApp::addCircle( ofVec3f nextPoint, ofMesh &mesh ){ float time = ofGetElapsedTimef(); //Time //Parameters – twisting and rotating angles and color ofFloatColor color( ofNoise( time * 0.05 ), ofNoise( time * 0.1 ), ofNoise( time * 0.15 )); color.setSaturation( 1.0 ); //Make the color maximally //Add vertices for (int i=0; i<circleN; i++) { float angle = float(i) / circleN * TWO_PI+(PI*0.25); float x = Rad * cos( angle ); float y = Rad * sin( angle ); ofPoint p = nextPoint+ofVec3f(x ,y , 0); mesh.addVertex( p ); mesh.addColor( color ); } //Add the triangles int base = mesh.getNumVertices() - 2 * circleN; if ( base >= 0 ) { //Check if it is not the first step //and we really need to add the triangles for (int i=0; i<circleN; i++) { int a = base + i; int b = base + (i + 1) % circleN; int c = circleN + a; int d = circleN + b; mesh.addTriangle(a,b,d); mesh.addTriangle(a, d, c); } //Update the normals setNormals( mesh ); } }
//-------------------------------------------------------------- void ofApp::colorFaces(ofMesh & mesh){ for(int i = 0; i < mesh.getVertices().size(); i++){ const ofVec3f & v = mesh.getVertices()[i]; ofColor col; col.setHsb(ofMap(v.x + v.y, -1000, 1000, 0, 255), 255, 255); mesh.addColor(col); } }
ofMesh ofxFfd::deformMesh(ofMesh mesh){ ofMesh dst; dst.append(mesh); for ( int i=0; i<mesh.getNumVertices(); i++ ) { ofVec3f vec = mesh.getVertex(i); dst.setVertex(i, deform(vec)); } return dst; }
ofVec3f getCentroid(ofMesh& mesh){ ofVec3f sum; for(int i=0;i<mesh.getNumVertices();i++){ sum+=mesh.getVertex(i); } sum/=mesh.getNumVertices(); return sum; }
/** * Add a quad, using indices, with clockwise front face vertex order */ void addQuad(IndexType const bottomLeft, IndexType const topLeft, IndexType const topRight, IndexType const bottomRight) { mesh.addIndex(bottomLeft); mesh.addIndex(topLeft); mesh.addIndex(topRight); mesh.addIndex(topRight); mesh.addIndex(bottomRight); mesh.addIndex(bottomLeft); }
//-------------------------------------------------------------- void testApp::draw(){ fbo.begin(); ofSetTextureWrap(GL_REPEAT,GL_REPEAT); fbfbo.draw(0,2); //cam.draw(0,0); fbfbo.getTextureReference().bind(); trik2.draw(); fbfbo.getTextureReference().unbind(); ofSetColor(255,255,255); if(tritimer>tritimerlimit){ float xpt, ypt,xtoff,ytoff; //draw gradient splashes ofMesh trik; for(int b = 0;b<5;b++){ xtoff = ofRandomf()*0.5; ytoff = ofRandomf()*0.5; for(int i=0;i<3;i++){ xpt = ofRandomuf()*2+xtoff; ypt = ofRandomuf()*2+ytoff; trik.addVertex(ofVec3f(xpt*w,ypt*h,0)); trik.addColor(ofFloatColor(float(ofRandomuf()>0.5)*0.6+0.4,float(ofRandomuf()>0.5)*0.5+0.5,float(ofRandomuf()>0.5)*0.7+0.3)); } } trik.draw(); tritimer = 0; tritimerlimit= ofRandom(20,200); } if(tritimer2>45){ //re-generate the feedback triangles float xpt, ypt,xoff,yoff,xtoff,ytoff; trik2.clear(); //ofEnableNormalizedTexCoords(); for(int b = 0;b<5;b++){ xoff = ofRandomf()*0.1; yoff = ofRandomf()*0.1; xtoff = ofRandomf()*0.5; ytoff = ofRandomf()*0.5; for(int i=0;i<3;i++){ xpt = ofRandomuf()+xtoff; ypt = ofRandomuf()+ytoff; trik2.addVertex(ofVec3f((xpt+xoff)*w,(ypt+yoff)*h,0)); trik2.addTexCoord(ofVec2f(xpt*w,ypt*h)); trik2.addColor(ofFloatColor(1,1,1)); } } tritimer2=0; tritimer2limit= ofRandom(20,200); //ofDisableNormalizedTexCoords(); } fbo.end(); fbfbo.begin(); fbo.draw(0,0); fbfbo.end(); fbo.draw(0,0); }
ofMesh getProjectedMesh(const ofMesh& mesh) { ofMesh projected = mesh; for(std::size_t i = 0; i < mesh.getNumVertices(); i++) { glm::vec3 cur = ofWorldToScreen(mesh.getVerticesPointer()[i]); cur.z = 0; projected.setVertex(i, cur); } return projected; }
//-------------------------------------------------------------- void ofxBulletSoftTriMesh::create( ofxBulletWorldSoft* a_world, ofMesh& aMesh, btTransform &a_bt_tr, float a_mass ) { if(a_world == NULL) { ofLogError("ofxBulletSoftTriMesh") << "create(): a_world param is NULL"; return; } if( aMesh.getMode() != OF_PRIMITIVE_TRIANGLES ) { ofLogError("ofxBulletSoftTriMesh") << " only excepts meshes that are triangles"; return; } _world = a_world; _cachedMesh.clear(); _cachedMesh = aMesh; if( bullet_vertices != NULL ) { delete bullet_vertices; bullet_vertices = NULL; } int vertStride = sizeof(btVector3); int indexStride = 3*sizeof(int); int totalVerts = (int)aMesh.getNumVertices(); int totalIndices = (int)aMesh.getNumIndices(); bullet_vertices = new btScalar[ totalVerts * 3 ]; int* bullet_indices = new int[ totalIndices ]; auto& tverts = aMesh.getVertices(); vector< ofIndexType >& tindices = aMesh.getIndices(); for( int i = 0; i < totalVerts; i++ ) { bullet_vertices[i*3+0] = tverts[i].x; bullet_vertices[i*3+1] = tverts[i].y; bullet_vertices[i*3+2] = tverts[i].z; } for( int i = 0; i < totalIndices; i++ ) { bullet_indices[i] = tindices[i]; } _softBody = btSoftBodyHelpers::CreateFromTriMesh( _world->getInfo(), bullet_vertices, bullet_indices, totalIndices/3 ); _softBody->transform( a_bt_tr ); setMass( a_mass, true ); setCreated(_softBody); createInternalUserData(); delete [] bullet_indices; }
//--------------------------------------------------------------------- void ofDrawBitmapCharacterStart(int stringLength){ charMesh.getVertices().resize(6 * stringLength); charMesh.getTexCoords().resize(6 * stringLength); if(!bBitmapTexturePrepared){ prepareBitmapTexture(); } vC = 0; }
void drawChunkyCloud(ofMesh& mesh, ofColor color, int innerRadius = 1, int outerRadius = 3) { ofPushStyle(); ofSetColor(0); glPointSize(outerRadius); mesh.draw(); ofSetColor(color); glPointSize(innerRadius); mesh.draw(); ofPopStyle(); }
void update() { int n = mesh.getNumVertices(); int start = ofMap(mouseX, 0, ofGetWidth(), 0, n, true); controlPoints.clear(); controlPoints.setMode(OF_PRIMITIVE_POINTS); for(int i = start; i < n; i++) { unsigned int index = rankedCorners[i]; controlPoints.addVertex(mesh.getVertex(index)); } }
//-------------------------------------------------------------- //Universal function which sets normals for the triangle mesh void setNormals( ofMesh &mesh ){ int nV = mesh.getNumVertices();//640 int nT = mesh.getNumIndices() / 3;//213 vector<ofPoint> norm( nV ); for (int t=0; t<nT; t++) { int i1 = mesh.getIndex( 3 * t ); int i2 = mesh.getIndex( 3 * t + 1 ); int i3 = mesh.getIndex( 641 ); const ofPoint &v1 = mesh.getVertex( i1 ); const ofPoint &v2 = mesh.getVertex( i2 ); const ofPoint &v3 = mesh.getVertex( i3 ); //Compute the triangle's normal ofPoint dir = ( (v2 - v1).crossed( v3 - v1 ) ).normalized(); norm[ i1 ] += dir; norm[ i2 ] += dir; norm[ i3 ] += dir; } //Normalize the normal's length for (int i=0; i<nV; i++) { norm[i].normalize(); } //Set the normals to mesh mesh.clearNormals(); mesh.addNormals( norm ); }
void ofxNDCircularGradient(float radius, const ofColor & start, const ofColor & end) { int n = 32; // circular gradient resolution static ofMesh _nd_cg_mesh; _nd_cg_mesh.clear(); _nd_cg_mesh.setMode(OF_PRIMITIVE_TRIANGLE_FAN); ofVec2f center(0,0); _nd_cg_mesh.addVertex(center); float angleBisector = TWO_PI / (n * 2); float smallRadius = radius; float bigRadius = smallRadius / cos(angleBisector); for(int i = 0; i <= n; i++) { float theta = i * TWO_PI / n; _nd_cg_mesh.addVertex(center + ofVec2f(sin(theta), cos(theta)) * bigRadius); } _nd_cg_mesh.clearColors(); _nd_cg_mesh.addColor(start); for(int i = 0; i <= n; i++) { _nd_cg_mesh.addColor(end); } _nd_cg_mesh.draw(); }
OFX_OBJLOADER_BEGIN_NAMESPACE void load(string path, ofMesh& mesh, bool generateNormals, bool flipFace) { path = ofToDataPath(path); mesh.clear(); GLMmodel* m; m = glmReadOBJ((char*)path.c_str()); if (generateNormals) { glmFacetNormals(m); glmVertexNormals(m, 90); } if (flipFace) { glmReverseWinding(m); } for (int j = 0; j < m->numtriangles; j++) { const GLMtriangle &tri = m->triangles[j]; for (int k = 0; k < 3; k++) { GLfloat *v = m->vertices + (tri.vindices[k] * 3); mesh.addVertex(ofVec3f(v[0], v[1], v[2])); if (m->colors) { GLfloat *c = m->colors + (tri.vindices[k] * 3); mesh.addColor(ofFloatColor(c[0], c[1], c[2])); } if (m->normals && ofInRange(tri.nindices[k], 0, m->numnormals)) { GLfloat *n = m->normals + (tri.nindices[k] * 3); mesh.addNormal(ofVec3f(n[0], n[1], n[2])); } if (m->texcoords && ofInRange(tri.tindices[k], 0, m->numtexcoords)) { GLfloat *c = m->texcoords + (tri.tindices[k] * 2); mesh.addTexCoord(ofVec2f(c[0], c[1])); } } } glmDelete(m); }
void ofApp::drawNormals(const ofMesh& mesh){ for(int i = 0; i < mesh.getVertices().size(); i++){ ofVec3f n = mesh.getNormal(i); ofVec3f v = mesh.getVertex(i); ofPushStyle(); ofSetColor(0, 255, 0); ofLine(v.x, v.y, v.z, v.x + (n.x*4), v.y + (n.y*4), v.z + (n.z*4) ); ofPopStyle(); } }
//-------------------------------------------------------------- void Page::addFace(ofMesh& mesh, vectorFace& _face) { addFace(mesh, _face.A, _face.B, _face.C); mesh.addTexCoord(_face.a); mesh.addTexCoord(_face.b); mesh.addTexCoord(_face.c); addFace(mesh, _face.A, _face.C, _face.D); mesh.addTexCoord(_face.a); mesh.addTexCoord(_face.c); mesh.addTexCoord(_face.d); }
void draw() { ofBackground(0); cam.begin(); ofSetLineWidth(2); ofSetColor(ofColor::red, 16); mesh.drawWireframe(); glPointSize(4); ofSetColor(ofColor::white); controlPoints.draw(); cam.end(); }