OpenCASCADE Texture Mapping

eryar@163.com

Abstract. 纹理贴图技术的出现和流行是图形显示技术的一个非常重要的里程碑,直接影响3D技术从工业进入娱乐领域。本文结合OpenCASCADE中纹理贴图的源码,来说明纹理贴图在OpenCASCADE中实现。

Key Words. OpenCASCADE 纹理贴图, Texture Mapping

1.Introduction

纹理贴图技术的出现和流行是图形显示技术的一个非常重要的里程碑,直接影响了3D技术从工业界进入娱乐领域。在OpenGL中面片的着色方法很有限,只能在顶点设定颜色,每个面片上像素的颜色使用各个顶点颜色的插值。这就导致了显示的图像不真实。可以想象画一个有砖墙,每个砖面都需要用一个多边形表示,砖与砖连接的水泥也要用多边形表示,并且设计者要精心摆放这些多边形,还要为不同的多边形设定各自的颜色。即使工作量如此之大,也不能更加真实地绘制出砖面上的裂纹、凹槽等更加丰富的细节。

Figure 1. Textured Box in OpenCASCADE Viewer

而有了纹理贴图一切就简单了,设计者只需要准备好一小砖面的图片,然后画一个矩形表示墙面,就可以将图片贴到矩形面上。这个砖块的图片文件称为纹理或者纹理图像。因为这个方法核心思想就是把图片和图形对应起来,所以也称为纹理映射(Texture Mapping)。

本文主要介绍OpenCASCADE中纹理映射的实现。理解了其实现原理,就可以理解其优势和局限性,在此基础上便于去扩展,实现一些个性化的功能。

2.Texture Mapping

Texture mapping is a method for defining high frequency detail, surface texture or color information on a computer-generated graphic or 3d mode. Its application to 3d graphics was pioneered by Edwin Catmull in 1974. Texture mapping originally  referred to a method that simply wrapped and mapped pixels from a texture to a 3d surface.

A texture map is an image applied(mapped) to the surface of a shape or polygon. This may be a bitmap image or procedural texture.

They may have 1~3 dimensions, although 2 dimensions are most common for visible surfaces.

以上内容来自wikipedia,原文链接:https://en.wikipedia.org/wiki/Texture_mapping

纹理映射的原理就是将二维的图片映射到三维的面上去。这个过程和参数曲面类似,就是一个二元函数,则纹理坐标就与参数曲面的参数u,v的含义一致了。

Figure 2. Applying a 2d texture to a quad

3.OpenCASCADE Texture Mapping

OpenCASCADE中提供类AIS_TexturedShape来实现带纹理的模型,通过构造函数来设置模型,通过函数SetTextureFileName()来指定纹理贴图。当纹理贴图的文件名是个数字时,则使用内置的贴图文件,也就是如下enum定义的标准纹理贴图:

//! Types of standard textures.

enum Graphic3d_NameOfTexture2D

{

Graphic3d_NOT_2D_MATRA,

Graphic3d_NOT_2D_ALIENSKIN,

Graphic3d_NOT_2D_BLUE_ROCK,

Graphic3d_NOT_2D_BLUEWHITE_PAPER,

Graphic3d_NOT_2D_BRUSHED,

Graphic3d_NOT_2D_BUBBLES,

Graphic3d_NOT_2D_BUMP,

Graphic3d_NOT_2D_CAST,

Graphic3d_NOT_2D_CHIPBD,

Graphic3d_NOT_2D_CLOUDS,

Graphic3d_NOT_2D_FLESH,

Graphic3d_NOT_2D_FLOOR,

Graphic3d_NOT_2D_GALVNISD,

Graphic3d_NOT_2D_GRASS,

Graphic3d_NOT_2D_ALUMINUM,

Graphic3d_NOT_2D_ROCK,

Graphic3d_NOT_2D_KNURL,

Graphic3d_NOT_2D_MAPLE,

Graphic3d_NOT_2D_MARBLE,

Graphic3d_NOT_2D_MOTTLED,

Graphic3d_NOT_2D_RAIN,

Graphic3d_NOT_2D_CHESS,

Graphic3d_NOT_2D_UNKNOWN

};

这些enum变量分别对应环境变量CSF_MDTVTexturesDirectory文件夹中的那些以2d开头的rgb文件。如下图所示:

Figure 3. OpenCASCADE Standard Textures

在类AIS_TexturedShape的函数Compute()中通过类StdPrs_ShadedShape来计算纹理坐标UV,相关代码如下:

StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,

Standard_True,

myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),

myUVRepeat,

myToScale        ? myUVScale  : gp_Pnt2d (1.0, 1.0));

updateAttributes (thePrs);

在类StdPrs_ShadedShape中有个静态函数fillTriangles()来生成显示数据,其中计算纹理坐标相关的代码列出如下所示:

if (theHasTexels)

{

BRepTools::UVBounds (aFace, aUmin, aUmax, aVmin, aVmax);

dUmax = (aUmax - aUmin);

dVmax = (aVmax - aVmin);

}

const Standard_Integer aDecal = anArray->VertexNumber();

for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)

{

aPoint = aNodes (aNodeIter);

const Standard_Integer anId = 3 * (aNodeIter - aNodes.Lower());

gp_Dir aNorm (aNormArr[anId + 0], aNormArr[anId + 1], aNormArr[anId + 2]);

if (aFace.Orientation() == TopAbs_REVERSED)

{

aNorm.Reverse();

}

if (!aLoc.IsIdentity())

{

aPoint.Transform (aTrsf);

aNorm.Transform (aTrsf);

}

if (theHasTexels && aUVNodes.Upper() == aNodes.Upper())

{

const gp_Pnt2d aTexel = gp_Pnt2d ((-theUVOrigin.X() + (theUVRepeat.X() * (aUVNodes (aNodeIter).X() - aUmin)) / dUmax) / theUVScale.X(),

(-theUVOrigin.Y() + (theUVRepeat.Y() * (aUVNodes (aNodeIter).Y() - aVmin)) / dVmax) / theUVScale.Y());

anArray->AddVertex (aPoint, aNorm, aTexel);

}

else

{

anArray->AddVertex (aPoint, aNorm);

}

}

从上述代码中可以看到,OpenCASCADE中计算纹理UV坐标的方法就是对Shape的每个面,取出其几何参数曲面,并将曲面的参数空间单位化后作为纹理坐标。

4.Draw Test Harness

OpenCASCADE在Draw Test Harness中提供了命令vtexture来生成纹理贴图的模型。如本文开始的那个地板贴图的长方体可以用如下命令来实现:

box b 1 2 3

vdisplay b

vtexture b 11

Figure 4. OpenCASCADE Standard Texture

当贴图使用数字时,使用的是opencascade内置的贴图文件。也可以指定自定义的贴图文件,如下所示:

psphere ps 100

vdisplay ps

vtexture ps d:/earth.jpg

其中d:/earth.jpg为D盘的一个贴图文件。

Figure 5. Map world texture to Sphere

5.Conclusion

OpenGL的纹理贴图可以理解为参数曲面的参数空间,即是一个二维UV空间,通过参数UV可以计算出曲面上对应的三维点,即将二维的贴图映射到三维的曲面上去。

OpenCASCADE中纹理贴图使用类AIS_TexturedShape,其对纹理坐标UV的计算使用类StdPrs_ShadedShape,就是将Shape中每个面对应的几何参数曲面的参数空间单位化后作为纹理坐标。

在Draw Test Harness中有命令vtexture来生成纹理贴图。当纹理贴图文件是数字时,会使用OpenCASCADE内置的贴图文件,也可以指定自定义的贴图文件。

在OpenCASCADE的官网论坛上有个关于纹理贴图的问题:

https://www.opencascade.com/comment/20458 将其中KGV的答复摘出如下:

You shape consists of 3 Faces (see attached screenshot from CAD Assistant).

Standard presentation builder StdPrs_ShadedShape (used by AIS_TexturedShape) defines texture mapping UV coordinates from Surface parametric space individually for each Face.

To achieve desired result, either you have to create a single Face instead of 3 Faces in this specific place (I don't know if you are going to texture other shapes and in which way),

or compute UV texture coordinates manually using some alternative mapping logic (like projecting triangle nodes onto plane).

In general case (complex shape definition), you might need creating a mesh Unfolding to be able generating a texture coordinates considering seam edges:

https://www.opencascade.com/content/unfolding-library

There are also visualization-only solutions for generating texture UV coordinates on GPU, depending on what you are trying to achieve

(Graphic3d_Texture2Dplane/Graphic3d_TextureEnv, though these are rarely used due to limited use cases where such mapping makes sense).

其中也提到OpenCASCADE中纹理坐标的生成方式。

其实纹理坐标UV的处理是三维软件的一个功能,像Blender中就专门有UV Editing来编辑UV坐标,以实现个性化的要求。

Figure 7. UV Editing in Blender

6.References

1. https://www.opencascade.com/comment/20458

2. https://www.bilibili.com/video/av18054281?from=search&seid=231681022662274909

3. https://en.wikipedia.org/wiki/World_map

4. OpenGL Programming Guide

5. OpenGL Super Bible

OpenCASCADE Texture Mapping的更多相关文章

  1. 投影纹理映射(Projective Texture Mapping)

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人”  投影纹理映射( Projective ...

  2. OpenGL 4.0 GLSL 实现 投影纹理映射(Projective Texture Mapping) (转)

    http://blog.csdn.net/zhuyingqingfen/article/details/19331721   分类: GLSL  投影纹理映射 (projective texture ...

  3. 投影纹理映射(Projective Texture Mapping) 【转】

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人”  投影纹理映射( Projective ...

  4. Method for sub-pixel texture mapping and filtering

    BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention relates to a method for ...

  5. Projective Texture Mapping - 投影纹理

    昨天导师让写一个投影纹理,将一个相机渲染的图片的一部分投影到另外一个相机里面,目的是无缝的拼接. 投影纹理就和shadow map一样,都是将片元转换到另外一个相机/光源坐标系下,投影后找到对应的纹素 ...

  6. Direct3D 11 Tutorial 7:Texture Mapping and Constant Buffers_Direct3D 11 教程7:纹理映射和常量缓冲区

    概述 在上一个教程中,我们为项目引入了照明. 现在我们将通过向我们的立方体添加纹理来构建它. 此外,我们将介绍常量缓冲区的概念,并解释如何使用缓冲区通过最小化带宽使用来加速处理. 本教程的目的是修改中 ...

  7. Texture tiling and swizzling

    Texture tiling and swizzling 原帖地址:http://fgiesen.wordpress.com If you’re working with images in your ...

  8. OpenGL阴影,Shadow Mapping(附源程序)

    实验平台:Win7,VS2010 先上结果截图(文章最后下载程序,解压后直接运行BIN文件夹下的EXE程序): 本文描述图形学的两个最常用的阴影技术之一,Shadow Mapping方法(另一种是Sh ...

  9. anisotropy texture filtering

    http://www.extremetech.com/computing/51994-the-naked-truth-about-anisotropic-filtering 1. 为什么在纹理采样时需 ...

随机推荐

  1. Linux进程模型

    ----原文链接:http://www.cnblogs.com/biyeymyhjob/archive/2012/08/01/2617884.html------ Linux进程通过一个task_st ...

  2. Android开发艺术探索——新的征程,程序人生路漫漫!

    Android开发艺术探索--新的征程,程序人生路漫漫! 偶尔写点东西分享,但是我还是比较喜欢写笔记,看书,群英传看完了,是学到了点东西,开始看这本更加深入Android的书籍了,不知道适不适合自己, ...

  3. Hive 配置

    <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...

  4. 《java入门第一季》之面向对象(如何使用帮助文档)

    1:打开帮助文档 2:点击显示,找到索引,看到输入框 3:知道你要找谁?以Scanner举例 4:在输入框里面输入Scanner,然后回车 5:看包 java.lang包下的类不需要导入包,其他的全部 ...

  5. Erlang Rebar 使用指南之四:依赖管理

    Erlang Rebar 使用指南之四:依赖管理 全文目录: https://github.com/rebar/rebar/wiki 本章链接: https://github.com/rebar/re ...

  6. Concurrent包常用方法简介

    1 Executor接口 留给开发者自己实现的接口,一般情况下不需要再去实现.它只有一个方法 void execute(Runnable command) 2 ExecutorService接口 它继 ...

  7. Gradle 1.12用户指南翻译——第三十六章. Sonar Runner 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  8. Linux进程管理 - PRI,nice,free,uname,netstat

    优先运行序 (priority, PRI) 这个 PRI 值越低代表越优先的意思.不过这个 PRI 值是由核心动态调整的, 使用者无法直接调整 PRI 值的. 由於 PRI 是核心动态调整的,我们使用 ...

  9. 结合FireBreath在Chrome/FireFox的多进程模式下崩溃一例

    FireBreath是跨浏览器跨操作系统的插件方案,它封装了ActiveX和NPAPI的插件接口,使用统一的API来暴露JSAPI.Chrome和FireFox使用NPAPI,IE使用ActiveX. ...

  10. Android使用统计图AChartEngine 来展示数据

    本文采用的统计图参考:AChartEngine 访问地址 :http://code.google.com/p/achartengine/ 先给出效果图 本文的开发代码主要是这些:对一些代码进行修改 以 ...