Download - Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Transcript
Page 1: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Computer GraphicsRendering 2D Geometry

CO2409 Computer Graphics

Week 2

Page 2: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Today’s LectureToday’s Lecture

1. 2D Geometry

2. Coordinate Systems & Viewports

3. Rendering Points and Lines

4. Polygons and Circles

5. Anti-Aliasing

Page 3: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

2D Geometry Basics2D Geometry Basics

• Will look at 2D geometry represented by points and lines in two dimensional Cartesian space

• Represented on a graph with two axes, usually X (horizontal) and Y (vertical)

• Origin of the graph and of the 2D space is where the axes cross (X = Y = 0)

Page 4: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

2D Geometry Definitions2D Geometry Definitions

• I will often use informal English to describe 2D geometry. However, here are a couple of specific definitions that will be used:

A vertex: a single point (plural = vertices) defined by coordinates on the axes

An edge: a straight line joining two vertices

A polygon: a single closed loop of edges

Page 5: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Specifying 2D GeometrySpecifying 2D Geometry

• Example vertices:A(10, 20), B(30, 30), C(35, 15)

• Edges:AB, BC, AC

• Edges with direction (a form of vector, see later lectures) , ,

• Polygons:ABCA, or ABC (implies the final edge to A)

AB BC CA

Page 6: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Coordinate Systems / SpacesCoordinate Systems / Spaces

• Define a coordinate system as a particular choices for:– The location of the origin– Orientation and scale of the axes

• This is also known as a space

• Can view the same geometry in different spaces:– World space: an overview of all

the geometry– Viewport space: where the

geometry is relative to the viewing window (see next slide)

– Will see more spaces later, an important concept

Page 7: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

ViewportsViewports

• A viewport (or window) is a rectangle of pixels representing a view into the world space

• A viewport has its own coordinate system, which may not match that of the geometry it shows– The axes will usually be X horizontal & Y vertical

• But don’t have to be – rotated viewports

– The scale of the axes may be different– The direction of the Y axis may differ.

E.g. the geometry may be stored with +ve Y up, but the viewport has +ve Y down.

– The origin (usually in the corners or centre of the viewport) may not match the geometry origin.

Page 8: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Viewport ExampleViewport Example

• Example of changing coordinate system from world space to viewport space:

P = (20,15) in world space. Where is P’ in viewport space?

Page 9: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Rendering PointsRendering Points

• Rendering is the process of converting 2D or 3D geometry into pixels

• To render a vertex (a point):– Convert vertex coordinates into viewport space– Set the colour of the pixel at those coordinates– The colour might be stored with the geometry, or we can use a

fixed colour (e.g. black)

• In the previous diagram the vertex P would be rendered by setting the colour of the pixel at coordinates (266, 450) in the viewport

Page 10: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Rendering Lines: 1st AttemptRendering Lines: 1st Attempt

• A possible method to render an edge (a line):– Convert the start and end vertex into

viewport space– Trace along this converted edge– Colour each pixel traced over

• Unfortunately, this method creates thick lines.

• We need to colour fewer pixels to get a cleaner result.

Page 11: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Rendering Lines: 2nd AttemptRendering Lines: 2nd Attempt

• Consider drawing a horizontal line with previous method:– Trace along horizontally adjacent

pixels, setting the colour of each– Result is fine

• Adapt this for a diagonal line:– Start at the left end of the line– Move rightwards, colouring each pixel– Periodically move up or down one

pixel to create ‘steps’ of the correct gradient

– Use variations of above for lines in different directions

To go from start to end need to move right 9 pixels and down 4 pixels.

So move down one pixel about every 9/4 pixels right

Page 12: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Rendering Lines: AlgorithmsRendering Lines: Algorithms

• We will use a basic version of this algorithm to render lines in the lab– Using floating point calculations

• A widely used method is Bresenham’s Line Algorithm – Wikipedia version– Highly efficient - only uses integer calculations– We won’t cover this, or other efficient 2D algorithms in this module

as we will concentrate on higher level graphics work– Many 2D algorithms like this are implemented in hardware now

Page 13: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

• A polygon is a sequence of edges, so we render a polygon outline by simply rendering each edge– We only need the set of vertices defining the polygon

• Axis-aligned squares and rectangles are special cases of a polygon and can be rendered more efficiently.– Horizontal / vertical lines are simple cases

• But how to render a filled polygon?

Rendering PolygonsRendering Polygons

Page 14: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

• Case of a triangle:– Simultaneously render the two polygon

edges starting from the top corner– Each time we move down a pixel

connect left & right edges• Horizontal line is quick to render

– Partway down the triangle one edge changes direction

• Left edge in this example

– Process is relatively simple extension of the line algorithm

• This is called Rasterisation

Filling PolygonsFilling Polygons

Page 15: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

• Split more complex polygons into triangles– Always possible

• Need to identify whether dealing with a convex or concave polygon– A concave polygon has

“indentations”

• What are the algorithms used here?

Filling PolygonsFilling Polygons

Page 16: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Rendering Circles?Rendering Circles?

• The coordinates of a circle can be written as:X = A + R sin(α), Y = B + R cos(α), for 0 ≤ α < 360ºwhere (A, B) is the circle centre and R the radius

• We could use this to render a circle:– Step α from 0 to 360 and calculate X & Y– Convert to viewport space and colour the pixels

• Not ideal though – why not? Try it in the lab

• Again there is a better integer-based method• Called the midpoint circle algorithm (derived from

Bresenham’s line algorithm, but not by him)– (Wikipedia article)

Page 17: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Regular PolygonsRegular Polygons

• Can use the circle equations to create regular polygons

• A regular polygon has all its points on a circle– Can calculate the polygon

vertices using the circle equations

• Conversely - can we use regular polygons to draw a circle?

Page 18: Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2.

Anti-AliasingAnti-Aliasing

• Returning to the naive line algorithm: – Again we colour every pixel passed through– This time consider how close the centre of the pixel is to the line

we are drawing– The colour used is more intense for pixels that lie nearer the line

• This is an anti-aliased line– Aliasing is the ‘jagged’ look of pixel

edges– This method smoothes these

effects – hence the name: anti-aliasing

– Note: one example of anti-aliasing, there are several other approaches