void OgreDisplay::createCylinderMesh(std::string name, std::string material) { //make a cube mesh ManualObject* cylinderMesh = sceneMgr->createManualObject("cylinder"); cylinderMesh->begin(material, RenderOperation::OT_TRIANGLE_LIST); for(double i = 0.0; i < Math::PI * 2; i += Math::PI / 5) { cylinderMesh->position(cos(i), 1, sin(i)); cylinderMesh->textureCoord(i / (Math::PI * 2), 1.0); Ogre::Vector3 myNormal(cos(i), 0, sin(i)); myNormal.normalise(); cylinderMesh->normal(myNormal); } for(double i = 0.0; i < Math::PI * 2; i += Math::PI / 5) { cylinderMesh->position(cos(i), -1, sin(i)); cylinderMesh->textureCoord(i / (Math::PI * 2), 0.0); Ogre::Vector3 myNormal(cos(i), 0, sin(i)); myNormal.normalise(); cylinderMesh->normal(myNormal); } for(int i = 0; i < 10; i++) { cylinderMesh->triangle(i, (i+1) % 10, ((i + 10) % 20)); } for(int i = 10; i < 20; i++) { cylinderMesh->triangle(i, (i+1) % 10, (i+1) % 10 + 10); } cylinderMesh->position(0, 1, 0); cylinderMesh->textureCoord(0.5, 0.5); cylinderMesh->position(0, -1, 0); cylinderMesh->textureCoord(0.5, 0.5); for(int i = 0; i < 10; i++) { cylinderMesh->triangle(20, (i+1) % 10, i); cylinderMesh->triangle(21, ((i+10) % 10) + 10, ((i+ 11) % 10) + 10); } cylinderMesh->end(); cylinderMesh->convertToMesh(name); }
void Vertex::draw(Object const &obj) const { // Indices can be -1 to indicate that data does not exist. // We don't do anything with normals because there isn't a myNormal. if (ti_ >= 0) { myTexCoord(obj.texCoords_[ti_][0], obj.texCoords_[ti_][1]); } if (ci_ >= 0) { myColor(obj.colors_[ci_][0], obj.colors_[ci_][1], obj.colors_[ci_][2]); } if (ni_ >= 0) { myNormal(obj.normals_[ni_][0], obj.normals_[ni_][1], obj.normals_[ni_][2]); } myVertex(obj.positions_[pi_][0], obj.positions_[pi_][1], obj.positions_[pi_][2]); }
void DrawFace(TopoDS_Face face,void(*callbackfunc)(const double* x, const double* n), bool just_one_average_normal) { double x[9], n[9]; StdPrs_ToolShadedShape SST; // Get triangulation TopLoc_Location L; Handle_Poly_Triangulation facing = BRep_Tool::Triangulation(face,L); gp_Trsf tr = L; if(facing.IsNull()){ #if 0 BRepAdaptor_Surface surface(face, Standard_True); double u0 = surface.FirstUParameter(); double u1 = surface.LastUParameter(); double v0 = surface.FirstVParameter(); double v1 = surface.LastVParameter(); const int numi = 10; const int numj = 10; for(int i = 0; i<numi; i++) { for(int j = 0; j<numj; j++) { double uA = -1.2 + 2.5 *(double)i / numi; double uB = -1.2 + 2.5 *(double)(i+1) / numi; double vA = 10* (double)j / numj; double vB = 10*(double)(j+1) / numj; gp_Pnt p0, p1, p2, p3; gp_Dir n0 = GetFaceNormalAtUV(face, uA, vA, &p0); gp_Dir n1 = GetFaceNormalAtUV(face, uB, vA, &p1); gp_Dir n2 = GetFaceNormalAtUV(face, uB, vB, &p2); gp_Dir n3 = GetFaceNormalAtUV(face, uA, vB, &p3); x[0] = p0.X(); x[1] = p0.Y(); x[2] = p0.Z(); x[3] = p1.X(); x[4] = p1.Y(); x[5] = p1.Z(); x[6] = p2.X(); x[7] = p2.Y(); x[8] = p2.Z(); n[0] = n0.X(); n[1] = n0.Y(); n[2] = n0.Z(); n[3] = n1.X(); n[4] = n1.Y(); n[5] = n1.Z(); n[6] = n2.X(); n[7] = n2.Y(); n[8] = n2.Z(); (*callbackfunc)(x, n); x[0] = p0.X(); x[1] = p0.Y(); x[2] = p0.Z(); x[3] = p2.X(); x[4] = p2.Y(); x[5] = p2.Z(); x[6] = p3.X(); x[7] = p3.Y(); x[8] = p3.Z(); n[0] = n0.X(); n[1] = n0.Y(); n[2] = n0.Z(); n[3] = n2.X(); n[4] = n2.Y(); n[5] = n2.Z(); n[6] = n3.X(); n[7] = n3.Y(); n[8] = n3.Z(); (*callbackfunc)(x, n); } } #endif } else { Poly_Connect pc(facing); const TColgp_Array1OfPnt& Nodes = facing->Nodes(); const Poly_Array1OfTriangle& triangles = facing->Triangles(); TColgp_Array1OfDir myNormal(Nodes.Lower(), Nodes.Upper()); SST.Normal(face, pc, myNormal); Standard_Integer nnn = facing->NbTriangles(); // nnn : nombre de triangles Standard_Integer nt, n1, n2, n3 = 0; // nt : triangle courant // ni : sommet i du triangle courant for (nt = 1; nt <= nnn; nt++) { if (SST.Orientation(face) == TopAbs_REVERSED) // si la face est "reversed" triangles(nt).Get(n1,n3,n2); // le triangle est n1,n3,n2 else triangles(nt).Get(n1,n2,n3); // le triangle est n1,n2,n3 if (TriangleIsValid (Nodes(n1),Nodes(n2),Nodes(n3)) ) { gp_Pnt v1 = Nodes(n1).Transformed(tr); gp_Pnt v2 = Nodes(n2).Transformed(tr); gp_Pnt v3 = Nodes(n3).Transformed(tr); x[0] = v1.X(); x[1] = v1.Y(); x[2] = v1.Z(); x[3] = v2.X(); x[4] = v2.Y(); x[5] = v2.Z(); x[6] = v3.X(); x[7] = v3.Y(); x[8] = v3.Z(); if(just_one_average_normal) { gp_Vec V1(v1, v2); gp_Vec V2(v1, v3); extract((V1 ^ V2).Normalized(), n); } else { n[0] = myNormal(n1).X(); n[1] = myNormal(n1).Y(); n[2] = myNormal(n1).Z(); n[3] = myNormal(n2).X(); n[4] = myNormal(n2).Y(); n[5] = myNormal(n2).Z(); n[6] = myNormal(n3).X(); n[7] = myNormal(n3).Y(); n[8] = myNormal(n3).Z(); } (*callbackfunc)(x, n); } } } }
SALOME_WNT_EXPORT int Export( const TopoDS_Shape& theShape, const TCollection_AsciiString& theFileName, const TCollection_AsciiString& theFormatName) { MESSAGE("Export OBJ into file " << theFileName.ToCString()); std::ofstream fout(theFileName.ToCString()); Standard_Real Umin, Umax, Vmin, Vmax, dUmax, dVmax; TopExp_Explorer ExpFace; StdPrs_ToolShadedShape SST; //Triangulate BRepMesh::Mesh(theShape, DEFAULT_DEVIATION); Standard_Integer ShapeId = 1; Standard_Integer baseV = 0; Standard_Integer baseN = 0; Standard_Integer baseT = 0; for(ExpFace.Init(theShape, TopAbs_FACE); ExpFace.More(); ExpFace.Next()) { TopoDS_Face myFace = TopoDS::Face(ExpFace.Current()); TopLoc_Location aLocation; Handle(Poly_Triangulation) myT = BRep_Tool::Triangulation(myFace, aLocation); if (!myT.IsNull()) { Poly_Connect pc(myT); //write vertex buffer const TColgp_Array1OfPnt& Nodes = myT->Nodes(); for (int i = Nodes.Lower(); i <= Nodes.Upper(); i++) { gp_Pnt p = Nodes(i).Transformed(aLocation.Transformation()); fout << "v " << p.X() << " " << p.Y() << " " << p.Z() << std::endl; } fout << std::endl; //write normal buffer TColgp_Array1OfDir myNormal(Nodes.Lower(), Nodes.Upper()); SST.Normal(myFace, pc, myNormal); //myNormal.Length(); for (int i = myNormal.Lower(); i <= myNormal.Upper(); i++) { gp_Dir d = myNormal(i).Transformed(aLocation.Transformation()); fout << "vn " << d.X() << " " << d.Y() << " " << d.Z() << std::endl; } fout << std::endl; //write uvcoord buffer BRepTools::UVBounds(myFace,Umin, Umax, Vmin, Vmax); dUmax = (Umax - Umin); dVmax = (Vmax - Vmin); const TColgp_Array1OfPnt2d& UVNodes = myT->UVNodes(); for (int i = UVNodes.Lower(); i <= UVNodes.Upper(); i++) { gp_Pnt2d d = UVNodes(i); Standard_Real u = (-UOrigin+(URepeat*(d.X()-Umin))/dUmax)/ScaleU; Standard_Real v = (-VOrigin+(VRepeat*(d.Y()-Vmin))/dVmax)/ScaleV; fout << "vt " << u << " " << v << " 0" << std::endl; } fout << std::endl; //write triangle buffer if (Interface_Static::IVal("write.obj.groups")) fout << "g face_" << ShapeId++ << std::endl; Standard_Integer n1 , n2 , n3; const Poly_Array1OfTriangle& triangles = myT->Triangles(); for (int nt = 1; nt <= myT->NbTriangles(); nt++) { if (SST.Orientation(myFace) == TopAbs_REVERSED) triangles(nt).Get(n1,n3,n2); else triangles(nt).Get(n1,n2,n3); if (TriangleIsValid (Nodes(n1),Nodes(n2),Nodes(n3)) ) { fout << "f " <<n1 + baseV<<"/"<<n1 + baseT<<"/"<<n1 + baseN<<" " <<n2 + baseV<<"/"<<n2 + baseT<<"/"<<n2 + baseN<<" " <<n3 + baseV<<"/"<<n3 + baseT<<"/"<<n3 + baseN<<" " <<std::endl; } } fout << std::endl; baseV += Nodes.Length(); baseN += myNormal.Length(); baseT += UVNodes.Length(); } } fout << std::flush; fout.close(); return 1; }