There is no (0,0,0) in the real world. Objects are always defined relative to each other. We can move (0,0,0) and thus move all the points defined relative to that origin.
A motion takes place relative to a local origin.
The term local origin refers to the (0,0,0) that is chosen to measure the motion from. The local origin may be moving relative to some greater frame of reference.
Start with a symbol (prototype). Each appearance of the object in the scene is an instance. We must scale, orient and position it to define the instance transformation
M = T ⋅ R ⋅ S M=T\cdot R\cdot S M=T⋅R⋅S
Model stored in a table by
In a scene, some objects may be grouped together in some way. For example, an articulated figure may contain several rigid components connected together in a specified fashion.
Articulated model
An articulated model is an example of a hierarchical model consisting of rigid parts and connecting joints.
The moving parts can be arranged into a tree data structure if we choose some particular piece as the ‘root’.
For an articulated model (like a biped character), we usually choose the root to be somewhere near the centre of the torso.
Each joint in the figure has specific allowable degrees of freedom (DOFs) that define the range of possible poses for the model.
Each node in the tree represents an object that has a matrix describing its location and a model describing its geometry.
When a node up in the tree moves its matrix
Each node in the tree stores a local matrix which is its transformation relative to its parent.
To compute a node’s world space matrix, we need to concatenate its local matrix with its parent’s world matrix: M w o r l d = M p a r e n t ⋅ M l o c a l M_{world}=M_{parent}\cdot M_{local} Mworld=Mparent⋅Mlocal
A light source can be defined with a number of properties, such as position, colour, direction, shape and reflectivity.
A single value can be assigned to each of the RGB colour components to describe the amount or intensity of the colour component.
This is the simplest model for an object, which emits radiant energy with a single colour specified by the three RGB components.
Large sources can be treated as point emitters if they are not too close to the scene.
The light rays are generated along radially diverging paths from the light source position.
A large light source (e.g. the sun), which is very far away from the scene, can also be approximated as a point emitter, but there is little variation in its directional effects.
This is called an infinitely distant / directional light (the light beams are parallel).
一种假定离被照亮的对象有无穷远的光。从光源发出的所有光线到达场景时全是平行的。场景中每个顶点可以使用一个单独的光方向矢量。
A local light source can easily be modified to produce a spotlight beam of light
If an object is outside the directional limits of the light source, it is excluded for illumination by that light source.
A spot light source can be set up by assigning a vector direction and an angular limit θ l θ_l θl measured from the vector direction along with the position and colour.
Although a light source delivers a single distribution of frequencies, the ambient, diffuse and specular components might be different.
The effect is a general background non-directed illumination. Each object is displayed using an intensity intrinsic to it, i.e. a world of non-reflective, self-luminous object.
Consequently each object appears as a monochromatic silhouette, unless its constituent parts are given different shades when the object is created.
The ambient component is the lighting effect produced by the reflected light from various surfaces in the scene.
When ambient light strikes a surface, it is scattered equally in all directions. The light has been scattered so much by the environment that it is impossible to determine its direction - it seems to come from all directions.
Back lighting in a room has a large ambient component, since most of the light that reaches the eye has been bounced off many surfaces first.
A spotlight outdoors has a tiny ambient component; most of the light travels in the same direction, and since it is outdoors, very little of the light reaches the eye after bouncing off other objects.
Diffuse light comes from one direction, so it is brighter if it comes squarely down on a surface than if it barely glances off the surface.
Once it hits a surface, however, it is scattered equally in all directions, so it appears equally bright, no matter where the eye is located.
Any light coming from a particular position or direction probably has a diffuse component.
Rough or grainy surfaces tend to scatter the reflected light in all directions (called diffuse reflection).
Each object is considered to present a dull, matte surface and all objects are illuminated by a point light source whose rays emanate uniformly in all directions.
The factors which affect the illumination are the point light source intensity, the material’s diffuse reflection coefficient, and the angle of incidence of the light.
Specular light comes from a particular direction, and it tends to bounce off the surface in a preferred direction.
A well-collimated laser beam bouncing off a high-quality mirror produces almost 100% specular reflection.
Shiny metal or plastic has a high specular component, and chalk or carpet has almost none. Specularity can be considered as shininess.
This effect can be seen on any shiny surface and the light reflected off the surface tends to have the same colour as the light source.
The reflectance tends to fall off rapidly as the viewpoint direction veers away from the direction of reflection; for perfect reflectors, specular light appears only in the direction of reflection.
For positional/point light sources, we consider the attenuation of light received due to the distance from the source. Attenuation is disabled for directional lights as they are infinitely far away, and it does not make sense to attenuate their intensity over distance.
Although for an ideal source the attenuation is inversely proportional to the square of the distance d, we can gain more flexibility by using the following distance-attenuation model, f ( d ) = 1 k c + k l d + k q d 2 f(d)=\frac1{k_c+k_ld+k_qd^2} f(d)=kc+kld+kqd21 where k c , k l , k q k_c,k_l,k_q kc,kl,kq are three floats.
A lighting model (also called illumination or shading model) is used to calculate the colour of an illuminated position on the surface of an object.
The lighting model computes the lighting effects for a surface using various optical properties, which have been assigned to the surface, such as the degree of transparency, colour reflectance coefficients and texture parameters.
The lighting model can be applied to every projection position, or the surface rendering can be accomplished by interpolating colours on the surface using a small set of lighting-model calculations.
A surface rendering method uses the colour calculation from a lighting model to determine the pixel colours for all projected positions in a scene.
A simple model that can be computed rapidly. Three components considered:
Four vectors used
For each point light source, there are 9 coefficients I d r , I d g , I d b , I s r , I s g , I s b , I a r , I a g , I a b I_{dr},I_{dg},I_{db},I_{sr},I_{sg},I_{sb},I_{ar},I_{ag},I_{ab} Idr,Idg,Idb,Isr,Isg,Isb,Iar,Iag,Iab
Material properties match light source properties, there are 9 coefficients and a Shininess coefficient α \alpha α k d r , k d g , k d b , k s r , k s g , k s b , k a r , k a g , k a b k_{dr},k_{dg},k_{db},k_{sr},k_{sg},k_{sb},k_{ar},k_{ag},k_{ab} kdr,kdg,kdb,ksr,ksg,ksb,kar,kag,kab
For each light source and each colour component, the Phong model can be written (without the distance terms) as I = k d I d l ⃗ ⋅ n ⃗ + k s I s ( v ⃗ ⋅ r ⃗ ) α + k a I a I=k_dI_d\vec l\cdot\vec n+k_sI_s(\vec v\cdot\vec r)^\alpha+k_aI_a I=kdIdl⋅n+ksIs(v⋅r)α+kaIa
For each colour component, we add contributions from all sources
In vertex shading, shading calculations are done for each vertex
By default, vertex shades are interpolated across an object if passed to the fragment shader as a varying variable (smooth shading). We can also use uniform variables to shade with a single shade (flat shading)
If the three vectors l ⃗ , v ⃗ , n ⃗ \vec l,\vec v,\vec n l,v,n are constant, then the shading calculation needs to be carried out only once for each polygon, and each point on the polygon is assigned the same shade.
The polygonal mesh is usually designed to model a smooth surface, and flat shading will almost always be disappointing because we can see even small differences in shading between adjacent polygons.
The rasteriser interpolates colours assigned to vertices across a polygon.
Suppose that the lighting calculation is made at each vertex using the material properties and vectors n ⃗ \vec n n, v ⃗ \vec v v, and l ⃗ \vec l l computed for each vertex. Thus, each vertex will have its own colour that the rasteriser can use to interpolate a shade for each fragment.
Note that if the light source is distant, and either the viewer is distant or there are no specular reflections, then smooth (or interpolative) shading shades a polygon in a constant colour.
FLAT与SMOOTH绘制的区别
Gouraud shading
The vertex normals are determined (average of the normals around a mesh vertex) for a polygon and used to calculate the pixel intensities at each vertex, using whatever lighting model.
Then the intensities of all points on the edges of the polygon are calculated by a process of weighted averaging (linear interpolation) of the vertex values.
The vertex intensities are linearly interpolated over the surface of the polygon.
Gouraud shading 与 Phong shading 的区别
A lighting model may approximate a material colour depending on the percentages of the incoming red, green, and blue light reflected.
Like lights, materials have different ambient, diffuse and specular colours, which determine the ambient, diffuse and specular reflectances of the material.
The ambient reflectance of a material is combined with the ambient component of each incoming light source, the diffuse reflectance with the light’s diffuse component, and similarly for the specular reflectance component.
Ambient and diffuse reflectances define the material colour and are typically similar if not identical.
Specular reflectance is usually white or grey, so that specular highlights become the colour of the specular intensity of the light source. If a white light shines on a shiny red plastic sphere, most of the sphere appears red, but the shiny highlight is white.
The colour components specified for lights mean something different from those for materials.
RGB values for two lights
Similarly, if two lights, ( R 1 , G 1 , B 1 ) (R_1,G_1,B_1) (R1,G1,B1) and ( R 2 , G 2 , B 2 ) (R_2,G_2,B_2) (R2,G2,B2) are sent to the eye, these components are added up, giving ( R 1 + R 2 , G 1 + G 2 , B 1 + B 2 ) (R_1+R_2,G_1+G_2,B_1+B_2) (R1+R2,G1+G2,B1+B2). If any of the sums are greater than 1 (corresponding to a colour brighter than the equipment can display), the component is clamped to 1.
Although graphics cards can render over 10 million polygons per second, this may be insufficient or there could be alternative way to process phenomena, e.g. clouds, grass, terrain and skin.
Although graphics cards can render over 10 million polygons per second, this may be insufficient or there could be alternative way to process phenomena, e.g. clouds, grass, terrain and skin.
What makes texture mapping tricky is that a rectangular texture can be mapped to nonrectangular regions, and this must be done in a reasonable way.
When a texture is mapped, its display on the screen might be distorted due to various transformations applied – rotations, translations, scaling and projections.
Types of texture mapping
There are two common types of texture:
The texture can be defined as one-dimensional, two-dimensional or three-dimensional patterns.
Texture functions in a graphics package often allow the number of colour components for each position in a pattern to be specified as an option.
The individual values in a texture array are often called texels (texture elements).
Depending on the texture size, the distortion and the size of the screen image, a texel may be mapped to more than one pixel (called magnification), and a pixel may be covered by multiple texels (called minification).
Since the texture is made up of discrete texels, filtering operations must be performed to map texels to pixels.
For example, if many texels correspond to a pixel, they are averaged down to fit; if texel boundaries fall across pixel boundaries, a weighted average of the applicable texels is performed.
Because of these calculations, texture mapping can be computationally expensive, which is why many specialised graphics systems include hardware support for texture mapping.
Texture mapping techniques are implemented at the end of the rendering pipeline. It is very efficient because a few polygons make it past the clipper.
We really want to go backwards:
One solution to the mapping problem is to first map the texture to a simple intermediate surface.
Cylindrical mapping
A parametric cylinder x = r cos ( 2 π u ) y = r sin ( 2 π u ) z = v h \begin{aligned}x&=r\cos(2\pi u)\\y&=r\sin(2\pi u)\\z&=vh\end{aligned} xyz=rcos(2πu)=rsin(2πu)=vh
maps a rectangle in the (u,v) space to the cylinder of radius r and height h in the world co-ordinates s = u t = v s=u\\t=v s=ut=v
Spherical mapping
x = r cos ( 2 π u ) y = r sin ( 2 π u ) cos ( 2 π v ) z = r sin ( 2 π u ) sin ( 2 π v ) \begin{aligned}x&=r\cos(2\pi u)\\y&=r\sin(2\pi u)\cos(2\pi v)\\z&=r\sin(2\pi u)\sin(2\pi v)\end{aligned} xyz=rcos(2πu)=rsin(2πu)cos(2πv)=rsin(2πu)sin(2πv)
Box mapping
Easy to use with simple orthographic projection. Also used in environmental maps
Mapping from an intermediate object to an actual object
Point sampling of the texture can lead to aliasing errors
Area averaging
A better but slower option is to use area averaging.
Clipping: Remove objects or parts of objects that are
outside the clipping window.
Rasterization: Convert high level object descriptions to pixel colours in the framebuffer.
Why clipping
Rasterization is very expensive. Approximately linear with the number of fragments created. If we only rasterize what is actually viewable, we can save a lot of expensive computation.
Rendering pipeline
Required tasks
Rasterization meta algorithms
Consider two approaches to rendering a scene with opaque objects:
The clipping window
Clipping window vs viewport
The viewport is part of the state
Determine whether a point (x,y) is inside or outside of the window.
这个很简单。
Determine whether a line is inside, outside or partially inside the window. If a line is partially inside, we need to display the inside segment.
solve simultaneous equations using y = m x + b y = mx + b y=mx+b for line and four clip edges
Cohen-Sutherland
steps
Idea: eliminate as many cases as possible without computing intersections
Start with four lines that determine the sides of the clipping window
defining outcodes
Efficiency
In many applications, the clipping window is small relative to the size of the entire database.
Most line segments are outside one or more sides of the window and can be eliminated based on their outcodes.
Inefficient when code has to be re-executed for line segments that must be shortened in more than one step, because two intersections have to be calculated anyway, in addition to calculating the outcode (extra computing in such a case).
Use 6-bit outcodes, when needed, clip line segment against planes
Consider the parametric form of a line segment p ( α ) = ( 1 − α ) p ⃗ 1 + α p ⃗ 2 , 0 ≤ α ≤ 1 p(\alpha)=(1-\alpha)\vec p_1+\alpha\vec p_2,0\leq\alpha\leq1 p(α)=(1−α)p1+αp2,0≤α≤1 We can distinguish the cases by looking at the ordering of the α \alpha α values where the line is determined by the line segment crossing the lines that determine the window.
If we write the line and plane equations in matrix form (where n n n is the normal to the plane and p 0 p_0 p0 is a point on the plane), we must solve the equations. n ⃗ ⋅ ( p ( α ) − p ⃗ 0 ) = 0 \vec n\cdot(p(\alpha)-\vec p_0)=0 n⋅(p(α)−p0)=0For the α \alpha α corresponding to the point of intersection, this value is α = n ⃗ ⋅ ( p ⃗ 0 − p ⃗ 1 ) n ⃗ ⋅ ( p ⃗ 2 − p ⃗ 1 ) \alpha=\frac{\vec n\cdot(\vec p_0-\vec p_1)}{\vec n\cdot(\vec p_2-\vec p_1)} α=n⋅(p2−p1)n⋅(p0−p1)The intersection requires 6 multiplications and 1 division.
General 3D clipping
General clipping in 3D requires intersection of line segments against arbitrary plane.
Normalised form
Normalisation is part of viewing (pre clipping) but after normalisation, we clip against sides of right parallelepiped.
Not as simple as line segment clipping
However, clipping a convex polygon can yield at most one other polygon.
One strategy is to replace non-convex (concave) polygons with a set of triangular polygons (a tessellation). It also makes fill easier.
We can consider line segment clipping as a process that takes in two vertices and produces either no vertices or the vertices of a clipped line segment.
Pipeline clipping of line segments
Clipping against each side of window is independent of other sides so we can use four independent clippers in a pipeline.
Pipeline clipping of polygons
add front and back clippers (Strategy used in SGI Geometry Engine)
Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extent:
Clipping has much in common with hidden-surface removal. In both cases, we try to remove objects that are not visible to the camera.
Often we can use visibility or occlusion testing early in the process to eliminate as many polygons as possible before going through the entire pipeline.
Object space algorithms: determine which objects are in front of others.
Image space algorithms: determine which object is visible at each pixel
It is an object-space algorithm. Render polygons in the back to front order so that polygons behind others are simply painted over.
Sort surfaces/polygons by their depth (z value). Not every polygon is either in front or behind all other polygons.
Back-face culling is the process of comparing the position and orientation of polygons against the viewing direction v ⃗ \vec v v, with polygons facing away from the camera eliminated.
This elimination minimises the amount of computational overhead involved in hidden-surface removal.
Back-face culling is basically a test for determining the visibility of a polygon, and based on this test the polygon can be removed if not visible – also called back-surface removal.
For each polygon P i P_i Pi
Back-face culling does not work for:
Look at each projector ( n ∗ m n*m n∗m projectors for an n ∗ m n*m n∗m framebuffer) and find the closest among k k k polygons.
We now know which pixels contain which objects; however since some pixels may contain two or more objects, we must calculate which of these objects are visible and which are hidden.
In graphics hardware, hidden-surface removal is generally accomplished using the Z-buffer algorithm.
we set aside a two-dimensional array of memory (the Z-buffer) of the same size as the screen (number of rows * number of columns). This is in addition to the colour buffer. The Z-buffer will hold values which are depths (quite often z-values). The Z-buffer is initialised so that each element has the value of the far clipping plane (the largest possible z-value after clipping is performed). The colour buffer is initialised so that each element contains a value which is the background colour.
Now for each polygon, we have a set of pixel values which the polygon covers. For each of the pixels, we compare its depth (z-value) with the value of the corresponding element already stored in the Z-buffer:
Repeat for all polygons in the image. The implementation is typically done in normalised co-ordinates so that depth values range from 0 at the near clipping plane to 1.0 at the far clipping plane.
Advantages
Memory requirements
If we work scan line by scan line as we move across a scan line, the depth changes satisfy a Δ x + b Δ y + c Δ z = 0 a\Delta x+b\Delta y+c\Delta z=0 aΔx+bΔy+cΔz=0, noticing that the plane which contains the olygon is represented by a x + b y + c z + d = 0 ax+by+cz+d=0 ax+by+cz+d=0
We can combine shading and hidden-surface removal through scan line algorithm.
implementation
In many real-time applications, such as games, we want to eliminate as many objects as possible within the application so that we can
Partition space with Binary Spatial Partition (BSP) Tree
Consider 6 parallel planes. Plane A separates planes B and C from planes D, E and F.
We can put this information in a BSP tree. Use the BSP tree for visibility and occlusion testing.
The painter’s algorithm for hidden-surface removal works by drawing all faces, from back to front.
constructing
Rendering
The rendering is recursive descent of the BSP tree.
At each node (for back to front rendering):