前言

因为项目需要,所以最近研究了下给 用OpenCasCade库创建的模型贴纹理的问题。因为网上没找到相关的文章,特此记录下来。

官方示例

本文中的代码是完全仿照官方示例(All-vc10 中的 Viewer3d)所写的,筒子们可以自己去研究一下这块代码,如图:

然后看下官方示例的贴纹理效果:

代码解读

这里为简单起见,只解读图一中 Bottle 的代码, 代码的作用,请仔细阅读注释

sampleBottle函数(创建Bottle,调用Texturize函数):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

//通过载入brep文件,创建bottle,并贴纹理

void TexturesExt_Presentation::sampleBottle()

{

//创建一个bottle

TopoDS_Shape aShape;

if (!loadShape(aShape, "bottle.brep"))

return;

// resize and move the shape to the center of the viewer  这块代码对纹理显示无贡献

gp_Trsf aTrsf1, aTrsf2;

aTrsf1.SetScale(gp_Pnt(0,0,0), /*0.8*/0.1);    //对 bottle 进行缩放

aTrsf2.SetTranslation(gp_Pnt(0,0,0),gp_Pnt(0,0,-20));

aTrsf1.Multiply(aTrsf2);

BRepBuilderAPI_Transform Transformer(aTrsf1);

Transformer.Perform(aShape);

aShape = Transformer.Shape();

TopTools_IndexedMapOfShape aFaces;

// 函数原型:static void TopExp::MapShapes(const TopoDS_Shape& S, const TopAbs_ShapeEnum T, TopTools_IndexedMapOfShape& M)

// 第二个参数: TopAbs_ShapeEnum 是一个枚举类型,读者需要自己去读一下

// Tool to explore a topological data structure. Stores in the map <M> all the sub-shapes of <S> of type <T>.

TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);

//没有这几句代码 则只显示指定贴了纹理的那些FACE,不显示瓶子整体。但同样,这段对纹理的显示没有任何贡献。

//display original shape in shaded display mode

Handle_AIS_Shape aShapeIO = drawShape(aShape, Graphic3d_NOM_BRASS, Standard_False);

getAISContext()->SetDisplayMode(aShapeIO, AIS_Shaded, Standard_False);

// Set increased polygon offset for the main shape to avoid depth collision with textured faces

aShapeIO->SetPolygonOffsets(Aspect_POM_Fill, 1.5, 0.5);

DISP(aShapeIO);    // #define DISP(OBJ) getAISContext()->Display((OBJ), Standard_False)

//指定需要贴纹理的FACE,设置相关参数

Handle_AIS_TexturedShape aTFace1 = Texturize(aFaces(16), "carrelage1.gif", 1, 1, 3, 2);

DISP(aTFace1);

Handle_AIS_TexturedShape aTFace2 = Texturize(aFaces(21), "carrelage1.gif", 1, 1, 3, 2);

DISP(aTFace2);

//更新视图

getViewer()->Update();

}

Texturize函数(贴纹理):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

Handle_AIS_TexturedShape TexturesExt_Presentation::Texturize(const TopoDS_Shape& aShape,

TCollection_AsciiString aTFileName,

Standard_Real toScaleU,

Standard_Real toScaleV,

Standard_Real toRepeatU,

Standard_Real toRepeatV,

Standard_Real originU,

Standard_Real originV)

{

//create a textured presentation object for aShape

//AIS_TexturedShape 是一个非常重要的类!下面是官方文档中的说明

//This class allows to map textures on shapes. Presentations modes AIS_WireFrame (0) and AIS_Shaded (1) behave in the same manner as in AIS_Shape

//whilst new modes 2 (bounding box) and 3 (texture mapping) extends it functionality.

//The texture itself is parametrized in (0,1)x(0,1). Each face of a shape located in UV space is provided with these parameters:

//•Umin - starting position in U

//•Umax - ending position in U

//•Vmin - starting position in V

//•Vmax - ending position in V

//Each face is triangulated and a texel is assigned to each node. Facets are then filled using a linear interpolation of texture between each 'three texels'.

//User can act on:

//•the number of occurrences of the texture on the face

//•the position of the origin of the texture

//•the scale factor of the texture

Handle_AIS_TexturedShape aTShape = new AIS_TexturedShape(aShape);

TCollection_AsciiString TFileName;

//load texture from file if it is not an integer value

//integer value indicates a number of texture in predefined TexturesExt enumeration

CString initfile(((OCC_App*) AfxGetApp())->GetInitDataDir());

initfile += "\\Data\\";

if (!aTFileName.IsIntegerValue())

{

initfile += aTFileName.ToCString();

}

aTShape->SetTextureFileName((Standard_CString)(LPCTSTR)initfile);

// do other initialization of AIS_TexturedShape

aTShape->SetTextureMapOn();

aTShape->SetTextureScale(Standard_True, toScaleU, toScaleV);

aTShape->SetTextureRepeat(Standard_True, toRepeatU, toRepeatV);

aTShape->SetTextureOrigin(Standard_True, originU, originV);

aTShape->SetDisplayMode(3); // mode 3 is "textured" mode,参见上面AIS_TexturedShape类的解释

return aTShape;

}

至此,就可以实现上图中显示的Bottle效果。同学们可以自己创建一个简单的 box 来尝试一下,这里我给个简单的示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

void CneedAVView::MakeBottle()

{

TopoDS_Shape aRes = BRepPrimAPI_MakeBox(gp_Pnt(0,0,0),555,555,555).Shape();

TopTools_IndexedMapOfShape aFaces;

TopExp::MapShapes(aRes, TopAbs_FACE, aFaces);

Handle(AIS_TexturedShape) aTFace1 = Texturize(aFaces(1), "./carrelage1.gif", 1, 1, 3, 2);

DISP(aTFace1);

Handle(AIS_TexturedShape) aTFace2 = Texturize(aFaces(2), "./carrelage1.gif", 1, 1, 3, 2);  //16 21 3 2

DISP(aTFace2);

Handle(AIS_TexturedShape) aTFace5 = Texturize(aFaces(5), "./carrelage1.gif", 1, 1, 3, 2);

DISP(aTFace5);

myView->Update();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

Handle(AIS_TexturedShape) CneedAVView::Texturize(const TopoDS_Shape& aShape,

TCollection_AsciiString aTFileName,

Standard_Real toScaleU,

Standard_Real toScaleV,

Standard_Real toRepeatU,

Standard_Real toRepeatV,

Standard_Real originU,

Standard_Real originV)

{

// create a textured presentation object for aShape

Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape);

//此地纯属偷懒

CString temp("d://program files//opencascade-6.7.1//samples//mfc//standard//04_viewer3d//data//cookerplate.gif");

aTShape->SetTextureFileName((Standard_CString)(LPCTSTR)temp);

// do other initialization of AIS_TexturedShape

aTShape->SetTextureMapOn();

aTShape->SetTextureScale(Standard_True, toScaleU, toScaleV);

aTShape->SetTextureRepeat(Standard_True, toRepeatU, toRepeatV);

aTShape->SetTextureOrigin(Standard_True, originU, originV);

aTShape->SetDisplayMode(3); // mode 3 is "textured" mode

return aTShape;

}

也许你发现这样还是看不到显示的纹理!

后来发现,需要设置这样关键的一句

1

myView->SetSurfaceDetail(V3d_TEX_ALL);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

函数原型:void V3d_View::SetSurfaceDetail(const V3d_TypeOfSurfaceDetail SurfaceDetail)

功    能:select the kind of rendering for texture mapping no texture mapping by default

其中 V3d_TypeOfSurfaceDetail 是一个枚举类

enum V3d_TypeOfSurfaceDetail

Modes of visualization for objects in a view.

·V3d_TEX_NONE: no texture mapping,

·V3d_TEX_ENVIRONMENT: environment mapping only,

·V3d_TEX_ALL: environment and texture mapping.

Enumerator:

·V3d_TEX_NONE

·V3d_TEX_ENVIRONMENT

·V3d_TEX_ALL

如官方文档中所述:

1

no texture mapping by default

所以我们要显示的把参数设置为:V3d_TEX_ALL

如此,你就可以看到自己贴的纹理了,效果图如下(因为我省略了一些函数所以这里只显示了贴纹理的面(见 sampleBottle 函数中所述)):

至此,OpenCasCade的中的贴纹理就搞定了,想了解更多,请自行阅读官方示例。

感谢原作者的知识分享。

转自按地址:http://www.zyh1690.org/opencascade-texture/

OpenCasCade – 贴纹理相关推荐

  1. OpenCASCADE Texture Mapping

    OpenCASCADE Texture Mapping eryar@163.com Abstract. 纹理贴图技术的出现和流行是图形显示技术的一个非常重要的里程碑,直接影响3D技术从工业进入娱乐领域 ...

  2. OpenCASCADE绘制测试线束:拓扑命令之纹理映射到形状

    OpenCASCADE绘制测试线束:拓扑命令之纹理映射到形状 纹理映射到形状 vtexture vtexscale vtexorigin vtexrepeat vtexdefault 纹理映射到形状 ...

  3. OpenCASCADE可视化:3D演示之图形基元

    OpenCASCADE可视化:3D演示之图形基元 结构层次 图形基元 原始数组 文本原语 材料 纹理 自定义着色器 所述Graphic3d包用于在3D查看器来创建3D图形对象.这些称为结构的对象由一组 ...

  4. Opencascade可视化--视图渲染流程分析

    一.概述 视图V3d_View创建成功,接下来就是三维场景渲染.在原生窗口的绘制时间中,调用V3d_View::Redraw()方法,实现三维场景渲染. void SinnDockView::pain ...

  5. OpenCasCade 简介

    Open CASCADE是一套开放原始码的CAD/CAM/CAE几何模型核心,源自于法国的Matra Datavision公司,这一套函式库系统原来是著名的CADCAM软体EUCLID的开发平台,但是 ...

  6. opencascade造型引擎功能介绍

    opencascade造型引擎功能介绍 现今的CAD 系统大多通常都基于CAD 系统提供的二次开发包,用户根据要求定制符合自己要求的功能.AutoCAD就提供了AutoLISP.ADS 等都是比较通用 ...

  7. CUDA C 纹理提取Texture Fetching

    CUDA C 纹理提取Texture Fetching 一.参数曲面的纹理 使用纹理指定参数曲面属性. 二.CUDA C 纹理获取开发 用于计算纹理函数,根据纹理引用的各种属性返回的值的公式(请参见纹 ...

  8. 虚拟纹理与几何图像技术

    虚拟纹理与几何图像技术 一. 基本图形学概念 图1. 几何与纹理. 曲面一般表示成三角网格和纹理图像,三角网格表示曲面的几何拓扑信息,纹理图像给出曲面的颜色材质等信息.将三角网格映射到平面区域的过程被 ...

  9. 基于颜色特征,形状特征和纹理特征的数字图像的检索(Digital Image Retrieval)MATLAB GUI实现

    ** 数字图像的检索 下载地址:代码.数据集下载地址 如需论文请联系:hqucuihao@163.com ** 1. 摘要 随着互联网发展的日新月异,人们对于信息的需求不再是简单的文字,大量的图像.语 ...

  10. 基于颜色特征,形状特征和纹理特征的数字图像的检索(Digital Image Retrieval)MATLAB GUI实现(本科毕业设计)

    该程序实现的功能为:检索出指定的图像文件,并从检索出的图像中检索出指定的物体 . 1,主程序为Recognition和Recognition. 2.颜色特征,形状特征,纹理特征为对应的各子程序. 3. ...

最新文章

  1. Path Sum II leetcode java
  2. VTK:几何对象演示用法实战
  3. php-5.6.26源代码 - opcode处理器,“函数调用opcode”处理器,如何调用扩展模块的函数...
  4. Vue -项目创建(rem适配项的设置)
  5. 配置了坐标还是找不到serv_你那么努力,为何还是找不到工作?从优势发展观来看个人职业发展...
  6. css:before和after中的content属性
  7. 高频Linux命令小结(新手向)
  8. Padrino 生成器指南
  9. 大数据城市规划 杨东_空头转多!前期大比例减仓的私募,目前开始加仓
  10. Amlogic_Android7.1 HDMI显示流程源码分析
  11. MSSQL 构建函数提取指定的字符
  12. Windows 注册表清理
  13. CAM350 V14.6 检查gerber文件
  14. mysql怎么创建blog_「MySQL创建与删除数据库」- 海风纷飞Blog
  15. html.ex.day02
  16. PYTHON MIP 算法实现
  17. 实战三:根据父母的身高预测儿子的身高
  18. SpringCloud2020学习笔记13——SpringCloud Stream消息驱动
  19. 结构光相机国产、非国产统计参数对比分析
  20. (附源码)ssm大学校园慈善拍卖网站的设计与实现 毕业设计250910

热门文章

  1. kido机器人用流量吗_海底捞、呷哺、巴奴都在用!送餐机器人会成为“火锅标配”吗?...
  2. 电脑运行很慢怎么办_为什么电脑用久了,就算重新安装系统也会变得很慢?
  3. C语言编程QQ管理系统,c语言制作学生管理系统srrpqq67.doc
  4. mysql hy000 死锁_mysql 数据库死锁-解决
  5. wordprss只显示一个当前主题 问题的解决
  6. Android Button设置
  7. Docker从理论到实践(一)------基础知识必备
  8. dart安装:sdk下载地址( 2.4.0)
  9. vue + element ui 阻止表单输入框回车刷新页面
  10. C 语言调用python 脚本函数