# Texturing and Solids

### Contents

1. Introduction
2. Surface Parametrization
3. Bump Mapping
4. Geometry Displacement
5. Environmental Mapping
6. Procedural Textures

# Introduction

## What is Texture ?

Texture is the perceived surface quality. In CG texture is a 2D multi-channel image overlaid on the surface of a 3D solid to give it color or illusion of relief. Being the property of the geometry, textures play essential role in modeling BRDFs and thus they are managed entirely by the shaders. The quality of a textured surface is determined by the number of pixels per texture element (textel).

in OpenRT, textures are described by class rt::CTexture : public cv::Mat

# Introduction

## What is Texture ?

### Image Texture

Image texture is a bitmap image usually stored in a common image file format. The quality of textured surface is determined by the number of pixels per telex. The resolution of image texture play a major role, which affects the overall impression of the quality of graphics in 3D-application.

# Introduction

## What is Texture ?

### Procedural Texture

Procedural texture is created using a mathematical description (i.e. an algorithm) rather than directly stored data. The advantage of this approach is low storage cost, unlimited texture resolution and easy texture mapping. Procedural textures are used to model surface of natural elements such as clouds, marble, granite, metal, stone, and others.

# Introduction

## Texture-Modulated Quantities

### Radiance at a point $$\vec{p}$$

• Color (RGB)
• Diffuse coefficient $$k_d$$
• Specular coefficient $$k_s$$
• Opacity $$\alpha$$

### Deviation of normal vector at a point $$\vec{p}$$

Bump mapping or Normal mapping

• If vectors $$\vec{a}$$ and $$\vec{b}$$ are tangent to the surface at point $$\vec{p}$$ and together with normal $$\vec{n}$$ form an orthogonal basis, then deviate the normal by $$(\Delta_1, \Delta_2)$$ from the texture:  $$\vec{n} = \vec{n} + \Delta_1\cdot\vec{a} + \Delta_2\cdot\vec{b}$$

### Geometry Displacement

Displacement mapping

• Displace every point $$\vec{p}$$ along its normal $$\vec{n}$$ by the value of $$\Delta$$ given in the texture:
$$\vec{p} = \vec{p} + \Delta\cdot\vec{n}$$

### Distant Illumination

Environmental mapping or Reflection mapping

(r,g,b,k_d,k_s,\alpha)\in CV\_8UC6
(\Delta_1, \Delta_2)\in CV\_8UC2
\Delta\in CV\_8UC1

# Introduction

Solid is a rigid geometrical construction which consists out of multiple primitives, usually triangles. Primitives are combined into solids in order to make application of geometrical transformations and texturing easier.

in OpenRT, solids are described by class rt::CSolid

# Surface Parametrization

The bidirectional reflectance distribution function:

$$f_r(\vec{p},\omega_o,\omega_i) = \frac{dL_o(\vec{p},\omega_o)}{dE(\vec{p},\omega_i)},$$

where $$\vec{p}\equiv\vec{p}(x, y, z)$$ is a 3 dimentional point on solid's surface

## From 3D hit-point to 2D texture coordinates

Texture is about modeling point - dependent characteristics of BRDF

### Parametrization

To map a two-dimensional texture to three-dimensional surface for every 3D point $$\vec{p}(x,y,z)$$ a corresponding 2D point $$\vec{q}(u,v)$$ in texture must be found.

$$f:(x,y,z)\rightarrow (u,v),$$

where usually

$$u,v\in[0; 1]$$

# Surface Parametrization

## From 3D hit-point to 2D texture coordinates

### Parametrization

To map a two-dimensional texture to three-dimensional surface for every 3D point $$\vec{p}(x,y,z)$$ a corresponding 2D point $$\vec{q}(u,v)$$ in texture must be found.

$$f:(x,y,z)\rightarrow (u,v),$$

where usually

$$u,v\in[0; 1]$$

When ray - primitive intersection is found, the hit-point $$\vec{p} = \vec{o} + t\cdot\vec{d}$$ is encoded into the ray structure, thus parametrization can be implemented for every primitive in a method

Vec2f rt::IPrim::getTextureCoords(const Ray& ray) const = 0;

# Surface Parametrization

## Primitive: Sphere

\vec{p}(x,y,z)
\theta
\varphi

### Cartesian to Spherical

$$r=\sqrt{x^2+y^2+z^2}$$

$$\varphi=\arctan_2\frac{y}{x}$$

$$\theta=\arccos\frac{z}{r}$$

(read more about $$\arctan_2$$ function)

y
z
x

### Spherical to Texture

Since $$\varphi\in[-\pi; \pi]$$ and $$\theta\in[0;\pi]$$

$$u=\frac{\pi+\varphi}{2\pi}$$

$$v=\frac{\theta}{\pi}$$

Vec2f CPrimSphere::getTextureCoords(const Ray& ray) const
{
Vec3f hitPoint = ray.hitPoint() - m_origin;
float theta = acosf(hitPoint.val / m_radius);	  // [0; Pif]

float phi = atan2(hitPoint.val, hitPoint.val);  // [-Pif; Pif]
if (isnan(phi)) phi = 0;

return Vec2f((Pif + phi) / (2 * Pif), theta / Pif);
}

# Surface Parametrization

## Primitive: Triangle

### Möller-Trumbore intersection algorithm

in addition to intersection distance $$t$$ implicitly calculates the barycentric intersection coordinates $$(u,v)\in[0; 1]$$, which may be used directly as the texel coordinates

1. On construction provide additionally texture coordinates for all 3 triangle vertices
2. Store barycentric coordinates in the ray structure together with $$t$$ value
Vec2f CPrimTriangle::getTextureCoords(const Ray& ray) const {
return (1.0f - ray.u - ray.v) * m_ta
+ ray.u * m_tb
+ ray.v * m_tc;
}
\vec{a}(u_a,v_a)
\vec{p}(u,v)
\vec{o}
struct Ray {
Vec3f        org;	// Ray origin
Vec3f        dir;	// Ray direction
double       t;     // Current/maximum hit distance
const IPrim* hit;	// Pointer to currently closest primitive
float        u;     // Barycentric u coordinate
float        v;     // Barycentric v coordinate
} ray;
t
\vec{b}(u_b,v_b)
\vec{c}(u_c,v_c)

# Surface Parametrization

Quadrilateral solid $$(a,b,c,d)$$ consists out of two triangles $$a,b,c)$$ and $$(a,c,d)$$.

1. On construction provide texture coordinates for all 4 quadrilateral vertices
2. There is no ray-quad intersection check, only 2 ray-triangle intersection checks
\vec{a}(u_a,v_a)
\vec{b}(u_b,v_b)
\vec{c}(u_c,v_c)
\vec{p}(u,v)
\vec{o}
t
\vec{d}(u_d,v_d)

# Surface Parametrization

## Solid: Cylinder

\vec{p}(x,y,z)
\varphi

### Cartesian to Cylindrical

$$r=\sqrt{x^2+y^2}$$

$$\varphi=\arctan_2\frac{y}{x}$$

$$h=h$$

(read more about $$\arctan_2$$ function)

y
z
x

### Spherical to Texture

Since $$\varphi\in[-\pi; \pi]$$ and $$h\in[0;H]$$

$$u=\frac{\pi+\varphi}{2\pi}$$

$$v=\frac{h}{H}$$

h
H

# Surface Parametrization

## Solid: Cylinder

\vec{p}(x,y,z)
\varphi
z
x
h
H
a
b
c
d

If a cylinder has $$n$$ sides and every side $$s$$ of a cylinder is modeled with a quad $$(a,b,c,d)$$, it is straightforward to calculate the 3D vertex positions as well as 2D texture coordinates:

$$u_a = \frac{s+1}{n}; v_a = 1$$

$$u_b = \frac{s}{n}; v_b = 1$$

$$u_c = \frac{s}{n}; v_c = 0$$

$$u_d = \frac{s+1}{n}; v_d = 0$$

# Surface Parametrization

## Complex Solids

Inverse mapping for arbitrary 3D surfaces is too complex

### Two-Stage Texture Mapping

is an approximation technique to generate texture coordinates for every vertex of a complex-shaped solid

• Stage 1: Mapping from a texture space to a more simple intermediate 3D surface which is a reasonable approximation of the destination surface (e.g., cylinder, sphere, box or plane)
• Stage 2: Mapping from a the intermediate surface to the destination object surface

# Surface Parametrization

## Complex Solids

Inverse mapping for arbitrary 3D surfaces is too complex

### Example

Texturing a vase with different intermediate surfaces:

### Plane

Strong distortion where object surface normal is orthogonal to the intermediate plane normal

### Cylinder

Reasonably uniform mapping (symmetry!)

### Sphere

Problems with concave regions