void BasicApplicationApp::setup() { plane.appendVertex(Vec3f(0, 0, 0)); // [ (0,0,0) ] plane.appendColorRgba(Colorf(1.f,0,0)); plane.appendTexCoord(Vec2f(0,0)); plane.appendVertex(Vec3f(600, 0, 0)); // [ (0,0,0), (600,0,0) ] plane.appendColorRgba(Colorf(1.f,1.f,0)); plane.appendTexCoord(Vec2f(1.f,0)); plane.appendVertex(Vec3f(600, 600, 0)); // [ (0,0,0), (600,0,0), (600,600,0)] plane.appendColorRgba(Colorf(0,1.f,0)); plane.appendTexCoord(Vec2f(1.f,1.f)); plane.appendVertex(Vec3f(0, 600, 0)); // [ (0,0,0), (600,0,0), (600,600,0), (0,600,0)] plane.appendColorRgba(Colorf(0,0,1.f)); plane.appendTexCoord(Vec2f(0.f,1.f)); uint indices[6] = {0,1,2,2,3,0}; plane.appendIndices(&indices[0], 6); mTexture = gl::Texture( loadImage())) }
BaseMeshRef SimpleMesh::generateQuad(Rectf dimensions, Rectf uvCoords = Rectf(0.0f, 0.0f, 1.0f, 1.0f)) { // cout << "SimpleMesh::GenerateQuad(); dims: " << dimensions // << " uvCoords: " << uvCoords << endl; TriMesh mesh; mesh.clear(); // Vertexes mesh.appendVertex(Vec3f(dimensions.x1, dimensions.y1, 0)); mesh.appendVertex(Vec3f(dimensions.x1, dimensions.y2, 0)); mesh.appendVertex(Vec3f(dimensions.x2, dimensions.y2, 0)); mesh.appendVertex(Vec3f(dimensions.x2, dimensions.y1, 0)); // Vertex Colors mesh.appendColorRgb(Color(1.0f, 1.0f, 1.0f)); mesh.appendColorRgb(Color(1.0f, 1.0f, 1.0f)); mesh.appendColorRgb(Color(1.0f, 1.0f, 1.0f)); mesh.appendColorRgb(Color(1.0f, 1.0f, 1.0f)); // Tex coords mesh.appendTexCoord(Vec2f(uvCoords.x1, uvCoords.y1)); mesh.appendTexCoord(Vec2f(uvCoords.x1, uvCoords.y2)); mesh.appendTexCoord(Vec2f(uvCoords.x2, uvCoords.y2)); mesh.appendTexCoord(Vec2f(uvCoords.x2, uvCoords.y1)); int vert0 = mesh.getNumVertices() - 4; int vert1 = mesh.getNumVertices() - 1; int vert2 = mesh.getNumVertices() - 2; int vert3 = mesh.getNumVertices() - 3; mesh.appendTriangle(vert0, vert1, vert3); mesh.appendTriangle(vert3, vert1, vert2); mesh.recalculateNormals(); SimpleMeshRef meshWrapper = make_shared<SimpleMesh>(mesh); meshWrapper->_bounds = mesh.calcBoundingBox(); return dynamic_pointer_cast<BaseMesh>(meshWrapper); }
TriMesh ciFaceTracker::getMesh(const vector<vec3>& points) const { TriMesh mesh; if (!mIsFailed) { int n = size(); for (int i = 0; i < n; i++) { mesh.appendPosition(points[i]); mesh.appendTexCoord({ getImagePoint(i).x / mImgSize.x, getImagePoint(i).y / mImgSize.y }); } addTriangleIndices(mesh); } return mesh; }
void SmoothDisplacementMappingApp::createMesh() { // use the TriMesh class to easily construct the vertex buffer object TriMesh mesh; // create vertex, normal and texcoord buffers const int RES_X = 400; const int RES_Z = 100; const Vec3f size(200.0f, 1.0f, 50.0f); for(int x=0;x<RES_X;++x) { for(int z=0;z<RES_Z;++z) { float u = float(x) / RES_X; float v = float(z) / RES_Z; mesh.appendVertex( size * Vec3f( u - 0.5f , 0.0f, v - 0.5f ) ); mesh.appendNormal( Vec3f::yAxis() ); mesh.appendTexCoord( Vec2f( u, v ) ); } } // create index buffer vector< uint32_t > indices; for(int x=0;x<RES_X-1;++x) { for(int z=0;z<RES_Z-1;++z) { uint32_t i = x * RES_Z + z; indices.push_back( i ); indices.push_back( i + 1 ); indices.push_back( i + RES_Z ); indices.push_back( i + RES_Z ); indices.push_back( i + 1 ); indices.push_back( i + RES_Z + 1 ); } } mesh.appendIndices( &indices.front(), indices.size() ); // construct vertex buffer object gl::VboMesh::Layout layout; layout.setStaticPositions(); layout.setStaticTexCoords2d(); layout.setStaticIndices(); layout.setStaticNormals(); mVboMesh = gl::VboMesh( mesh, layout ); }
TriMesh MeshHelper::createTriMesh( vector<uint32_t> &indices, const vector<Vec3f> &positions, const vector<Vec3f> &normals, const vector<Vec2f> &texCoords ) { TriMesh mesh; if ( indices.size() > 0 ) { mesh.appendIndices( &indices[ 0 ], indices.size() ); } if ( normals.size() > 0 ) { for ( vector<Vec3f>::const_iterator iter = normals.begin(); iter != normals.end(); ++iter ) { mesh.appendNormal( *iter ); } } if ( positions.size() > 0 ) { mesh.appendVertices( &positions[ 0 ], positions.size() ); } if ( texCoords.size() > 0 ) { for ( vector<Vec2f>::const_iterator iter = texCoords.begin(); iter != texCoords.end(); ++iter ) { mesh.appendTexCoord( *iter ); } } return mesh; }
void ProjectionMappingApp::bezierMesh(const int mode) { const int span = mSpan; float hx = mHandleSize; float hy = mHandleSize; //if (mode == 0) { mMesh.clear(); } int k = 0; for (int ix = 0; ix < mGridNum.x-1; ++ix) { for (int iu = 0; iu < span; ++iu) { if (ix > 0 && iu == 0) continue; for (int iy = 0; iy < mGridNum.y-1; ++iy) { for (int iv = 0; iv < span; ++iv) { if (iy > 0 && iv == 0) continue; int loc0 = ((ix+0)*(mGridNum.y)) + (iy+0); int loc1 = ((ix+0)*(mGridNum.y)) + (iy+1); int loc2 = ((ix+1)*(mGridNum.y)) + (iy+0); int loc3 = ((ix+1)*(mGridNum.y)) + (iy+1); Vec2f p[4][4]; p[0][0] = deform(mCtrlPoints[loc0].base+Vec2f(0, 0)) + mCtrlPoints[loc0].mag; p[0][1] = deform(mCtrlPoints[loc0].base+Vec2f(0, hy)) + mCtrlPoints[loc0].mag; p[0][2] = deform(mCtrlPoints[loc1].base+Vec2f(0, -hy)) + mCtrlPoints[loc1].mag; p[0][3] = deform(mCtrlPoints[loc1].base+Vec2f(0, 0)) + mCtrlPoints[loc1].mag; p[1][0] = deform(mCtrlPoints[loc0].base+Vec2f(hx, 0)) + mCtrlPoints[loc0].mag; p[1][1] = deform(mCtrlPoints[loc0].base+Vec2f(hx, hy)) + mCtrlPoints[loc0].mag; p[1][2] = deform(mCtrlPoints[loc1].base+Vec2f(hx, -hy)) + mCtrlPoints[loc1].mag; p[1][3] = deform(mCtrlPoints[loc1].base+Vec2f(hx, 0)) + mCtrlPoints[loc1].mag; p[2][0] = deform(mCtrlPoints[loc2].base+Vec2f(-hx, 0)) + mCtrlPoints[loc2].mag; p[2][1] = deform(mCtrlPoints[loc2].base+Vec2f(-hx, hy)) + mCtrlPoints[loc2].mag; p[2][2] = deform(mCtrlPoints[loc3].base+Vec2f(-hx, -hy)) + mCtrlPoints[loc3].mag; p[2][3] = deform(mCtrlPoints[loc3].base+Vec2f(-hx, 0)) + mCtrlPoints[loc3].mag; p[3][0] = deform(mCtrlPoints[loc2].base+Vec2f(0, 0)) + mCtrlPoints[loc2].mag; p[3][1] = deform(mCtrlPoints[loc2].base+Vec2f(0, hy)) + mCtrlPoints[loc2].mag; p[3][2] = deform(mCtrlPoints[loc3].base+Vec2f(0, -hy)) + mCtrlPoints[loc3].mag; p[3][3] = deform(mCtrlPoints[loc3].base+Vec2f(0, 0)) + mCtrlPoints[loc3].mag; mCtrlPoints[loc0].pos = p[0][0]; mCtrlPoints[loc1].pos = p[0][3]; mCtrlPoints[loc2].pos = p[3][0]; mCtrlPoints[loc3].pos = p[3][3]; float v = (float)iv/(float)(span-1); float u = (float)iu/(float)(span-1); Vec2f r[4]; r[0] = bezierNrm(p[0], v); r[1] = bezierNrm(p[1], v); r[2] = bezierNrm(p[2], v); r[3] = bezierNrm(p[3], v); Vec2f fp = bezierNrm(r, u); if (mode == 0) { // create mMesh.appendVertex(Vec3f(fp.x, fp.y, 0)); mMesh.appendTexCoord(Vec2f(fp.x, fp.y)); } else { // update mMesh.getVertices()[k] = Vec3f(fp.x, fp.y, 0); } k++; } } } } if (mode == 0) { // create int nx = ((mGridNum.x-1)*span) - (mGridNum.x-2); int ny = ((mGridNum.y-1)*span) - (mGridNum.y-2); int id = 0; for (int ix = 0; ix < nx-1; ++ix) { for (int iy = 0; iy < ny-1; ++iy) { int id0 = id; int id1 = id+1; int id2 = id1+ny; int id3 = id2-1; mMesh.appendTriangle(id0, id1, id2); mMesh.appendTriangle(id0, id2, id3); ++id; } ++id; } } }
void RodSoundApp::setup() { // std::cout << solveBEM(constants::radius) << "\n\n"; // std::cout << "Expected:\n" << -constants::pi * constants::radius * constants::radius * Mat2e::Identity() << "\n\n"; // Setup scene cam.setPerspective(40.0, getWindowAspectRatio(), 0.1, 1000.0); cam.lookAt(eyePos, targetPos, Vec3c(0.0, 1.0, 0.0)); // Setup rendering stuff spheredl = new gl::DisplayList(GL_COMPILE); spheredl->newList(); gl::drawSphere(Vec3c::zero(), constants::radius); spheredl->endList(); cylinderdl = new gl::DisplayList(GL_COMPILE); cylinderdl->newList(); gl::drawCylinder(constants::radius, constants::radius, 1.0); cylinderdl->endList(); l = new gl::Light(gl::Light::POINT, 0); try { rodTex = loadImage(loadResource(RES_SIM_YARN_TEX)); } catch (ImageIoException e) { std::cerr << "Error loading textures: " << e.what(); exit(1); } // Load and compile shaders try { diffuseProg = gl::GlslProg(loadResource(RES_SIM_VERT_GLSL), loadResource(RES_SIM_FRAG_GLSL)); rodProg = gl::GlslProg(loadResource(RES_SIM_VERT_TEX_GLSL), loadResource(RES_SIM_FRAG_TEX_GLSL)); } catch (gl::GlslProgCompileExc e) { std::cerr << "Error compiling GLSL program: " << e.what(); exit(1); } catch (ResourceLoadExc e) { std::cerr << "Error loading shaders: " << e.what(); exit(1); } floor.appendVertex(Vec3c(-100.0, 0.0, -100.0)); floor.appendNormal(Vec3c(0.0, 1.0, 0.0)); floor.appendTexCoord(Vec2c(-12.0, -12.0)); floor.appendVertex(Vec3c(100.0, 0.0, -100.0)); floor.appendNormal(Vec3c(0.0, 1.0, 0.0)); floor.appendTexCoord(Vec2c(12.0, -12.0)); floor.appendVertex(Vec3c(100.0, 0.0, 100.0)); floor.appendNormal(Vec3c(0.0, 1.0, 0.0)); floor.appendTexCoord(Vec2c(12.0, 12.0)); floor.appendVertex(Vec3c(-100.0, 0.0, 100.0)); floor.appendNormal(Vec3c(0.0, 1.0, 0.0)); floor.appendTexCoord(Vec2c(-12.0, 12.0)); floor.appendTriangle(0, 1, 2); floor.appendTriangle(0, 3, 2); ci::Surface s(4, 4, false); auto iter = s.getIter(); do { do { Vec2i pos = iter.getPos(); unsigned char val = pos.x > 0 && pos.x < 3 && pos.y > 0 && pos.y < 3 ? 100 : 150; iter.r() = iter.g() = iter.b() = val; } while (iter.pixel()); } while (iter.line()); floorTex = gl::Texture(s); floorTex.setMagFilter(GL_NEAREST); floorTex.setWrap(GL_REPEAT, GL_REPEAT); // Load the rod loadDefaultRod(50); // loadRodFile(""); loadStdEnergies(); PROFILER_START("Total"); }
void FaceOff::trackerThreadFn() { jing::Option option; option.scale = 0.5f; mOfflineTracker = jing::BaseFaceTracker::create(); mOnlineTracker = jing::BaseFaceTracker::create(option); bool shouldInitFaceMesh = false; while (!mShouldQuit) { // TODO: more robust with update_signal if (!mCapture.checkNewFrame()) { sleep(1.0f); continue; } if (mDoesCaptureNeedsInit) { // TODO: more robust with setup_signal mDoesCaptureNeedsInit = false; CAM_W = mCapture.size.x; CAM_H = mCapture.size.y; auto createFboFn = [this] { gl::Fbo::Format fboFormat; fboFormat.enableDepthBuffer(false); mRenderedOfflineFaceFbo = gl::Fbo::create(CAM_W, CAM_H, fboFormat); mFaceMaskFbo = gl::Fbo::create(CAM_W, CAM_H, fboFormat); mClone.setup(CAM_W, CAM_H); mClone.setStrength(16); }; dispatchAsync(createFboFn); } if (mPeopleId != PEOPLE_ID) { mPeopleId = PEOPLE_ID; ImageSourceRef img = loadImage(loadAsset("people/" + mPeopleNames[PEOPLE_ID])); updateOfflineImage(img); shouldInitFaceMesh = true; } mOnlineTracker->update(mCapture.surface); if (!mOnlineTracker->getFound()) { mHasNewRenderedFace = false; continue; } int nPoints = mOnlineTracker->size(); if (shouldInitFaceMesh) { shouldInitFaceMesh = false; mFaceMesh.getBufferTexCoords0().clear(); auto imgSize = mOfflineTracker->getImageSize(); for (int i = 0; i < nPoints; i++) { vec3 point = mOfflineTracker->getImagePoint(i); mFaceMesh.appendTexCoord({ point.x / imgSize.x, point.y / imgSize.y }); } mOnlineTracker->addTriangleIndices(mFaceMesh); } mFaceMesh.getBufferPositions().clear(); for (int i = 0; i < nPoints; i++) { mFaceMesh.appendPosition(mOnlineTracker->getImagePoint(i)); } if (VFX_VISIBLE && mOfflineFaceTex) { dispatchAsync(bind(&FaceOff::updateClone, this)); } } }
void verticesApp::generateCapsule() { vector<uint> indices; double fDeltaRingAngle = (M_PI_2 / mNumRings); double fDeltaSegAngle = ((M_PI * 2.0) / mNumSegments); double sphereRatio = mRadius / (2 * mRadius + mHeight); double cylinderRatio = mHeight / (2 * mRadius + mHeight); int offset = 0; // Top half sphere // Generate the group of rings for the sphere for(unsigned int ring = 0; ring <= mNumRings; ring++ ) { double r0 = mRadius * sinf ( ring * fDeltaRingAngle); double y0 = mRadius * cosf (ring * fDeltaRingAngle); // Generate the group of segments for the current ring for(unsigned int seg = 0; seg <= mNumSegments; seg++) { double x0 = r0 * cosf(seg * fDeltaSegAngle); double z0 = r0 * sinf(seg * fDeltaSegAngle); Vec3f p(x0, 0.5f * mHeight + y0, z0); Vec3f n(x0, y0, z0); mesh.appendVertex(p); mesh.appendNormal(n.normalized()); mesh.appendTexCoord(Vec2f((double) seg / (double) mNumSegments, (double) ring / (double) mNumRings * sphereRatio)); mesh.appendColorRgb(Colorf(1.0, 0, 0)); // each vertex (except the last) has six indices pointing to it indices.push_back(offset + mNumSegments + 1); indices.push_back(offset + mNumSegments); indices.push_back(offset); indices.push_back(offset + mNumSegments + 1); indices.push_back(offset); indices.push_back(offset + 1); offset ++; } // end for seg } // end for ring // Cylinder part double deltaAngle = ((M_PI * 2.0) / mNumSegments); double deltamHeight = mHeight/(double)mNumSegHeight; for (unsigned short i = 1; i < mNumSegHeight; i++) { for (unsigned short j = 0; j<=mNumSegments; j++) { double x0 = mRadius * cosf(j*deltaAngle); double z0 = mRadius * sinf(j*deltaAngle); Vec3f p(x0, 0.5f*mHeight-i*deltamHeight, z0); Vec3f n(x0, 0, z0); mesh.appendVertex(p); mesh.appendNormal(n.normalized()); mesh.appendTexCoord(Vec2f(j/(double)mNumSegments, i/(double)mNumSegHeight * cylinderRatio + sphereRatio)); mesh.appendColorRgb(Colorf(0, 1.0 - (float(j)/float(mNumSegments)), 0)); indices.push_back(offset + mNumSegments + 1); indices.push_back(offset + mNumSegments); indices.push_back(offset); indices.push_back(offset + mNumSegments + 1); indices.push_back(offset); indices.push_back(offset + 1); offset ++; } } // Bottom half sphere // Generate the group of rings for the sphere for(unsigned int ring = 0; ring <= mNumRings; ring++) { double r0 = mRadius * sinf (M_PI_2 + ring * fDeltaRingAngle); double y0 = mRadius * cosf (M_PI_2 + ring * fDeltaRingAngle); // Generate the group of segments for the current ring for(unsigned int seg = 0; seg <= mNumSegments; seg++) { double x0 = r0 * cosf(seg * fDeltaSegAngle); double z0 = r0 * sinf(seg * fDeltaSegAngle); Vec3f p(x0, -0.5f*mHeight + y0, z0); Vec3f n(x0, y0, z0); mesh.appendVertex(p); mesh.appendNormal(n.normalized()); mesh.appendTexCoord(Vec2f((double) seg / (double) mNumSegments, (double) ring / (double) mNumRings*sphereRatio + cylinderRatio + sphereRatio)); mesh.appendColorRgb(Colorf(0, 0, float(ring)/float(mNumRings))); if (ring != mNumRings) { // each vertex (except the last) has six indices pointing to it indices.push_back(offset + mNumSegments + 1); indices.push_back(offset + mNumSegments); indices.push_back(offset); indices.push_back(offset + mNumSegments + 1); indices.push_back(offset); indices.push_back(offset + 1); } offset ++; } // end for seg } // end for ring mesh.appendIndices( &indices[0], indices.size()); }