MaterialRecPtr VRMesure::setTransMat() { SimpleMaterialRecPtr mat = SimpleMaterial::create(); mat->setDiffuse(Color3f(0.7,0.7,0.7)); mat->setAmbient(Color3f(0.2, 0.2, 0.2)); mat->setTransparency(0.7); return mat; }
VRGeometryPtr loadPly(string filename) { GeoUInt8PropertyRecPtr Type = GeoUInt8Property::create(); GeoUInt32PropertyRecPtr Length = GeoUInt32Property::create(); GeoPnt3fPropertyRecPtr Pos = GeoPnt3fProperty::create(); GeoVec3fPropertyRecPtr Norms = GeoVec3fProperty::create(); GeoVec3fPropertyRecPtr Cols = GeoVec3fProperty::create(); GeoUInt32PropertyRecPtr Indices = GeoUInt32Property::create(); SimpleMaterialRecPtr Mat = SimpleMaterial::create(); GeoVec2fPropertyRecPtr Tex = GeoVec2fProperty::create(); Mat->setLit(false); Mat->setDiffuse(Color3f(0.8,0.8,0.6)); Mat->setAmbient(Color3f(0.4, 0.4, 0.2)); Mat->setSpecular(Color3f(0.1, 0.1, 0.1)); ifstream file(filename.c_str()); string line; list<element> elements; while (getline(file, line)) { if (line == "end_header") break; auto data = splitString(line, ' '); if (data[0] == "element") elements.push_back( element(data[1], toInt(data[2]) ) ); if (data[0] == "property") { if (data.size() == 3) elements.back().properties.push_back( property(data[1], data[2]) ); else elements.back().properties.push_back( property(data[1], data[2]) ); } } int N = 0; for (auto e : elements) N += e.N; VRProgress progress("load PLY " + filename, N); for (auto e : elements) { if (e.type == "vertex") { Vec3f p, n; Vec3i c; Vec2f t; bool doP = 0, doN = 0, doC = 0, doT = 0; for (int i=0; i<e.N; i++) { progress.update(1); getline(file, line); istringstream iss(line); for (auto prop : e.properties) { if (prop.name == "x") { iss >> p[0]; doP = 1; } if (prop.name == "y") { iss >> p[1]; doP = 1; } if (prop.name == "z") { iss >> p[2]; doP = 1; } if (prop.name == "nx") { iss >> n[0]; doN = 1; }
/** Create a mesh using vectors with positions, normals, indices && optionaly texture coordinates **/ void VRGeometry::create(int type, vector<Vec3f> pos, vector<Vec3f> norms, vector<int> inds, vector<Vec2f> texs) { bool doTex = (texs.size() == pos.size()); GeoUInt8PropertyRecPtr Type = GeoUInt8Property::create(); GeoUInt32PropertyRecPtr Length = GeoUInt32Property::create(); GeoPnt3fPropertyRecPtr Pos = GeoPnt3fProperty::create(); GeoVec3fPropertyRecPtr Norms = GeoVec3fProperty::create(); GeoUInt32PropertyRecPtr Indices = GeoUInt32Property::create(); SimpleMaterialRecPtr Mat = SimpleMaterial::create(); GeoVec2fPropertyRecPtr Tex = 0; if (doTex) Tex = GeoVec2fProperty::create(); Type->addValue(type); Length->addValue(inds.size()); //positionen und Normalen for(uint i=0;i<pos.size();i++) { Pos->addValue(pos[i]); Norms->addValue(norms[i]); if (doTex) Tex->addValue(texs[i]); } for(uint i=0;i<inds.size();i++) { Indices->addValue(inds[i]); } Mat->setDiffuse(Color3f(0.8,0.8,0.6)); Mat->setAmbient(Color3f(0.4, 0.4, 0.2)); Mat->setSpecular(Color3f(0.1, 0.1, 0.1)); GeometryRecPtr geo = Geometry::create(); geo->setTypes(Type); geo->setLengths(Length); geo->setIndices(Indices); geo->setPositions(Pos); geo->setNormals(Norms); if (doTex) geo->setTexCoords(Tex); geo->setMaterial(Mat); setMesh(geo); }
void VRMaterial::setMaterial(MaterialRecPtr m) { if ( dynamic_pointer_cast<MultiPassMaterial>(m) ) { MultiPassMaterialRecPtr mm = dynamic_pointer_cast<MultiPassMaterial>(m); for (unsigned int i=0; i<mm->getNPasses(); i++) { if (i > 0) addPass(); setMaterial(mm->getMaterials(i)); } setActivePass(0); return; } if ( isSMat(m) ) { SimpleMaterialRecPtr sm = dynamic_pointer_cast<SimpleMaterial>(m); setDiffuse(sm->getDiffuse()); setAmbient(sm->getAmbient()); setSpecular(sm->getSpecular()); if ( isSTMat(m) ) { SimpleTexturedMaterialRecPtr stm = dynamic_pointer_cast<SimpleTexturedMaterial>(m); setTexture( VRTexture::create(stm->getImage()), true); } return; } if ( isCMat(m) ) { MaterialChunkRecPtr mc = 0; BlendChunkRecPtr bc = 0; TextureEnvChunkRecPtr ec = 0; TextureObjChunkRecPtr tc = 0; ChunkMaterialRecPtr cmat = dynamic_pointer_cast<ChunkMaterial>(m); for (uint i=0; i<cmat->getMFChunks()->size(); i++) { StateChunkRecPtr chunk = cmat->getChunk(i); if (mc == 0) mc = dynamic_pointer_cast<MaterialChunk>(chunk); if (bc == 0) bc = dynamic_pointer_cast<BlendChunk>(chunk); if (ec == 0) ec = dynamic_pointer_cast<TextureEnvChunk>(chunk); if (tc == 0) tc = dynamic_pointer_cast<TextureObjChunk>(chunk); } auto md = mats[activePass]; if (mc) mc->setBackMaterial(false); if (mc) { if (md->colChunk) md->mat->subChunk(md->colChunk); md->colChunk = mc; md->mat->addChunk(mc); } if (bc) { if (md->blendChunk) md->mat->subChunk(md->blendChunk); md->blendChunk = bc; md->mat->addChunk(bc); } if (ec) { if (md->envChunk) md->mat->subChunk(md->envChunk); md->envChunk = ec; md->mat->addChunk(ec); } if (tc) { if (md->texChunk) md->mat->subChunk(md->texChunk); md->texChunk = tc; md->mat->addChunk(tc); } return; } cout << "Warning: unhandled material type\n"; if (dynamic_pointer_cast<Material>(m)) cout << " Material" << endl; if (dynamic_pointer_cast<PrimeMaterial>(m)) cout << " PrimeMaterial" << endl; if (dynamic_pointer_cast<SimpleMaterial>(m)) cout << " SimpleMaterial" << endl; if (dynamic_pointer_cast<SimpleTexturedMaterial>(m)) cout << " SimpleTexturedMaterial" << endl; if (dynamic_pointer_cast<VariantMaterial>(m)) cout << " VariantMaterial" << endl; if (dynamic_pointer_cast<ChunkMaterial>(m)) cout << " ChunkMaterial" << endl; if (dynamic_pointer_cast<MultiPassMaterial>(m)) cout << " MultiPassMaterial" << endl; if (dynamic_pointer_cast<CompositeMaterial>(m)) cout << " CompositeMaterial" << endl; if (dynamic_pointer_cast<SwitchMaterial>(m)) cout << " SwitchMaterial" << endl; }
void VRBlinds::create() { vector<Vec3f> norms; vector<int> inds; vector<Vec2f> texs; for (int i=0;i<20;i++) {//20 blend elements bl_pos_closed.push_back(Vec3f(0.55, -0.07*i, 0)); bl_pos_closed.push_back(Vec3f(0.55, -0.07*(i+0.3), -0.04)); bl_pos_closed.push_back(Vec3f(0.55, -0.07*(i+0.9), -0.08)); bl_pos_closed.push_back(Vec3f(-0.55, -0.07*(i+0.9), -0.08)); bl_pos_closed.push_back(Vec3f(-0.55, -0.07*(i+0.3), -0.04)); bl_pos_closed.push_back(Vec3f(-0.55, -0.07*i, 0)); bl_pos_open.push_back(Vec3f(0.55, -0.01*i, 0)); bl_pos_open.push_back(Vec3f(0.55, -0.01*(i-0.7), -0.05)); bl_pos_open.push_back(Vec3f(0.55, -0.01*i, -0.1)); bl_pos_open.push_back(Vec3f(-0.55, -0.01*i, -0.1)); bl_pos_open.push_back(Vec3f(-0.55, -0.01*(i-0.7), -0.05)); bl_pos_open.push_back(Vec3f(-0.55, -0.01*i, 0)); inds.push_back(i*6+0);//quad1 inds.push_back(i*6+1);//quad1 inds.push_back(i*6+4);//quad1 inds.push_back(i*6+5);//quad1 for (int j=1;j<5;j++) inds.push_back(i*6+j);//quad2 for (int j=0;j<6;j++) norms.push_back(Vec3f(0,1,0)); texs.push_back(Vec2f(1,0)); texs.push_back(Vec2f(1,0.5)); texs.push_back(Vec2f(1,1)); texs.push_back(Vec2f(0,1)); texs.push_back(Vec2f(0,0.5)); texs.push_back(Vec2f(0,0)); } blend_geo = new VRGeometry("blend"); blend_geo->create(GL_QUADS, bl_pos_open, norms, inds, texs); Vec3f pos = window->getGeometricCenter(); Vec3f norm = window->getAverageNormal(); norm.normalize(); pos[2] = window->getMax(2); Matrix m = window->getWorldMatrix(); m.mult(pos, pos); m.mult(norm, norm); blend_geo->setPose(pos, pos + norm, Vec3f(0,1,0)); scene->add(blend_geo); SimpleMaterialRecPtr mat = SimpleMaterial::create(); mat->setDiffuse(Color3f(0.5,0.5,0.5)); mat->setAmbient(Color3f(0.2, 0.2, 0.2)); mat->setSpecular(Color3f(0.1, 0.1, 0.1)); blend_geo->setMaterial(mat); }
void VRStroke::strokeProfile(vector<Vec3f> profile, bool closed, bool lit) { mode = 0; this->profile = profile; this->closed = closed; this->lit = lit; GeoUInt8PropertyRecPtr Type = GeoUInt8Property::create(); GeoUInt32PropertyRecPtr Length = GeoUInt32Property::create(); GeoPnt3fPropertyRecPtr Pos = GeoPnt3fProperty::create(); GeoVec3fPropertyRecPtr Norms = GeoVec3fProperty::create(); GeoVec3fPropertyRecPtr Colors = GeoVec3fProperty::create(); GeoUInt32PropertyRecPtr Indices = GeoUInt32Property::create(); bool doCaps = closed && profile.size() > 1; Vec3f z = Vec3f(0,0,1); if (profile.size() == 1) Type->addValue(GL_LINES); else Type->addValue(GL_QUADS); clearChildren(); for (uint i=0; i<paths.size(); i++) { vector<Vec3f> pnts = paths[i]->getPositions(); vector<Vec3f> directions = paths[i]->getDirections(); vector<Vec3f> up_vectors = paths[i]->getUpvectors(); vector<Vec3f> cols = paths[i]->getColors(); Vec3f _p; for (uint j=0; j<pnts.size(); j++) { Vec3f p = pnts[j]; Vec3f n = directions[j]; Vec3f u = up_vectors[j]; Vec3f c = cols[j]; Matrix m; MatrixLookAt(m, Vec3f(0,0,0), n, u); // add new profile points && normals for (uint k=0; k<profile.size(); k++) { Vec3f tmp = profile[k]; m.mult(tmp, tmp); Pos->addValue(p+tmp); tmp.normalize(); Norms->addValue(tmp); Colors->addValue(c); } if (j==0 && profile.size() > 1) continue; // add line if (profile.size() == 1) { int N = Pos->size(); Indices->addValue(N-2); Indices->addValue(N-1); } else { // add quad for (uint k=0; k<profile.size()-1; k++) { int N1 = Pos->size() - 2*profile.size() + k; int N2 = Pos->size() - profile.size() + k; Indices->addValue(N1); Indices->addValue(N2); Indices->addValue(N2+1); Indices->addValue(N1+1); //cout << "\nN1N2 " << N1 << " " << N2 << " " << N2+1 << " " << N1+1 << flush; } if (closed) { int N0 = Pos->size() - 2*profile.size(); int N1 = Pos->size() - profile.size() - 1; int N2 = Pos->size() - 1; Indices->addValue(N1); Indices->addValue(N2); Indices->addValue(N1+1); Indices->addValue(N0); //cout << "\nN1N2 " << N1 << " " << N2 << " " << N1+1 << " " << N0 << flush; } } } } Length->addValue(Indices->size()); // caps if (doCaps) { int Nt = 0; for (uint i=0; i<paths.size(); i++) { vector<Vec3f> pnts = paths[i]->getPositions(); vector<Vec3f> directions = paths[i]->getDirections(); vector<Vec3f> up_vectors = paths[i]->getUpvectors(); vector<Vec3f> cols = paths[i]->getColors(); Matrix m; // first cap Vec3f p = pnts[0]; Vec3f n = directions[0]; Vec3f u = up_vectors[0]; Vec3f c = cols[0]; int Ni = Pos->size(); Pos->addValue(p); Norms->addValue(-n); Colors->addValue(c); MatrixLookAt(m, Vec3f(0,0,0), n, u); for (uint k=0; k<profile.size(); k++) { Vec3f tmp = profile[k]; m.mult(tmp, tmp); Pos->addValue(p+tmp); Norms->addValue(-n); Colors->addValue(c); } for (uint k=1; k<=profile.size(); k++) { int j = k+1; if (k == profile.size()) j = 1; Indices->addValue(Ni); Indices->addValue(Ni+k); Indices->addValue(Ni+j); Nt+=3; } // last cap int N = pnts.size()-1; Ni = Pos->size(); p = pnts[N]; n = directions[N]; u = up_vectors[N]; c = cols[N]; Pos->addValue(p); Norms->addValue(n); Colors->addValue(c); MatrixLookAt(m, Vec3f(0,0,0), n, u); for (uint k=0; k<profile.size(); k++) { Vec3f tmp = profile[k]; m.mult(tmp, tmp); Pos->addValue(p+tmp); Norms->addValue(n); Colors->addValue(c); } for (uint k=1; k<=profile.size(); k++) { int j = k+1; if (k == profile.size()) j = 1; Indices->addValue(Ni); Indices->addValue(Ni+j); Indices->addValue(Ni+k); Nt+=3; } } Type->addValue(GL_TRIANGLES); Length->addValue(Nt); // caps triangles } SimpleMaterialRecPtr Mat = SimpleMaterial::create(); GeometryRecPtr g = Geometry::create(); g->setTypes(Type); g->setLengths(Length); g->setPositions(Pos); g->setNormals(Norms); g->setColors(Colors); g->setIndices(Indices); g->setMaterial(Mat); Mat->setLit(lit); setMesh(g); // test for ccw faces /*TriangleIterator it(getMesh()); while (!it.isAtEnd()) { Vec3f p0 = Vec3f(it.getPosition(0)); Vec3f p1 = Vec3f(it.getPosition(1)); Vec3f p2 = Vec3f(it.getPosition(2)); Vec3f n0 = it.getNormal(0); Vec3f n1 = it.getNormal(1); Vec3f n2 = it.getNormal(2); Vec3f np1 = (p1-p0).cross(p2-p0); Vec3f np2 = n0+n1+n2; np2.normalize(); cout << " face orientation " << np1.dot(np2) << endl; ++it; }*/ }
void VRStroke::strokeProfile(vector<Vec3f> profile, bool closed, bool lit) { mode = 0; this->profile = profile; this->closed = closed; this->lit = lit; GeoUInt8PropertyRecPtr Type = GeoUInt8Property::create(); GeoUInt32PropertyRefPtr Length = GeoUInt32Property::create(); GeoPnt3fPropertyRecPtr Pos = GeoPnt3fProperty::create(); GeoVec3fPropertyRefPtr Norms = GeoVec3fProperty::create(); GeoVec3fPropertyRefPtr Colors = GeoVec3fProperty::create(); GeoUInt32PropertyRefPtr Indices = GeoUInt32Property::create(); Vec3f z = Vec3f(0,0,1); if (profile.size() == 1) Type->addValue(GL_LINES); else Type->addValue(GL_QUADS); clearChildren(); for (uint i=0; i<paths.size(); i++) { vector<Vec3f> pnts = paths[i]->get(); vector<Vec3f> norms = paths[i]->getNormals(); vector<Vec3f> cols = paths[i]->getColors(); Vec3f _p; for (uint j=0; j<pnts.size(); j++) { Vec3f p = pnts[j]; Vec3f n = norms[j]; Vec3f c = cols[j]; //float ca = n.dot(z); Matrix m; //MatrixLookAt(m, Vec3f(0,0,0), n, z.cross(n)); MatrixLookAt(m, Vec3f(0,0,0), n, Vec3f(0,1,0)); // add new profile points and normals for (uint k=0; k<profile.size(); k++) { Vec3f tmp = profile[k]; m.mult(tmp, tmp); Pos->addValue(p+tmp); tmp.normalize(); Norms->addValue(tmp); Colors->addValue(c); } if (j==0 and profile.size() > 1) continue; // add line if (profile.size() == 1) { int N = Pos->size(); Indices->addValue(N-2); Indices->addValue(N-1); } else { // add quad for (uint k=0; k<profile.size()-1; k++) { int N1 = Pos->size() - 2*profile.size() + k; int N2 = Pos->size() - profile.size() + k; Indices->addValue(N1); Indices->addValue(N2); Indices->addValue(N2+1); Indices->addValue(N1+1); //cout << "\nN1N2 " << N1 << " " << N2 << " " << N2+1 << " " << N1+1 << flush; } if (closed) { int N0 = Pos->size() - 2*profile.size(); int N1 = Pos->size() - profile.size() - 1; int N2 = Pos->size() - 1; Indices->addValue(N1); Indices->addValue(N2); Indices->addValue(N1+1); Indices->addValue(N0); //cout << "\nN1N2 " << N1 << " " << N2 << " " << N1+1 << " " << N0 << flush; } } } } Length->addValue(Indices->size()); SimpleMaterialRecPtr Mat = SimpleMaterial::create(); GeometryRecPtr g = Geometry::create(); g->setTypes(Type); g->setLengths(Length); g->setPositions(Pos); g->setNormals(Norms); g->setColors(Colors); g->setIndices(Indices); g->setMaterial(Mat); Mat->setLit(lit); VRGeometry* geo = new VRGeometry("stroke"); geo->setMesh(g); addChild(geo); }