CPT205小豪的笔记

Computer Graphics

1. Introduction

1.1 Hardware and Software

  • Graphics Hardware
    • Input, Processing and Output Devices
    • Framebuffers
    • Pixels and Screen Resolution
  • Graphics Software
    • Techniques (Algorithms, Procedures)
    • Programming Library / API (OpenGL, JOGL and so on)
    • Not our focus: High level Interactive Systems (Maya, Studio Max, Unity, AutoCAD and so on)

Framebuffer

Framebuffer – A block of memory, dedicated to graphics output, that holds the contents of what will be displayed.

Pixel - an element of the framebuffer.

Framebuffer -> monitor

The values in the framebuffer are converted from a digital (1s and 0s representation, the bits) to an analog signal that goes out to the monitor. This is done automatically (not controlled by your code), and the conversion can be done while writing to the framebuffer.

1.2 Image quality issues

  • Screen resolution
  • Colour
  • Refresh rate
  • Brightness
  • Contrast
  • Sensitivity of display to viewing angle

1.3 Pixels

  • Pixel - The most basic addressable image element in a screen
    • CRT - Colour triad (RGB phosphor dots)
    • LCD - Single colour element
  • Screen Resolution - measure of number of pixels on a screen (m by n)
    • m - Horizontal screen resolution
    • n - Vertical screen resolution

1.4 Video formats

  • NTSC - 525x480, 30f/s, interlaced
  • PAL - 625x480, 25f/s, interlaced
  • VGA - 640x480, 60f/s, non-interlaced
  • SVGA - 800x600, 60f/s non-interlaced
  • RGB - 3 independent video signals and synchronisation signal, vary in resolution and refresh rate
  • Interlaced - scan every other line at a time, or scan odd and even lines alternatively; the even scan lines are drawn and then the odd scan lines are drawn on the screen to make up one video frame.

##1.5 Raster display

  • Cathode Ray Tubes (CRTs), most “tube” monitors you might see. Used to be very common, but big and bulky.
  • Liquid Crystal Displays (LCDs), there are two types transmissive (laptops, those snazzy new flat panel monitors) and reflective (wrist watches).

Cathode ray tubes (CRTs)

Heating element on the yolk.

Phosphor coated screen Electrons are boiled off the filament and drawn to the focusing system.

The electrons are focused into a beam and “shot” down the cylinder.

The deflection plates “aim” the electrons to a specific position on the screen.

  • Strong electrical fields and high voltage
  • Very good resolution
  • Heavy, not flat

Liquid crystal displays (LCDs)

  • Also divided into pixels, but without an electron gun firing at a screen, LCDs have cells that either allow light to flow through, or block it.

  • Liquid crystal displays use small flat chips which change their transparency properties when a voltage is applied.

  • LCD elements are arranged in an n x m array called the LCD matrix.

  • The level of voltage controls grey levels.

  • LCDs elements do not emit light; use backlights behind the LCD matrix.

  • Colour is obtained by placing filters in front of each LCD element.

  • Image quality dependent on viewing angle.

Advantages of LCDs

  • Flat
  • Light weight
  • Low power consumption

1.6 OpenGL

  • First introduced in 1992.

  • The OpenGL graphics system is a software interface to graphics hardware (GL stands for Graphics Library).

  • For interactive programs that produce colour images of moving three-dimensional objects.

  • It consists of over 200 distinct commands that you can use to specify the objects and operations needed to produce interactive three-dimensional applications.

  • OpenGL is designed as a streamlined, hardware-independent interface to be implemented on many different hardware platforms.

  • Similarly, OpenGL does not provide high-level commands for describing models of three-dimensional objects.

  • With OpenGL, you must build up your desired model from a small set of geometric primitives - points, lines, and polygons.

  • With OpenGL, you can control computer-graphics technology to produce realistic pictures or ones that depart from reality in imaginative ways.

  • OpenGL has become the industry standard for graphics applications and games.

#2.

2.1 Graphics Primitives

  • Points
  • Lines
  • Polygons

Geometric primitives – polygons and triangles

  • The basic graphics primitives are points, lines and polygons
    • A polygon can be defined by an ordered set of vertices
  • Graphics hardware is optimised for processing points and flat polygons
  • Complex objects are eventually divided into triangular polygons (a process called tessellation)
    • Because triangular polygons are always flat

2.2 Line Algorithms

2.2.1 Digital Differential Analyser (DDA)

y = mx + b

m = (y2 – y1) / (x2 – x1) = ∆y / ∆x

∆y = m∆x

As we move along x by incrementing x, ∆x = 1, so ∆y = m

//When 0 ≤ |m| ≤ 1
int x;
float y=y1;
for(x=x1; x<=x2; x++){
write_pixel(x, round(y), line_color);
y+=m;
}
//When |m| > 1, we swap the roles of x and y(∆y = m∆x so ∆x = ∆y/m = 1/m).
int y;
float x=x1;
for(y=y1; y<=y2; y++){
write_pixel(y, round(x), line_color);
x+=1/m;
}

1

2.2.2 The Bresenham line algorithm

  • The Bresenham algorithm is another incremental scan conversion algorithm.
  • It is accurate and efficient.
  • Its big advantage is that it uses only integer calculations (unlike DDA which requires float-point additions).
  • The calculation of each successive pixel requires only an addition and a sign test.
  • It is so efficient that it has been incorporated as a single instruction on graphics chips.

2.2.3 Generation of circles

2

2.2.4 Scan conversion

  • Also called rasterization.
  • The 3D to 2D projection gives us 2D vertices (points) to define 2D graphic primitives.
  • We need to fill in the interior
    • the rasterizer must determine which pixels in the framebuffer are inside the polygon.

2.3 Polygon Fill

  • Rasterize edges into framebuffer.
  • Find a seed pixel inside the polygon.
  • Visit neighbours recursively and colour if they are not edge pixels.
  • When vertices lie on the scanlines, cases (a) and (b) must be treated differently when using odd-even fill definition
    • Case (a): zero or two crossings
    • Case (b): one edge crossing

2.4 Graphics Primitives with OpenGL

3

glBegin(parameters)

  • GL_POINTS: individual points
  • GL_LINES: pairs of vertices interpreted as individual line segments
  • GL_LINE_STRIP: series of connected line segments
  • GL_LINE_LOOP: same as above, with a segment added between last and first vertices
  • GL_TRIANGLES: triples of vertices interpreted as triangles
  • GL_TRIANGLE_STRIP: linked strip of triangles
  • GL_TRIANGLE_FAN: linked fan of triangles
  • GL_QUADS: quadruples of vertices interpreted as four-sided polygons
  • GL_QUAD_STRIP: linked strip of quadrilaterals
  • GL_POLYGON: boundary of a simple, convex polygon

3. Transformation Pipeline and Geometric Transformations

3.1 Transformation pipeline

1

  • Modelling Transformation - to place an object into the Virtual World.
  • Viewing Transformation - to view the object from a different vantage point in the virtual world.
  • Projection Transformation - to see depth in the object.
  • Viewport Transformation - to temporarily map the volume defined by the “window of interest” plus the front and rear clipping planes into a unit cube. When this is the case, certain other operations are easier to perform.
  • Device Transformation - to map the user defined “window of interest” (in the virtual world) to the dimensions of the display area

3.2 Types of geometric transformation

2

3.2.1 2D translation

3

P = [ x y ] , P ′ = [ x ′ y ′ ] , T = [ t x t y ] \mathbf{P}=\left[\begin{array}{l} x \\ y \end{array}\right], \quad \mathbf{P}^{\prime}=\left[\begin{array}{l} x^{\prime} \\ y^{\prime} \end{array}\right], \quad \mathbf{T}=\left[\begin{array}{l} t_{x} \\ t_{y} \end{array}\right] P=[xy],P=[xy],T=[txty]

P ′ = P + T  or  [ x ′ y ′ ] = [ x y ] + [ t x t y ] \mathbf{P}^{\prime}=\mathbf{P}+\mathbf{T} \quad \text { or } \quad\left[\begin{array}{l} x^{\prime} \\ y^{\prime} \end{array}\right]=\left[\begin{array}{l} x \\ y \end{array}\right]+\left[\begin{array}{l} t_{x} \\ t_{y} \end{array}\right] P=P+T or [xy]=[xy]+[txty]

3.2.2 2D rotation

4

P ′ = R ⋅ P R = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] s o [ x ′ y ′ ] = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] . [ x y ] \begin{aligned}&\mathbf{P}^{\prime}=\mathbf{R}\cdot\mathbf{P}\\&\mathbf{R}=\begin{bmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{bmatrix}\quad\mathbf{so}\quad\begin{bmatrix}x^{\prime}\\y^{\prime}\end{bmatrix}=\begin{bmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{bmatrix}.\begin{bmatrix}x\\y\end{bmatrix}\end{aligned} P=RPR=[cosθsinθsinθcosθ]so[xy]=[cosθsinθsinθcosθ].[xy]

where θ is the rotation angle and φ is the angle between the x-axis and the line from the origin to (x, y).

Notice that the rotation point (or pivot point) is the coordinate origin.

Rotation about a fixed point rather than the origin

  • Move the fixed point to the origin
  • Rotate the object
  • Move the fixed point back to its initial position
  • M = T(pf ) R(θ) T(-pf )

5

3.2.3 2D scaling

6

When an object is scaled, both the size and location change.

{ x ′ = x ⋅ s x y ′ = y ⋅ s y P ′ = S ⋅ P s o [ x ′ y ′ ] = [ s x 0 0 s y ] . [ x y ] \begin{aligned}&\begin{cases}x^{\prime}=x\cdot s_x\\y^{\prime}=y\cdot s_y&\end{cases}\\\\&\mathbf{P}^{\prime}=\mathbf{S}\cdot\mathbf{P}\quad\mathrm{so}\quad\begin{bmatrix}x^{\prime}\\y^{\prime}\end{bmatrix}=\begin{bmatrix}s_x&\mathbf{0}\\\mathbf{0}&s_y\end{bmatrix}.\begin{bmatrix}x\\y\end{bmatrix}\end{aligned} {x=xsxy=ysyP=SPso[xy]=[sx00sy].[xy]

where P, and P’ are the original and new positions, and s x s_x sxand $s_y $are the scaling factors along the x- and y-axes.

3.2.4 2D reflection

Special case of scaling - corresponding to negative scale factors.

7

3.2.5 2D shearing

Equivalent to pulling faces in opposite directions.

8

x ’ = x + y cot ⁡ θ y ’ = y z ’ = z \begin{aligned}x’&=x+y\cot\theta\\y’&=y\\z’&=z\end{aligned} xyz=x+ycotθ=y=z

[ x ′ y ′ ] = [ 1 cot ⁡ θ 0 1 ] . [ x y ] \begin{bmatrix}x′\\y′\end{bmatrix}=\begin{bmatrix}1&\cot\theta\\0&1\end{bmatrix}.\begin{bmatrix}x\\y\end{bmatrix} [xy]=[10cotθ1].[xy]

9

3.2.6 2D homogeneous co-ordinates

  • By expanding the 2x2 matrices to 3x3 matrices, homogeneous co-ordinates are used.
  • A homogeneous parameter is applied so that each 2D position is represented with homogeneous co-ordinates (h·x, h·y, h).
  • The homogeneous parameter has a non-zero value, and can be set to 1 for convenient use.

3.2.7 2D homogeneous matrices

[ x ′ y ′ 1 ] = [ 1 0 t x 0 1 t y 0 0 1 ] ⋅ [ x y 1 ] (2D translation) \begin{bmatrix}x^{\prime}\\y^{\prime}\\1\end{bmatrix}=\begin{bmatrix}1&0&t_x\\0&1&t_y\\0&0&1\end{bmatrix}\cdot\begin{bmatrix}x\\y\\1\end{bmatrix}\quad\text{(2D translation)} xy1 = 100010txty1 xy1 (2D translation)

[ x ’ y ’ 1 ] = [ cos ⁡ θ − sin ⁡ θ 0 sin ⁡ θ cos ⁡ θ 0 0 0 1 ] . [ x y 1 ] (2D rotation) \begin{bmatrix}x’\\y’\\1\end{bmatrix}=\begin{bmatrix}\cos\theta&-\sin\theta&0\\\sin\theta&\cos\theta&0\\0&0&1\end{bmatrix}.\begin{bmatrix}x\\y\\1\end{bmatrix}\quad\text{(2D rotation)} xy1 = cosθsinθ0sinθcosθ0001 . xy1 (2D rotation)

[ x ′ y ′ 1 ] = [ s x 0 0 0 s y 0 0 0 1 ] . [ x y 1 ] (2D scaling) [ x ′ y ′ 1 ] = [ 1 cot ⁡ θ 0 0 1 0 0 0 1 ] . [ x y 1 ] (2D shearing) \begin{aligned}&\begin{bmatrix}x^{\prime}\\y^{\prime}\\1\end{bmatrix}=\begin{bmatrix}s_x&0&0\\0&s_y&0\\0&0&1\end{bmatrix}.\begin{bmatrix}x\\y\\1\end{bmatrix}\quad\text{(2D scaling)}\\\\&\begin{bmatrix}x^{\prime}\\y^{\prime}\\1\end{bmatrix}=\begin{bmatrix}1&\cot\theta&0\\0&1&0\\0&0&1\end{bmatrix}.\begin{bmatrix}x\\y\\1\end{bmatrix}\quad\text{(2D shearing)}\end{aligned} xy1 = sx000sy0001 . xy1 (2D scaling) xy1 = 100cotθ10001 . xy1 (2D shearing)

3.2.8 2D composite transformation

[ x ′ y ′ 1 ] = [ r s x x r s x y t r s x r s y x r s y y t r s y 0 0 1 ] ⋅ [ x y 1 ] \begin{bmatrix}x^{\prime}\\y^{\prime}\\1\end{bmatrix}=\begin{bmatrix}rs_{xx}&rs_{xy}&trs_{x}\\rs_{yx}&rs_{yy}&trs_{y}\\0&0&1\end{bmatrix}\cdotp\begin{bmatrix}x\\y\\1\end{bmatrix} xy1 = rsxxrsyx0rsxyrsyy0trsxtrsy1 xy1

where elements rs are the multiplicative rotation-scaling terms in the transformation (which involve only rotation angles and scaling factors);

elements trs are the translation terms, containing combination of translation distances, pivot-point and fixedpoint co-ordinates, rotation angles and scaling parameters.

"rs"元素是指变换中的乘法旋转-缩放项,这些项只涉及旋转角度和缩放因子。

"trs"元素是指变换中的平移项,其中包括了平移距离、旋转角度、缩放参数以及轴心点和固定点坐标的组合。

3.3 3D translation

3D translations and scaling can be simply extended from the corresponding 2D methods.

[ x ′ y ′ z ′ 1 ] = [ 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ] . [ x y z 1 ] \begin{bmatrix}x^{\prime}\\y^{\prime}\\z^{\prime}\\1\end{bmatrix}=\begin{bmatrix}1&0&0&t_x\\0&1&0&t_y\\0&0&1&t_z\\0&0&0&1\end{bmatrix}.\begin{bmatrix}x\\y\\z\\1\end{bmatrix} xyz1 = 100001000010txtytz1 . xyz1

3.3.1 3D co-ordinate axis rotations

  • The extension from 2D rotation methods to 3D rotation is less straightforward (because this is about an arbitrary axis instead of an arbitrary point).

  • Equivalent to rotation in two dimensions in planes of constant z (i.e. about the origin).

[ x ′ y ′ z ′ 1 ] = [ cos ⁡ θ − sin ⁡ θ 0 0 sin ⁡ θ cos ⁡ θ 0 0 0 0 1 0 0 0 0 1 ] . [ x y z 1 ] ( About   z -axis ) \begin{bmatrix}x^{\prime}\\y^{\prime}\\z^{\prime}\\1\end{bmatrix}=\begin{bmatrix}\cos\theta&-\sin\theta&0&0\\\sin\theta&\cos\theta&0&0\\0&0&1&0\\0&0&0&1\end{bmatrix}.\begin{bmatrix}x\\y\\z\\1\end{bmatrix}\quad\quad\quad(\textsf{About }z\text{-axis}) xyz1 = cosθsinθ00sinθcosθ0000100001 . xyz1 (About z-axis)

[ x ′ y ′ z ′ 1 ] = [ cos ⁡ θ 0 sin ⁡ θ 0 0 1 0 0 − sin ⁡ θ 0 cos ⁡ θ 0 0 0 0 1 ] . [ x y z z 1 ] ( About   y-axis ) \begin{bmatrix}x^{\prime}\\y^{\prime}\\z^{\prime}\\1\end{bmatrix}=\begin{bmatrix}\cos\theta&0&\sin\theta&0\\0&1&0&0\\-\sin\theta&0&\cos\theta&0\\0&0&0&1\end{bmatrix}.\begin{bmatrix}x\\y\\z\\z\\1\end{bmatrix}\quad\quad\quad(\textsf{About y-axis}) xyz1 = cosθ0sinθ00100sinθ0cosθ00001 . xyzz1 (About y-axis)

[ x ′ y ′ z ′ 1 ] = [ 1 0 0 0 0 cos ⁡ θ − sin ⁡ θ 0 0 sin ⁡ θ cos ⁡ θ 0 0 0 0 1 ] . [ x y z z 1 ] (About x-axis) \begin{bmatrix}x^{\prime}\\y^{\prime}\\z^{\prime}\\1\end{bmatrix}=\begin{bmatrix}1&0&0&0\\0&\cos\theta&-\sin\theta&0\\0&\sin\theta&\cos\theta&0\\0&0&0&1\end{bmatrix}.\begin{bmatrix}x\\y\\z\\z\\1\end{bmatrix}\quad\quad\quad\text{(About x-axis)} xyz1 = 10000cosθsinθ00sinθcosθ00001 . xyzz1 (About x-axis)

3.3.2 General rotation about the origin

A rotation by q about an arbitrary axis can be decomposed into the concatenation of rotations about the x, y, and z axes.

R ( q ) = R z ( q z )   R y ( q y )   R x ( q x ) \mathbf{R(q)=R_z(q_z)~R_y(q_y)~R_x(q_x)} R(q)=Rz(qz) Ry(qy) Rx(qx)

  w h e r e   q x , q y   a n d   q z are called the Euler angles . \mathrm{~where~}q_x,q_y\mathrm{~and~}q_z\text{are called the Euler angles}.  where qx,qy and qzare called the Euler angles.

Note that rotations do not commute though we can use rotations in another order but with different angles.

3.3.3 3D scaling

x ’ = s x x y ’ = s y ’ z ’ = s z z [ x ’ y ’ z ’ 1 ] = [ s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ] . [ x y z 1 ] \begin{aligned}&\begin{array}{c}x’=s_xx\\y’=s_y’\\z’=s_zz\end{array}\\\\&\begin{bmatrix}x’\\y’\\z’\\1\end{bmatrix}=\begin{bmatrix}s_x&0&0&0\\0&s_y&0&0\\0&0&s_z&0\\0&0&0&1\end{bmatrix}.\begin{bmatrix}x\\y\\z\\1\end{bmatrix}\end{aligned} x=sxxy=syz=szz xyz1 = sx0000sy0000sz00001 . xyz1

10

3.3.4 3D composite transformation

  • As with 2D transformation, a composite 3D transformation can be formed by multiplying the matrix representations for the individual operations in the transformation sequence.

  • There are further forms of transformation, namely reflection and shearing which can be implemented with the other three transformations.

  • Translation, scaling, rotation, reflection and shearing are all affine transformations in that transformed point P’(x’ ,y’ ,z’) is a linear combination of the original point P(x,y,z).

  • Matrix multiplication is associative

    M3⋅M2⋅M1 = (M3⋅M2)⋅M1 = M3⋅(M2⋅M1)

  • Transformation products are not always commutative

    A⋅B ≠ B⋅A

3.3.5 Instancing

  • In modelling, we often start with a simple object centred at the origin, oriented with an axis, and of a standard size.
  • We apply an instance transformation to its vertices to
    • Scale
    • Orient
    • Locate

11

3.3.6 OpenGL matrices

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小豪GO!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值