OpenCASCADE Texture Mapping

OpenCASCADE Texture Mapping

[email protected]

Abstract. The emergence and popularity of texture mapping technology is a very important milestone in graphics display technology, which directly affects 3D technology from industry to entertainment. This article combines the source code of texture maps in OpenCASCADE to illustrate the implementation of texture maps in OpenCASCADE.

Key Words. OpenCASCADE Texture Map, Texture Mapping

1.Introduction

The emergence and popularity of texture mapping technology is a very important milestone in graphics display technology, which directly affects 3D technology from industry to entertainment. The coloring methods of patches in OpenGL are very limited, and only colors can be set at the vertices, and the color of the pixels on each patch uses the interpolation of the color of each vertex. This results in an unrealistic image displayed. It is conceivable to draw a brick wall, each brick surface needs to be represented by a polygon, and the cement connecting the bricks should also be represented by polygons, and the designer must carefully place these polygons, and set different polygons for different polygons. s color. Even with such a large amount of work, the cracks, grooves and other richer details on the brick surface cannot be drawn more realistically.

wps_clip_image-14862

Figure 1. Textured Box in OpenCASCADE Viewer

With texture mapping, everything is simple. The designer only needs to prepare a picture of a small brick surface, then draw a rectangle to represent the wall, and then paste the picture on the rectangular surface. The image file of this brick is called a texture or texture image. Because the core idea of ​​this method is to correspond images and graphics, it is also called texture mapping (Texture Mapping).

This article mainly introduces the implementation of texture mapping in OpenCASCADE. After understanding its implementation principle, you can understand its advantages and limitations, and on this basis, it is easy to expand and realize some personalized functions.

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.

The above content comes from wikipedia, the original link: https://en.wikipedia.org/wiki/Texture_mapping

The principle of texture mapping is to map a two-dimensional image to a three-dimensional surface. This process is similar to the parametric surface, which is a binary function, and the texture coordinates have the same meaning as the parameters u and v of the parametric surface.

wps_clip_image-8831

Figure 2. Applying a 2d texture to a quad

3.OpenCASCADE Texture Mapping

The class AIS_TexturedShape is provided in OpenCASCADE to implement the textured model, the model is set through the constructor, and the texture map is specified through the function SetTextureFileName(). When the file name of the texture map is a number, the built-in map file is used, which is the standard texture map defined by the following 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

};

These enum variables correspond to those rgb files starting with 2d in the environment variable CSF_MDTVTexturesDirectory folder respectively. As shown below:

wps_clip_image-14544

Figure 3. OpenCASCADE Standard Textures

In the function Compute() of the class AIS_TexturedShape, the texture coordinate UV is calculated by the class StdPrs_ShadedShape. The relevant code is as follows:

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);

There is a static function fillTriangles() in the class StdPrs_ShadedShape to generate display data, and the code related to calculating texture coordinates is listed as follows:

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);

}

}

As can be seen from the above code, the method of calculating texture UV coordinates in OpenCASCADE is to take out the geometric parameter surface of each face of the Shape, and normalize the parameter space of the surface as texture coordinates.

4.Draw Test Harness

OpenCASCADE provides the command vtexture in Draw Test Harness to generate texture-mapped models. The cuboid of the floor texture at the beginning of this article can be implemented with the following command:

box b 1 2 3

vdisplay b

vtexture b 11

wps_clip_image-9246

Figure 4. OpenCASCADE Standard Texture

When the texture uses numbers, the texture files built into opencascade are used. It is also possible to specify a custom map file as follows:

psphere ps 100

vdisplay ps

vtexture ps d:/earth.jpg

Among them, d:/earth.jpg is a texture file of D drive.

wps_clip_image-16358

Figure 5. Map world texture to Sphere

5.Conclusion

The texture map of OpenGL can be understood as the parameter space of the parametric surface, that is, a two-dimensional UV space. Through the parameter UV, the corresponding three-dimensional point on the surface can be calculated, that is, the two-dimensional texture is mapped to the three-dimensional surface.

The texture map in OpenCASCADE uses the class AIS_TexturedShape, which uses the class StdPrs_ShadedShape for the calculation of the texture coordinate UV, that is, the parameter space of the geometric parameter surface corresponding to each face in the Shape is normalized as the texture coordinate.

In Draw Test Harness there is the command vtexture to generate texture maps. When the texture map file is digital, the built-in map file of OpenCASCADE is used, or a custom map file can be specified.

There is a question about texture maps on OpenCASCADE's official website forum:

https://www.opencascade.com/comment/20458 extracts the reply of KGV as follows:

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).

It also mentions how texture coordinates are generated in OpenCASCADE.

In fact, the processing of texture coordinates UV is a function of 3D software. For example, there is UV Editing in Blender to edit UV coordinates to achieve personalized requirements.

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326592619&siteId=291194637