02 - 2D Graphics
02 - 2D Graphics
2D Graphics
Computer Graphics
Winter term 2019/20
Marc Stamminger
Rendering
• “Rendering”:
• fill frame buffer with shapes, text, 3D-content, …
• Examples:
• render a rectangle → simple
• render a circle with radius 𝑟𝑟 and center (𝑥𝑥, 𝑦𝑦) → ???
• render a line from 𝑥𝑥1 , 𝑦𝑦1 to (𝑥𝑥2 , 𝑦𝑦2 ) → ???
• fill a triangle with vertices 𝑥𝑥1 , 𝑦𝑦1 , 𝑥𝑥2 , 𝑦𝑦2 , 𝑥𝑥3 , 𝑦𝑦3 → ???
• → next week “Rasterization”
• Today we will have a brief look into such APIs, but this is not the
general topic of this lecture
• Scenegraph-based APIs:
• the scene to be rendered is defined in an abstract manner in a hierarchical
(tree-like) structure, which is passed to the renderer as a whole
• In HTML, this can be integrated into the normal document hierarchy
(see later)
• 3D:
• the primitives to be rendered are defined in 3D-space. A virtual camera is to
be specified that defines the mapping of the 3D-world to the 2D image. Also
occlusion must be considered.
Computer Graphics 2019/20 - 2D Graphics 4
Rendering APIs
looked at in the
exercises
2D 3D
command based • JAVA graphics 2D • OpenGL
• HTML canvas • DirectX
• Postscript • …
• …
scenegraph based • SVG • X3D
• … • Unreal
• Unity
• Canvas Element
• Command-based API
• SVG Element
• Scenegraph-based API
ctx.beginPath();
ctx.moveTo(75,25);
ctx.quadraticCurveTo(25,25,25,62.5);
ctx.quadraticCurveTo(25,100,50,100);
ctx.quadraticCurveTo(50,120,30,125);
ctx.quadraticCurveTo(60,120,65,100);
ctx.quadraticCurveTo(125,100,125,62.5);
ctx.quadraticCurveTo(125,25,75,25);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(75,25);
ctx.quadraticCurveTo(25,25,25,62.5);
ctx.quadraticCurveTo(25,100,50,100);
ctx.quadraticCurveTo(50,120,30,125);
ctx.quadraticCurveTo(60,120,65,100);
ctx.quadraticCurveTo(125,100,125,62.5);
ctx.quadraticCurveTo(125,25,75,25);
ctx.strokeStyle = "#000000";
ctx.lineWidth = 2;
ctx.stroke();
ctx.fillStyle = “#808080";
ctx.fill();
ctx.fillStyle = “#80ff80”;
ctx.fillRect(100,100,300,300);
ctx.fillStyle = “#ff8080”;
ctx.fillRect(100,100,300,300);
• Important in CG
• Positioning objects in a scene
• Object Animations
• Changing the shape of objects
• Creation of multiple copies of objects
𝑒𝑒2
𝑒𝑒1
𝑂𝑂
→ homogeneous coordinates
Computer Graphics 2019/20 - 2D Graphics 22
Homogenous coordinates
• Add “1” as third homogeneous coordinate
x = (𝑥𝑥1 , x2 ) → (𝑥𝑥1 , x2 , 1)
0 0 1 0 0 1
Multiplication ≡ composition
𝑇𝑇 𝑆𝑆 𝑆𝑆𝑇𝑇
𝑥𝑥 → 𝑇𝑇𝑇𝑇 = 𝑦𝑦 → 𝑆𝑆𝑆𝑆 = 𝑧𝑧 ≡ 𝑥𝑥 𝑆𝑆𝑆𝑆𝑆𝑆 = 𝑧𝑧
• Rotations
• Scalings
• Reflections
Rigid / Euclidean
Identity
Translation
Rotation
Similarity
Rigid / Euclidean
Similarities
Linear
Rigid / Euclidean
Scaling
Identity
Translation Isotropic Scaling Reflection
Rotation
Shear
Computer Graphics 2019/20 - 2D Graphics 32
Scaling
• Examples
0.5 0 0
0 0.5 0
0 0 1
0.5 0 0
0 1.5 0
0 0 1
0
𝑒𝑒2 =
• Vertical ( x-coordinate will not change ) 1
1 0 0
𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑟𝑟𝑦𝑦 𝑠𝑠 = 𝑠𝑠 1 0
0 0 1 𝑂𝑂 1
𝑒𝑒1 =
𝑠𝑠
b a
φ
α
x
−𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠
𝑒𝑒2 =
𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 cos 𝜙𝜙
𝑒𝑒1 =
sin 𝜙𝜙
𝑂𝑂
1 0 0
0 −1 0
0 0 1
−1 0 0
0 1 0
0 0 1
1 0 0
0 0.5 0
0 0 1
2 2 2 2
− 0 − 0
2 4 2 2
2 2 2 2
0 0
2 4 2 2
0 0 1 0 0 1
Scale Rotate
Rotate Scale
R1 SR1 R2SR1
S1 S2S1 S3S2S1
1 𝑠𝑠 𝑖𝑖 𝑖𝑖 + 𝑠𝑠𝑠𝑠
=
0 1 𝑗𝑗 𝑗𝑗
<svg id="svg">
<defs>
<g id="prim">
<rect width="20" height="20“ />
</g>
</defs>
<g fill="green">
<use xlink:href="#prim" x="50" y="50” />
<use xlink:href="#prim" x="150" y="50“ />
</g>
<g fill="red">
<use xlink:href="#prim" x="50" y="150“ />
<use xlink:href="#prim" x="150" y="150“ />
</g>
</svg>
Computer Graphics 2019/20 - 2D Graphics 53
Scene Graph
<svg id="svg">
<defs>…</defs>
svg <g fill="green">
<use xlink:href="#prim" x="50"
<use xlink:href="#prim" x="150
</g>
g fill=“green” g fill=“red” <g fill="red">
<use xlink:href="#prim" x="50"
<use xlink:href="#prim" x="150
x=50 x=150 x=50 x=150
y=50 y=50 y=150 y=150
</g>
</svg>
#prim