GeometryTransitPtr CSGGeometry::toOsgGeometry(CGAL::Polyhedron *p) { GeoPnt3fPropertyRecPtr positions = GeoPnt3fProperty::create(); GeoVec3fPropertyRecPtr normals = GeoVec3fProperty::create(); GeoUInt32PropertyRecPtr indices = GeoUInt32Property::create(); /* * Iterate over all faces, add their vertices to 'positions' && write indices at * the same time. Results in no shared vertices && therefore no normal interpolation between * faces, but makes cubes look good. Well, well... */ Matrix localToWorld = getWorldMatrix(); OSG::Vec3f translation; OSG::Quaternion rotation; OSG::Vec3f scaleFactor; OSG::Quaternion scaleOrientation; localToWorld.getTransform(translation, rotation, scaleFactor, scaleOrientation); Matrix worldToLocal; worldToLocal.invertFrom(localToWorld); // Convert indices && positions int curIndex = 0; for (CGAL::Polyhedron::Facet_const_iterator it = p->facets_begin(); it != p->facets_end(); it++) { CGAL::Polyhedron::Halfedge_around_facet_const_circulator circ = it->facet_begin(); do { CGAL::Point cgalPos = circ->vertex()->point(); // We need to transform each point from global coordinates into our local coordinate system // (CGAL uses global, OpenSG has geometry in node-local coords) OSG::Vec3f vecPos = OSG::Vec3f(CGAL::to_double(cgalPos.x()), CGAL::to_double(cgalPos.y()), CGAL::to_double(cgalPos.z())); OSG::Vec3f localVec = worldToLocal * (vecPos - translation); OSG::Pnt3f osgPos(localVec.x(), localVec.y(), localVec.z()); positions->addValue(osgPos); normals->addValue(Vec3f(0,1,0)); indices->addValue(curIndex); curIndex++; } while (++circ != it->facet_begin()); } GeoUInt8PropertyRecPtr types = GeoUInt8Property::create(); types->addValue(GL_TRIANGLES); GeoUInt32PropertyRecPtr lengths = GeoUInt32Property::create(); lengths->addValue(indices->size()); GeometryRecPtr mesh = Geometry::create(); mesh->setPositions(positions); mesh->setNormals(normals); mesh->setIndices(indices); mesh->setTypes(types); mesh->setLengths(lengths); mesh->setMaterial(VRMaterial::getDefault()->getMaterial()); createSharedIndex(mesh); calcVertexNormals(mesh, 0.523598775598 /*30 deg in rad*/); return GeometryTransitPtr(mesh); }
GeometryTransitPtr createPathGeometry(const std::vector<Pnt3f>& Path) { //*******************Create the Geometry for the box GeoUInt8PropertyRecPtr type = GeoUInt8Property::create(); //Volume bound box type->push_back(GL_LINE_STRIP); GeoUInt32PropertyRefPtr lens = GeoUInt32Property::create(); //Volume bound box lens->push_back(Path.size()); Color3f CoolColor(0.0f,0.0f,1.0f), HotColor(1.0f,0.0f,0.0f); GeoUInt32PropertyRefPtr index = GeoUInt32Property::create(); GeoPnt3fPropertyRefPtr points = GeoPnt3fProperty::create(); GeoVec3fPropertyRefPtr colors = GeoVec3fProperty::create(); //Volume bound box Color3f Color; Real32 t; for(UInt32 i(0) ; i<Path.size() ; ++i) { t = static_cast<Real32>(i)/static_cast<Real32>(Path.size()); Color = (t*CoolColor) + ((1.0f-t)*HotColor); index->push_back(i); points->push_back(Path[i]); colors->push_back(Color); } GeometryRecPtr PathGeo = Geometry::create(); PathGeo->setTypes (type); PathGeo->setLengths (lens); PathGeo->setIndices (index); PathGeo->setPositions (points); PathGeo->setColors (colors); //Create the material for the line LineChunkRecPtr DefaultLineChunk = LineChunk::create(); DefaultLineChunk->setWidth(2.0f); MaterialChunkRecPtr DefaultMaterialChunk = MaterialChunk::create(); DefaultMaterialChunk->setLit(false); ChunkMaterialRecPtr DefaultChunkMaterial = ChunkMaterial::create(); DefaultChunkMaterial->addChunk(DefaultMaterialChunk); DefaultChunkMaterial->addChunk(DefaultLineChunk); PathGeo->setMaterial(DefaultChunkMaterial); return GeometryTransitPtr(PathGeo); }
/** 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); }
OSG_BEGIN_NAMESPACE NodeTransitPtr OctreeVisualization::createOctreeVisualization(OctreePtr tree, Int32 MaxDepth, bool filledGeometry, bool onlyLeaf) { OTNodeGeometryCreateFunc GeoCreateFunc; OTNodeMaterialCreateFunc MatCreateFunc; OTNodeIsVisibleFunc IsVisibleFunc; IsVisibleFunc = boost::bind(&OctreeVisualization::isNodeLeaf,_1, _2, false); NodeRecPtr VisRootNode = makeCoredNode<Group>(); if(filledGeometry) { NodeRecPtr BaseBox = makeBox(1.0f,1.0f,1.0f, 1, 1, 1); GeoCreateFunc = boost::bind(&OctreeVisualization::createNodeDistanceLOD,_1, _2, _3, BaseBox.get(), 10.0f); BlendChunkRecPtr DefaultBlendChunk = BlendChunk::create(); DefaultBlendChunk->setSrcFactor(GL_SRC_ALPHA); DefaultBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); PolygonChunkRecPtr DefaultPolygonChunk = PolygonChunk::create(); DefaultPolygonChunk->setCullFace(GL_BACK); MatCreateFunc = boost::bind(&OctreeVisualization::createMatFilled,_1, _2, Color3f(0.0f,1.0f,0.0f), Color3f(1.0f,0.0f,0.0f), 0.15f, DefaultBlendChunk.get(), DefaultPolygonChunk.get()); createOctreeVisualizationRec(tree, tree->getRoot(), VisRootNode, osgMin<UInt32>(tree->getDepth(),MaxDepth), GeoCreateFunc, MatCreateFunc, IsVisibleFunc); } else { //*******************Create the Geometry for the box GeoUInt8PropertyRecPtr type = GeoUInt8Property::create(); //Volume bound box type->push_back(GL_LINE_STRIP); type->push_back(GL_LINES); GeoUInt32PropertyRefPtr lens = GeoUInt32Property::create(); //Volume bound box lens->push_back(10); lens->push_back(6); GeoUInt32PropertyRefPtr index = GeoUInt32Property::create(); //Volume bound box index->push_back(0); index->push_back(1); index->push_back(3); index->push_back(2); index->push_back(0); index->push_back(4); index->push_back(5); index->push_back(7); index->push_back(6); index->push_back(4); index->push_back(1); index->push_back(5); index->push_back(2); index->push_back(6); index->push_back(3); index->push_back(7); GeoPnt3fPropertyRefPtr highlightPoints = GeoPnt3fProperty::create(); //Volume bound box highlightPoints->push_back(Pnt3f(-1, -1, -1)); highlightPoints->push_back(Pnt3f( 1, -1, -1)); highlightPoints->push_back(Pnt3f(-1, 1, -1)); highlightPoints->push_back(Pnt3f( 1, 1, -1)); highlightPoints->push_back(Pnt3f(-1, -1, 1)); highlightPoints->push_back(Pnt3f( 1, -1, 1)); highlightPoints->push_back(Pnt3f(-1, 1, 1)); highlightPoints->push_back(Pnt3f( 1, 1, 1)); //Colors Color4f BoundBoxColor(1.0f,1.0f,1.0,1.00f); GeoVec4fPropertyRefPtr highlightColors = GeoVec4fProperty::create(); //Volume bound box highlightColors->push_back(BoundBoxColor); highlightColors->push_back(BoundBoxColor); highlightColors->push_back(BoundBoxColor); highlightColors->push_back(BoundBoxColor); highlightColors->push_back(BoundBoxColor); highlightColors->push_back(BoundBoxColor); highlightColors->push_back(BoundBoxColor); highlightColors->push_back(BoundBoxColor); GeometryRecPtr BoxGeo =Geometry::create(); BoxGeo->setTypes (type); BoxGeo->setLengths (lens); BoxGeo->setIndices (index); BoxGeo->setPositions (highlightPoints); BoxGeo->setColors (highlightColors); //Highlight Bound Box Node NodeRecPtr BaseBox = makeNodeFor(BoxGeo); GeoCreateFunc = boost::bind(&OctreeVisualization::createNodeDistanceLOD,_1, _2, _3, BaseBox.get(), 10.0f); //*******************Create the Geometry for the highlight BlendChunkRecPtr DefaultBlendChunk = BlendChunk::create(); DefaultBlendChunk->setSrcFactor(GL_SRC_ALPHA); DefaultBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); LineChunkRecPtr DefaultLineChunk = LineChunk::create(); DefaultLineChunk->setSmooth(true); MatCreateFunc = boost::bind(&OctreeVisualization::createMatLine,_1, _2, Color3f(1.0f,0.0f,0.0f), Color3f(0.0f,0.0f,1.0f), 0.55f, DefaultBlendChunk.get(), DefaultLineChunk.get()); createOctreeVisualizationRec(tree, tree->getRoot(), VisRootNode, osgMin<UInt32>(tree->getDepth(),MaxDepth), GeoCreateFunc, MatCreateFunc, IsVisibleFunc); } return NodeTransitPtr(VisRootNode); }
void VRGeometry::setType(int t) { if (!meshSet) setMesh(Geometry::create()); GeoUInt8PropertyRecPtr Type = GeoUInt8Property::create(); Type->addValue(t); setTypes(Type); }
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); }