eryar

PipeCAD - Plant Piping Design Software.
RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
posts - 603, comments - 590, trackbacks - 0, articles - 0

OpenCASCADE Texture Mapping

Posted on 2018-05-09 20:47 eryar 阅读(1815) 评论(0)  编辑 收藏 引用 所属分类: 2.OpenCASCADE

OpenCASCADE Texture Mapping

eryar@163.com

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

Key Words. OpenCASCADE 纹理贴图, Texture Mapping

1.Introduction

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

wps_clip_image-14862

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的含义一致了。

wps_clip_image-8831

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文件。如下图所示:

wps_clip_image-14544

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

 

wps_clip_image-9246

Figure 4. OpenCASCADE Standard Texture

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

psphere ps 100
vdisplay ps
vtexture ps d:/earth.jpg

 

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

wps_clip_image-16358

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坐标,以实现个性化的要求。

wps_clip_image-23081

wps_clip_image-28289

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


为了方便大家在移动端也能看到我的博文和讨论交流,现已注册微信公众号,欢迎大家扫描下方二维码关注。
Shing Liu(eryar@163.com)

 


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理