其实这个功能在OCC自带的例子view3d中就有,不过这个例子是MFC例子,参考这个例子的条件下,整理出一个QT版本的贴图例子,参考效果如下:
案例如下:
//
申明:
Handle(AIS_Shape) Texturize(
const TopoDS_Shape& aShape, Standard_CString aTFileName,
Standard_Real toScaleU = 1.0, Standard_Real toScaleV = 1.0,
Standard_Real toRepeatU = 1.0, Standard_Real toRepeatV = 1.0,
Standard_Real originU = 0.0, Standard_Real originV = 0.0);
实现:
Handle(AIS_Shape) occQt::Texturize(const TopoDS_Shape& aShape,
Standard_CString aTFileName,
Standard_Real toScaleU,
Standard_Real toScaleV,
Standard_Real toRepeatU,
Standard_Real toRepeatV,
Standard_Real originU,
Standard_Real originV)
{
Handle(AIS_Shape) aTShape = new AIS_Shape(aShape);
if (!aTShape->Attributes()->HasOwnShadingAspect())
{
aTShape->Attributes()->SetShadingAspect(new Prs3d_ShadingAspect());
}
aTShape->Attributes()->ShadingAspect()->Aspect()->SetTextureMap(new Graphic3d_Texture2Dmanual(TCollection_AsciiString((Standard_CString)(LPCTSTR)aTFileName)));
aTShape->Attributes()->ShadingAspect()->Aspect()->SetTextureMapOn();
// do other initialization of AIS_Shape
aTShape->SetTextureScaleUV(gp_Pnt2d(toScaleU, toScaleV));
aTShape->SetTextureRepeatUV(gp_Pnt2d(toRepeatU, toRepeatV));
aTShape->SetTextureOriginUV(gp_Pnt2d(originU, originV));
aTShape->SetDisplayMode(AIS_Shaded);
return aTShape;
}
案例:
void occQt::test()
{
//测试纹理贴图 多文档
if (QMdiSubWindow *activeSubWindow = ui.mdiArea->activeSubWindow())
{
Mdi *formDoc = qobject_cast<Mdi *>(activeSubWindow->widget());
TopoDS_Shape aShape = formDoc->shape1;
TopTools_IndexedMapOfShape aFaces;
TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
// display original shape in shaded display mode
Handle(AIS_Shape) aShapeIO = formDoc->anshape1;
// Set increased polygon offset for the main shape to avoid depth collision with textured faces
aShapeIO->SetPolygonOffsets(Aspect_POM_Fill, 1.5, 0.5);
Handle(AIS_Shape) aTFace1 = Texturize(aShape, "carrelage1.gif", 1, 1, 3, 2);//第一个参数可以是topdds_face topds_shape
formDoc->myOccView->getContext()->Display(aTFace1, Standard_False);
formDoc->myOccView->myView->Update();
}
}
/